Home | History | Annotate | Download | only in Frontend
      1 //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
     11 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
     12 
     13 #include "clang/Frontend/FrontendAction.h"
     14 #include <string>
     15 #include <vector>
     16 
     17 namespace clang {
     18 
     19 class Module;
     20 class FileEntry;
     21 
     22 //===----------------------------------------------------------------------===//
     23 // Custom Consumer Actions
     24 //===----------------------------------------------------------------------===//
     25 
     26 class InitOnlyAction : public FrontendAction {
     27   void ExecuteAction() override;
     28 
     29   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     30                                                  StringRef InFile) override;
     31 
     32 public:
     33   // Don't claim to only use the preprocessor, we want to follow the AST path,
     34   // but do nothing.
     35   bool usesPreprocessorOnly() const override { return false; }
     36 };
     37 
     38 //===----------------------------------------------------------------------===//
     39 // AST Consumer Actions
     40 //===----------------------------------------------------------------------===//
     41 
     42 class ASTPrintAction : public ASTFrontendAction {
     43 protected:
     44   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     45                                                  StringRef InFile) override;
     46 };
     47 
     48 class ASTDumpAction : public ASTFrontendAction {
     49 protected:
     50   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     51                                                  StringRef InFile) override;
     52 };
     53 
     54 class ASTDeclListAction : public ASTFrontendAction {
     55 protected:
     56   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     57                                                  StringRef InFile) override;
     58 };
     59 
     60 class ASTViewAction : public ASTFrontendAction {
     61 protected:
     62   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     63                                                  StringRef InFile) override;
     64 };
     65 
     66 class DeclContextPrintAction : public ASTFrontendAction {
     67 protected:
     68   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     69                                                  StringRef InFile) override;
     70 };
     71 
     72 class GeneratePCHAction : public ASTFrontendAction {
     73 protected:
     74   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
     75                                                  StringRef InFile) override;
     76 
     77   TranslationUnitKind getTranslationUnitKind() override {
     78     return TU_Prefix;
     79   }
     80 
     81   bool hasASTFileSupport() const override { return false; }
     82 
     83   bool shouldEraseOutputFiles() override;
     84 
     85 public:
     86   /// \brief Compute the AST consumer arguments that will be used to
     87   /// create the PCHGenerator instance returned by CreateASTConsumer.
     88   ///
     89   /// \returns true if an error occurred, false otherwise.
     90   static std::unique_ptr<raw_pwrite_stream>
     91   ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile,
     92                               std::string &Sysroot, std::string &OutputFile);
     93 
     94   bool BeginSourceFileAction(CompilerInstance &CI) override;
     95 };
     96 
     97 class GenerateModuleAction : public ASTFrontendAction {
     98   virtual std::unique_ptr<raw_pwrite_stream>
     99   CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
    100 
    101 protected:
    102   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
    103                                                  StringRef InFile) override;
    104 
    105   TranslationUnitKind getTranslationUnitKind() override {
    106     return TU_Module;
    107   }
    108 
    109   bool hasASTFileSupport() const override { return false; }
    110 };
    111 
    112 class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
    113 private:
    114   std::unique_ptr<raw_pwrite_stream>
    115   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
    116 };
    117 
    118 class GenerateModuleInterfaceAction : public GenerateModuleAction {
    119 private:
    120   bool BeginSourceFileAction(CompilerInstance &CI) override;
    121 
    122   std::unique_ptr<raw_pwrite_stream>
    123   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
    124 };
    125 
    126 class SyntaxOnlyAction : public ASTFrontendAction {
    127 protected:
    128   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
    129                                                  StringRef InFile) override;
    130 
    131 public:
    132   ~SyntaxOnlyAction() override;
    133   bool hasCodeCompletionSupport() const override { return true; }
    134 };
    135 
    136 /// \brief Dump information about the given module file, to be used for
    137 /// basic debugging and discovery.
    138 class DumpModuleInfoAction : public ASTFrontendAction {
    139 protected:
    140   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
    141                                                  StringRef InFile) override;
    142   bool BeginInvocation(CompilerInstance &CI) override;
    143   void ExecuteAction() override;
    144 
    145 public:
    146   bool hasPCHSupport() const override { return false; }
    147   bool hasASTFileSupport() const override { return true; }
    148   bool hasIRSupport() const override { return false; }
    149   bool hasCodeCompletionSupport() const override { return false; }
    150 };
    151 
    152 class VerifyPCHAction : public ASTFrontendAction {
    153 protected:
    154   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
    155                                                  StringRef InFile) override;
    156 
    157   void ExecuteAction() override;
    158 
    159 public:
    160   bool hasCodeCompletionSupport() const override { return false; }
    161 };
    162 
    163 /**
    164  * \brief Frontend action adaptor that merges ASTs together.
    165  *
    166  * This action takes an existing AST file and "merges" it into the AST
    167  * context, producing a merged context. This action is an action
    168  * adaptor, which forwards most of its calls to another action that
    169  * will consume the merged context.
    170  */
    171 class ASTMergeAction : public FrontendAction {
    172   /// \brief The action that the merge action adapts.
    173   std::unique_ptr<FrontendAction> AdaptedAction;
    174 
    175   /// \brief The set of AST files to merge.
    176   std::vector<std::string> ASTFiles;
    177 
    178 protected:
    179   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
    180                                                  StringRef InFile) override;
    181 
    182   bool BeginSourceFileAction(CompilerInstance &CI) override;
    183 
    184   void ExecuteAction() override;
    185   void EndSourceFileAction() override;
    186 
    187 public:
    188   ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
    189                  ArrayRef<std::string> ASTFiles);
    190   ~ASTMergeAction() override;
    191 
    192   bool usesPreprocessorOnly() const override;
    193   TranslationUnitKind getTranslationUnitKind() override;
    194   bool hasPCHSupport() const override;
    195   bool hasASTFileSupport() const override;
    196   bool hasCodeCompletionSupport() const override;
    197 };
    198 
    199 class PrintPreambleAction : public FrontendAction {
    200 protected:
    201   void ExecuteAction() override;
    202   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
    203                                                  StringRef) override {
    204     return nullptr;
    205   }
    206 
    207   bool usesPreprocessorOnly() const override { return true; }
    208 };
    209 
    210 //===----------------------------------------------------------------------===//
    211 // Preprocessor Actions
    212 //===----------------------------------------------------------------------===//
    213 
    214 class DumpRawTokensAction : public PreprocessorFrontendAction {
    215 protected:
    216   void ExecuteAction() override;
    217 };
    218 
    219 class DumpTokensAction : public PreprocessorFrontendAction {
    220 protected:
    221   void ExecuteAction() override;
    222 };
    223 
    224 class GeneratePTHAction : public PreprocessorFrontendAction {
    225 protected:
    226   void ExecuteAction() override;
    227 };
    228 
    229 class PreprocessOnlyAction : public PreprocessorFrontendAction {
    230 protected:
    231   void ExecuteAction() override;
    232 };
    233 
    234 class PrintPreprocessedAction : public PreprocessorFrontendAction {
    235 protected:
    236   void ExecuteAction() override;
    237 
    238   bool hasPCHSupport() const override { return true; }
    239 };
    240 
    241 }  // end namespace clang
    242 
    243 #endif
    244