clang 10.0.0git

StandaloneExecution.cpp
Go to the documentation of this file.
1 //===- lib/Tooling/Execution.cpp - Standalone clang action execution. -----===//
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 #include "clang/Tooling/StandaloneExecution.h"
10 #include "clang/Tooling/ToolExecutorPluginRegistry.h"
11
12 namespace clang {
13 namespace tooling {
14
15 static llvm::Error make_string_error(const llvm::Twine &Message) {
16 return llvm::make_error<llvm::StringError>(Message,
17 llvm::inconvertibleErrorCode());
18 }
19
20 const char *StandaloneToolExecutor::ExecutorName = "StandaloneToolExecutor";
21
22 static ArgumentsAdjuster getDefaultArgumentsAdjusters() {
23 return combineAdjusters(
24 getClangStripOutputAdjuster(),
25 combineAdjusters(getClangSyntaxOnlyAdjuster(),
26 getClangStripDependencyFileAdjuster()));
27 }
28
29 StandaloneToolExecutor::StandaloneToolExecutor(
30 const CompilationDatabase &Compilations,
31 llvm::ArrayRef<std::string> SourcePaths,
32 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
33 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
34 : Tool(Compilations, SourcePaths, std::move(PCHContainerOps),
35 std::move(BaseFS)),
36 Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
37 // Use self-defined default argument adjusters instead of the default
38 // adjusters that come with the old `ClangTool`.
39 Tool.clearArgumentsAdjusters();
40 }
41
42 StandaloneToolExecutor::StandaloneToolExecutor(
43 CommonOptionsParser Options,
44 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
45 : OptionsParser(std::move(Options)),
46 Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList(),
47 std::move(PCHContainerOps)),
48 Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
49 Tool.clearArgumentsAdjusters();
50 }
51
52 llvm::Error StandaloneToolExecutor::execute(
53 llvm::ArrayRef<
54 std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
55 Actions) {
56 if (Actions.empty())
57 return make_string_error("No action to execute.");
58
59 if (Actions.size() != 1)
60 return make_string_error(
61 "Only support executing exactly 1 action at this point.");
62
63 auto &Action = Actions.front();
64 Tool.appendArgumentsAdjuster(Action.second);
65 Tool.appendArgumentsAdjuster(ArgsAdjuster);
66 if (Tool.run(Action.first.get()))
67 return make_string_error("Failed to run action.");
68
69 return llvm::Error::success();
70 }
71
72 class StandaloneToolExecutorPlugin : public ToolExecutorPlugin {
73 public:
74 llvm::Expected<std::unique_ptr<ToolExecutor>>
75 create(CommonOptionsParser &OptionsParser) override {
76 if (OptionsParser.getSourcePathList().empty())
77 return make_string_error(
78 "[StandaloneToolExecutorPlugin] No positional argument found.");
79 return std::make_unique<StandaloneToolExecutor>(std::move(OptionsParser));
80 }
81 };
82
83 static ToolExecutorPluginRegistry::Add<StandaloneToolExecutorPlugin>
84 X("standalone", "Runs FrontendActions on a set of files provided "
85 "via positional arguments.");
86
87 // This anchor is used to force the linker to link in the generated object file
88 // and thus register the plugin.
89 volatile int StandaloneToolExecutorAnchorSource = 0;
90
91 } // end namespace tooling
92 } // end namespace clang
clang::tooling::StandaloneToolExecutorPlugin
Definition: StandaloneExecution.cpp:72
clang::tooling::make_string_error
static llvm::Error make_string_error(const llvm::Twine &Message)
Definition: StandaloneExecution.cpp:15
clang::tooling::StandaloneToolExecutorPlugin::create
llvm::Expected< std::unique_ptr< ToolExecutor > > create(CommonOptionsParser &OptionsParser) override
Create an ToolExecutor.
Definition: StandaloneExecution.cpp:75
clang::tooling::StandaloneToolExecutor::execute
llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions) override
Executes each action with a corresponding arguments adjuster.
Definition: StandaloneExecution.cpp:52
clang::tooling::StandaloneToolExecutorAnchorSource
volatile int StandaloneToolExecutorAnchorSource
Definition: StandaloneExecution.cpp:89
clang::tooling::getClangStripDependencyFileAdjuster
ArgumentsAdjuster getClangStripDependencyFileAdjuster()
Gets an argument adjuster which removes dependency-file related command line arguments.
Definition: ArgumentsAdjusters.cpp:88
clang::tooling::combineAdjusters
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, ArgumentsAdjuster Second)
Gets an argument adjuster which adjusts the arguments in sequence with the First adjuster and then wi...
Definition: ArgumentsAdjusters.cpp:131
std
Definition: Format.h:2445
clang::tooling::ClangTool::clearArgumentsAdjusters
void clearArgumentsAdjusters()
Clear the command line arguments adjuster chain.
Definition: Tooling.cpp:432
ToolExecutorPluginRegistry.h
StandaloneExecution.h
llvm::Expected
Definition: LLVM.h:38
clang::tooling::StandaloneToolExecutor::ExecutorName
static const char * ExecutorName
Definition: StandaloneExecution.h:32
llvm::ArrayRef< std::string >
clang::tooling::getClangStripOutputAdjuster
ArgumentsAdjuster getClangStripOutputAdjuster()
Gets an argument adjuster which removes output-related command line arguments.
Definition: ArgumentsAdjusters.cpp:54
clang::tooling::ArgumentsAdjuster
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
Definition: ArgumentsAdjusters.h:36
clang::DiagnosticLevelMask::Error
clang::tooling::CompilationDatabase
Interface for compilation databases.
Definition: CompilationDatabase.h:87
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >
clang::tooling::CommonOptionsParser
A parser for options common to all command-line Clang tools.
Definition: CommonOptionsParser.h:65
clang::tooling::ClangTool::run
int run(ToolAction *Action)
Runs an action over all files specified in the command line.
Definition: Tooling.cpp:448
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::tooling::ToolExecutorPlugin
Interface for factories that create specific executors.
Definition: Execution.h:151
clang::tooling::getClangSyntaxOnlyAdjuster
ArgumentsAdjuster getClangSyntaxOnlyAdjuster()
Gets an argument adjuster that converts input command line arguments to the "syntax check only" varia...
Definition: ArgumentsAdjusters.cpp:25
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::tooling::ClangTool::appendArgumentsAdjuster
void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster)
Append a command line arguments adjuster to the adjuster chain.
Definition: Tooling.cpp:428
clang::tooling::CommonOptionsParser::getSourcePathList
const std::vector< std::string > & getSourcePathList() const
Returns a list of source file paths to process.
Definition: CommonOptionsParser.h:107
clang::tooling::StandaloneToolExecutor::StandaloneToolExecutor
StandaloneToolExecutor(const CompilationDatabase &Compilations, llvm::ArrayRef< std::string > SourcePaths, IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS=llvm::vfs::getRealFileSystem(), std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Init with CompilationDatabase and the paths of all files to be proccessed.
Definition: StandaloneExecution.cpp:29
clang::tooling::getDefaultArgumentsAdjusters
static ArgumentsAdjuster getDefaultArgumentsAdjusters()
Definition: StandaloneExecution.cpp:22