Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Execution.cpp
Line
Count
Source (jump to first uncovered line)
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
104
void InMemoryToolResults::addResult(StringRef Key, StringRef Value) {
23
104
  KVResults.push_back({Strings.save(Key), Strings.save(Value)});
24
104
}
25
26
std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
27
3
InMemoryToolResults::AllKVResults() {
28
3
  return KVResults;
29
3
}
30
31
void InMemoryToolResults::forEachResult(
32
2
    llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) {
33
101
  for (const auto &KV : KVResults) {
34
101
    Callback(KV.first, KV.second);
35
101
  }
36
2
}
37
38
104
void ExecutionContext::reportResult(StringRef Key, StringRef Value) {
39
104
  Results->addResult(Key, Value);
40
104
}
41
42
llvm::Error
43
4
ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) {
44
4
  return execute(std::move(Action), ArgumentsAdjuster());
45
4
}
46
47
llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action,
48
5
                                  ArgumentsAdjuster Adjuster) {
49
5
  std::vector<
50
5
      std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
51
5
      Actions;
52
5
  Actions.emplace_back(std::move(Action), std::move(Adjuster));
53
5
  return execute(Actions);
54
5
}
55
56
namespace internal {
57
llvm::Expected<std::unique_ptr<ToolExecutor>>
58
createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
59
                                      llvm::cl::OptionCategory &Category,
60
4
                                      const char *Overview) {
61
4
  auto OptionsParser =
62
4
      CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,
63
4
                                  /*Overview=*/Overview);
64
4
  if (!OptionsParser)
65
1
    return OptionsParser.takeError();
66
3
  for (auto I = ToolExecutorPluginRegistry::begin(),
67
3
            E = ToolExecutorPluginRegistry::end();
68
7
       I != E; 
++I4
) {
69
7
    if (I->getName() != ExecutorName) {
70
4
      continue;
71
4
    }
72
3
    std::unique_ptr<ToolExecutorPlugin> Plugin(I->instantiate());
73
3
    llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =
74
3
        Plugin->create(*OptionsParser);
75
3
    if (!Executor) {
76
0
      return llvm::make_error<llvm::StringError>(
77
0
          llvm::Twine("Failed to create '") + I->getName() +
78
0
              "': " + llvm::toString(Executor.takeError()) + "\n",
79
0
          llvm::inconvertibleErrorCode());
80
0
    }
81
3
    return std::move(*Executor);
82
3
  }
83
3
  return llvm::make_error<llvm::StringError>(
84
0
      llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",
85
0
      llvm::inconvertibleErrorCode());
86
3
}
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
0
                                  const char *Overview) {
93
0
  return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category,
94
0
                                                         Overview);
95
0
}
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