clang 19.0.0git
Execution.cpp
Go to the documentation of this file.
1//===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===//
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
12
13LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry)
14
15namespace clang {
16namespace tooling {
17
18llvm::cl::opt<std::string>
19 ExecutorName("executor", llvm::cl::desc("The name of the executor to use."),
20 llvm::cl::init("standalone"));
21
22void InMemoryToolResults::addResult(StringRef Key, StringRef Value) {
23 KVResults.push_back({Strings.save(Key), Strings.save(Value)});
24}
25
26std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
28 return KVResults;
29}
30
32 llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) {
33 for (const auto &KV : KVResults) {
34 Callback(KV.first, KV.second);
35 }
36}
37
38void ExecutionContext::reportResult(StringRef Key, StringRef Value) {
39 Results->addResult(Key, Value);
40}
41
42llvm::Error
43ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) {
44 return execute(std::move(Action), ArgumentsAdjuster());
45}
46
47llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action,
48 ArgumentsAdjuster Adjuster) {
49 std::vector<
50 std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
51 Actions;
52 Actions.emplace_back(std::move(Action), std::move(Adjuster));
53 return execute(Actions);
54}
55
56namespace internal {
58createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
59 llvm::cl::OptionCategory &Category,
60 const char *Overview) {
61 auto OptionsParser =
62 CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,
63 /*Overview=*/Overview);
64 if (!OptionsParser)
65 return OptionsParser.takeError();
66 for (const auto &TEPlugin : ToolExecutorPluginRegistry::entries()) {
67 if (TEPlugin.getName() != ExecutorName) {
68 continue;
69 }
70 std::unique_ptr<ToolExecutorPlugin> Plugin(TEPlugin.instantiate());
72 Plugin->create(*OptionsParser);
73 if (!Executor) {
74 return llvm::make_error<llvm::StringError>(
75 llvm::Twine("Failed to create '") + TEPlugin.getName() +
76 "': " + llvm::toString(Executor.takeError()) + "\n",
77 llvm::inconvertibleErrorCode());
78 }
79 return std::move(*Executor);
80 }
81 return llvm::make_error<llvm::StringError>(
82 llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",
83 llvm::inconvertibleErrorCode());
84}
85} // end namespace internal
86
88createExecutorFromCommandLineArgs(int &argc, const char **argv,
89 llvm::cl::OptionCategory &Category,
90 const char *Overview) {
92 Overview);
93}
94
95// This anchor is used to force the linker to link in the generated object file
96// and thus register the StandaloneToolExecutorPlugin etc.
97extern volatile int StandaloneToolExecutorAnchorSource;
98extern volatile int AllTUsToolExecutorAnchorSource;
99static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =
101static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest =
103
104} // end namespace tooling
105} // end namespace clang
int Category
Definition: Format.cpp:2975
static llvm::Expected< CommonOptionsParser > create(int &argc, const char **argv, llvm::cl::OptionCategory &Category, llvm::cl::NumOccurrencesFlag OccurrencesFlag=llvm::cl::OneOrMore, const char *Overview=nullptr)
A factory method that is similar to the above constructor, except this returns an error instead exiti...
void reportResult(StringRef Key, StringRef Value)
Adds a KV pair to the result container of this execution.
Definition: Execution.cpp:38
void addResult(StringRef Key, StringRef Value) override
Definition: Execution.cpp:22
std::vector< std::pair< llvm::StringRef, llvm::StringRef > > AllKVResults() override
Definition: Execution.cpp:27
void forEachResult(llvm::function_ref< void(StringRef Key, StringRef Value)> Callback) override
Definition: Execution.cpp:31
virtual llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster > > Actions)=0
Executes each action with a corresponding arguments adjuster.
virtual void addResult(StringRef Key, StringRef Value)=0
llvm::Expected< std::unique_ptr< ToolExecutor > > createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, llvm::cl::OptionCategory &Category, const char *Overview=nullptr)
Definition: Execution.cpp:58
volatile int StandaloneToolExecutorAnchorSource
volatile int AllTUsToolExecutorAnchorSource
llvm::Registry< ToolExecutorPlugin > ToolExecutorPluginRegistry
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
llvm::cl::opt< std::string > ExecutorName
static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest
Definition: Execution.cpp:101
llvm::Expected< std::unique_ptr< ToolExecutor > > createExecutorFromCommandLineArgs(int &argc, const char **argv, llvm::cl::OptionCategory &Category, const char *Overview=nullptr)
This creates a ToolExecutor that is in the global registry based on commandline arguments.
Definition: Execution.cpp:88
static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest
Definition: Execution.cpp:99
The JSON file list parser is used to communicate input to InstallAPI.