clang 22.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
92 return TU_Prefix;
93 }
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
117 virtual std::unique_ptr<raw_pwrite_stream>
118 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
119
120protected:
121 std::vector<std::unique_ptr<ASTConsumer>>
122 CreateMultiplexConsumer(CompilerInstance &CI, StringRef InFile);
123
124 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
125 StringRef InFile) override;
126
128 return TU_ClangModule;
129 }
130
131 bool hasASTFileSupport() const override { return false; }
132
133 bool shouldEraseOutputFiles() override;
134};
135
137protected:
138 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
139 StringRef InFile) override;
140
142 return TU_ClangModule;
143 }
144 bool hasASTFileSupport() const override { return false; }
145};
146
148private:
149 bool BeginSourceFileAction(CompilerInstance &CI) override;
150
151 std::unique_ptr<raw_pwrite_stream>
152 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
153};
154
155/// Generates full BMI (which contains full information to generate the object
156/// files) for C++20 Named Modules.
158protected:
159 bool PrepareToExecuteAction(CompilerInstance &CI) override;
160 bool BeginSourceFileAction(CompilerInstance &CI) override;
161
162 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
163 StringRef InFile) override;
164
166
167 std::unique_ptr<raw_pwrite_stream>
168 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
169};
170
171/// Only generates the reduced BMI. This action is mainly used by tests.
174private:
175 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
176 StringRef InFile) override;
177};
178
180
181private:
182 bool BeginSourceFileAction(CompilerInstance &CI) override;
183
184 std::unique_ptr<raw_pwrite_stream>
185 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
186};
187
189protected:
190 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
191 StringRef InFile) override;
192
193public:
194 ~SyntaxOnlyAction() override;
195 bool hasCodeCompletionSupport() const override { return true; }
196};
197
198/// Dump information about the given module file, to be used for
199/// basic debugging and discovery.
201 // Allow other tools (ex lldb) to direct output for their use.
202 std::shared_ptr<llvm::raw_ostream> OutputStream;
203
204protected:
205 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
206 StringRef InFile) override;
207 bool BeginInvocation(CompilerInstance &CI) override;
208 void ExecuteAction() override;
209
210public:
212 explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
213 : OutputStream(Out) {}
214 bool hasPCHSupport() const override { return false; }
215 bool hasASTFileSupport() const override { return true; }
216 bool hasIRSupport() const override { return false; }
217 bool hasCodeCompletionSupport() const override { return false; }
218};
219
221protected:
222 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
223 StringRef InFile) override;
224
225 void ExecuteAction() override;
226
227public:
228 bool hasCodeCompletionSupport() const override { return false; }
229};
230
232protected:
233 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
234 StringRef InFile) override;
235
236 void ExecuteAction() override;
237};
238
239/**
240 * Frontend action adaptor that merges ASTs together.
241 *
242 * This action takes an existing AST file and "merges" it into the AST
243 * context, producing a merged context. This action is an action
244 * adaptor, which forwards most of its calls to another action that
245 * will consume the merged context.
246 */
248 /// The action that the merge action adapts.
249 std::unique_ptr<FrontendAction> AdaptedAction;
250
251 /// The set of AST files to merge.
252 std::vector<std::string> ASTFiles;
253
254protected:
255 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
256 StringRef InFile) override;
257
258 bool BeginSourceFileAction(CompilerInstance &CI) override;
259
260 void ExecuteAction() override;
261 void EndSourceFileAction() override;
262
263public:
264 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
265 ArrayRef<std::string> ASTFiles);
266 ~ASTMergeAction() override;
267
268 bool usesPreprocessorOnly() const override;
270 bool hasPCHSupport() const override;
271 bool hasASTFileSupport() const override;
272 bool hasCodeCompletionSupport() const override;
273};
274
276protected:
277 void ExecuteAction() override;
278 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
279 StringRef) override {
280 return nullptr;
281 }
282
283 bool usesPreprocessorOnly() const override { return true; }
284};
285
287protected:
288 void ExecuteAction() override;
289 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
290 StringRef) override {
291 return nullptr;
292 }
293
294 bool usesPreprocessorOnly() const override { return true; }
295};
296
297//===----------------------------------------------------------------------===//
298// Preprocessor Actions
299//===----------------------------------------------------------------------===//
300
302protected:
303 void ExecuteAction() override;
304};
305
307protected:
308 void ExecuteAction() override;
309};
310
312protected:
313 void ExecuteAction() override;
314};
315
317protected:
318 void ExecuteAction() override;
319
320 bool hasPCHSupport() const override { return true; }
321};
322
324 StringRef ModuleName;
325 void ExecuteAction() override;
326
327public:
329 : ModuleName(ModuleName) {}
330};
331
332//===----------------------------------------------------------------------===//
333// HLSL Specific Actions
334//===----------------------------------------------------------------------===//
335
337protected:
338 void ExecuteAction() override;
339
340public:
341 HLSLFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
342};
343
344} // end namespace clang
345
346#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.
Abstract base class to use for AST consumer-based frontend actions.
Frontend action adaptor that merges ASTs together.
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: ASTMerge.cpp:111
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: ASTMerge.cpp:82
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: ASTMerge.cpp:99
~ASTMergeAction() override
Definition: ASTMerge.cpp:92
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: ASTMerge.cpp:95
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: ASTMerge.cpp:107
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:103
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?
Dump information about the given module file, to be used for basic debugging and discovery.
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.
Abstract base class for actions which can be performed by the frontend.
friend class ASTMergeAction
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)
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
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.
GetDependenciesByModuleNameAction(StringRef ModuleName)
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.
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.
A frontend action which simply wraps some other runtime-specified frontend action.
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.
Definition: LangOptions.h:1097
@ TU_Complete
The translation unit is a complete translation unit.
Definition: LangOptions.h:1099
@ TU_ClangModule
The translation unit is a clang module.
Definition: LangOptions.h:1106
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:1103