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