1 //===--- StandaloneExecution.h - Standalone execution. -*- C++ ----------*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines standalone execution of clang tools.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
15 #define LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
17 #include "clang/Tooling/ArgumentsAdjusters.h"
18 #include "clang/Tooling/Execution.h"
23 /// \brief A standalone executor that runs FrontendActions on a given set of
26 /// By default, this executor uses the following arguments adjusters (as defined
27 /// in `clang/Tooling/ArgumentsAdjusters.h`):
28 /// - `getClangStripOutputAdjuster()`
29 /// - `getClangSyntaxOnlyAdjuster()`
30 /// - `getClangStripDependencyFileAdjuster()`
31 class StandaloneToolExecutor : public ToolExecutor {
33 static const char *ExecutorName;
35 /// \brief Init with \p CompilationDatabase and the paths of all files to be
37 StandaloneToolExecutor(
38 const CompilationDatabase &Compilations,
39 llvm::ArrayRef<std::string> SourcePaths,
40 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
41 std::make_shared<PCHContainerOperations>());
43 /// \brief Init with \p CommonOptionsParser. This is expected to be used by
44 /// `createExecutorFromCommandLineArgs` based on commandline options.
46 /// The executor takes ownership of \p Options.
47 StandaloneToolExecutor(
48 CommonOptionsParser Options,
49 std::shared_ptr<PCHContainerOperations> PCHContainerOps =
50 std::make_shared<PCHContainerOperations>());
52 StringRef getExecutorName() const override { return ExecutorName; }
54 using ToolExecutor::execute;
57 execute(llvm::ArrayRef<
58 std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
61 /// \brief Set a \c DiagnosticConsumer to use during parsing.
62 void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
63 Tool.setDiagnosticConsumer(DiagConsumer);
66 ExecutionContext *getExecutionContext() override { return &Context; };
68 ToolResults *getToolResults() override { return &Results; }
70 llvm::ArrayRef<std::string> getSourcePaths() const {
71 return Tool.getSourcePaths();
74 void mapVirtualFile(StringRef FilePath, StringRef Content) override {
75 Tool.mapVirtualFile(FilePath, Content);
78 /// \brief Returns the file manager used in the tool.
80 /// The file manager is shared between all translation units.
81 FileManager &getFiles() { return Tool.getFiles(); }
84 // Used to store the parser when the executor is initialized with parser.
85 llvm::Optional<CommonOptionsParser> OptionsParser;
86 // FIXME: The standalone executor is currently just a wrapper of `ClangTool`.
87 // Merge `ClangTool` implementation into the this.
89 ExecutionContext Context;
90 InMemoryToolResults Results;
91 ArgumentsAdjuster ArgsAdjuster;
94 } // end namespace tooling
95 } // end namespace clang
97 #endif // LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H