clang 23.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
17namespace 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
29public:
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
42public:
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
54public:
55 bool usesPreprocessorOnly() const override { return true; }
56};
57
58//===----------------------------------------------------------------------===//
59// AST Consumer Actions
60//===----------------------------------------------------------------------===//
61
63protected:
64 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
65 StringRef InFile) override;
66};
67
69protected:
70 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
71 StringRef InFile) override;
72};
73
75protected:
76 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
77 StringRef InFile) override;
78};
79
81protected:
82 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
83 StringRef InFile) override;
84};
85
87protected:
88 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
89 StringRef InFile) override;
90
94
95 bool hasASTFileSupport() const override { return false; }
96
97 bool shouldEraseOutputFiles() override;
98
99public:
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
117public:
118 /// When \c OS is non-null, uses it for outputting the PCM file instead of
119 /// automatically creating an output file.
120 explicit GenerateModuleAction(std::unique_ptr<raw_pwrite_stream> OS = nullptr)
121 : OS(std::move(OS)) {}
122
123private:
124 std::unique_ptr<raw_pwrite_stream> OS;
125
126 virtual std::unique_ptr<raw_pwrite_stream>
127 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
128
129protected:
130 std::vector<std::unique_ptr<ASTConsumer>>
131 CreateMultiplexConsumer(CompilerInstance &CI, StringRef InFile);
132
133 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
134 StringRef InFile) override;
135
139
140 bool hasASTFileSupport() const override { return false; }
141
142 bool shouldEraseOutputFiles() override;
143};
144
146protected:
147 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
148 StringRef InFile) override;
149
153 bool hasASTFileSupport() const override { return false; }
154};
155
157public:
159
160private:
161 bool BeginSourceFileAction(CompilerInstance &CI) override;
162
163 std::unique_ptr<raw_pwrite_stream>
164 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
165};
166
167/// Generates full BMI (which contains full information to generate the object
168/// files) for C++20 Named Modules.
170protected:
171 bool PrepareToExecuteAction(CompilerInstance &CI) override;
172 bool BeginSourceFileAction(CompilerInstance &CI) override;
173
174 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
175 StringRef InFile) override;
176
178
179 std::unique_ptr<raw_pwrite_stream>
180 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
181};
182
183/// Only generates the reduced BMI. This action is mainly used by tests.
186private:
187 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
188 StringRef InFile) override;
189};
190
192
193private:
194 bool BeginSourceFileAction(CompilerInstance &CI) override;
195
196 std::unique_ptr<raw_pwrite_stream>
197 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
198};
199
201protected:
202 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
203 StringRef InFile) override;
204
205public:
206 ~SyntaxOnlyAction() override;
207 bool hasCodeCompletionSupport() const override { return true; }
208};
209
210/// Dump information about the given module file, to be used for
211/// basic debugging and discovery.
213 // Allow other tools (ex lldb) to direct output for their use.
214 std::shared_ptr<llvm::raw_ostream> OutputStream;
215
216protected:
217 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
218 StringRef InFile) override;
219 bool BeginInvocation(CompilerInstance &CI) override;
220 void ExecuteAction() override;
221
222public:
224 explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
225 : OutputStream(Out) {}
226 bool hasPCHSupport() const override { return false; }
227 bool hasASTFileSupport() const override { return true; }
228 bool hasIRSupport() const override { return false; }
229 bool hasCodeCompletionSupport() const override { return false; }
230};
231
233protected:
234 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
235 StringRef InFile) override;
236
237 void ExecuteAction() override;
238
239public:
240 bool hasCodeCompletionSupport() const override { return false; }
241};
242
244protected:
245 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
246 StringRef InFile) override;
247
248 void ExecuteAction() override;
249};
250
251/**
252 * Frontend action adaptor that merges ASTs together.
253 *
254 * This action takes an existing AST file and "merges" it into the AST
255 * context, producing a merged context. This action is an action
256 * adaptor, which forwards most of its calls to another action that
257 * will consume the merged context.
258 */
260 /// The action that the merge action adapts.
261 std::unique_ptr<FrontendAction> AdaptedAction;
262
263 /// The set of AST files to merge.
264 std::vector<std::string> ASTFiles;
265
266protected:
267 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
268 StringRef InFile) override;
269
270 bool BeginSourceFileAction(CompilerInstance &CI) override;
271
272 void ExecuteAction() override;
273 void EndSourceFileAction() override;
274
275public:
276 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
277 ArrayRef<std::string> ASTFiles);
278 ~ASTMergeAction() override;
279
280 bool usesPreprocessorOnly() const override;
282 bool hasPCHSupport() const override;
283 bool hasASTFileSupport() const override;
284 bool hasCodeCompletionSupport() const override;
285};
286
288protected:
289 void ExecuteAction() override;
290 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
291 StringRef) override {
292 return nullptr;
293 }
294
295 bool usesPreprocessorOnly() const override { return true; }
296};
297
299protected:
300 void ExecuteAction() override;
301 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
302 StringRef) override {
303 return nullptr;
304 }
305
306 bool usesPreprocessorOnly() const override { return true; }
307};
308
309//===----------------------------------------------------------------------===//
310// Preprocessor Actions
311//===----------------------------------------------------------------------===//
312
314protected:
315 void ExecuteAction() override;
316};
317
319protected:
320 void ExecuteAction() override;
321};
322
324protected:
325 void ExecuteAction() override;
326};
327
329protected:
330 void ExecuteAction() override;
331
332 bool hasPCHSupport() const override { return true; }
333};
334
335//===----------------------------------------------------------------------===//
336// HLSL Specific Actions
337//===----------------------------------------------------------------------===//
338
340protected:
341 void ExecuteAction() override;
342
343public:
344 HLSLFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
345};
346
347} // end namespace clang
348
349#endif
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition ASTMerge.cpp:112
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition ASTMerge.cpp:83
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition ASTMerge.cpp:100
ASTMergeAction(std::unique_ptr< FrontendAction > AdaptedAction, ArrayRef< std::string > ASTFiles)
Definition ASTMerge.cpp:87
~ASTMergeAction() override
Definition ASTMerge.cpp:93
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition ASTMerge.cpp:96
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition ASTMerge.cpp:108
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition ASTMerge.cpp:33
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition ASTMerge.cpp:24
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition ASTMerge.cpp:20
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition ASTMerge.cpp:104
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
DumpModuleInfoAction(std::shared_ptr< llvm::raw_ostream > Out)
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 hasIRSupport() const override
Does this action support use with IR files?
bool hasPCHSupport() const override
Does this action support use with PCH?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
friend class ASTMergeAction
virtual void ExecuteAction()=0
Callback to run the program action, using the initialized compiler instance.
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool hasASTFileSupport() const override
Does this action support use with AST files?
bool shouldEraseOutputFiles() override
Callback at the end of processing a single input, to determine if the output files should be erased o...
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
bool hasASTFileSupport() const override
Does this action support use with AST files?
std::vector< std::unique_ptr< ASTConsumer > > CreateMultiplexConsumer(CompilerInstance &CI, StringRef InFile)
GenerateModuleAction(std::unique_ptr< raw_pwrite_stream > OS=nullptr)
When OS is non-null, uses it for outputting the PCM file instead of automatically creating an output ...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
GenerateModuleAction(std::unique_ptr< raw_pwrite_stream > OS=nullptr)
When OS is non-null, uses it for outputting the PCM file instead of automatically creating an output ...
Generates full BMI (which contains full information to generate the object files) for C++20 Named Mod...
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
bool PrepareToExecuteAction(CompilerInstance &CI) override
Prepare to execute the action on the given CompilerInstance.
std::unique_ptr< raw_pwrite_stream > CreateOutputFile(CompilerInstance &CI, StringRef InFile) override
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
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.
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
static bool ComputeASTConsumerArguments(CompilerInstance &CI, std::string &Sysroot)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool hasASTFileSupport() const override
Does this action support use with AST files?
bool shouldEraseOutputFiles() override
Callback at the end of processing a single input, to determine if the output files should be erased o...
Only generates the reduced BMI. This action is mainly used by tests.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
HLSLFrontendAction(std::unique_ptr< FrontendAction > WrappedAction)
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Abstract base class to use for preprocessor-based frontend actions.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &, StringRef) override
Create the AST consumer object for this action, if supported.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
bool hasPCHSupport() const override
Does this action support use with PCH?
Preprocessor-based frontend action that also loads PCH files.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
WrapperFrontendAction(std::unique_ptr< FrontendAction > WrappedAction)
Construct a WrapperFrontendAction from an existing action, taking ownership of it.
std::unique_ptr< FrontendAction > WrappedAction
The JSON file list parser is used to communicate input to InstallAPI.
TranslationUnitKind
Describes the kind of translation unit being processed.
@ TU_Complete
The translation unit is a complete translation unit.
@ TU_ClangModule
The translation unit is a clang module.
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.