clang  15.0.0git
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 <memory>
14 #include <string>
15 #include <vector>
16 
17 namespace clang {
18 
19 //===----------------------------------------------------------------------===//
20 // Custom Consumer Actions
21 //===----------------------------------------------------------------------===//
22 
24  void ExecuteAction() override;
25 
26  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
27  StringRef InFile) override;
28 
29 public:
30  // Don't claim to only use the preprocessor, we want to follow the AST path,
31  // but do nothing.
32  bool usesPreprocessorOnly() const override { return false; }
33 };
34 
35 /// Preprocessor-based frontend action that also loads PCH files.
37  void ExecuteAction() override;
38 
39  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
40  StringRef InFile) override;
41 
42 public:
43  bool usesPreprocessorOnly() const override { return false; }
44 };
45 
47  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
48  StringRef InFile) override {
49  return nullptr;
50  }
51 
52  void ExecuteAction() override;
53 
54 public:
55  bool usesPreprocessorOnly() const override { return true; }
56 };
57 
58 //===----------------------------------------------------------------------===//
59 // AST Consumer Actions
60 //===----------------------------------------------------------------------===//
61 
63 protected:
64  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
65  StringRef InFile) override;
66 };
67 
69 protected:
70  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
71  StringRef InFile) override;
72 };
73 
75 protected:
76  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
77  StringRef InFile) override;
78 };
79 
81 protected:
82  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
83  StringRef InFile) override;
84 };
85 
87 protected:
88  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
89  StringRef InFile) override;
90 
92  return TU_Prefix;
93  }
94 
95  bool hasASTFileSupport() const override { return false; }
96 
97  bool shouldEraseOutputFiles() override;
98 
99 public:
100  /// Compute the AST consumer arguments that will be used to
101  /// create the PCHGenerator instance returned by CreateASTConsumer.
102  ///
103  /// \returns false if an error occurred, true otherwise.
105  std::string &Sysroot);
106 
107  /// Creates file to write the PCH into and returns a stream to write it
108  /// into. On error, returns null.
109  static std::unique_ptr<llvm::raw_pwrite_stream>
110  CreateOutputFile(CompilerInstance &CI, StringRef InFile,
111  std::string &OutputFile);
112 
113  bool BeginSourceFileAction(CompilerInstance &CI) override;
114 };
115 
117  virtual std::unique_ptr<raw_pwrite_stream>
118  CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
119 
120 protected:
121  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
122  StringRef InFile) override;
123 
125  return TU_Module;
126  }
127 
128  bool hasASTFileSupport() const override { return false; }
129 
130  bool shouldEraseOutputFiles() override;
131 };
132 
134 protected:
135  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
136  StringRef InFile) override;
137 
139  bool hasASTFileSupport() const override { return false; }
140 };
141 
143 private:
144  bool BeginSourceFileAction(CompilerInstance &CI) override;
145 
146  std::unique_ptr<raw_pwrite_stream>
147  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
148 };
149 
151 private:
152  bool BeginSourceFileAction(CompilerInstance &CI) override;
153 
154  std::unique_ptr<raw_pwrite_stream>
155  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
156 };
157 
159  /// The synthesized module input buffer for the current compilation.
160  std::unique_ptr<llvm::MemoryBuffer> Buffer;
161  std::vector<std::string> ModuleHeaders;
162 
163 private:
164  bool PrepareToExecuteAction(CompilerInstance &CI) override;
165  bool BeginSourceFileAction(CompilerInstance &CI) override;
166 
167  std::unique_ptr<raw_pwrite_stream>
168  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
169 };
170 
172 
173 private:
174  bool BeginSourceFileAction(CompilerInstance &CI) override;
175 
176  std::unique_ptr<raw_pwrite_stream>
177  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
178 };
179 
181 protected:
182  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
183  StringRef InFile) override;
184 
185 public:
186  ~SyntaxOnlyAction() override;
187  bool hasCodeCompletionSupport() const override { return true; }
188 };
189 
190 /// Dump information about the given module file, to be used for
191 /// basic debugging and discovery.
193 protected:
194  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
195  StringRef InFile) override;
196  bool BeginInvocation(CompilerInstance &CI) override;
197  void ExecuteAction() override;
198 
199 public:
200  bool hasPCHSupport() const override { return false; }
201  bool hasASTFileSupport() const override { return true; }
202  bool hasIRSupport() const override { return false; }
203  bool hasCodeCompletionSupport() const override { return false; }
204 };
205 
207 protected:
208  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
209  StringRef InFile) override;
210 
211  void ExecuteAction() override;
212 
213 public:
214  bool hasCodeCompletionSupport() const override { return false; }
215 };
216 
218 protected:
219  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
220  StringRef InFile) override;
221 
222  void ExecuteAction() override;
223 };
224 
225 /**
226  * Frontend action adaptor that merges ASTs together.
227  *
228  * This action takes an existing AST file and "merges" it into the AST
229  * context, producing a merged context. This action is an action
230  * adaptor, which forwards most of its calls to another action that
231  * will consume the merged context.
232  */
234  /// The action that the merge action adapts.
235  std::unique_ptr<FrontendAction> AdaptedAction;
236 
237  /// The set of AST files to merge.
238  std::vector<std::string> ASTFiles;
239 
240 protected:
241  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
242  StringRef InFile) override;
243 
244  bool BeginSourceFileAction(CompilerInstance &CI) override;
245 
246  void ExecuteAction() override;
247  void EndSourceFileAction() override;
248 
249 public:
250  ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
251  ArrayRef<std::string> ASTFiles);
252  ~ASTMergeAction() override;
253 
254  bool usesPreprocessorOnly() const override;
256  bool hasPCHSupport() const override;
257  bool hasASTFileSupport() const override;
258  bool hasCodeCompletionSupport() const override;
259 };
260 
262 protected:
263  void ExecuteAction() override;
264  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
265  StringRef) override {
266  return nullptr;
267  }
268 
269  bool usesPreprocessorOnly() const override { return true; }
270 };
271 
273 protected:
274  void ExecuteAction() override;
275  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
276  StringRef) override {
277  return nullptr;
278  }
279 
280  bool usesPreprocessorOnly() const override { return true; }
281 };
282 
283 //===----------------------------------------------------------------------===//
284 // Preprocessor Actions
285 //===----------------------------------------------------------------------===//
286 
288 protected:
289  void ExecuteAction() override;
290 };
291 
293 protected:
294  void ExecuteAction() override;
295 };
296 
298 protected:
299  void ExecuteAction() override;
300 };
301 
303 protected:
304  void ExecuteAction() override;
305 
306  bool hasPCHSupport() const override { return true; }
307 };
308 
310  StringRef ModuleName;
311  void ExecuteAction() override;
312 
313 public:
314  GetDependenciesByModuleNameAction(StringRef ModuleName)
315  : ModuleName(ModuleName) {}
316 };
317 
318 } // end namespace clang
319 
320 #endif
clang::ReadPCHAndPreprocessAction
Preprocessor-based frontend action that also loads PCH files.
Definition: FrontendActions.h:36
clang::PrintPreambleAction
Definition: FrontendActions.h:261
clang::TemplightDumpAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:603
clang::GenerateModuleAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:128
clang::PrintDependencyDirectivesSourceMinimizerAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.h:275
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PreprocessorFrontendAction
Abstract base class to use for preprocessor-based frontend actions.
Definition: FrontendAction.h:288
clang::GeneratePCHAction::shouldEraseOutputFiles
bool shouldEraseOutputFiles() override
Callback at the end of processing a single input, to determine if the output files should be erased o...
Definition: FrontendActions.cpp:174
clang::ASTMergeAction::BeginSourceFileAction
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition: ASTMerge.cpp:24
clang::PrintPreprocessedAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:1005
clang::DumpModuleInfoAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: FrontendActions.h:203
clang::ASTViewAction
Definition: FrontendActions.h:80
clang::ASTPrintAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:95
clang::DumpModuleInfoAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:363
clang::ASTMergeAction::hasPCHSupport
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: ASTMerge.cpp:104
clang::VerifyPCHAction
Definition: FrontendActions.h:206
clang::PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:1155
clang::GeneratePCHAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:95
clang::GenerateModuleFromModuleMapAction
Definition: FrontendActions.h:142
FrontendAction.h
clang::FrontendAction::ASTMergeAction
friend class ASTMergeAction
Definition: FrontendAction.h:40
clang::DumpModuleInfoAction::hasPCHSupport
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: FrontendActions.h:200
clang::TemplightDumpAction
Definition: FrontendActions.h:217
clang::DumpModuleInfoAction::BeginInvocation
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
Definition: FrontendActions.cpp:821
clang::ASTMergeAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: ASTMerge.cpp:20
clang::TU_Prefix
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:822
clang::DumpRawTokensAction
Definition: FrontendActions.h:287
clang::DumpTokensAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:979
clang::DumpModuleInfoAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendActions.cpp:848
clang::GeneratePCHAction::CreateOutputFile
static std::unique_ptr< llvm::raw_pwrite_stream > CreateOutputFile(CompilerInstance &CI, StringRef InFile, std::string &OutputFile)
Creates file to write the PCH into and returns a stream to write it into.
Definition: FrontendActions.cpp:162
clang::DumpRawTokensAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:961
clang::DumpCompilerOptionsAction
Definition: FrontendActions.h:46
clang::TU_Module
@ TU_Module
The translation unit is a module.
Definition: LangOptions.h:825
clang::VerifyPCHAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendActions.cpp:373
clang::GenerateModuleAction::shouldEraseOutputFiles
bool shouldEraseOutputFiles() override
Callback at the end of processing a single input, to determine if the output files should be erased o...
Definition: FrontendActions.cpp:212
clang::GetDependenciesByModuleNameAction
Definition: FrontendActions.h:309
clang::GenerateModuleAction
Definition: FrontendActions.h:116
clang::DumpModuleInfoAction::hasIRSupport
bool hasIRSupport() const override
Does this action support use with IR files?
Definition: FrontendActions.h:202
clang::ASTMergeAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: ASTMerge.cpp:112
clang::DumpModuleInfoAction
Dump information about the given module file, to be used for basic debugging and discovery.
Definition: FrontendActions.h:192
clang::SyntaxOnlyAction
Definition: FrontendActions.h:180
clang::ReadPCHAndPreprocessAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:43
clang::SyntaxOnlyAction::~SyntaxOnlyAction
~SyntaxOnlyAction() override
Definition: FrontendActions.cpp:354
clang::GenerateInterfaceStubsAction
Definition: FrontendActions.h:133
clang::ASTMergeAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: ASTMerge.cpp:96
clang::SyntaxOnlyAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: FrontendActions.h:187
clang::DumpModuleInfoAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:201
clang::GenerateModuleAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:186
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::DumpTokensAction
Definition: FrontendActions.h:292
clang::ASTDumpAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:103
clang::ASTMergeAction::~ASTMergeAction
~ASTMergeAction() override
Definition: ASTMerge.cpp:93
clang::GenerateInterfaceStubsAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:139
clang::ASTMergeAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: ASTMerge.cpp:108
clang::PrintPreprocessedAction::hasPCHSupport
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: FrontendActions.h:306
clang::ASTPrintAction
Definition: FrontendActions.h:62
clang::GeneratePCHAction
Definition: FrontendActions.h:86
clang::GeneratePCHAction::ComputeASTConsumerArguments
static bool ComputeASTConsumerArguments(CompilerInstance &CI, std::string &Sysroot)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
Definition: FrontendActions.cpp:150
clang::PreprocessOnlyAction
Definition: FrontendActions.h:297
clang::ASTViewAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:117
clang::PrintPreambleAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.h:264
clang::PrintPreambleAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:269
clang::ASTMergeAction
Frontend action adaptor that merges ASTs together.
Definition: FrontendActions.h:233
llvm::ArrayRef< std::string >
clang::ASTDeclListAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:112
clang::PreprocessOnlyAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:991
clang::PrintPreambleAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:1073
clang::ASTDeclListAction
Definition: FrontendActions.h:74
clang::VerifyPCHAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: FrontendActions.h:214
clang::GeneratePCHAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: FrontendActions.h:91
clang::GenerateHeaderUnitAction
Definition: FrontendActions.h:171
clang::TranslationUnitKind
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:816
clang::PrintPreprocessedAction
Definition: FrontendActions.h:302
clang::FrontendAction
Abstract base class for actions which can be performed by the frontend.
Definition: FrontendAction.h:36
clang::DumpCompilerOptionsAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:55
clang
Definition: CalledOnceCheck.h:17
clang::GenerateModuleInterfaceAction
Definition: FrontendActions.h:150
clang::TemplightDumpAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendActions.cpp:607
clang::PrintDependencyDirectivesSourceMinimizerAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:280
clang::ASTMergeAction::EndSourceFileAction
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: ASTMerge.cpp:83
clang::ASTDumpAction
Definition: FrontendActions.h:68
clang::GenerateInterfaceStubsAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: FrontendActions.h:138
clang::VerifyPCHAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:369
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::PrintDependencyDirectivesSourceMinimizerAction
Definition: FrontendActions.h:272
clang::GenerateHeaderModuleAction
Definition: FrontendActions.h:158
clang::GetDependenciesByModuleNameAction::GetDependenciesByModuleNameAction
GetDependenciesByModuleNameAction(StringRef ModuleName)
Definition: FrontendActions.h:314
clang::ASTMergeAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: ASTMerge.cpp:100
clang::InitOnlyAction
Definition: FrontendActions.h:23
clang::ASTMergeAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: ASTMerge.cpp:33
clang::GenerateModuleAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: FrontendActions.h:124
clang::GenerateInterfaceStubsAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: InterfaceStubFunctionsConsumer.cpp:337
clang::GeneratePCHAction::BeginSourceFileAction
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition: FrontendActions.cpp:180
clang::SyntaxOnlyAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:358
clang::GeneratePCHAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.cpp:122
clang::InitOnlyAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:32