2017-10-26 18:38:14 +08:00
|
|
|
//===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-10-26 18:38:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/Execution.h"
|
|
|
|
#include "clang/Tooling/ToolExecutorPluginRegistry.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
|
|
|
|
LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry)
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
2018-10-12 19:47:36 +08:00
|
|
|
llvm::cl::opt<std::string>
|
2017-10-26 18:38:14 +08:00
|
|
|
ExecutorName("executor", llvm::cl::desc("The name of the executor to use."),
|
|
|
|
llvm::cl::init("standalone"));
|
|
|
|
|
|
|
|
void InMemoryToolResults::addResult(StringRef Key, StringRef Value) {
|
2018-07-23 19:25:25 +08:00
|
|
|
KVResults.push_back({Strings.save(Key), Strings.save(Value)});
|
2017-10-26 18:38:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 16:13:07 +08:00
|
|
|
std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
|
2017-10-26 18:38:14 +08:00
|
|
|
InMemoryToolResults::AllKVResults() {
|
|
|
|
return KVResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InMemoryToolResults::forEachResult(
|
|
|
|
llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) {
|
|
|
|
for (const auto &KV : KVResults) {
|
|
|
|
Callback(KV.first, KV.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecutionContext::reportResult(StringRef Key, StringRef Value) {
|
|
|
|
Results->addResult(Key, Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error
|
|
|
|
ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) {
|
|
|
|
return execute(std::move(Action), ArgumentsAdjuster());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action,
|
|
|
|
ArgumentsAdjuster Adjuster) {
|
|
|
|
std::vector<
|
|
|
|
std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
|
|
|
|
Actions;
|
|
|
|
Actions.emplace_back(std::move(Action), std::move(Adjuster));
|
|
|
|
return execute(Actions);
|
|
|
|
}
|
|
|
|
|
2017-11-03 23:20:57 +08:00
|
|
|
namespace internal {
|
2017-10-26 18:38:14 +08:00
|
|
|
llvm::Expected<std::unique_ptr<ToolExecutor>>
|
2017-11-03 23:20:57 +08:00
|
|
|
createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
|
|
|
|
llvm::cl::OptionCategory &Category,
|
|
|
|
const char *Overview) {
|
2017-10-26 18:38:14 +08:00
|
|
|
auto OptionsParser =
|
|
|
|
CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,
|
2017-11-03 23:57:27 +08:00
|
|
|
/*Overview=*/Overview);
|
2017-10-26 18:38:14 +08:00
|
|
|
if (!OptionsParser)
|
|
|
|
return OptionsParser.takeError();
|
|
|
|
for (auto I = ToolExecutorPluginRegistry::begin(),
|
|
|
|
E = ToolExecutorPluginRegistry::end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->getName() != ExecutorName) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::unique_ptr<ToolExecutorPlugin> Plugin(I->instantiate());
|
|
|
|
llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =
|
|
|
|
Plugin->create(*OptionsParser);
|
|
|
|
if (!Executor) {
|
|
|
|
return llvm::make_error<llvm::StringError>(
|
|
|
|
llvm::Twine("Failed to create '") + I->getName() +
|
|
|
|
"': " + llvm::toString(Executor.takeError()) + "\n",
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
return std::move(*Executor);
|
|
|
|
}
|
|
|
|
return llvm::make_error<llvm::StringError>(
|
|
|
|
llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
2017-11-03 23:20:57 +08:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
llvm::Expected<std::unique_ptr<ToolExecutor>>
|
|
|
|
createExecutorFromCommandLineArgs(int &argc, const char **argv,
|
|
|
|
llvm::cl::OptionCategory &Category,
|
|
|
|
const char *Overview) {
|
|
|
|
return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category,
|
|
|
|
Overview);
|
|
|
|
}
|
2017-10-26 18:38:14 +08:00
|
|
|
|
2017-11-03 23:57:27 +08:00
|
|
|
// This anchor is used to force the linker to link in the generated object file
|
2018-01-05 18:32:16 +08:00
|
|
|
// and thus register the StandaloneToolExecutorPlugin etc.
|
2017-11-03 23:57:27 +08:00
|
|
|
extern volatile int StandaloneToolExecutorAnchorSource;
|
2018-01-05 18:32:16 +08:00
|
|
|
extern volatile int AllTUsToolExecutorAnchorSource;
|
2017-11-03 23:57:27 +08:00
|
|
|
static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =
|
|
|
|
StandaloneToolExecutorAnchorSource;
|
2018-01-05 18:32:16 +08:00
|
|
|
static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest =
|
|
|
|
AllTUsToolExecutorAnchorSource;
|
2017-11-03 23:57:27 +08:00
|
|
|
|
2017-10-26 18:38:14 +08:00
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|