clang 22.0.0git
FrontendAction.h
Go to the documentation of this file.
1//===-- FrontendAction.h - Generic Frontend Action Interface ----*- 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/// \file
10/// Defines the clang::FrontendAction interface and various convenience
11/// abstract classes (clang::ASTFrontendAction, clang::PluginASTAction,
12/// clang::PreprocessorFrontendAction, and clang::WrapperFrontendAction)
13/// derived from it.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_FRONTEND_FRONTENDACTION_H
18#define LLVM_CLANG_FRONTEND_FRONTENDACTION_H
19
21#include "clang/Basic/LLVM.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Support/Error.h"
28#include <memory>
29#include <string>
30#include <vector>
31
32namespace clang {
33class ASTMergeAction;
34class CompilerInstance;
35
36/// Abstract base class for actions which can be performed by the frontend.
38 FrontendInputFile CurrentInput;
39 std::unique_ptr<ASTUnit> CurrentASTUnit;
40 CompilerInstance *Instance;
41 friend class ASTMergeAction;
43
44private:
45 std::unique_ptr<ASTConsumer> CreateWrappedASTConsumer(CompilerInstance &CI,
46 StringRef InFile);
47
48protected:
49 /// @name Implementation Action Interface
50 /// @{
51
52 /// Prepare to execute the action on the given CompilerInstance.
53 ///
54 /// This is called before executing the action on any inputs, and can modify
55 /// the configuration as needed (including adjusting the input list).
56 virtual bool PrepareToExecuteAction(CompilerInstance &CI) { return true; }
57
58 /// Create the AST consumer object for this action, if supported.
59 ///
60 /// This routine is called as part of BeginSourceFile(), which will
61 /// fail if the AST consumer cannot be created. This will not be called if the
62 /// action has indicated that it only uses the preprocessor.
63 ///
64 /// \param CI - The current compiler instance, provided as a convenience, see
65 /// getCompilerInstance().
66 ///
67 /// \param InFile - The current input file, provided as a convenience, see
68 /// getCurrentFile().
69 ///
70 /// \return The new AST consumer, or null on failure.
71 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
72 StringRef InFile) = 0;
73
74 /// Callback before starting processing a single input, giving the
75 /// opportunity to modify the CompilerInvocation or do some other action
76 /// before BeginSourceFileAction is called.
77 ///
78 /// \return True on success; on failure BeginSourceFileAction(),
79 /// ExecuteAction() and EndSourceFileAction() will not be called.
80 virtual bool BeginInvocation(CompilerInstance &CI) { return true; }
81
82 /// Callback at the start of processing a single input.
83 ///
84 /// \return True on success; on failure ExecutionAction() and
85 /// EndSourceFileAction() will not be called.
87 if (CurrentInput.isPreprocessed())
89 return true;
90 }
91
92 /// Callback to run the program action, using the initialized
93 /// compiler instance.
94 ///
95 /// This is guaranteed to only be called between BeginSourceFileAction()
96 /// and EndSourceFileAction().
97 virtual void ExecuteAction() = 0;
98
99 /// Callback at the end of processing a single input.
100 ///
101 /// This is guaranteed to only be called following a successful call to
102 /// BeginSourceFileAction (and BeginSourceFile).
103 virtual void EndSourceFileAction() {
104 if (CurrentInput.isPreprocessed())
105 // Reset the preprocessor macro expansion to the default.
107 }
108
109 /// Callback at the end of processing a single input, to determine
110 /// if the output files should be erased or not.
111 ///
112 /// By default it returns true if a compiler error occurred.
113 /// This is guaranteed to only be called following a successful call to
114 /// BeginSourceFileAction (and BeginSourceFile).
116
117 /// @}
118
119public:
122
123 /// @name Compiler Instance Access
124 /// @{
125
127 assert(Instance && "Compiler instance not registered!");
128 return *Instance;
129 }
130
132
133 /// @}
134 /// @name Current File Information
135 /// @{
136
137 bool isCurrentFileAST() const {
138 assert(!CurrentInput.isEmpty() && "No current file!");
139 return (bool)CurrentASTUnit;
140 }
141
143 return CurrentInput;
144 }
145
146 StringRef getCurrentFile() const {
147 assert(!CurrentInput.isEmpty() && "No current file!");
148 return CurrentInput.getFile();
149 }
150
151 StringRef getCurrentFileOrBufferName() const {
152 assert(!CurrentInput.isEmpty() && "No current file!");
153 return CurrentInput.isFile()
154 ? CurrentInput.getFile()
155 : CurrentInput.getBuffer().getBufferIdentifier();
156 }
157
159 assert(!CurrentInput.isEmpty() && "No current file!");
160 return CurrentInput.getKind();
161 }
162
164 assert(CurrentASTUnit && "No current AST unit!");
165 return *CurrentASTUnit;
166 }
167
169
170 std::unique_ptr<ASTUnit> takeCurrentASTUnit() {
171 return std::move(CurrentASTUnit);
172 }
173
174 void setCurrentInput(const FrontendInputFile &CurrentInput,
175 std::unique_ptr<ASTUnit> AST = nullptr);
176
177 /// @}
178 /// @name Supported Modes
179 /// @{
180
181 /// Is this action invoked on a model file?
182 ///
183 /// Model files are incomplete translation units that relies on type
184 /// information from another translation unit. Check ParseModelFileAction for
185 /// details.
186 virtual bool isModelParsingAction() const { return false; }
187
188 /// Does this action only use the preprocessor?
189 ///
190 /// If so no AST context will be created and this action will be invalid
191 /// with AST file inputs.
192 virtual bool usesPreprocessorOnly() const = 0;
193
194 /// For AST-based actions, the kind of translation unit we're handling.
196 // The ASTContext, if exists, knows the exact TUKind of the frondend.
197 if (Instance && Instance->hasASTContext())
198 return Instance->getASTContext().TUKind;
199 return TU_Complete;
200 }
201
202 /// Does this action support use with PCH?
203 virtual bool hasPCHSupport() const { return true; }
204
205 /// Does this action support use with AST files?
206 virtual bool hasASTFileSupport() const { return true; }
207
208 /// Does this action support use with IR files?
209 virtual bool hasIRSupport() const { return false; }
210
211 /// Does this action support use with code completion?
212 virtual bool hasCodeCompletionSupport() const { return false; }
213
214 /// @}
215 /// @name Public Action Interface
216 /// @{
217
218 /// Prepare the action to execute on the given compiler instance.
220 return PrepareToExecuteAction(CI);
221 }
222
223 /// Prepare the action for processing the input file \p Input.
224 ///
225 /// This is run after the options and frontend have been initialized,
226 /// but prior to executing any per-file processing.
227 ///
228 /// \param CI - The compiler instance this action is being run from. The
229 /// action may store and use this object up until the matching EndSourceFile
230 /// action.
231 ///
232 /// \param Input - The input filename and kind. Some input kinds are handled
233 /// specially, for example AST inputs, since the AST file itself contains
234 /// several objects which would normally be owned by the
235 /// CompilerInstance. When processing AST input files, these objects should
236 /// generally not be initialized in the CompilerInstance -- they will
237 /// automatically be shared with the AST file in between
238 /// BeginSourceFile() and EndSourceFile().
239 ///
240 /// \return True on success; on failure the compilation of this file should
241 /// be aborted and neither Execute() nor EndSourceFile() should be called.
243
244 /// Set the source manager's main input file, and run the action.
245 llvm::Error Execute();
246
247 /// Perform any per-file post processing, deallocate per-file
248 /// objects, and run statistics and output file cleanup code.
249 virtual void EndSourceFile();
250
251 /// @}
252};
253
254/// Abstract base class to use for AST consumer-based frontend actions.
256protected:
257 /// Implement the ExecuteAction interface by running Sema on
258 /// the already-initialized AST consumer.
259 ///
260 /// This will also take care of instantiating a code completion consumer if
261 /// the user requested it and the action supports it.
262 void ExecuteAction() override;
263
264public:
266 bool usesPreprocessorOnly() const override { return false; }
267};
268
270 virtual void anchor();
271public:
272 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
273 StringRef InFile) override = 0;
274
275 /// Parse the given plugin command line arguments.
276 ///
277 /// \param CI - The compiler instance, for use in reporting diagnostics.
278 /// \return True if the parsing succeeded; otherwise the plugin will be
279 /// destroyed and no action run. The plugin is responsible for using the
280 /// CompilerInstance's Diagnostic object to report errors.
281 virtual bool ParseArgs(const CompilerInstance &CI,
282 const std::vector<std::string> &arg) = 0;
283
285 CmdlineBeforeMainAction, ///< Execute the action before the main action if
286 ///< on the command line
287 CmdlineAfterMainAction, ///< Execute the action after the main action if on
288 ///< the command line
289 ReplaceAction, ///< Replace the main action
290 AddBeforeMainAction, ///< Execute the action before the main action
291 AddAfterMainAction ///< Execute the action after the main action
292 };
293 /// Get the action type for this plugin
294 ///
295 /// \return The action type. By default we use CmdlineAfterMainAction.
297};
298
299/// Abstract base class to use for preprocessor-based frontend actions.
301protected:
302 /// Provide a default implementation which returns aborts;
303 /// this method should never be called by FrontendAction clients.
304 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
305 StringRef InFile) override;
306
307public:
308 bool usesPreprocessorOnly() const override { return true; }
309};
310
311/// A frontend action which simply wraps some other runtime-specified
312/// frontend action.
313///
314/// Deriving from this class allows an action to inject custom logic around
315/// some existing action's behavior. It implements every virtual method in
316/// the FrontendAction interface by forwarding to the wrapped action.
318protected:
319 std::unique_ptr<FrontendAction> WrappedAction;
320
322 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
323 StringRef InFile) override;
326 void ExecuteAction() override;
327 void EndSourceFile() override;
328 void EndSourceFileAction() override;
329 bool shouldEraseOutputFiles() override;
330
331public:
332 /// Construct a WrapperFrontendAction from an existing action, taking
333 /// ownership of it.
334 WrapperFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
335
336 bool usesPreprocessorOnly() const override;
338 bool hasPCHSupport() const override;
339 bool hasASTFileSupport() const override;
340 bool hasIRSupport() const override;
341 bool hasCodeCompletionSupport() const override;
342};
343
344} // end namespace clang
345
346#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
Abstract base class to use for AST consumer-based frontend actions.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Frontend action adaptor that merges ASTs together.
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:90
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Abstract base class for actions which can be performed by the frontend.
const FrontendInputFile & getCurrentInput() const
InputKind getCurrentFileKind() const
virtual bool hasIRSupport() const
Does this action support use with IR files?
virtual void EndSourceFileAction()
Callback at the end of processing a single input.
bool PrepareToExecute(CompilerInstance &CI)
Prepare the action to execute on the given compiler instance.
virtual std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile)=0
Create the AST consumer object for this action, if supported.
virtual bool shouldEraseOutputFiles()
Callback at the end of processing a single input, to determine if the output files should be erased o...
bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input)
Prepare the action for processing the input file Input.
ASTUnit & getCurrentASTUnit() const
CompilerInstance & getCompilerInstance() const
virtual bool PrepareToExecuteAction(CompilerInstance &CI)
Prepare to execute the action on the given CompilerInstance.
virtual bool hasASTFileSupport() const
Does this action support use with AST files?
StringRef getCurrentFile() const
std::unique_ptr< ASTUnit > takeCurrentASTUnit()
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
virtual void ExecuteAction()=0
Callback to run the program action, using the initialized compiler instance.
llvm::Error Execute()
Set the source manager's main input file, and run the action.
virtual void EndSourceFile()
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
void setCompilerInstance(CompilerInstance *Value)
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we're handling.
virtual bool hasCodeCompletionSupport() const
Does this action support use with code completion?
StringRef getCurrentFileOrBufferName() const
virtual bool isModelParsingAction() const
Is this action invoked on a model file?
bool isCurrentFileAST() const
virtual bool usesPreprocessorOnly() const =0
Does this action only use the preprocessor?
void setCurrentInput(const FrontendInputFile &CurrentInput, std::unique_ptr< ASTUnit > AST=nullptr)
virtual bool BeginInvocation(CompilerInstance &CI)
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
virtual bool hasPCHSupport() const
Does this action support use with PCH?
Module * getCurrentModule() const
An input file for the front end.
llvm::MemoryBufferRef getBuffer() const
InputKind getKind() const
StringRef getFile() const
The kind of a file that we've been handed as an input.
Describes a module or submodule.
Definition: Module.h:144
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override=0
Create the AST consumer object for this action, if supported.
virtual bool ParseArgs(const CompilerInstance &CI, const std::vector< std::string > &arg)=0
Parse the given plugin command line arguments.
@ AddAfterMainAction
Execute the action after the main action.
@ ReplaceAction
Replace the main action.
@ AddBeforeMainAction
Execute the action before the main action.
@ CmdlineBeforeMainAction
Execute the action before the main action if on the command line.
@ CmdlineAfterMainAction
Execute the action after the main action if on the command line.
virtual ActionType getActionType()
Get the action type for this plugin.
Abstract base class to use for preprocessor-based frontend actions.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Provide a default implementation which returns aborts; this method should never be called by Frontend...
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
void SetMacroExpansionOnlyInDirectives()
Disables macro expansion everywhere except for preprocessor directives.
void SetEnableMacroExpansion()
A frontend action which simply wraps some other runtime-specified frontend action.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool hasPCHSupport() const override
Does this action support use with PCH?
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
bool hasIRSupport() const override
Does this action support use with IR files?
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
WrapperFrontendAction(std::unique_ptr< FrontendAction > WrappedAction)
Construct a WrapperFrontendAction from an existing action, taking ownership of it.
bool PrepareToExecuteAction(CompilerInstance &CI) override
Prepare to execute the action on the given CompilerInstance.
void EndSourceFileAction() override
Callback at the end of processing a single input.
bool hasASTFileSupport() const override
Does this action support use with AST files?
std::unique_ptr< FrontendAction > WrappedAction
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
bool shouldEraseOutputFiles() override
Callback at the end of processing a single input, to determine if the output files should be erased o...
void EndSourceFile() override
Perform any per-file post processing, deallocate per-file objects, and run statistics and output file...
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