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
100
101public:
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, bool SetOnlyIfDifferent = false);
114
115 bool BeginSourceFileAction(CompilerInstance &CI) override;
116
119};
120
122public:
123 /// When \c OS is non-null, uses it for outputting the PCM file instead of
124 /// automatically creating an output file.
125 explicit GenerateModuleAction(std::unique_ptr<raw_pwrite_stream> OS = nullptr)
126 : OS(std::move(OS)) {}
127
128private:
129 std::unique_ptr<raw_pwrite_stream> OS;
130
131 virtual std::unique_ptr<raw_pwrite_stream>
132 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
133
134protected:
135 std::vector<std::unique_ptr<ASTConsumer>>
136 CreateMultiplexConsumer(CompilerInstance &CI, StringRef InFile);
137
138 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
139 StringRef InFile) override;
140
144
145 bool hasASTFileSupport() const override { return false; }
146
147 bool shouldEraseOutputFiles() override;
148};
149
151protected:
152 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
153 StringRef InFile) override;
154
158 bool hasASTFileSupport() const override { return false; }
159};
160
162public:
164
165private:
166 bool BeginSourceFileAction(CompilerInstance &CI) override;
167
168 std::unique_ptr<raw_pwrite_stream>
169 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
170
171 bool SetOnlyIfDifferent;
172
173public:
174 GenerateModuleFromModuleMapAction(bool SetOnlyIfDifferent = false)
175 : SetOnlyIfDifferent(SetOnlyIfDifferent) {}
176};
177
178/// Generates full BMI (which contains full information to generate the object
179/// files) for C++20 Named Modules.
181protected:
182 bool PrepareToExecuteAction(CompilerInstance &CI) override;
183 bool BeginSourceFileAction(CompilerInstance &CI) override;
184
185 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
186 StringRef InFile) override;
187
189
190 std::unique_ptr<raw_pwrite_stream>
191 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
192};
193
194/// Only generates the reduced BMI. This action is mainly used by tests.
197private:
198 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
199 StringRef InFile) override;
200};
201
203
204private:
205 bool BeginSourceFileAction(CompilerInstance &CI) override;
206
207 std::unique_ptr<raw_pwrite_stream>
208 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
209};
210
212protected:
213 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
214 StringRef InFile) override;
215
216public:
217 ~SyntaxOnlyAction() override;
218 bool hasCodeCompletionSupport() const override { return true; }
219};
220
221/// Dump information about the given module file, to be used for
222/// basic debugging and discovery.
224 // Allow other tools (ex lldb) to direct output for their use.
225 std::shared_ptr<llvm::raw_ostream> OutputStream;
226
227protected:
228 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
229 StringRef InFile) override;
230 bool BeginInvocation(CompilerInstance &CI) override;
231 void ExecuteAction() override;
232
233public:
235 explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
236 : OutputStream(Out) {}
237 bool hasPCHSupport() const override { return false; }
238 bool hasASTFileSupport() const override { return true; }
239 bool hasIRSupport() const override { return false; }
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
250public:
251 bool hasCodeCompletionSupport() const override { return false; }
252};
253
255protected:
256 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
257 StringRef InFile) override;
258
259 void ExecuteAction() override;
260};
261
262/**
263 * Frontend action adaptor that merges ASTs together.
264 *
265 * This action takes an existing AST file and "merges" it into the AST
266 * context, producing a merged context. This action is an action
267 * adaptor, which forwards most of its calls to another action that
268 * will consume the merged context.
269 */
271 /// The action that the merge action adapts.
272 std::unique_ptr<FrontendAction> AdaptedAction;
273
274 /// The set of AST files to merge.
275 std::vector<std::string> ASTFiles;
276
277protected:
278 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
279 StringRef InFile) override;
280
281 bool BeginSourceFileAction(CompilerInstance &CI) override;
282
283 void ExecuteAction() override;
284 void EndSourceFileAction() override;
285
286public:
287 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
288 ArrayRef<std::string> ASTFiles);
289 ~ASTMergeAction() override;
290
291 bool usesPreprocessorOnly() const override;
293 bool hasPCHSupport() const override;
294 bool hasASTFileSupport() const override;
295 bool hasCodeCompletionSupport() const override;
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
310protected:
311 void ExecuteAction() override;
312 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
313 StringRef) override {
314 return nullptr;
315 }
316
317 bool usesPreprocessorOnly() const override { return true; }
318};
319
320//===----------------------------------------------------------------------===//
321// Preprocessor Actions
322//===----------------------------------------------------------------------===//
323
325protected:
326 void ExecuteAction() override;
327};
328
330protected:
331 void ExecuteAction() override;
332};
333
335protected:
336 void ExecuteAction() override;
337};
338
340protected:
341 void ExecuteAction() override;
342
343 bool hasPCHSupport() const override { return true; }
344};
345
346//===----------------------------------------------------------------------===//
347// HLSL Specific Actions
348//===----------------------------------------------------------------------===//
349
351protected:
352 void ExecuteAction() override;
353
354public:
355 HLSLFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
356};
357
358} // end namespace clang
359
360#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.
GenerateModuleFromModuleMapAction(bool SetOnlyIfDifferent=false)
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.
GeneratePCHAction(bool SetOnlyIfDifferent=false)
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?
static std::unique_ptr< llvm::raw_pwrite_stream > CreateOutputFile(CompilerInstance &CI, StringRef InFile, std::string &OutputFile, bool SetOnlyIfDifferent=false)
Creates file to write the PCH into and returns a stream to write it into.
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.