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