Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:55

0001 //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
0010 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
0011 
0012 #include "clang/Frontend/FrontendAction.h"
0013 #include <memory>
0014 #include <string>
0015 #include <vector>
0016 
0017 namespace clang {
0018 
0019 //===----------------------------------------------------------------------===//
0020 // Custom Consumer Actions
0021 //===----------------------------------------------------------------------===//
0022 
0023 class InitOnlyAction : public FrontendAction {
0024   void ExecuteAction() override;
0025 
0026   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0027                                                  StringRef InFile) override;
0028 
0029 public:
0030   // Don't claim to only use the preprocessor, we want to follow the AST path,
0031   // but do nothing.
0032   bool usesPreprocessorOnly() const override { return false; }
0033 };
0034 
0035 /// Preprocessor-based frontend action that also loads PCH files.
0036 class ReadPCHAndPreprocessAction : public FrontendAction {
0037   void ExecuteAction() override;
0038 
0039   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0040                                                  StringRef InFile) override;
0041 
0042 public:
0043   bool usesPreprocessorOnly() const override { return false; }
0044 };
0045 
0046 class DumpCompilerOptionsAction : public FrontendAction {
0047   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0048                                                  StringRef InFile) override {
0049     return nullptr;
0050   }
0051 
0052   void ExecuteAction() override;
0053 
0054 public:
0055   bool usesPreprocessorOnly() const override { return true; }
0056 };
0057 
0058 //===----------------------------------------------------------------------===//
0059 // AST Consumer Actions
0060 //===----------------------------------------------------------------------===//
0061 
0062 class ASTPrintAction : public ASTFrontendAction {
0063 protected:
0064   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0065                                                  StringRef InFile) override;
0066 };
0067 
0068 class ASTDumpAction : public ASTFrontendAction {
0069 protected:
0070   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0071                                                  StringRef InFile) override;
0072 };
0073 
0074 class ASTDeclListAction : public ASTFrontendAction {
0075 protected:
0076   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0077                                                  StringRef InFile) override;
0078 };
0079 
0080 class ASTViewAction : public ASTFrontendAction {
0081 protected:
0082   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0083                                                  StringRef InFile) override;
0084 };
0085 
0086 class GeneratePCHAction : public ASTFrontendAction {
0087 protected:
0088   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0089                                                  StringRef InFile) override;
0090 
0091   TranslationUnitKind getTranslationUnitKind() override {
0092     return TU_Prefix;
0093   }
0094 
0095   bool hasASTFileSupport() const override { return false; }
0096 
0097   bool shouldEraseOutputFiles() override;
0098 
0099 public:
0100   /// Compute the AST consumer arguments that will be used to
0101   /// create the PCHGenerator instance returned by CreateASTConsumer.
0102   ///
0103   /// \returns false if an error occurred, true otherwise.
0104   static bool ComputeASTConsumerArguments(CompilerInstance &CI,
0105                                           std::string &Sysroot);
0106 
0107   /// Creates file to write the PCH into and returns a stream to write it
0108   /// into. On error, returns null.
0109   static std::unique_ptr<llvm::raw_pwrite_stream>
0110   CreateOutputFile(CompilerInstance &CI, StringRef InFile,
0111                    std::string &OutputFile);
0112 
0113   bool BeginSourceFileAction(CompilerInstance &CI) override;
0114 };
0115 
0116 class GenerateModuleAction : public ASTFrontendAction {
0117   virtual std::unique_ptr<raw_pwrite_stream>
0118   CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
0119 
0120 protected:
0121   std::vector<std::unique_ptr<ASTConsumer>>
0122   CreateMultiplexConsumer(CompilerInstance &CI, StringRef InFile);
0123 
0124   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0125                                                  StringRef InFile) override;
0126 
0127   TranslationUnitKind getTranslationUnitKind() override {
0128     return TU_ClangModule;
0129   }
0130 
0131   bool hasASTFileSupport() const override { return false; }
0132 
0133   bool shouldEraseOutputFiles() override;
0134 };
0135 
0136 class GenerateInterfaceStubsAction : public ASTFrontendAction {
0137 protected:
0138   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0139                                                  StringRef InFile) override;
0140 
0141   TranslationUnitKind getTranslationUnitKind() override {
0142     return TU_ClangModule;
0143   }
0144   bool hasASTFileSupport() const override { return false; }
0145 };
0146 
0147 class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
0148 private:
0149   bool BeginSourceFileAction(CompilerInstance &CI) override;
0150 
0151   std::unique_ptr<raw_pwrite_stream>
0152   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
0153 };
0154 
0155 /// Generates full BMI (which contains full information to generate the object
0156 /// files) for C++20 Named Modules.
0157 class GenerateModuleInterfaceAction : public GenerateModuleAction {
0158 protected:
0159   bool BeginSourceFileAction(CompilerInstance &CI) override;
0160 
0161   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0162                                                  StringRef InFile) override;
0163 
0164   TranslationUnitKind getTranslationUnitKind() override { return TU_Complete; }
0165 
0166   std::unique_ptr<raw_pwrite_stream>
0167   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
0168 };
0169 
0170 /// Only generates the reduced BMI. This action is mainly used by tests.
0171 class GenerateReducedModuleInterfaceAction
0172     : public GenerateModuleInterfaceAction {
0173 private:
0174   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0175                                                  StringRef InFile) override;
0176 };
0177 
0178 class GenerateHeaderUnitAction : public GenerateModuleAction {
0179 
0180 private:
0181   bool BeginSourceFileAction(CompilerInstance &CI) override;
0182 
0183   std::unique_ptr<raw_pwrite_stream>
0184   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
0185 };
0186 
0187 class SyntaxOnlyAction : public ASTFrontendAction {
0188 protected:
0189   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0190                                                  StringRef InFile) override;
0191 
0192 public:
0193   ~SyntaxOnlyAction() override;
0194   bool hasCodeCompletionSupport() const override { return true; }
0195 };
0196 
0197 /// Dump information about the given module file, to be used for
0198 /// basic debugging and discovery.
0199 class DumpModuleInfoAction : public ASTFrontendAction {
0200   // Allow other tools (ex lldb) to direct output for their use.
0201   std::shared_ptr<llvm::raw_ostream> OutputStream;
0202 
0203 protected:
0204   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0205                                                  StringRef InFile) override;
0206   bool BeginInvocation(CompilerInstance &CI) override;
0207   void ExecuteAction() override;
0208 
0209 public:
0210   DumpModuleInfoAction() = default;
0211   explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
0212       : OutputStream(Out) {}
0213   bool hasPCHSupport() const override { return false; }
0214   bool hasASTFileSupport() const override { return true; }
0215   bool hasIRSupport() const override { return false; }
0216   bool hasCodeCompletionSupport() const override { return false; }
0217 };
0218 
0219 class VerifyPCHAction : public ASTFrontendAction {
0220 protected:
0221   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0222                                                  StringRef InFile) override;
0223 
0224   void ExecuteAction() override;
0225 
0226 public:
0227   bool hasCodeCompletionSupport() const override { return false; }
0228 };
0229 
0230 class TemplightDumpAction : public ASTFrontendAction {
0231 protected:
0232   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0233                                                  StringRef InFile) override;
0234 
0235   void ExecuteAction() override;
0236 };
0237 
0238 /**
0239  * Frontend action adaptor that merges ASTs together.
0240  *
0241  * This action takes an existing AST file and "merges" it into the AST
0242  * context, producing a merged context. This action is an action
0243  * adaptor, which forwards most of its calls to another action that
0244  * will consume the merged context.
0245  */
0246 class ASTMergeAction : public FrontendAction {
0247   /// The action that the merge action adapts.
0248   std::unique_ptr<FrontendAction> AdaptedAction;
0249 
0250   /// The set of AST files to merge.
0251   std::vector<std::string> ASTFiles;
0252 
0253 protected:
0254   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0255                                                  StringRef InFile) override;
0256 
0257   bool BeginSourceFileAction(CompilerInstance &CI) override;
0258 
0259   void ExecuteAction() override;
0260   void EndSourceFileAction() override;
0261 
0262 public:
0263   ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
0264                  ArrayRef<std::string> ASTFiles);
0265   ~ASTMergeAction() override;
0266 
0267   bool usesPreprocessorOnly() const override;
0268   TranslationUnitKind getTranslationUnitKind() override;
0269   bool hasPCHSupport() const override;
0270   bool hasASTFileSupport() const override;
0271   bool hasCodeCompletionSupport() const override;
0272 };
0273 
0274 class PrintPreambleAction : public FrontendAction {
0275 protected:
0276   void ExecuteAction() override;
0277   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
0278                                                  StringRef) override {
0279     return nullptr;
0280   }
0281 
0282   bool usesPreprocessorOnly() const override { return true; }
0283 };
0284 
0285 class PrintDependencyDirectivesSourceMinimizerAction : public FrontendAction {
0286 protected:
0287   void ExecuteAction() override;
0288   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
0289                                                  StringRef) override {
0290     return nullptr;
0291   }
0292 
0293   bool usesPreprocessorOnly() const override { return true; }
0294 };
0295 
0296 //===----------------------------------------------------------------------===//
0297 // Preprocessor Actions
0298 //===----------------------------------------------------------------------===//
0299 
0300 class DumpRawTokensAction : public PreprocessorFrontendAction {
0301 protected:
0302   void ExecuteAction() override;
0303 };
0304 
0305 class DumpTokensAction : public PreprocessorFrontendAction {
0306 protected:
0307   void ExecuteAction() override;
0308 };
0309 
0310 class PreprocessOnlyAction : public PreprocessorFrontendAction {
0311 protected:
0312   void ExecuteAction() override;
0313 };
0314 
0315 class PrintPreprocessedAction : public PreprocessorFrontendAction {
0316 protected:
0317   void ExecuteAction() override;
0318 
0319   bool hasPCHSupport() const override { return true; }
0320 };
0321 
0322 class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
0323   StringRef ModuleName;
0324   void ExecuteAction() override;
0325 
0326 public:
0327   GetDependenciesByModuleNameAction(StringRef ModuleName)
0328       : ModuleName(ModuleName) {}
0329 };
0330 
0331 }  // end namespace clang
0332 
0333 #endif