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 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTION_H
11 #define LLVM_CLANG_FRONTEND_FRONTENDACTION_H
13 #include "clang/Basic/LLVM.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/OwningPtr.h"
24 class CompilerInstance;
36 IK_PreprocessedObjCXX,
44 /// FrontendAction - Abstract base class for actions which can be performed by
46 class FrontendAction {
47 std::string CurrentFile;
48 InputKind CurrentFileKind;
49 llvm::OwningPtr<ASTUnit> CurrentASTUnit;
50 CompilerInstance *Instance;
51 friend class ASTMergeAction;
52 friend class WrapperFrontendAction;
55 ASTConsumer* CreateWrappedASTConsumer(CompilerInstance &CI,
59 /// @name Implementation Action Interface
62 /// CreateASTConsumer - Create the AST consumer object for this action, if
65 /// This routine is called as part of \see BeginSourceAction(), which will
66 /// fail if the AST consumer cannot be created. This will not be called if the
67 /// action has indicated that it only uses the preprocessor.
69 /// \param CI - The current compiler instance, provided as a convenience, \see
70 /// getCompilerInstance().
72 /// \param InFile - The current input file, provided as a convenience, \see
75 /// \return The new AST consumer, or 0 on failure.
76 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
77 StringRef InFile) = 0;
79 /// \brief Callback before starting processing a single input, giving the
80 /// opportunity to modify the CompilerInvocation or do some other action
81 /// before BeginSourceFileAction is called.
83 /// \return True on success; on failure \see BeginSourceFileAction() and
84 /// ExecutionAction() and EndSourceFileAction() will not be called.
85 virtual bool BeginInvocation(CompilerInstance &CI) { return true; }
87 /// BeginSourceFileAction - Callback at the start of processing a single
90 /// \return True on success; on failure \see ExecutionAction() and
91 /// EndSourceFileAction() will not be called.
92 virtual bool BeginSourceFileAction(CompilerInstance &CI,
97 /// ExecuteAction - Callback to run the program action, using the initialized
98 /// compiler instance.
100 /// This routine is guaranteed to only be called between \see
101 /// BeginSourceFileAction() and \see EndSourceFileAction().
102 virtual void ExecuteAction() = 0;
104 /// EndSourceFileAction - Callback at the end of processing a single input;
105 /// this is guaranteed to only be called following a successful call to
106 /// BeginSourceFileAction (and BeingSourceFile).
107 virtual void EndSourceFileAction() {}
113 virtual ~FrontendAction();
115 /// @name Compiler Instance Access
118 CompilerInstance &getCompilerInstance() const {
119 assert(Instance && "Compiler instance not registered!");
123 void setCompilerInstance(CompilerInstance *Value) { Instance = Value; }
126 /// @name Current File Information
129 bool isCurrentFileAST() const {
130 assert(!CurrentFile.empty() && "No current file!");
131 return CurrentASTUnit != 0;
134 const std::string &getCurrentFile() const {
135 assert(!CurrentFile.empty() && "No current file!");
139 InputKind getCurrentFileKind() const {
140 assert(!CurrentFile.empty() && "No current file!");
141 return CurrentFileKind;
144 ASTUnit &getCurrentASTUnit() const {
145 assert(CurrentASTUnit && "No current AST unit!");
146 return *CurrentASTUnit;
149 ASTUnit *takeCurrentASTUnit() {
150 return CurrentASTUnit.take();
153 void setCurrentFile(StringRef Value, InputKind Kind, ASTUnit *AST = 0);
156 /// @name Supported Modes
159 /// usesPreprocessorOnly - Does this action only use the preprocessor? If so
160 /// no AST context will be created and this action will be invalid with AST
162 virtual bool usesPreprocessorOnly() const = 0;
164 /// \brief For AST-based actions, the kind of translation unit we're handling.
165 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Complete; }
167 /// hasPCHSupport - Does this action support use with PCH?
168 virtual bool hasPCHSupport() const { return !usesPreprocessorOnly(); }
170 /// hasASTFileSupport - Does this action support use with AST files?
171 virtual bool hasASTFileSupport() const { return !usesPreprocessorOnly(); }
173 /// hasIRSupport - Does this action support use with IR files?
174 virtual bool hasIRSupport() const { return false; }
176 /// hasCodeCompletionSupport - Does this action support use with code
178 virtual bool hasCodeCompletionSupport() const { return false; }
181 /// @name Public Action Interface
184 /// BeginSourceFile - Prepare the action for processing the input file \arg
185 /// Filename; this is run after the options and frontend have been
186 /// initialized, but prior to executing any per-file processing.
188 /// \param CI - The compiler instance this action is being run from. The
189 /// action may store and use this object up until the matching EndSourceFile
192 /// \param Filename - The input filename, which will be made available to
193 /// clients via \see getCurrentFile().
195 /// \param InputKind - The type of input. Some input kinds are handled
196 /// specially, for example AST inputs, since the AST file itself contains
197 /// several objects which would normally be owned by the
198 /// CompilerInstance. When processing AST input files, these objects should
199 /// generally not be initialized in the CompilerInstance -- they will
200 /// automatically be shared with the AST file in between \see
201 /// BeginSourceFile() and \see EndSourceFile().
203 /// \return True on success; the compilation of this file should be aborted
204 /// and neither Execute nor EndSourceFile should be called.
205 bool BeginSourceFile(CompilerInstance &CI, StringRef Filename,
208 /// Execute - Set the source managers main input file, and run the action.
211 /// EndSourceFile - Perform any per-file post processing, deallocate per-file
212 /// objects, and run statistics and output file cleanup code.
213 void EndSourceFile();
218 /// ASTFrontendAction - Abstract base class to use for AST consumer based
219 /// frontend actions.
220 class ASTFrontendAction : public FrontendAction {
222 /// ExecuteAction - Implement the ExecuteAction interface by running Sema on
223 /// the already initialized AST consumer.
225 /// This will also take care of instantiating a code completion consumer if
226 /// the user requested it and the action supports it.
227 virtual void ExecuteAction();
230 virtual bool usesPreprocessorOnly() const { return false; }
233 class PluginASTAction : public ASTFrontendAction {
235 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
236 StringRef InFile) = 0;
239 /// ParseArgs - Parse the given plugin command line arguments.
241 /// \param CI - The compiler instance, for use in reporting diagnostics.
242 /// \return True if the parsing succeeded; otherwise the plugin will be
243 /// destroyed and no action run. The plugin is responsible for using the
244 /// CompilerInstance's Diagnostic object to report errors.
245 virtual bool ParseArgs(const CompilerInstance &CI,
246 const std::vector<std::string> &arg) = 0;
249 /// PreprocessorFrontendAction - Abstract base class to use for preprocessor
250 /// based frontend actions.
251 class PreprocessorFrontendAction : public FrontendAction {
253 /// CreateASTConsumer - Provide a default implementation which returns aborts,
254 /// this method should never be called by FrontendAction clients.
255 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
259 virtual bool usesPreprocessorOnly() const { return true; }
262 /// WrapperFrontendAction - A frontend action which simply wraps some other
263 /// runtime specified frontend action. Deriving from this class allows an
264 /// action to inject custom logic around some existing action's behavior. It
265 /// implements every virtual method in the FrontendAction interface by
266 /// forwarding to the wrapped action.
267 class WrapperFrontendAction : public FrontendAction {
268 llvm::OwningPtr<FrontendAction> WrappedAction;
271 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
273 virtual bool BeginInvocation(CompilerInstance &CI);
274 virtual bool BeginSourceFileAction(CompilerInstance &CI,
276 virtual void ExecuteAction();
277 virtual void EndSourceFileAction();
280 /// Construct a WrapperFrontendAction from an existing action, taking
282 WrapperFrontendAction(FrontendAction *WrappedAction);
284 virtual bool usesPreprocessorOnly() const;
285 virtual TranslationUnitKind getTranslationUnitKind();
286 virtual bool hasPCHSupport() const;
287 virtual bool hasASTFileSupport() const;
288 virtual bool hasIRSupport() const;
289 virtual bool hasCodeCompletionSupport() const;
292 } // end namespace clang