clang API Documentation

Frontend/FrontendActions.h
Go to the documentation of this file.
00001 //===-- FrontendActions.h - Useful Frontend Actions -------------*- 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_FRONTENDACTIONS_H
00011 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
00012 
00013 #include "clang/Frontend/FrontendAction.h"
00014 #include <string>
00015 #include <vector>
00016 
00017 namespace clang {
00018 
00019 class Module;
00020 class FileEntry;
00021   
00022 //===----------------------------------------------------------------------===//
00023 // Custom Consumer Actions
00024 //===----------------------------------------------------------------------===//
00025 
00026 class InitOnlyAction : public FrontendAction {
00027   void ExecuteAction() override;
00028 
00029   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00030                                                  StringRef InFile) override;
00031 
00032 public:
00033   // Don't claim to only use the preprocessor, we want to follow the AST path,
00034   // but do nothing.
00035   bool usesPreprocessorOnly() const override { return false; }
00036 };
00037 
00038 //===----------------------------------------------------------------------===//
00039 // AST Consumer Actions
00040 //===----------------------------------------------------------------------===//
00041 
00042 class ASTPrintAction : public ASTFrontendAction {
00043 protected:
00044   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00045                                                  StringRef InFile) override;
00046 };
00047 
00048 class ASTDumpAction : public ASTFrontendAction {
00049 protected:
00050   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00051                                                  StringRef InFile) override;
00052 };
00053 
00054 class ASTDeclListAction : public ASTFrontendAction {
00055 protected:
00056   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00057                                                  StringRef InFile) override;
00058 };
00059 
00060 class ASTViewAction : public ASTFrontendAction {
00061 protected:
00062   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00063                                                  StringRef InFile) override;
00064 };
00065 
00066 class DeclContextPrintAction : public ASTFrontendAction {
00067 protected:
00068   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00069                                                  StringRef InFile) override;
00070 };
00071 
00072 class GeneratePCHAction : public ASTFrontendAction {
00073 protected:
00074   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00075                                                  StringRef InFile) override;
00076 
00077   TranslationUnitKind getTranslationUnitKind() override {
00078     return TU_Prefix;
00079   }
00080 
00081   bool hasASTFileSupport() const override { return false; }
00082 
00083 public:
00084   /// \brief Compute the AST consumer arguments that will be used to
00085   /// create the PCHGenerator instance returned by CreateASTConsumer.
00086   ///
00087   /// \returns true if an error occurred, false otherwise.
00088   static bool ComputeASTConsumerArguments(CompilerInstance &CI,
00089                                           StringRef InFile,
00090                                           std::string &Sysroot,
00091                                           std::string &OutputFile,
00092                                           raw_ostream *&OS);
00093 };
00094 
00095 class GenerateModuleAction : public ASTFrontendAction {
00096   clang::Module *Module;
00097   const FileEntry *ModuleMapForUniquing;
00098   bool IsSystem;
00099   
00100 protected:
00101   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00102                                                  StringRef InFile) override;
00103 
00104   TranslationUnitKind getTranslationUnitKind() override {
00105     return TU_Module;
00106   }
00107 
00108   bool hasASTFileSupport() const override { return false; }
00109 
00110 public:
00111   GenerateModuleAction(const FileEntry *ModuleMap = nullptr,
00112                        bool IsSystem = false)
00113     : ASTFrontendAction(), ModuleMapForUniquing(ModuleMap), IsSystem(IsSystem)
00114   { }
00115 
00116   bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override;
00117 
00118   /// \brief Compute the AST consumer arguments that will be used to
00119   /// create the PCHGenerator instance returned by CreateASTConsumer.
00120   ///
00121   /// \returns true if an error occurred, false otherwise.
00122   bool ComputeASTConsumerArguments(CompilerInstance &CI,
00123                                    StringRef InFile,
00124                                    std::string &Sysroot,
00125                                    std::string &OutputFile,
00126                                    raw_ostream *&OS);
00127 };
00128 
00129 class SyntaxOnlyAction : public ASTFrontendAction {
00130 protected:
00131   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00132                                                  StringRef InFile) override;
00133 
00134 public:
00135   bool hasCodeCompletionSupport() const override { return true; }
00136 };
00137 
00138 /// \brief Dump information about the given module file, to be used for
00139 /// basic debugging and discovery.
00140 class DumpModuleInfoAction : public ASTFrontendAction {
00141 protected:
00142   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00143                                                  StringRef InFile) override;
00144   void ExecuteAction() override;
00145 
00146 public:
00147   bool hasPCHSupport() const override { return false; }
00148   bool hasASTFileSupport() const override { return true; }
00149   bool hasIRSupport() const override { return false; }
00150   bool hasCodeCompletionSupport() const override { return false; }
00151 };
00152 
00153 class VerifyPCHAction : public ASTFrontendAction {
00154 protected:
00155   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00156                                                  StringRef InFile) override;
00157 
00158   void ExecuteAction() override;
00159 
00160 public:
00161   bool hasCodeCompletionSupport() const override { return false; }
00162 };
00163 
00164 /**
00165  * \brief Frontend action adaptor that merges ASTs together.
00166  *
00167  * This action takes an existing AST file and "merges" it into the AST
00168  * context, producing a merged context. This action is an action
00169  * adaptor, which forwards most of its calls to another action that
00170  * will consume the merged context.
00171  */
00172 class ASTMergeAction : public FrontendAction {
00173   /// \brief The action that the merge action adapts.
00174   FrontendAction *AdaptedAction;
00175   
00176   /// \brief The set of AST files to merge.
00177   std::vector<std::string> ASTFiles;
00178 
00179 protected:
00180   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
00181                                                  StringRef InFile) override;
00182 
00183   bool BeginSourceFileAction(CompilerInstance &CI,
00184                              StringRef Filename) override;
00185 
00186   void ExecuteAction() override;
00187   void EndSourceFileAction() override;
00188 
00189 public:
00190   ASTMergeAction(FrontendAction *AdaptedAction, ArrayRef<std::string> ASTFiles);
00191   virtual ~ASTMergeAction();
00192 
00193   bool usesPreprocessorOnly() const override;
00194   TranslationUnitKind getTranslationUnitKind() override;
00195   bool hasPCHSupport() const override;
00196   bool hasASTFileSupport() const override;
00197   bool hasCodeCompletionSupport() const override;
00198 };
00199 
00200 class PrintPreambleAction : public FrontendAction {
00201 protected:
00202   void ExecuteAction() override;
00203   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
00204                                                  StringRef) override {
00205     return nullptr;
00206   }
00207 
00208   bool usesPreprocessorOnly() const override { return true; }
00209 };
00210   
00211 //===----------------------------------------------------------------------===//
00212 // Preprocessor Actions
00213 //===----------------------------------------------------------------------===//
00214 
00215 class DumpRawTokensAction : public PreprocessorFrontendAction {
00216 protected:
00217   void ExecuteAction() override;
00218 };
00219 
00220 class DumpTokensAction : public PreprocessorFrontendAction {
00221 protected:
00222   void ExecuteAction() override;
00223 };
00224 
00225 class GeneratePTHAction : public PreprocessorFrontendAction {
00226 protected:
00227   void ExecuteAction() override;
00228 };
00229 
00230 class PreprocessOnlyAction : public PreprocessorFrontendAction {
00231 protected:
00232   void ExecuteAction() override;
00233 };
00234 
00235 class PrintPreprocessedAction : public PreprocessorFrontendAction {
00236 protected:
00237   void ExecuteAction() override;
00238 
00239   bool hasPCHSupport() const override { return true; }
00240 };
00241   
00242 }  // end namespace clang
00243 
00244 #endif