clang  9.0.0svn
FrontendActions.h
Go to the documentation of this file.
1 //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
10 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
11 
13 #include <string>
14 #include <vector>
15 
16 namespace clang {
17 
18 class Module;
19 class FileEntry;
20 
21 //===----------------------------------------------------------------------===//
22 // Custom Consumer Actions
23 //===----------------------------------------------------------------------===//
24 
26  void ExecuteAction() override;
27 
28  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
29  StringRef InFile) override;
30 
31 public:
32  // Don't claim to only use the preprocessor, we want to follow the AST path,
33  // but do nothing.
34  bool usesPreprocessorOnly() const override { return false; }
35 };
36 
38  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
39  StringRef InFile) override {
40  return nullptr;
41  }
42 
43  void ExecuteAction() override;
44 
45 public:
46  bool usesPreprocessorOnly() const override { return true; }
47 };
48 
49 //===----------------------------------------------------------------------===//
50 // AST Consumer Actions
51 //===----------------------------------------------------------------------===//
52 
54 protected:
55  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
56  StringRef InFile) override;
57 };
58 
60 protected:
61  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
62  StringRef InFile) override;
63 };
64 
66 protected:
67  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
68  StringRef InFile) override;
69 };
70 
72 protected:
73  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
74  StringRef InFile) override;
75 };
76 
78 protected:
79  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
80  StringRef InFile) override;
81 };
82 
84 protected:
85  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
86  StringRef InFile) override;
87 
89  return TU_Prefix;
90  }
91 
92  bool hasASTFileSupport() const override { return false; }
93 
94  bool shouldEraseOutputFiles() override;
95 
96 public:
97  /// Compute the AST consumer arguments that will be used to
98  /// create the PCHGenerator instance returned by CreateASTConsumer.
99  ///
100  /// \returns false if an error occurred, true otherwise.
101  static bool ComputeASTConsumerArguments(CompilerInstance &CI,
102  std::string &Sysroot);
103 
104  /// Creates file to write the PCH into and returns a stream to write it
105  /// into. On error, returns null.
106  static std::unique_ptr<llvm::raw_pwrite_stream>
107  CreateOutputFile(CompilerInstance &CI, StringRef InFile,
108  std::string &OutputFile);
109 
110  bool BeginSourceFileAction(CompilerInstance &CI) override;
111 };
112 
114  virtual std::unique_ptr<raw_pwrite_stream>
115  CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
116 
117 protected:
118  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
119  StringRef InFile) override;
120 
122  return TU_Module;
123  }
124 
125  bool hasASTFileSupport() const override { return false; }
126 };
127 
129 private:
130  bool BeginSourceFileAction(CompilerInstance &CI) override;
131 
132  std::unique_ptr<raw_pwrite_stream>
133  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
134 };
135 
137 private:
138  bool BeginSourceFileAction(CompilerInstance &CI) override;
139 
140  std::unique_ptr<raw_pwrite_stream>
141  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
142 };
143 
145  /// The synthesized module input buffer for the current compilation.
146  std::unique_ptr<llvm::MemoryBuffer> Buffer;
147  std::vector<std::string> ModuleHeaders;
148 
149 private:
150  bool PrepareToExecuteAction(CompilerInstance &CI) override;
151  bool BeginSourceFileAction(CompilerInstance &CI) override;
152 
153  std::unique_ptr<raw_pwrite_stream>
154  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
155 };
156 
158 protected:
159  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
160  StringRef InFile) override;
161 
162 public:
163  ~SyntaxOnlyAction() override;
164  bool hasCodeCompletionSupport() const override { return true; }
165 };
166 
167 /// Dump information about the given module file, to be used for
168 /// basic debugging and discovery.
170 protected:
171  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
172  StringRef InFile) override;
173  bool BeginInvocation(CompilerInstance &CI) override;
174  void ExecuteAction() override;
175 
176 public:
177  bool hasPCHSupport() const override { return false; }
178  bool hasASTFileSupport() const override { return true; }
179  bool hasIRSupport() const override { return false; }
180  bool hasCodeCompletionSupport() const override { return false; }
181 };
182 
184 protected:
185  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
186  StringRef InFile) override;
187 
188  void ExecuteAction() override;
189 
190 public:
191  bool hasCodeCompletionSupport() const override { return false; }
192 };
193 
195 protected:
196  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
197  StringRef InFile) override;
198 
199  void ExecuteAction() override;
200 };
201 
202 /**
203  * Frontend action adaptor that merges ASTs together.
204  *
205  * This action takes an existing AST file and "merges" it into the AST
206  * context, producing a merged context. This action is an action
207  * adaptor, which forwards most of its calls to another action that
208  * will consume the merged context.
209  */
211  /// The action that the merge action adapts.
212  std::unique_ptr<FrontendAction> AdaptedAction;
213 
214  /// The set of AST files to merge.
215  std::vector<std::string> ASTFiles;
216 
217 protected:
218  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
219  StringRef InFile) override;
220 
221  bool BeginSourceFileAction(CompilerInstance &CI) override;
222 
223  void ExecuteAction() override;
224  void EndSourceFileAction() override;
225 
226 public:
227  ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
228  ArrayRef<std::string> ASTFiles);
229  ~ASTMergeAction() override;
230 
231  bool usesPreprocessorOnly() const override;
233  bool hasPCHSupport() const override;
234  bool hasASTFileSupport() const override;
235  bool hasCodeCompletionSupport() const override;
236 };
237 
239 protected:
240  void ExecuteAction() override;
241  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
242  StringRef) override {
243  return nullptr;
244  }
245 
246  bool usesPreprocessorOnly() const override { return true; }
247 };
248 
249 //===----------------------------------------------------------------------===//
250 // Preprocessor Actions
251 //===----------------------------------------------------------------------===//
252 
254 protected:
255  void ExecuteAction() override;
256 };
257 
259 protected:
260  void ExecuteAction() override;
261 };
262 
264 protected:
265  void ExecuteAction() override;
266 };
267 
269 protected:
270  void ExecuteAction() override;
271 
272  bool hasPCHSupport() const override { return true; }
273 };
274 
275 } // end namespace clang
276 
277 #endif
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Dump information about the given module file, to be used for basic debugging and discovery.
Abstract base class for actions which can be performed by the frontend.
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:367
virtual bool hasASTFileSupport() const
Does this action support use with AST files?
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we&#39;re handling.
virtual bool hasCodeCompletionSupport() const
Does this action support use with code completion?
bool hasPCHSupport() const override
Does this action support use with PCH?
friend class ASTMergeAction
bool hasIRSupport() const override
Does this action support use with IR files?
bool hasPCHSupport() const override
Does this action support use with PCH?
virtual bool BeginInvocation(CompilerInstance &CI)
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
virtual bool shouldEraseOutputFiles()
Callback at the end of processing a single input, to determine if the output files should be erased o...
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
bool hasASTFileSupport() const override
Does this action support use with AST files?
bool hasASTFileSupport() const override
Does this action support use with AST files?
virtual void EndSourceFileAction()
Callback at the end of processing a single input.
virtual bool hasPCHSupport() const
Does this action support use with PCH?
Frontend action adaptor that merges ASTs together.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we&#39;re handling.
bool hasASTFileSupport() const override
Does this action support use with AST files?
Abstract base class to use for AST consumer-based frontend actions.
virtual bool PrepareToExecuteAction(CompilerInstance &CI)
Prepare to execute the action on the given CompilerInstance.
Abstract base class to use for preprocessor-based frontend actions.
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we&#39;re handling.
Dataflow Directional Tag Classes.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:361
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
The translation unit is a module.
Definition: LangOptions.h:370