clang 18.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::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
122 StringRef InFile) override;
123
125 return TU_Module;
126 }
127
128 bool hasASTFileSupport() const override { return false; }
129
130 bool shouldEraseOutputFiles() override;
131};
132
134protected:
135 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
136 StringRef InFile) override;
137
139 bool hasASTFileSupport() const override { return false; }
140};
141
143private:
144 bool BeginSourceFileAction(CompilerInstance &CI) override;
145
146 std::unique_ptr<raw_pwrite_stream>
147 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
148};
149
151private:
152 bool BeginSourceFileAction(CompilerInstance &CI) override;
153
154 std::unique_ptr<raw_pwrite_stream>
155 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
156};
157
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
168protected:
169 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
170 StringRef InFile) override;
171
172public:
173 ~SyntaxOnlyAction() override;
174 bool hasCodeCompletionSupport() const override { return true; }
175};
176
177/// Dump information about the given module file, to be used for
178/// basic debugging and discovery.
180 // Allow other tools (ex lldb) to direct output for their use.
181 std::shared_ptr<llvm::raw_ostream> OutputStream;
182
183protected:
184 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
185 StringRef InFile) override;
186 bool BeginInvocation(CompilerInstance &CI) override;
187 void ExecuteAction() override;
188
189public:
191 explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
192 : OutputStream(Out) {}
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
200protected:
201 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
202 StringRef InFile) override;
203
204 void ExecuteAction() override;
205
206public:
207 bool hasCodeCompletionSupport() const override { return false; }
208};
209
211protected:
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
233protected:
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
242public:
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
255protected:
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
266protected:
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
281protected:
282 void ExecuteAction() override;
283};
284
286protected:
287 void ExecuteAction() override;
288};
289
291protected:
292 void ExecuteAction() override;
293};
294
296protected:
297 void ExecuteAction() override;
298
299 bool hasPCHSupport() const override { return true; }
300};
301
303 StringRef ModuleName;
304 void ExecuteAction() override;
305
306public:
308 : ModuleName(ModuleName) {}
309};
310
311} // end namespace clang
312
313#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: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() 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?
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::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...
GetDependenciesByModuleNameAction(StringRef ModuleName)
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.
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:923
@ TU_Module
The translation unit is a module.
Definition: LangOptions.h:932
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:929