Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 1 | //===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Tooling/Execution.h" |
| 10 | #include "clang/Tooling/ToolExecutorPluginRegistry.h" |
| 11 | #include "clang/Tooling/Tooling.h" |
| 12 | |
| 13 | LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry) |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tooling { |
| 17 | |
Eric Liu | f5617dc | 2018-10-12 11:47:36 +0000 | [diff] [blame] | 18 | llvm::cl::opt<std::string> |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 19 | ExecutorName("executor", llvm::cl::desc("The name of the executor to use."), |
| 20 | llvm::cl::init("standalone")); |
| 21 | |
| 22 | void InMemoryToolResults::addResult(StringRef Key, StringRef Value) { |
Sam McCall | fae3f89 | 2018-07-23 11:25:25 +0000 | [diff] [blame] | 23 | KVResults.push_back({Strings.save(Key), Strings.save(Value)}); |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Haojian Wu | 9f36c7e | 2018-04-11 08:13:07 +0000 | [diff] [blame] | 26 | std::vector<std::pair<llvm::StringRef, llvm::StringRef>> |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 27 | InMemoryToolResults::AllKVResults() { |
| 28 | return KVResults; |
| 29 | } |
| 30 | |
| 31 | void InMemoryToolResults::forEachResult( |
| 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 | |
| 38 | void ExecutionContext::reportResult(StringRef Key, StringRef Value) { |
| 39 | Results->addResult(Key, Value); |
| 40 | } |
| 41 | |
| 42 | llvm::Error |
| 43 | ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) { |
| 44 | return execute(std::move(Action), ArgumentsAdjuster()); |
| 45 | } |
| 46 | |
| 47 | llvm::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 | |
Eric Liu | 6e62284 | 2017-11-03 15:20:57 +0000 | [diff] [blame] | 56 | namespace internal { |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 57 | llvm::Expected<std::unique_ptr<ToolExecutor>> |
Eric Liu | 6e62284 | 2017-11-03 15:20:57 +0000 | [diff] [blame] | 58 | createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, |
| 59 | llvm::cl::OptionCategory &Category, |
| 60 | const char *Overview) { |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 61 | auto OptionsParser = |
| 62 | CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore, |
Eric Liu | 152ad05 | 2017-11-03 15:57:27 +0000 | [diff] [blame] | 63 | /*Overview=*/Overview); |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 64 | if (!OptionsParser) |
| 65 | return OptionsParser.takeError(); |
Nathan James | 8b0df1c | 2020-06-19 00:40:00 +0100 | [diff] [blame] | 66 | for (const auto &TEPlugin : ToolExecutorPluginRegistry::entries()) { |
| 67 | if (TEPlugin.getName() != ExecutorName) { |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 68 | continue; |
| 69 | } |
Nathan James | 8b0df1c | 2020-06-19 00:40:00 +0100 | [diff] [blame] | 70 | std::unique_ptr<ToolExecutorPlugin> Plugin(TEPlugin.instantiate()); |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 71 | llvm::Expected<std::unique_ptr<ToolExecutor>> Executor = |
| 72 | Plugin->create(*OptionsParser); |
| 73 | if (!Executor) { |
| 74 | return llvm::make_error<llvm::StringError>( |
Nathan James | 8b0df1c | 2020-06-19 00:40:00 +0100 | [diff] [blame] | 75 | llvm::Twine("Failed to create '") + TEPlugin.getName() + |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 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 | } |
Eric Liu | 6e62284 | 2017-11-03 15:20:57 +0000 | [diff] [blame] | 85 | } // end namespace internal |
| 86 | |
| 87 | llvm::Expected<std::unique_ptr<ToolExecutor>> |
| 88 | createExecutorFromCommandLineArgs(int &argc, const char **argv, |
| 89 | llvm::cl::OptionCategory &Category, |
| 90 | const char *Overview) { |
| 91 | return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category, |
| 92 | Overview); |
| 93 | } |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 94 | |
Eric Liu | 152ad05 | 2017-11-03 15:57:27 +0000 | [diff] [blame] | 95 | // This anchor is used to force the linker to link in the generated object file |
Eric Liu | e25f367 | 2018-01-05 10:32:16 +0000 | [diff] [blame] | 96 | // and thus register the StandaloneToolExecutorPlugin etc. |
Eric Liu | 152ad05 | 2017-11-03 15:57:27 +0000 | [diff] [blame] | 97 | extern volatile int StandaloneToolExecutorAnchorSource; |
Eric Liu | e25f367 | 2018-01-05 10:32:16 +0000 | [diff] [blame] | 98 | extern volatile int AllTUsToolExecutorAnchorSource; |
Eric Liu | 152ad05 | 2017-11-03 15:57:27 +0000 | [diff] [blame] | 99 | static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest = |
| 100 | StandaloneToolExecutorAnchorSource; |
Eric Liu | e25f367 | 2018-01-05 10:32:16 +0000 | [diff] [blame] | 101 | static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest = |
| 102 | AllTUsToolExecutorAnchorSource; |
Eric Liu | 152ad05 | 2017-11-03 15:57:27 +0000 | [diff] [blame] | 103 | |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 104 | } // end namespace tooling |
| 105 | } // end namespace clang |