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