| 1 | //===--- StandaloneExecution.h - Standalone execution. -*- 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 | // This file defines standalone execution of clang tools. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H |
| 14 | #define LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H |
| 15 | |
| 16 | #include "clang/Tooling/ArgumentsAdjusters.h" |
| 17 | #include "clang/Tooling/Execution.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace tooling { |
| 21 | |
| 22 | /// A standalone executor that runs FrontendActions on a given set of |
| 23 | /// TUs in sequence. |
| 24 | /// |
| 25 | /// By default, this executor uses the following arguments adjusters (as defined |
| 26 | /// in `clang/Tooling/ArgumentsAdjusters.h`): |
| 27 | /// - `getClangStripOutputAdjuster()` |
| 28 | /// - `getClangSyntaxOnlyAdjuster()` |
| 29 | /// - `getClangStripDependencyFileAdjuster()` |
| 30 | class StandaloneToolExecutor : public ToolExecutor { |
| 31 | public: |
| 32 | static const char *ExecutorName; |
| 33 | |
| 34 | /// Init with \p CompilationDatabase and the paths of all files to be |
| 35 | /// proccessed. |
| 36 | StandaloneToolExecutor( |
| 37 | const CompilationDatabase &Compilations, |
| 38 | llvm::ArrayRef<std::string> SourcePaths, |
| 39 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS = |
| 40 | llvm::vfs::getRealFileSystem(), |
| 41 | std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
| 42 | std::make_shared<PCHContainerOperations>()); |
| 43 | |
| 44 | /// Init with \p CommonOptionsParser. This is expected to be used by |
| 45 | /// `createExecutorFromCommandLineArgs` based on commandline options. |
| 46 | /// |
| 47 | /// The executor takes ownership of \p Options. |
| 48 | StandaloneToolExecutor( |
| 49 | CommonOptionsParser Options, |
| 50 | std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
| 51 | std::make_shared<PCHContainerOperations>()); |
| 52 | |
| 53 | StringRef getExecutorName() const override { return ExecutorName; } |
| 54 | |
| 55 | bool isSingleProcess() const override { return true; } |
| 56 | |
| 57 | using ToolExecutor::execute; |
| 58 | |
| 59 | llvm::Error |
| 60 | execute(llvm::ArrayRef< |
| 61 | std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>> |
| 62 | Actions) override; |
| 63 | |
| 64 | /// Set a \c DiagnosticConsumer to use during parsing. |
| 65 | void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) { |
| 66 | Tool.setDiagnosticConsumer(DiagConsumer); |
| 67 | } |
| 68 | |
| 69 | ExecutionContext *getExecutionContext() override { return &Context; }; |
| 70 | |
| 71 | ToolResults *getToolResults() override { return &Results; } |
| 72 | |
| 73 | llvm::ArrayRef<std::string> getSourcePaths() const { |
| 74 | return Tool.getSourcePaths(); |
| 75 | } |
| 76 | |
| 77 | void mapVirtualFile(StringRef FilePath, StringRef Content) override { |
| 78 | Tool.mapVirtualFile(FilePath, Content); |
| 79 | } |
| 80 | |
| 81 | /// Returns the file manager used in the tool. |
| 82 | /// |
| 83 | /// The file manager is shared between all translation units. |
| 84 | FileManager &getFiles() { return Tool.getFiles(); } |
| 85 | |
| 86 | private: |
| 87 | // Used to store the parser when the executor is initialized with parser. |
| 88 | llvm::Optional<CommonOptionsParser> OptionsParser; |
| 89 | // FIXME: The standalone executor is currently just a wrapper of `ClangTool`. |
| 90 | // Merge `ClangTool` implementation into the this. |
| 91 | ClangTool Tool; |
| 92 | ExecutionContext Context; |
| 93 | InMemoryToolResults Results; |
| 94 | ArgumentsAdjuster ArgsAdjuster; |
| 95 | }; |
| 96 | |
| 97 | } // end namespace tooling |
| 98 | } // end namespace clang |
| 99 | |
| 100 | #endif // LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H |
| 101 |