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 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({Strings.save(Key), Strings.save(Value)}); 25 } 26 27 std::vector<std::pair<llvm::StringRef, llvm::StringRef>> 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 57 namespace internal { 58 llvm::Expected<std::unique_ptr<ToolExecutor>> 59 createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, 60 llvm::cl::OptionCategory &Category, 61 const char *Overview) { 62 auto OptionsParser = 63 CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore, 64 /*Overview=*/Overview); 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 } 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 } 97 98 // This anchor is used to force the linker to link in the generated object file 99 // and thus register the StandaloneToolExecutorPlugin etc. 100 extern volatile int StandaloneToolExecutorAnchorSource; 101 extern volatile int AllTUsToolExecutorAnchorSource; 102 static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest = 103 StandaloneToolExecutorAnchorSource; 104 static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest = 105 AllTUsToolExecutorAnchorSource; 106 107 } // end namespace tooling 108 } // end namespace clang 109