clang  6.0.0svn
Execution.cpp
Go to the documentation of this file.
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 
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>>
29  return KVResults;
30 }
31 
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 
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 {
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());
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 
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.
100 extern volatile int StandaloneToolExecutorAnchorSource;
101 static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =
103 
104 } // end namespace tooling
105 } // end namespace clang
virtual llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions)=0
Executes each action with a corresponding arguments adjuster.
llvm::Expected< std::unique_ptr< ToolExecutor > > createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, llvm::cl::OptionCategory &Category, const char *Overview=nullptr)
Definition: Execution.cpp:59
void forEachResult(llvm::function_ref< void(StringRef Key, StringRef Value)> Callback) override
Definition: Execution.cpp:32
volatile int StandaloneToolExecutorAnchorSource
int Category
Definition: Format.cpp:1348
std::vector< std::pair< std::string, std::string > > AllKVResults() override
Definition: Execution.cpp:28
void addResult(StringRef Key, StringRef Value) override
Definition: Execution.cpp:23
void reportResult(StringRef Key, StringRef Value)
Adds a KV pair to the result container of this execution.
Definition: Execution.cpp:39
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
static llvm::cl::opt< std::string > ExecutorName("executor", llvm::cl::desc("The name of the executor to use."), llvm::cl::init("standalone"))
Dataflow Directional Tag Classes.
llvm::Registry< ToolExecutorPlugin > ToolExecutorPluginRegistry
static llvm::Expected< CommonOptionsParser > create(int &argc, const char **argv, llvm::cl::OptionCategory &Category, llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview=nullptr)
A factory method that is similar to the above constructor, except this returns an error instead exiti...
std::string toString(const til::SExpr *E)
llvm::Expected< std::unique_ptr< ToolExecutor > > createExecutorFromCommandLineArgs(int &argc, const char **argv, llvm::cl::OptionCategory &Category, const char *Overview=nullptr)
This creates a ToolExecutor that is in the global registry based on commandline arguments.
Definition: Execution.cpp:91
static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest
Definition: Execution.cpp:101