clang API Documentation

FrontendAction.h
Go to the documentation of this file.
00001 //===-- FrontendAction.h - Generic Frontend Action Interface ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTION_H
00011 #define LLVM_CLANG_FRONTEND_FRONTENDACTION_H
00012 
00013 #include "clang/Basic/LLVM.h"
00014 #include "clang/Basic/LangOptions.h"
00015 #include "clang/Frontend/FrontendOptions.h"
00016 #include "llvm/ADT/StringRef.h"
00017 #include "llvm/ADT/OwningPtr.h"
00018 #include <string>
00019 #include <vector>
00020 
00021 namespace clang {
00022 class ASTConsumer;
00023 class ASTMergeAction;
00024 class ASTUnit;
00025 class CompilerInstance;
00026 
00027 /// FrontendAction - Abstract base class for actions which can be performed by
00028 /// the frontend.
00029 class FrontendAction {
00030   FrontendInputFile CurrentInput;
00031   OwningPtr<ASTUnit> CurrentASTUnit;
00032   CompilerInstance *Instance;
00033   friend class ASTMergeAction;
00034   friend class WrapperFrontendAction;
00035 
00036 private:
00037   ASTConsumer* CreateWrappedASTConsumer(CompilerInstance &CI,
00038                                         StringRef InFile);
00039 
00040 protected:
00041   /// @name Implementation Action Interface
00042   /// @{
00043 
00044   /// CreateASTConsumer - Create the AST consumer object for this action, if
00045   /// supported.
00046   ///
00047   /// This routine is called as part of \see BeginSourceAction(), which will
00048   /// fail if the AST consumer cannot be created. This will not be called if the
00049   /// action has indicated that it only uses the preprocessor.
00050   ///
00051   /// \param CI - The current compiler instance, provided as a convenience, \see
00052   /// getCompilerInstance().
00053   ///
00054   /// \param InFile - The current input file, provided as a convenience, \see
00055   /// getCurrentFile().
00056   ///
00057   /// \return The new AST consumer, or 0 on failure.
00058   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
00059                                          StringRef InFile) = 0;
00060 
00061   /// \brief Callback before starting processing a single input, giving the
00062   /// opportunity to modify the CompilerInvocation or do some other action
00063   /// before BeginSourceFileAction is called.
00064   ///
00065   /// \return True on success; on failure \see BeginSourceFileAction() and
00066   /// ExecutionAction() and EndSourceFileAction() will not be called.
00067   virtual bool BeginInvocation(CompilerInstance &CI) { return true; }
00068 
00069   /// BeginSourceFileAction - Callback at the start of processing a single
00070   /// input.
00071   ///
00072   /// \return True on success; on failure \see ExecutionAction() and
00073   /// EndSourceFileAction() will not be called.
00074   virtual bool BeginSourceFileAction(CompilerInstance &CI,
00075                                      StringRef Filename) {
00076     return true;
00077   }
00078 
00079   /// ExecuteAction - Callback to run the program action, using the initialized
00080   /// compiler instance.
00081   ///
00082   /// This routine is guaranteed to only be called between \see
00083   /// BeginSourceFileAction() and \see EndSourceFileAction().
00084   virtual void ExecuteAction() = 0;
00085 
00086   /// EndSourceFileAction - Callback at the end of processing a single input;
00087   /// this is guaranteed to only be called following a successful call to
00088   /// BeginSourceFileAction (and BeginSourceFile).
00089   virtual void EndSourceFileAction() {}
00090 
00091   /// @}
00092 
00093 public:
00094   FrontendAction();
00095   virtual ~FrontendAction();
00096 
00097   /// @name Compiler Instance Access
00098   /// @{
00099 
00100   CompilerInstance &getCompilerInstance() const {
00101     assert(Instance && "Compiler instance not registered!");
00102     return *Instance;
00103   }
00104 
00105   void setCompilerInstance(CompilerInstance *Value) { Instance = Value; }
00106 
00107   /// @}
00108   /// @name Current File Information
00109   /// @{
00110 
00111   bool isCurrentFileAST() const {
00112     assert(!CurrentInput.File.empty() && "No current file!");
00113     return CurrentASTUnit != 0;
00114   }
00115 
00116   const FrontendInputFile &getCurrentInput() const {
00117     return CurrentInput;
00118   }
00119   
00120   const std::string &getCurrentFile() const {
00121     assert(!CurrentInput.File.empty() && "No current file!");
00122     return CurrentInput.File;
00123   }
00124 
00125   InputKind getCurrentFileKind() const {
00126     assert(!CurrentInput.File.empty() && "No current file!");
00127     return CurrentInput.Kind;
00128   }
00129 
00130   ASTUnit &getCurrentASTUnit() const {
00131     assert(CurrentASTUnit && "No current AST unit!");
00132     return *CurrentASTUnit;
00133   }
00134 
00135   ASTUnit *takeCurrentASTUnit() {
00136     return CurrentASTUnit.take();
00137   }
00138 
00139   void setCurrentInput(const FrontendInputFile &CurrentInput, ASTUnit *AST = 0);
00140 
00141   /// @}
00142   /// @name Supported Modes
00143   /// @{
00144 
00145   /// usesPreprocessorOnly - Does this action only use the preprocessor? If so
00146   /// no AST context will be created and this action will be invalid with AST
00147   /// file inputs.
00148   virtual bool usesPreprocessorOnly() const = 0;
00149 
00150   /// \brief For AST-based actions, the kind of translation unit we're handling.
00151   virtual TranslationUnitKind getTranslationUnitKind() { return TU_Complete; }
00152 
00153   /// hasPCHSupport - Does this action support use with PCH?
00154   virtual bool hasPCHSupport() const { return !usesPreprocessorOnly(); }
00155 
00156   /// hasASTFileSupport - Does this action support use with AST files?
00157   virtual bool hasASTFileSupport() const { return !usesPreprocessorOnly(); }
00158 
00159   /// hasIRSupport - Does this action support use with IR files?
00160   virtual bool hasIRSupport() const { return false; }
00161 
00162   /// hasCodeCompletionSupport - Does this action support use with code
00163   /// completion?
00164   virtual bool hasCodeCompletionSupport() const { return false; }
00165 
00166   /// @}
00167   /// @name Public Action Interface
00168   /// @{
00169 
00170   /// BeginSourceFile - Prepare the action for processing the input file \arg
00171   /// Filename; this is run after the options and frontend have been
00172   /// initialized, but prior to executing any per-file processing.
00173   ///
00174   /// \param CI - The compiler instance this action is being run from. The
00175   /// action may store and use this object up until the matching EndSourceFile
00176   /// action.
00177   ///
00178   /// \param Input - The input filename and kind. Some input kinds are handled
00179   /// specially, for example AST inputs, since the AST file itself contains
00180   /// several objects which would normally be owned by the
00181   /// CompilerInstance. When processing AST input files, these objects should
00182   /// generally not be initialized in the CompilerInstance -- they will
00183   /// automatically be shared with the AST file in between \see
00184   /// BeginSourceFile() and \see EndSourceFile().
00185   ///
00186   /// \return True on success; on failure the compilation of this file should
00187   /// be aborted and neither Execute nor EndSourceFile should be called.
00188   bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input);
00189 
00190   /// Execute - Set the source managers main input file, and run the action.
00191   void Execute();
00192 
00193   /// EndSourceFile - Perform any per-file post processing, deallocate per-file
00194   /// objects, and run statistics and output file cleanup code.
00195   void EndSourceFile();
00196 
00197   /// @}
00198 };
00199 
00200 /// ASTFrontendAction - Abstract base class to use for AST consumer based
00201 /// frontend actions.
00202 class ASTFrontendAction : public FrontendAction {
00203 protected:
00204   /// ExecuteAction - Implement the ExecuteAction interface by running Sema on
00205   /// the already initialized AST consumer.
00206   ///
00207   /// This will also take care of instantiating a code completion consumer if
00208   /// the user requested it and the action supports it.
00209   virtual void ExecuteAction();
00210 
00211 public:
00212   virtual bool usesPreprocessorOnly() const { return false; }
00213 };
00214 
00215 class PluginASTAction : public ASTFrontendAction {
00216   virtual void anchor();
00217 protected:
00218   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
00219                                          StringRef InFile) = 0;
00220 
00221 public:
00222   /// ParseArgs - Parse the given plugin command line arguments.
00223   ///
00224   /// \param CI - The compiler instance, for use in reporting diagnostics.
00225   /// \return True if the parsing succeeded; otherwise the plugin will be
00226   /// destroyed and no action run. The plugin is responsible for using the
00227   /// CompilerInstance's Diagnostic object to report errors.
00228   virtual bool ParseArgs(const CompilerInstance &CI,
00229                          const std::vector<std::string> &arg) = 0;
00230 };
00231 
00232 /// PreprocessorFrontendAction - Abstract base class to use for preprocessor
00233 /// based frontend actions.
00234 class PreprocessorFrontendAction : public FrontendAction {
00235 protected:
00236   /// CreateASTConsumer - Provide a default implementation which returns aborts,
00237   /// this method should never be called by FrontendAction clients.
00238   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
00239                                          StringRef InFile);
00240 
00241 public:
00242   virtual bool usesPreprocessorOnly() const { return true; }
00243 };
00244 
00245 /// WrapperFrontendAction - A frontend action which simply wraps some other
00246 /// runtime specified frontend action. Deriving from this class allows an
00247 /// action to inject custom logic around some existing action's behavior. It
00248 /// implements every virtual method in the FrontendAction interface by
00249 /// forwarding to the wrapped action.
00250 class WrapperFrontendAction : public FrontendAction {
00251   OwningPtr<FrontendAction> WrappedAction;
00252 
00253 protected:
00254   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
00255                                          StringRef InFile);
00256   virtual bool BeginInvocation(CompilerInstance &CI);
00257   virtual bool BeginSourceFileAction(CompilerInstance &CI,
00258                                      StringRef Filename);
00259   virtual void ExecuteAction();
00260   virtual void EndSourceFileAction();
00261 
00262 public:
00263   /// Construct a WrapperFrontendAction from an existing action, taking
00264   /// ownership of it.
00265   WrapperFrontendAction(FrontendAction *WrappedAction);
00266 
00267   virtual bool usesPreprocessorOnly() const;
00268   virtual TranslationUnitKind getTranslationUnitKind();
00269   virtual bool hasPCHSupport() const;
00270   virtual bool hasASTFileSupport() const;
00271   virtual bool hasIRSupport() const;
00272   virtual bool hasCodeCompletionSupport() const;
00273 };
00274 
00275 }  // end namespace clang
00276 
00277 #endif