clang 10.0.0git

Execution.cpp
Go to the documentation of this file.
1 //===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===//
2 //
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
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
18 llvm::cl::opt<std::string>
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) {
23 KVResults.push_back({Strings.save(Key), Strings.save(Value)});
24 }
25
26 std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
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
56 namespace internal {
57 llvm::Expected<std::unique_ptr<ToolExecutor>>
58 createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
59 llvm::cl::OptionCategory &Category,
60 const char *Overview) {
61 auto OptionsParser =
62 CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,
63 /*Overview=*/Overview);
64 if (!OptionsParser)
65 return OptionsParser.takeError();
66 for (auto I = ToolExecutorPluginRegistry::begin(),
67 E = ToolExecutorPluginRegistry::end();
68 I != E; ++I) {
69 if (I->getName() != ExecutorName) {
70 continue;
71 }
72 std::unique_ptr<ToolExecutorPlugin> Plugin(I->instantiate());
73 llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =
74 Plugin->create(*OptionsParser);
75 if (!Executor) {
76 return llvm::make_error<llvm::StringError>(
77 llvm::Twine("Failed to create '") + I->getName() +
78 "': " + llvm::toString(Executor.takeError()) + "\n",
79 llvm::inconvertibleErrorCode());
80 }
81 return std::move(*Executor);
82 }
83 return llvm::make_error<llvm::StringError>(
84 llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",
85 llvm::inconvertibleErrorCode());
86 }
87 } // end namespace internal
88
89 llvm::Expected<std::unique_ptr<ToolExecutor>>
90 createExecutorFromCommandLineArgs(int &argc, const char **argv,
91 llvm::cl::OptionCategory &Category,
92 const char *Overview) {
93 return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category,
94 Overview);
95 }
96
97 // This anchor is used to force the linker to link in the generated object file
98 // and thus register the StandaloneToolExecutorPlugin etc.
99 extern volatile int StandaloneToolExecutorAnchorSource;
100 extern volatile int AllTUsToolExecutorAnchorSource;
101 static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =
102 StandaloneToolExecutorAnchorSource;
103 static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest =
104 AllTUsToolExecutorAnchorSource;
105
106 } // end namespace tooling
107 } // 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:58
Execution.h
clang::tooling::InMemoryToolResults::forEachResult
void forEachResult(llvm::function_ref< void(StringRef Key, StringRef Value)> Callback) override
Definition: Execution.cpp:31
Value
Value
Definition: UninitializedValues.cpp:101
clang::tooling::AllTUsToolExecutorAnchorDest
static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest
Definition: Execution.cpp:103
clang::tooling::StandaloneToolExecutorAnchorSource
volatile int StandaloneToolExecutorAnchorSource
Definition: StandaloneExecution.cpp:89
toString
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Definition: SanitizerArgs.cpp:856
clang::tooling::ExecutorName
llvm::cl::opt< std::string > ExecutorName
ToolExecutorPluginRegistry.h
Category
int Category
Definition: Format.cpp:1828
clang::tooling::InMemoryToolResults::addResult
void addResult(StringRef Key, StringRef Value) override
Definition: Execution.cpp:22
llvm::Expected
Definition: LLVM.h:38
clang::tooling::ExecutionContext::reportResult
void reportResult(StringRef Key, StringRef Value)
Adds a KV pair to the result container of this execution.
Definition: Execution.cpp:38
clang::tooling::InMemoryToolResults::AllKVResults
std::vector< std::pair< llvm::StringRef, llvm::StringRef > > AllKVResults() override
Definition: Execution.cpp:27
clang::tooling::ArgumentsAdjuster
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
Definition: ArgumentsAdjusters.h:36
clang::DiagnosticLevelMask::Error
clang::tooling::AllTUsToolExecutorAnchorSource
volatile int AllTUsToolExecutorAnchorSource
Definition: AllTUsExecution.cpp:177
Tooling.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::tooling::ToolExecutorPluginRegistry
llvm::Registry< ToolExecutorPlugin > ToolExecutorPluginRegistry
Definition: ToolExecutorPluginRegistry.h:18
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:157
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:90
clang::tooling::StandaloneToolExecutorAnchorDest
static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest
Definition: Execution.cpp:101