clang  10.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 
83  return TU_Prefix;
84  }
85 
86  bool hasASTFileSupport() const override { return false; }
87 
88  bool shouldEraseOutputFiles() override;
89 
90 public:
91  /// Compute the AST consumer arguments that will be used to
92  /// create the PCHGenerator instance returned by CreateASTConsumer.
93  ///
94  /// \returns false if an error occurred, true otherwise.
95  static bool ComputeASTConsumerArguments(CompilerInstance &CI,
96  std::string &Sysroot);
97 
98  /// Creates file to write the PCH into and returns a stream to write it
99  /// into. On error, returns null.
100  static std::unique_ptr<llvm::raw_pwrite_stream>
101  CreateOutputFile(CompilerInstance &CI, StringRef InFile,
102  std::string &OutputFile);
103 
104  bool BeginSourceFileAction(CompilerInstance &CI) override;
105 };
106 
108  virtual std::unique_ptr<raw_pwrite_stream>
109  CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
110 
111 protected:
112  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
113  StringRef InFile) override;
114 
116  return TU_Module;
117  }
118 
119  bool hasASTFileSupport() const override { return false; }
120 };
121 
123 protected:
125 
126  bool hasASTFileSupport() const override { return false; }
127 };
128 
130 protected:
131  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
132  StringRef InFile) override;
133 };
134 
136 private:
137  bool BeginSourceFileAction(CompilerInstance &CI) override;
138 
139  std::unique_ptr<raw_pwrite_stream>
140  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
141 };
142 
144 private:
145  bool BeginSourceFileAction(CompilerInstance &CI) override;
146 
147  std::unique_ptr<raw_pwrite_stream>
148  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
149 };
150 
152  /// The synthesized module input buffer for the current compilation.
153  std::unique_ptr<llvm::MemoryBuffer> Buffer;
154  std::vector<std::string> ModuleHeaders;
155 
156 private:
157  bool PrepareToExecuteAction(CompilerInstance &CI) override;
158  bool BeginSourceFileAction(CompilerInstance &CI) override;
159 
160  std::unique_ptr<raw_pwrite_stream>
161  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
162 };
163 
165 protected:
166  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
167  StringRef InFile) override;
168 
169 public:
170  ~SyntaxOnlyAction() override;
171  bool hasCodeCompletionSupport() const override { return true; }
172 };
173 
174 /// Dump information about the given module file, to be used for
175 /// basic debugging and discovery.
177 protected:
178  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
179  StringRef InFile) override;
180  bool BeginInvocation(CompilerInstance &CI) override;
181  void ExecuteAction() override;
182 
183 public:
184  bool hasPCHSupport() const override { return false; }
185  bool hasASTFileSupport() const override { return true; }
186  bool hasIRSupport() const override { return false; }
187  bool hasCodeCompletionSupport() const override { return false; }
188 };
189 
191 protected:
192  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
193  StringRef InFile) override;
194 
195  void ExecuteAction() override;
196 
197 public:
198  bool hasCodeCompletionSupport() const override { return false; }
199 };
200 
202 protected:
203  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
204  StringRef InFile) override;
205 
206  void ExecuteAction() override;
207 };
208 
209 /**
210  * Frontend action adaptor that merges ASTs together.
211  *
212  * This action takes an existing AST file and "merges" it into the AST
213  * context, producing a merged context. This action is an action
214  * adaptor, which forwards most of its calls to another action that
215  * will consume the merged context.
216  */
218  /// The action that the merge action adapts.
219  std::unique_ptr<FrontendAction> AdaptedAction;
220 
221  /// The set of AST files to merge.
222  std::vector<std::string> ASTFiles;
223 
224 protected:
225  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
226  StringRef InFile) override;
227 
228  bool BeginSourceFileAction(CompilerInstance &CI) override;
229 
230  void ExecuteAction() override;
231  void EndSourceFileAction() override;
232 
233 public:
234  ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
235  ArrayRef<std::string> ASTFiles);
236  ~ASTMergeAction() override;
237 
238  bool usesPreprocessorOnly() const override;
240  bool hasPCHSupport() const override;
241  bool hasASTFileSupport() const override;
242  bool hasCodeCompletionSupport() const override;
243 };
244 
246 protected:
247  void ExecuteAction() override;
248  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
249  StringRef) override {
250  return nullptr;
251  }
252 
253  bool usesPreprocessorOnly() const override { return true; }
254 };
255 
257 protected:
258  void ExecuteAction() override;
259  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
260  StringRef) override {
261  return nullptr;
262  }
263 
264  bool usesPreprocessorOnly() const override { return true; }
265 };
266 
267 //===----------------------------------------------------------------------===//
268 // Preprocessor Actions
269 //===----------------------------------------------------------------------===//
270 
272 protected:
273  void ExecuteAction() override;
274 };
275 
277 protected:
278  void ExecuteAction() override;
279 };
280 
282 protected:
283  void ExecuteAction() override;
284 };
285 
287 protected:
288  void ExecuteAction() override;
289 
290  bool hasPCHSupport() const override { return true; }
291 };
292 
293 } // end namespace clang
294 
295 #endif
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
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:384
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 usesPreprocessorOnly() const override
Does this action only use the preprocessor?
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.
bool hasASTFileSupport() const override
Does this action support use with AST files?
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:378
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...
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we&#39;re handling.
The translation unit is a module.
Definition: LangOptions.h:387