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