Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===--- StandaloneExecution.h - Standalone execution. -*- 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 //  This file defines standalone execution of clang tools.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
0014 #define LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
0015 
0016 #include "clang/Tooling/ArgumentsAdjusters.h"
0017 #include "clang/Tooling/Execution.h"
0018 #include <optional>
0019 
0020 namespace clang {
0021 namespace tooling {
0022 
0023 /// A standalone executor that runs FrontendActions on a given set of
0024 /// TUs in sequence.
0025 ///
0026 /// By default, this executor uses the following arguments adjusters (as defined
0027 /// in `clang/Tooling/ArgumentsAdjusters.h`):
0028 ///   - `getClangStripOutputAdjuster()`
0029 ///   - `getClangSyntaxOnlyAdjuster()`
0030 ///   - `getClangStripDependencyFileAdjuster()`
0031 class StandaloneToolExecutor : public ToolExecutor {
0032 public:
0033   static const char *ExecutorName;
0034 
0035   /// Init with \p CompilationDatabase and the paths of all files to be
0036   /// proccessed.
0037   StandaloneToolExecutor(
0038       const CompilationDatabase &Compilations,
0039       llvm::ArrayRef<std::string> SourcePaths,
0040       IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS =
0041           llvm::vfs::getRealFileSystem(),
0042       std::shared_ptr<PCHContainerOperations> PCHContainerOps =
0043           std::make_shared<PCHContainerOperations>());
0044 
0045   /// Init with \p CommonOptionsParser. This is expected to be used by
0046   /// `createExecutorFromCommandLineArgs` based on commandline options.
0047   ///
0048   /// The executor takes ownership of \p Options.
0049   StandaloneToolExecutor(
0050       CommonOptionsParser Options,
0051       std::shared_ptr<PCHContainerOperations> PCHContainerOps =
0052           std::make_shared<PCHContainerOperations>());
0053 
0054   StringRef getExecutorName() const override { return ExecutorName; }
0055 
0056   using ToolExecutor::execute;
0057 
0058   llvm::Error
0059   execute(llvm::ArrayRef<
0060           std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
0061               Actions) override;
0062 
0063   /// Set a \c DiagnosticConsumer to use during parsing.
0064   void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
0065     Tool.setDiagnosticConsumer(DiagConsumer);
0066   }
0067 
0068   ExecutionContext *getExecutionContext() override { return &Context; };
0069 
0070   ToolResults *getToolResults() override { return &Results; }
0071 
0072   llvm::ArrayRef<std::string> getSourcePaths() const {
0073     return Tool.getSourcePaths();
0074   }
0075 
0076   void mapVirtualFile(StringRef FilePath, StringRef Content) override {
0077     Tool.mapVirtualFile(FilePath, Content);
0078   }
0079 
0080   /// Returns the file manager used in the tool.
0081   ///
0082   /// The file manager is shared between all translation units.
0083   FileManager &getFiles() { return Tool.getFiles(); }
0084 
0085 private:
0086   // Used to store the parser when the executor is initialized with parser.
0087   std::optional<CommonOptionsParser> OptionsParser;
0088   // FIXME: The standalone executor is currently just a wrapper of `ClangTool`.
0089   // Merge `ClangTool` implementation into the this.
0090   ClangTool Tool;
0091   ExecutionContext Context;
0092   InMemoryToolResults Results;
0093   ArgumentsAdjuster ArgsAdjuster;
0094 };
0095 
0096 } // end namespace tooling
0097 } // end namespace clang
0098 
0099 #endif // LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H