1 //===-- FrontendAction.h - Generic Frontend Action Interface ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines the clang::FrontendAction interface and various convenience
12 /// abstract classes (clang::ASTFrontendAction, clang::PluginASTAction,
13 /// clang::PreprocessorFrontendAction, and clang::WrapperFrontendAction)
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTION_H
19 #define LLVM_CLANG_FRONTEND_FRONTENDACTION_H
21 #include "clang/AST/ASTConsumer.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/Basic/LangOptions.h"
24 #include "clang/Frontend/ASTUnit.h"
25 #include "clang/Frontend/FrontendOptions.h"
26 #include "llvm/ADT/StringRef.h"
33 class CompilerInstance;
35 /// Abstract base class for actions which can be performed by the frontend.
36 class FrontendAction {
37 FrontendInputFile CurrentInput;
38 std::unique_ptr<ASTUnit> CurrentASTUnit;
39 CompilerInstance *Instance;
40 friend class ASTMergeAction;
41 friend class WrapperFrontendAction;
44 std::unique_ptr<ASTConsumer> CreateWrappedASTConsumer(CompilerInstance &CI,
48 /// @name Implementation Action Interface
51 /// Prepare to execute the action on the given CompilerInstance.
53 /// This is called before executing the action on any inputs, and can modify
54 /// the configuration as needed (including adjusting the input list).
55 virtual bool PrepareToExecuteAction(CompilerInstance &CI) { return true; }
57 /// Create the AST consumer object for this action, if supported.
59 /// This routine is called as part of BeginSourceFile(), which will
60 /// fail if the AST consumer cannot be created. This will not be called if the
61 /// action has indicated that it only uses the preprocessor.
63 /// \param CI - The current compiler instance, provided as a convenience, see
64 /// getCompilerInstance().
66 /// \param InFile - The current input file, provided as a convenience, see
69 /// \return The new AST consumer, or null on failure.
70 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
71 StringRef InFile) = 0;
73 /// Callback before starting processing a single input, giving the
74 /// opportunity to modify the CompilerInvocation or do some other action
75 /// before BeginSourceFileAction is called.
77 /// \return True on success; on failure BeginSourceFileAction(),
78 /// ExecuteAction() and EndSourceFileAction() will not be called.
79 virtual bool BeginInvocation(CompilerInstance &CI) { return true; }
81 /// Callback at the start of processing a single input.
83 /// \return True on success; on failure ExecutionAction() and
84 /// EndSourceFileAction() will not be called.
85 virtual bool BeginSourceFileAction(CompilerInstance &CI) {
89 /// Callback to run the program action, using the initialized
90 /// compiler instance.
92 /// This is guaranteed to only be called between BeginSourceFileAction()
93 /// and EndSourceFileAction().
94 virtual void ExecuteAction() = 0;
96 /// Callback at the end of processing a single input.
98 /// This is guaranteed to only be called following a successful call to
99 /// BeginSourceFileAction (and BeginSourceFile).
100 virtual void EndSourceFileAction() {}
102 /// Callback at the end of processing a single input, to determine
103 /// if the output files should be erased or not.
105 /// By default it returns true if a compiler error occurred.
106 /// This is guaranteed to only be called following a successful call to
107 /// BeginSourceFileAction (and BeginSourceFile).
108 virtual bool shouldEraseOutputFiles();
114 virtual ~FrontendAction();
116 /// @name Compiler Instance Access
119 CompilerInstance &getCompilerInstance() const {
120 assert(Instance && "Compiler instance not registered!");
124 void setCompilerInstance(CompilerInstance *Value) { Instance = Value; }
127 /// @name Current File Information
130 bool isCurrentFileAST() const {
131 assert(!CurrentInput.isEmpty() && "No current file!");
132 return (bool)CurrentASTUnit;
135 const FrontendInputFile &getCurrentInput() const {
139 StringRef getCurrentFile() const {
140 assert(!CurrentInput.isEmpty() && "No current file!");
141 return CurrentInput.getFile();
144 StringRef getCurrentFileOrBufferName() const {
145 assert(!CurrentInput.isEmpty() && "No current file!");
146 return CurrentInput.isFile()
147 ? CurrentInput.getFile()
148 : CurrentInput.getBuffer()->getBufferIdentifier();
151 InputKind getCurrentFileKind() const {
152 assert(!CurrentInput.isEmpty() && "No current file!");
153 return CurrentInput.getKind();
156 ASTUnit &getCurrentASTUnit() const {
157 assert(CurrentASTUnit && "No current AST unit!");
158 return *CurrentASTUnit;
161 Module *getCurrentModule() const;
163 std::unique_ptr<ASTUnit> takeCurrentASTUnit() {
164 return std::move(CurrentASTUnit);
167 void setCurrentInput(const FrontendInputFile &CurrentInput,
168 std::unique_ptr<ASTUnit> AST = nullptr);
171 /// @name Supported Modes
174 /// Is this action invoked on a model file?
176 /// Model files are incomplete translation units that relies on type
177 /// information from another translation unit. Check ParseModelFileAction for
179 virtual bool isModelParsingAction() const { return false; }
181 /// Does this action only use the preprocessor?
183 /// If so no AST context will be created and this action will be invalid
184 /// with AST file inputs.
185 virtual bool usesPreprocessorOnly() const = 0;
187 /// For AST-based actions, the kind of translation unit we're handling.
188 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Complete; }
190 /// Does this action support use with PCH?
191 virtual bool hasPCHSupport() const { return true; }
193 /// Does this action support use with AST files?
194 virtual bool hasASTFileSupport() const { return true; }
196 /// Does this action support use with IR files?
197 virtual bool hasIRSupport() const { return false; }
199 /// Does this action support use with code completion?
200 virtual bool hasCodeCompletionSupport() const { return false; }
203 /// @name Public Action Interface
206 /// Prepare the action to execute on the given compiler instance.
207 bool PrepareToExecute(CompilerInstance &CI) {
208 return PrepareToExecuteAction(CI);
211 /// Prepare the action for processing the input file \p Input.
213 /// This is run after the options and frontend have been initialized,
214 /// but prior to executing any per-file processing.
216 /// \param CI - The compiler instance this action is being run from. The
217 /// action may store and use this object up until the matching EndSourceFile
220 /// \param Input - The input filename and kind. Some input kinds are handled
221 /// specially, for example AST inputs, since the AST file itself contains
222 /// several objects which would normally be owned by the
223 /// CompilerInstance. When processing AST input files, these objects should
224 /// generally not be initialized in the CompilerInstance -- they will
225 /// automatically be shared with the AST file in between
226 /// BeginSourceFile() and EndSourceFile().
228 /// \return True on success; on failure the compilation of this file should
229 /// be aborted and neither Execute() nor EndSourceFile() should be called.
230 bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input);
232 /// Set the source manager's main input file, and run the action.
235 /// Perform any per-file post processing, deallocate per-file
236 /// objects, and run statistics and output file cleanup code.
237 void EndSourceFile();
242 /// Abstract base class to use for AST consumer-based frontend actions.
243 class ASTFrontendAction : public FrontendAction {
245 /// Implement the ExecuteAction interface by running Sema on
246 /// the already-initialized AST consumer.
248 /// This will also take care of instantiating a code completion consumer if
249 /// the user requested it and the action supports it.
250 void ExecuteAction() override;
253 ASTFrontendAction() {}
254 bool usesPreprocessorOnly() const override { return false; }
257 class PluginASTAction : public ASTFrontendAction {
258 virtual void anchor();
260 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
261 StringRef InFile) override = 0;
263 /// Parse the given plugin command line arguments.
265 /// \param CI - The compiler instance, for use in reporting diagnostics.
266 /// \return True if the parsing succeeded; otherwise the plugin will be
267 /// destroyed and no action run. The plugin is responsible for using the
268 /// CompilerInstance's Diagnostic object to report errors.
269 virtual bool ParseArgs(const CompilerInstance &CI,
270 const std::vector<std::string> &arg) = 0;
273 Cmdline, ///< Action is determined by the cc1 command-line
274 ReplaceAction, ///< Replace the main action
275 AddBeforeMainAction, ///< Execute the action before the main action
276 AddAfterMainAction ///< Execute the action after the main action
278 /// Get the action type for this plugin
280 /// \return The action type. If the type is Cmdline then by default the
281 /// plugin does nothing and what it does is determined by the cc1
283 virtual ActionType getActionType() { return Cmdline; }
286 /// Abstract base class to use for preprocessor-based frontend actions.
287 class PreprocessorFrontendAction : public FrontendAction {
289 /// Provide a default implementation which returns aborts;
290 /// this method should never be called by FrontendAction clients.
291 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
292 StringRef InFile) override;
295 bool usesPreprocessorOnly() const override { return true; }
298 /// A frontend action which simply wraps some other runtime-specified
301 /// Deriving from this class allows an action to inject custom logic around
302 /// some existing action's behavior. It implements every virtual method in
303 /// the FrontendAction interface by forwarding to the wrapped action.
304 class WrapperFrontendAction : public FrontendAction {
305 std::unique_ptr<FrontendAction> WrappedAction;
308 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
309 StringRef InFile) override;
310 bool BeginInvocation(CompilerInstance &CI) override;
311 bool BeginSourceFileAction(CompilerInstance &CI) override;
312 void ExecuteAction() override;
313 void EndSourceFileAction() override;
316 /// Construct a WrapperFrontendAction from an existing action, taking
318 WrapperFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
320 bool usesPreprocessorOnly() const override;
321 TranslationUnitKind getTranslationUnitKind() override;
322 bool hasPCHSupport() const override;
323 bool hasASTFileSupport() const override;
324 bool hasIRSupport() const override;
325 bool hasCodeCompletionSupport() const override;
328 } // end namespace clang