Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 1 | //===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Tooling/Execution.h" |
| 11 | #include "clang/Tooling/ToolExecutorPluginRegistry.h" |
| 12 | #include "clang/Tooling/Tooling.h" |
| 13 | |
| 14 | LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry) |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tooling { |
| 18 | |
| 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")); |
| 22 | |
| 23 | void InMemoryToolResults::addResult(StringRef Key, StringRef Value) { |
| 24 | KVResults.push_back({Key.str(), Value.str()}); |
| 25 | } |
| 26 | |
| 27 | std::vector<std::pair<std::string, std::string>> |
| 28 | InMemoryToolResults::AllKVResults() { |
| 29 | return KVResults; |
| 30 | } |
| 31 | |
| 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); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void ExecutionContext::reportResult(StringRef Key, StringRef Value) { |
| 40 | Results->addResult(Key, Value); |
| 41 | } |
| 42 | |
| 43 | llvm::Error |
| 44 | ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) { |
| 45 | return execute(std::move(Action), ArgumentsAdjuster()); |
| 46 | } |
| 47 | |
| 48 | llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action, |
| 49 | ArgumentsAdjuster Adjuster) { |
| 50 | std::vector< |
| 51 | std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>> |
| 52 | Actions; |
| 53 | Actions.emplace_back(std::move(Action), std::move(Adjuster)); |
| 54 | return execute(Actions); |
| 55 | } |
| 56 | |
Eric Liu | 6e62284 | 2017-11-03 15:20:57 +0000 | [diff] [blame^] | 57 | namespace internal { |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 58 | llvm::Expected<std::unique_ptr<ToolExecutor>> |
Eric Liu | 6e62284 | 2017-11-03 15:20:57 +0000 | [diff] [blame^] | 59 | createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, |
| 60 | llvm::cl::OptionCategory &Category, |
| 61 | const char *Overview) { |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 62 | auto OptionsParser = |
| 63 | CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore, |
| 64 | /*Overview=*/nullptr); |
| 65 | if (!OptionsParser) |
| 66 | return OptionsParser.takeError(); |
| 67 | for (auto I = ToolExecutorPluginRegistry::begin(), |
| 68 | E = ToolExecutorPluginRegistry::end(); |
| 69 | I != E; ++I) { |
| 70 | if (I->getName() != ExecutorName) { |
| 71 | continue; |
| 72 | } |
| 73 | std::unique_ptr<ToolExecutorPlugin> Plugin(I->instantiate()); |
| 74 | llvm::Expected<std::unique_ptr<ToolExecutor>> Executor = |
| 75 | Plugin->create(*OptionsParser); |
| 76 | if (!Executor) { |
| 77 | return llvm::make_error<llvm::StringError>( |
| 78 | llvm::Twine("Failed to create '") + I->getName() + |
| 79 | "': " + llvm::toString(Executor.takeError()) + "\n", |
| 80 | llvm::inconvertibleErrorCode()); |
| 81 | } |
| 82 | return std::move(*Executor); |
| 83 | } |
| 84 | return llvm::make_error<llvm::StringError>( |
| 85 | llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.", |
| 86 | llvm::inconvertibleErrorCode()); |
| 87 | } |
Eric Liu | 6e62284 | 2017-11-03 15:20:57 +0000 | [diff] [blame^] | 88 | } // end namespace internal |
| 89 | |
| 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, |
| 95 | Overview); |
| 96 | } |
Eric Liu | 826b783 | 2017-10-26 10:38:14 +0000 | [diff] [blame] | 97 | |
| 98 | } // end namespace tooling |
| 99 | } // end namespace clang |