clang  13.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 
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.
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  bool shouldEraseOutputFiles() override;
122 };
123 
125 protected:
126  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
127  StringRef InFile) override;
128 
130  bool hasASTFileSupport() const override { return false; }
131 };
132 
134 private:
135  bool BeginSourceFileAction(CompilerInstance &CI) override;
136 
137  std::unique_ptr<raw_pwrite_stream>
138  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
139 };
140 
142 private:
143  bool BeginSourceFileAction(CompilerInstance &CI) override;
144 
145  std::unique_ptr<raw_pwrite_stream>
146  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
147 };
148 
150  /// The synthesized module input buffer for the current compilation.
151  std::unique_ptr<llvm::MemoryBuffer> Buffer;
152  std::vector<std::string> ModuleHeaders;
153 
154 private:
155  bool PrepareToExecuteAction(CompilerInstance &CI) override;
156  bool BeginSourceFileAction(CompilerInstance &CI) override;
157 
158  std::unique_ptr<raw_pwrite_stream>
159  CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
160 };
161 
163 protected:
164  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
165  StringRef InFile) override;
166 
167 public:
168  ~SyntaxOnlyAction() override;
169  bool hasCodeCompletionSupport() const override { return true; }
170 };
171 
172 /// Dump information about the given module file, to be used for
173 /// basic debugging and discovery.
175 protected:
176  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
177  StringRef InFile) override;
178  bool BeginInvocation(CompilerInstance &CI) override;
179  void ExecuteAction() override;
180 
181 public:
182  bool hasPCHSupport() const override { return false; }
183  bool hasASTFileSupport() const override { return true; }
184  bool hasIRSupport() const override { return false; }
185  bool hasCodeCompletionSupport() const override { return false; }
186 };
187 
189 protected:
190  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
191  StringRef InFile) override;
192 
193  void ExecuteAction() override;
194 
195 public:
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 
207 /**
208  * Frontend action adaptor that merges ASTs together.
209  *
210  * This action takes an existing AST file and "merges" it into the AST
211  * context, producing a merged context. This action is an action
212  * adaptor, which forwards most of its calls to another action that
213  * will consume the merged context.
214  */
216  /// The action that the merge action adapts.
217  std::unique_ptr<FrontendAction> AdaptedAction;
218 
219  /// The set of AST files to merge.
220  std::vector<std::string> ASTFiles;
221 
222 protected:
223  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
224  StringRef InFile) override;
225 
226  bool BeginSourceFileAction(CompilerInstance &CI) override;
227 
228  void ExecuteAction() override;
229  void EndSourceFileAction() override;
230 
231 public:
232  ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
233  ArrayRef<std::string> ASTFiles);
234  ~ASTMergeAction() override;
235 
236  bool usesPreprocessorOnly() const override;
238  bool hasPCHSupport() const override;
239  bool hasASTFileSupport() const override;
240  bool hasCodeCompletionSupport() const override;
241 };
242 
244 protected:
245  void ExecuteAction() override;
246  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
247  StringRef) override {
248  return nullptr;
249  }
250 
251  bool usesPreprocessorOnly() const override { return true; }
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 
265 //===----------------------------------------------------------------------===//
266 // Preprocessor Actions
267 //===----------------------------------------------------------------------===//
268 
270 protected:
271  void ExecuteAction() override;
272 };
273 
275 protected:
276  void ExecuteAction() override;
277 };
278 
280 protected:
281  void ExecuteAction() override;
282 };
283 
285 protected:
286  void ExecuteAction() override;
287 
288  bool hasPCHSupport() const override { return true; }
289 };
290 
291 } // end namespace clang
292 
293 #endif
clang::PrintPreambleAction
Definition: FrontendActions.h:243
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:493
clang::GenerateModuleAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:119
clang::PrintDependencyDirectivesSourceMinimizerAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.h:257
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:287
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:149
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:795
clang::DumpModuleInfoAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: FrontendActions.h:185
clang::ASTViewAction
Definition: FrontendActions.h:71
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:70
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:322
clang::ASTMergeAction::hasPCHSupport
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: ASTMerge.cpp:104
clang::VerifyPCHAction
Definition: FrontendActions.h:188
clang::PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:944
clang::GeneratePCHAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:86
clang::GenerateModuleFromModuleMapAction
Definition: FrontendActions.h:133
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:182
clang::TemplightDumpAction
Definition: FrontendActions.h:199
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:711
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:660
clang::DumpRawTokensAction
Definition: FrontendActions.h:269
clang::DumpTokensAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:769
clang::DumpModuleInfoAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendActions.cpp:718
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:137
clang::DumpRawTokensAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:751
clang::DumpCompilerOptionsAction
Definition: FrontendActions.h:37
clang::TU_Module
@ TU_Module
The translation unit is a module.
Definition: LangOptions.h:663
clang::VerifyPCHAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendActions.cpp:332
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:187
clang::GenerateModuleAction
Definition: FrontendActions.h:107
clang::DumpModuleInfoAction::hasIRSupport
bool hasIRSupport() const override
Does this action support use with IR files?
Definition: FrontendActions.h:184
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:174
clang::index::SymbolKind::Module
@ Module
clang::SyntaxOnlyAction
Definition: FrontendActions.h:162
clang::SyntaxOnlyAction::~SyntaxOnlyAction
~SyntaxOnlyAction() override
Definition: FrontendActions.cpp:313
clang::GenerateInterfaceStubsAction
Definition: FrontendActions.h:124
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:169
clang::DumpModuleInfoAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: FrontendActions.h:183
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:161
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:73
clang::DumpTokensAction
Definition: FrontendActions.h:274
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:78
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:130
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:288
clang::ASTPrintAction
Definition: FrontendActions.h:53
clang::GeneratePCHAction
Definition: FrontendActions.h:77
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:125
clang::PreprocessOnlyAction
Definition: FrontendActions.h:279
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:92
clang::PrintPreambleAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
Definition: FrontendActions.h:246
clang::PrintPreambleAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:251
clang::ASTMergeAction
Frontend action adaptor that merges ASTs together.
Definition: FrontendActions.h:215
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:87
clang::PreprocessOnlyAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:781
clang::PrintPreambleAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: FrontendActions.cpp:863
clang::ASTDeclListAction
Definition: FrontendActions.h:65
clang::VerifyPCHAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: FrontendActions.h:196
clang::GeneratePCHAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: FrontendActions.h:82
clang::TranslationUnitKind
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:654
clang::PrintPreprocessedAction
Definition: FrontendActions.h:284
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:46
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::GenerateModuleInterfaceAction
Definition: FrontendActions.h:141
clang::TemplightDumpAction::ExecuteAction
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Definition: FrontendActions.cpp:497
clang::PrintDependencyDirectivesSourceMinimizerAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:262
clang::ASTMergeAction::EndSourceFileAction
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: ASTMerge.cpp:83
clang::ASTDumpAction
Definition: FrontendActions.h:59
clang::GenerateInterfaceStubsAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: FrontendActions.h:129
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:328
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::PrintDependencyDirectivesSourceMinimizerAction
Definition: FrontendActions.h:254
clang::GenerateHeaderModuleAction
Definition: FrontendActions.h:149
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:115
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:340
clang::GeneratePCHAction::BeginSourceFileAction
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition: FrontendActions.cpp:155
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:317
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:97
clang::InitOnlyAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: FrontendActions.h:34