Path: blob/main/contrib/llvm-project/clang/lib/Tooling/Execution.cpp
35233 views
//===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Tooling/Execution.h"9#include "clang/Tooling/ToolExecutorPluginRegistry.h"10#include "clang/Tooling/Tooling.h"1112LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry)1314namespace clang {15namespace tooling {1617llvm::cl::opt<std::string>18ExecutorName("executor", llvm::cl::desc("The name of the executor to use."),19llvm::cl::init("standalone"));2021void InMemoryToolResults::addResult(StringRef Key, StringRef Value) {22KVResults.push_back({Strings.save(Key), Strings.save(Value)});23}2425std::vector<std::pair<llvm::StringRef, llvm::StringRef>>26InMemoryToolResults::AllKVResults() {27return KVResults;28}2930void InMemoryToolResults::forEachResult(31llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) {32for (const auto &KV : KVResults) {33Callback(KV.first, KV.second);34}35}3637void ExecutionContext::reportResult(StringRef Key, StringRef Value) {38Results->addResult(Key, Value);39}4041llvm::Error42ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) {43return execute(std::move(Action), ArgumentsAdjuster());44}4546llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action,47ArgumentsAdjuster Adjuster) {48std::vector<49std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>50Actions;51Actions.emplace_back(std::move(Action), std::move(Adjuster));52return execute(Actions);53}5455namespace internal {56llvm::Expected<std::unique_ptr<ToolExecutor>>57createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,58llvm::cl::OptionCategory &Category,59const char *Overview) {60auto OptionsParser =61CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,62/*Overview=*/Overview);63if (!OptionsParser)64return OptionsParser.takeError();65for (const auto &TEPlugin : ToolExecutorPluginRegistry::entries()) {66if (TEPlugin.getName() != ExecutorName) {67continue;68}69std::unique_ptr<ToolExecutorPlugin> Plugin(TEPlugin.instantiate());70llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =71Plugin->create(*OptionsParser);72if (!Executor) {73return llvm::make_error<llvm::StringError>(74llvm::Twine("Failed to create '") + TEPlugin.getName() +75"': " + llvm::toString(Executor.takeError()) + "\n",76llvm::inconvertibleErrorCode());77}78return std::move(*Executor);79}80return llvm::make_error<llvm::StringError>(81llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",82llvm::inconvertibleErrorCode());83}84} // end namespace internal8586llvm::Expected<std::unique_ptr<ToolExecutor>>87createExecutorFromCommandLineArgs(int &argc, const char **argv,88llvm::cl::OptionCategory &Category,89const char *Overview) {90return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category,91Overview);92}9394// This anchor is used to force the linker to link in the generated object file95// and thus register the StandaloneToolExecutorPlugin etc.96extern volatile int StandaloneToolExecutorAnchorSource;97extern volatile int AllTUsToolExecutorAnchorSource;98static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =99StandaloneToolExecutorAnchorSource;100static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest =101AllTUsToolExecutorAnchorSource;102103} // end namespace tooling104} // end namespace clang105106107