1 //===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===//
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 #include "clang/Tooling/Execution.h"
11 #include "clang/Tooling/ToolExecutorPluginRegistry.h"
12 #include "clang/Tooling/Tooling.h"
14 LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry)
19 static llvm::cl::opt<std::string>
20 ExecutorName("executor", llvm::cl::desc("The name of the executor to use."),
21 llvm::cl::init("standalone"));
23 void InMemoryToolResults::addResult(StringRef Key, StringRef Value) {
24 KVResults.push_back({Key.str(), Value.str()});
27 std::vector<std::pair<std::string, std::string>>
28 InMemoryToolResults::AllKVResults() {
32 void InMemoryToolResults::forEachResult(
33 llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) {
34 for (const auto &KV : KVResults) {
35 Callback(KV.first, KV.second);
39 void ExecutionContext::reportResult(StringRef Key, StringRef Value) {
40 Results->addResult(Key, Value);
44 ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) {
45 return execute(std::move(Action), ArgumentsAdjuster());
48 llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action,
49 ArgumentsAdjuster Adjuster) {
51 std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
53 Actions.emplace_back(std::move(Action), std::move(Adjuster));
54 return execute(Actions);
58 llvm::Expected<std::unique_ptr<ToolExecutor>>
59 createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
60 llvm::cl::OptionCategory &Category,
61 const char *Overview) {
63 CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,
64 /*Overview=*/Overview);
66 return OptionsParser.takeError();
67 for (auto I = ToolExecutorPluginRegistry::begin(),
68 E = ToolExecutorPluginRegistry::end();
70 if (I->getName() != ExecutorName) {
73 std::unique_ptr<ToolExecutorPlugin> Plugin(I->instantiate());
74 llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =
75 Plugin->create(*OptionsParser);
77 return llvm::make_error<llvm::StringError>(
78 llvm::Twine("Failed to create '") + I->getName() +
79 "': " + llvm::toString(Executor.takeError()) + "\n",
80 llvm::inconvertibleErrorCode());
82 return std::move(*Executor);
84 return llvm::make_error<llvm::StringError>(
85 llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",
86 llvm::inconvertibleErrorCode());
88 } // end namespace internal
90 llvm::Expected<std::unique_ptr<ToolExecutor>>
91 createExecutorFromCommandLineArgs(int &argc, const char **argv,
92 llvm::cl::OptionCategory &Category,
93 const char *Overview) {
94 return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category,
98 // This anchor is used to force the linker to link in the generated object file
99 // and thus register the StandaloneToolExecutorPlugin.
100 extern volatile int StandaloneToolExecutorAnchorSource;
101 static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =
102 StandaloneToolExecutorAnchorSource;
104 } // end namespace tooling
105 } // end namespace clang