clang 8.0.0

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
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
clang::tooling::ToolExecutor::execute
virtual llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions)=0
Executes each action with a corresponding arguments adjuster.
clang::tooling::internal::createExecutorFromCommandLineArgsImpl
llvm::Expected< std::unique_ptr< ToolExecutor > > createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, llvm::cl::OptionCategory &Category, const char *Overview=nullptr)
Definition: Execution.cpp:59
Execution.h
clang::tooling::InMemoryToolResults::forEachResult
void forEachResult(llvm::function_ref< void(StringRef Key, StringRef Value)> Callback) override
Definition: Execution.cpp:32
Value
Value
Definition: UninitializedValues.cpp:102
clang::tooling::AllTUsToolExecutorAnchorDest
static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest
Definition: Execution.cpp:104
clang::tooling::StandaloneToolExecutorAnchorSource
volatile int StandaloneToolExecutorAnchorSource
Definition: StandaloneExecution.cpp:90
toString
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Definition: SanitizerArgs.cpp:775
clang::tooling::ExecutorName
llvm::cl::opt< std::string > ExecutorName
ToolExecutorPluginRegistry.h
Category
int Category
Definition: Format.cpp:1632
clang::tooling::InMemoryToolResults::addResult
void addResult(StringRef Key, StringRef Value) override
Definition: Execution.cpp:23
llvm::Expected
Definition: LLVM.h:39
clang::tooling::ExecutionContext::reportResult
void reportResult(StringRef Key, StringRef Value)
Adds a KV pair to the result container of this execution.
Definition: Execution.cpp:39
clang::tooling::InMemoryToolResults::AllKVResults
std::vector< std::pair< llvm::StringRef, llvm::StringRef > > AllKVResults() override
Definition: Execution.cpp:28
clang::tooling::ArgumentsAdjuster
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
Definition: ArgumentsAdjusters.h:37
clang::DiagnosticLevelMask::Error
clang::tooling::AllTUsToolExecutorAnchorSource
volatile int AllTUsToolExecutorAnchorSource
Definition: AllTUsExecution.cpp:184
Tooling.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::tooling::ToolExecutorPluginRegistry
llvm::Registry< ToolExecutorPlugin > ToolExecutorPluginRegistry
Definition: ToolExecutorPluginRegistry.h:19
clang::tooling::CommonOptionsParser::create
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...
Definition: CommonOptionsParser.cpp:160
clang::tooling::createExecutorFromCommandLineArgs
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
clang::tooling::StandaloneToolExecutorAnchorDest
static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest
Definition: Execution.cpp:102