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 ASTDumpXMLAction : public ASTFrontendAction {
     54 protected:
     55   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
     56                                          StringRef InFile);
     57 };
     58 
     59 class ASTViewAction : public ASTFrontendAction {
     60 protected:
     61   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
     62                                          StringRef InFile);
     63 };
     64 
     65 class DeclContextPrintAction : public ASTFrontendAction {
     66 protected:
     67   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
     68                                          StringRef InFile);
     69 };
     70 
     71 class GeneratePCHAction : public ASTFrontendAction {
     72 protected:
     73   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
     74                                          StringRef InFile);
     75 
     76   virtual TranslationUnitKind getTranslationUnitKind() {
     77     return TU_Prefix;
     78   }
     79 
     80   virtual bool hasASTFileSupport() const { return false; }
     81 
     82 public:
     83   /// \brief Compute the AST consumer arguments that will be used to
     84   /// create the PCHGenerator instance returned by CreateASTConsumer.
     85   ///
     86   /// \returns true if an error occurred, false otherwise.
     87   static bool ComputeASTConsumerArguments(CompilerInstance &CI,
     88                                           StringRef InFile,
     89                                           std::string &Sysroot,
     90                                           std::string &OutputFile,
     91                                           raw_ostream *&OS);
     92 };
     93 
     94 class GenerateModuleAction : public ASTFrontendAction {
     95   clang::Module *Module;
     96 
     97 protected:
     98   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
     99                                          StringRef InFile);
    100 
    101   virtual TranslationUnitKind getTranslationUnitKind() {
    102     return TU_Module;
    103   }
    104 
    105   virtual bool hasASTFileSupport() const { return false; }
    106 
    107 public:
    108   virtual bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename);
    109 
    110   /// \brief Compute the AST consumer arguments that will be used to
    111   /// create the PCHGenerator instance returned by CreateASTConsumer.
    112   ///
    113   /// \returns true if an error occurred, false otherwise.
    114   static bool ComputeASTConsumerArguments(CompilerInstance &CI,
    115                                           StringRef InFile,
    116                                           std::string &Sysroot,
    117                                           std::string &OutputFile,
    118                                           raw_ostream *&OS);
    119 };
    120 
    121 class SyntaxOnlyAction : public ASTFrontendAction {
    122 protected:
    123   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
    124                                          StringRef InFile);
    125 
    126 public:
    127   virtual bool hasCodeCompletionSupport() const { return true; }
    128 };
    129 
    130 /**
    131  * \brief Frontend action adaptor that merges ASTs together.
    132  *
    133  * This action takes an existing AST file and "merges" it into the AST
    134  * context, producing a merged context. This action is an action
    135  * adaptor, which forwards most of its calls to another action that
    136  * will consume the merged context.
    137  */
    138 class ASTMergeAction : public FrontendAction {
    139   /// \brief The action that the merge action adapts.
    140   FrontendAction *AdaptedAction;
    141 
    142   /// \brief The set of AST files to merge.
    143   std::vector<std::string> ASTFiles;
    144 
    145 protected:
    146   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
    147                                          StringRef InFile);
    148 
    149   virtual bool BeginSourceFileAction(CompilerInstance &CI,
    150                                      StringRef Filename);
    151 
    152   virtual void ExecuteAction();
    153   virtual void EndSourceFileAction();
    154 
    155 public:
    156   ASTMergeAction(FrontendAction *AdaptedAction, ArrayRef<std::string> ASTFiles);
    157   virtual ~ASTMergeAction();
    158 
    159   virtual bool usesPreprocessorOnly() const;
    160   virtual TranslationUnitKind getTranslationUnitKind();
    161   virtual bool hasPCHSupport() const;
    162   virtual bool hasASTFileSupport() const;
    163   virtual bool hasCodeCompletionSupport() const;
    164 };
    165 
    166 class PrintPreambleAction : public FrontendAction {
    167 protected:
    168   void ExecuteAction();
    169   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &, StringRef) {
    170     return 0;
    171   }
    172 
    173   virtual bool usesPreprocessorOnly() const { return true; }
    174 };
    175 
    176 //===----------------------------------------------------------------------===//
    177 // Preprocessor Actions
    178 //===----------------------------------------------------------------------===//
    179 
    180 class DumpRawTokensAction : public PreprocessorFrontendAction {
    181 protected:
    182   void ExecuteAction();
    183 };
    184 
    185 class DumpTokensAction : public PreprocessorFrontendAction {
    186 protected:
    187   void ExecuteAction();
    188 };
    189 
    190 class GeneratePTHAction : public PreprocessorFrontendAction {
    191 protected:
    192   void ExecuteAction();
    193 };
    194 
    195 class PreprocessOnlyAction : public PreprocessorFrontendAction {
    196 protected:
    197   void ExecuteAction();
    198 };
    199 
    200 class PrintPreprocessedAction : public PreprocessorFrontendAction {
    201 protected:
    202   void ExecuteAction();
    203 
    204   virtual bool hasPCHSupport() const { return true; }
    205 };
    206 
    207 }  // end namespace clang
    208 
    209 #endif
    210