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. usesPreprocessorOnly()35 bool usesPreprocessorOnly() const override { return false; } 36 }; 37 38 class DumpCompilerOptionsAction : public FrontendAction { CreateASTConsumer(CompilerInstance & CI,StringRef InFile)39 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 40 StringRef InFile) override { 41 return nullptr; 42 } 43 44 void ExecuteAction() override; 45 46 public: usesPreprocessorOnly()47 bool usesPreprocessorOnly() const override { return true; } 48 }; 49 50 //===----------------------------------------------------------------------===// 51 // AST Consumer Actions 52 //===----------------------------------------------------------------------===// 53 54 class ASTPrintAction : public ASTFrontendAction { 55 protected: 56 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 57 StringRef InFile) override; 58 }; 59 60 class ASTDumpAction : public ASTFrontendAction { 61 protected: 62 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 63 StringRef InFile) override; 64 }; 65 66 class ASTDeclListAction : public ASTFrontendAction { 67 protected: 68 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 69 StringRef InFile) override; 70 }; 71 72 class ASTViewAction : public ASTFrontendAction { 73 protected: 74 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 75 StringRef InFile) override; 76 }; 77 78 class DeclContextPrintAction : public ASTFrontendAction { 79 protected: 80 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 81 StringRef InFile) override; 82 }; 83 84 class GeneratePCHAction : public ASTFrontendAction { 85 protected: 86 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 87 StringRef InFile) override; 88 getTranslationUnitKind()89 TranslationUnitKind getTranslationUnitKind() override { 90 return TU_Prefix; 91 } 92 hasASTFileSupport()93 bool hasASTFileSupport() const override { return false; } 94 95 bool shouldEraseOutputFiles() override; 96 97 public: 98 /// Compute the AST consumer arguments that will be used to 99 /// create the PCHGenerator instance returned by CreateASTConsumer. 100 /// 101 /// \returns false if an error occurred, true otherwise. 102 static bool ComputeASTConsumerArguments(CompilerInstance &CI, 103 std::string &Sysroot); 104 105 /// Creates file to write the PCH into and returns a stream to write it 106 /// into. On error, returns null. 107 static std::unique_ptr<llvm::raw_pwrite_stream> 108 CreateOutputFile(CompilerInstance &CI, StringRef InFile, 109 std::string &OutputFile); 110 111 bool BeginSourceFileAction(CompilerInstance &CI) override; 112 }; 113 114 class GenerateModuleAction : public ASTFrontendAction { 115 virtual std::unique_ptr<raw_pwrite_stream> 116 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0; 117 118 protected: 119 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 120 StringRef InFile) override; 121 getTranslationUnitKind()122 TranslationUnitKind getTranslationUnitKind() override { 123 return TU_Module; 124 } 125 hasASTFileSupport()126 bool hasASTFileSupport() const override { return false; } 127 }; 128 129 class GenerateModuleFromModuleMapAction : public GenerateModuleAction { 130 private: 131 bool BeginSourceFileAction(CompilerInstance &CI) override; 132 133 std::unique_ptr<raw_pwrite_stream> 134 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override; 135 }; 136 137 class GenerateModuleInterfaceAction : public GenerateModuleAction { 138 private: 139 bool BeginSourceFileAction(CompilerInstance &CI) override; 140 141 std::unique_ptr<raw_pwrite_stream> 142 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override; 143 }; 144 145 class GenerateHeaderModuleAction : public GenerateModuleAction { 146 /// The synthesized module input buffer for the current compilation. 147 std::unique_ptr<llvm::MemoryBuffer> Buffer; 148 std::vector<std::string> ModuleHeaders; 149 150 private: 151 bool PrepareToExecuteAction(CompilerInstance &CI) override; 152 bool BeginSourceFileAction(CompilerInstance &CI) override; 153 154 std::unique_ptr<raw_pwrite_stream> 155 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override; 156 }; 157 158 class SyntaxOnlyAction : public ASTFrontendAction { 159 protected: 160 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 161 StringRef InFile) override; 162 163 public: 164 ~SyntaxOnlyAction() override; hasCodeCompletionSupport()165 bool hasCodeCompletionSupport() const override { return true; } 166 }; 167 168 /// Dump information about the given module file, to be used for 169 /// basic debugging and discovery. 170 class DumpModuleInfoAction : public ASTFrontendAction { 171 protected: 172 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 173 StringRef InFile) override; 174 bool BeginInvocation(CompilerInstance &CI) override; 175 void ExecuteAction() override; 176 177 public: hasPCHSupport()178 bool hasPCHSupport() const override { return false; } hasASTFileSupport()179 bool hasASTFileSupport() const override { return true; } hasIRSupport()180 bool hasIRSupport() const override { return false; } hasCodeCompletionSupport()181 bool hasCodeCompletionSupport() const override { return false; } 182 }; 183 184 class VerifyPCHAction : public ASTFrontendAction { 185 protected: 186 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 187 StringRef InFile) override; 188 189 void ExecuteAction() override; 190 191 public: hasCodeCompletionSupport()192 bool hasCodeCompletionSupport() const override { return false; } 193 }; 194 195 class TemplightDumpAction : public ASTFrontendAction { 196 protected: 197 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 198 StringRef InFile) override; 199 200 void ExecuteAction() override; 201 }; 202 203 /** 204 * Frontend action adaptor that merges ASTs together. 205 * 206 * This action takes an existing AST file and "merges" it into the AST 207 * context, producing a merged context. This action is an action 208 * adaptor, which forwards most of its calls to another action that 209 * will consume the merged context. 210 */ 211 class ASTMergeAction : public FrontendAction { 212 /// The action that the merge action adapts. 213 std::unique_ptr<FrontendAction> AdaptedAction; 214 215 /// The set of AST files to merge. 216 std::vector<std::string> ASTFiles; 217 218 protected: 219 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 220 StringRef InFile) override; 221 222 bool BeginSourceFileAction(CompilerInstance &CI) override; 223 224 void ExecuteAction() override; 225 void EndSourceFileAction() override; 226 227 public: 228 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction, 229 ArrayRef<std::string> ASTFiles); 230 ~ASTMergeAction() override; 231 232 bool usesPreprocessorOnly() const override; 233 TranslationUnitKind getTranslationUnitKind() override; 234 bool hasPCHSupport() const override; 235 bool hasASTFileSupport() const override; 236 bool hasCodeCompletionSupport() const override; 237 }; 238 239 class PrintPreambleAction : public FrontendAction { 240 protected: 241 void ExecuteAction() override; CreateASTConsumer(CompilerInstance &,StringRef)242 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &, 243 StringRef) override { 244 return nullptr; 245 } 246 usesPreprocessorOnly()247 bool usesPreprocessorOnly() const override { return true; } 248 }; 249 250 //===----------------------------------------------------------------------===// 251 // Preprocessor Actions 252 //===----------------------------------------------------------------------===// 253 254 class DumpRawTokensAction : public PreprocessorFrontendAction { 255 protected: 256 void ExecuteAction() override; 257 }; 258 259 class DumpTokensAction : public PreprocessorFrontendAction { 260 protected: 261 void ExecuteAction() override; 262 }; 263 264 class PreprocessOnlyAction : public PreprocessorFrontendAction { 265 protected: 266 void ExecuteAction() override; 267 }; 268 269 class PrintPreprocessedAction : public PreprocessorFrontendAction { 270 protected: 271 void ExecuteAction() override; 272 hasPCHSupport()273 bool hasPCHSupport() const override { return true; } 274 }; 275 276 } // end namespace clang 277 278 #endif 279