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 "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/OwningPtr.h"
26 class CompilerInstance;
38 IK_PreprocessedObjCXX,
46 /// FrontendAction - Abstract base class for actions which can be performed by
48 class FrontendAction {
49 std::string CurrentFile;
50 InputKind CurrentFileKind;
51 llvm::OwningPtr<ASTUnit> CurrentASTUnit;
52 CompilerInstance *Instance;
53 friend class ASTMergeAction;
54 friend class WrapperFrontendAction;
57 ASTConsumer* CreateWrappedASTConsumer(CompilerInstance &CI,
58 llvm::StringRef InFile);
61 /// @name Implementation Action Interface
64 /// CreateASTConsumer - Create the AST consumer object for this action, if
67 /// This routine is called as part of \see BeginSourceAction(), which will
68 /// fail if the AST consumer cannot be created. This will not be called if the
69 /// action has indicated that it only uses the preprocessor.
71 /// \param CI - The current compiler instance, provided as a convenience, \see
72 /// getCompilerInstance().
74 /// \param InFile - The current input file, provided as a convenience, \see
77 /// \return The new AST consumer, or 0 on failure.
78 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
79 llvm::StringRef InFile) = 0;
81 /// \brief Callback before starting processing a single input, giving the
82 /// opportunity to modify the CompilerInvocation or do some other action
83 /// before BeginSourceFileAction is called.
85 /// \return True on success; on failure \see BeginSourceFileAction() and
86 /// ExecutionAction() and EndSourceFileAction() will not be called.
87 virtual bool BeginInvocation(CompilerInstance &CI) { return true; }
89 /// BeginSourceFileAction - Callback at the start of processing a single
92 /// \return True on success; on failure \see ExecutionAction() and
93 /// EndSourceFileAction() will not be called.
94 virtual bool BeginSourceFileAction(CompilerInstance &CI,
95 llvm::StringRef Filename) {
99 /// ExecuteAction - Callback to run the program action, using the initialized
100 /// compiler instance.
102 /// This routine is guaranteed to only be called between \see
103 /// BeginSourceFileAction() and \see EndSourceFileAction().
104 virtual void ExecuteAction() = 0;
106 /// EndSourceFileAction - Callback at the end of processing a single input;
107 /// this is guaranteed to only be called following a successful call to
108 /// BeginSourceFileAction (and BeingSourceFile).
109 virtual void EndSourceFileAction() {}
115 virtual ~FrontendAction();
117 /// @name Compiler Instance Access
120 CompilerInstance &getCompilerInstance() const {
121 assert(Instance && "Compiler instance not registered!");
125 void setCompilerInstance(CompilerInstance *Value) { Instance = Value; }
128 /// @name Current File Information
131 bool isCurrentFileAST() const {
132 assert(!CurrentFile.empty() && "No current file!");
133 return CurrentASTUnit != 0;
136 const std::string &getCurrentFile() const {
137 assert(!CurrentFile.empty() && "No current file!");
141 InputKind getCurrentFileKind() const {
142 assert(!CurrentFile.empty() && "No current file!");
143 return CurrentFileKind;
146 ASTUnit &getCurrentASTUnit() const {
147 assert(CurrentASTUnit && "No current AST unit!");
148 return *CurrentASTUnit;
151 ASTUnit *takeCurrentASTUnit() {
152 return CurrentASTUnit.take();
155 void setCurrentFile(llvm::StringRef Value, InputKind Kind, ASTUnit *AST = 0);
158 /// @name Supported Modes
161 /// usesPreprocessorOnly - Does this action only use the preprocessor? If so
162 /// no AST context will be created and this action will be invalid with AST
164 virtual bool usesPreprocessorOnly() const = 0;
166 /// usesCompleteTranslationUnit - For AST based actions, should the
167 /// translation unit be completed?
168 virtual bool usesCompleteTranslationUnit() { return true; }
170 /// hasPCHSupport - Does this action support use with PCH?
171 virtual bool hasPCHSupport() const { return !usesPreprocessorOnly(); }
173 /// hasASTFileSupport - Does this action support use with AST files?
174 virtual bool hasASTFileSupport() const { return !usesPreprocessorOnly(); }
176 /// hasIRSupport - Does this action support use with IR files?
177 virtual bool hasIRSupport() const { return false; }
179 /// hasCodeCompletionSupport - Does this action support use with code
181 virtual bool hasCodeCompletionSupport() const { return false; }
184 /// @name Public Action Interface
187 /// BeginSourceFile - Prepare the action for processing the input file \arg
188 /// Filename; this is run after the options and frontend have been
189 /// initialized, but prior to executing any per-file processing.
191 /// \param CI - The compiler instance this action is being run from. The
192 /// action may store and use this object up until the matching EndSourceFile
195 /// \param Filename - The input filename, which will be made available to
196 /// clients via \see getCurrentFile().
198 /// \param InputKind - The type of input. Some input kinds are handled
199 /// specially, for example AST inputs, since the AST file itself contains
200 /// several objects which would normally be owned by the
201 /// CompilerInstance. When processing AST input files, these objects should
202 /// generally not be initialized in the CompilerInstance -- they will
203 /// automatically be shared with the AST file in between \see
204 /// BeginSourceFile() and \see EndSourceFile().
206 /// \return True on success; the compilation of this file should be aborted
207 /// and neither Execute nor EndSourceFile should be called.
208 bool BeginSourceFile(CompilerInstance &CI, llvm::StringRef Filename,
211 /// Execute - Set the source managers main input file, and run the action.
214 /// EndSourceFile - Perform any per-file post processing, deallocate per-file
215 /// objects, and run statistics and output file cleanup code.
216 void EndSourceFile();
221 /// ASTFrontendAction - Abstract base class to use for AST consumer based
222 /// frontend actions.
223 class ASTFrontendAction : public FrontendAction {
225 /// ExecuteAction - Implement the ExecuteAction interface by running Sema on
226 /// the already initialized AST consumer.
228 /// This will also take care of instantiating a code completion consumer if
229 /// the user requested it and the action supports it.
230 virtual void ExecuteAction();
233 virtual bool usesPreprocessorOnly() const { return false; }
236 class PluginASTAction : public ASTFrontendAction {
238 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
239 llvm::StringRef InFile) = 0;
242 /// ParseArgs - Parse the given plugin command line arguments.
244 /// \param CI - The compiler instance, for use in reporting diagnostics.
245 /// \return True if the parsing succeeded; otherwise the plugin will be
246 /// destroyed and no action run. The plugin is responsible for using the
247 /// CompilerInstance's Diagnostic object to report errors.
248 virtual bool ParseArgs(const CompilerInstance &CI,
249 const std::vector<std::string> &arg) = 0;
252 /// PreprocessorFrontendAction - Abstract base class to use for preprocessor
253 /// based frontend actions.
254 class PreprocessorFrontendAction : public FrontendAction {
256 /// CreateASTConsumer - Provide a default implementation which returns aborts,
257 /// this method should never be called by FrontendAction clients.
258 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
259 llvm::StringRef InFile);
262 virtual bool usesPreprocessorOnly() const { return true; }
265 /// WrapperFrontendAction - A frontend action which simply wraps some other
266 /// runtime specified frontend action. Deriving from this class allows an
267 /// action to inject custom logic around some existing action's behavior. It
268 /// implements every virtual method in the FrontendAction interface by
269 /// forwarding to the wrapped action.
270 class WrapperFrontendAction : public FrontendAction {
271 llvm::OwningPtr<FrontendAction> WrappedAction;
274 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
275 llvm::StringRef InFile);
276 virtual bool BeginInvocation(CompilerInstance &CI);
277 virtual bool BeginSourceFileAction(CompilerInstance &CI,
278 llvm::StringRef Filename);
279 virtual void ExecuteAction();
280 virtual void EndSourceFileAction();
283 /// Construct a WrapperFrontendAction from an existing action, taking
285 WrapperFrontendAction(FrontendAction *WrappedAction);
287 virtual bool usesPreprocessorOnly() const;
288 virtual bool usesCompleteTranslationUnit();
289 virtual bool hasPCHSupport() const;
290 virtual bool hasASTFileSupport() const;
291 virtual bool hasIRSupport() const;
292 virtual bool hasCodeCompletionSupport() const;
295 } // end namespace clang