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