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