Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DependencyScanningTool.cpp - clang-scan-deps service ---------------===//
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/DependencyScanning/DependencyScanningTool.h"
10
#include "clang/Frontend/Utils.h"
11
12
namespace clang{
13
namespace tooling{
14
namespace dependencies{
15
16
std::vector<std::string> FullDependencies::getAdditionalCommandLine(
17
    std::function<StringRef(ClangModuleDep)> LookupPCMPath,
18
4
    std::function<const ModuleDeps &(ClangModuleDep)> LookupModuleDeps) const {
19
4
  std::vector<std::string> Ret = AdditionalNonPathCommandLine;
20
4
21
4
  dependencies::detail::appendCommonModuleArguments(
22
4
      ClangModuleDeps, LookupPCMPath, LookupModuleDeps, Ret);
23
4
24
4
  return Ret;
25
4
}
26
27
DependencyScanningTool::DependencyScanningTool(
28
    DependencyScanningService &Service)
29
53
    : Worker(Service) {}
30
31
llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(
32
61
    const tooling::CompilationDatabase &Compilations, StringRef CWD) {
33
61
  /// Prints out all of the gathered dependencies into a string.
34
61
  class MakeDependencyPrinterConsumer : public DependencyConsumer {
35
61
  public:
36
61
    void handleFileDependency(const DependencyOutputOptions &Opts,
37
179
                              StringRef File) override {
38
179
      if (!this->Opts)
39
60
        this->Opts = std::make_unique<DependencyOutputOptions>(Opts);
40
179
      Dependencies.push_back(std::string(File));
41
179
    }
42
61
43
61
    void handleModuleDependency(ModuleDeps MD) override {
44
0
      // These are ignored for the make format as it can't support the full
45
0
      // set of deps, and handleFileDependency handles enough for implicitly
46
0
      // built modules to work.
47
0
    }
48
61
49
61
    void handleContextHash(std::string Hash) override 
{}0
50
61
51
61
    void printDependencies(std::string &S) {
52
57
      if (!Opts)
53
0
        return;
54
57
55
57
      class DependencyPrinter : public DependencyFileGenerator {
56
57
      public:
57
57
        DependencyPrinter(DependencyOutputOptions &Opts,
58
57
                          ArrayRef<std::string> Dependencies)
59
57
            : DependencyFileGenerator(Opts) {
60
57
          for (const auto &Dep : Dependencies)
61
176
            addDependency(Dep);
62
57
        }
63
57
64
58
        void printDependencies(std::string &S) {
65
58
          llvm::raw_string_ostream OS(S);
66
58
          outputDependencyFile(OS);
67
58
        }
68
57
      };
69
57
70
57
      DependencyPrinter Generator(*Opts, Dependencies);
71
57
      Generator.printDependencies(S);
72
57
    }
73
61
74
61
  private:
75
61
    std::unique_ptr<DependencyOutputOptions> Opts;
76
61
    std::vector<std::string> Dependencies;
77
61
  };
78
61
79
61
  // We expect a single command here because if a source file occurs multiple
80
61
  // times in the original CDB, then `computeDependencies` would run the
81
61
  // `DependencyScanningAction` once for every time the input occured in the
82
61
  // CDB. Instead we split up the CDB into single command chunks to avoid this
83
61
  // behavior.
84
61
  assert(Compilations.getAllCompileCommands().size() == 1 &&
85
61
         "Expected a compilation database with a single command!");
86
61
  std::string Input = Compilations.getAllCompileCommands().front().Filename;
87
61
88
61
  MakeDependencyPrinterConsumer Consumer;
89
61
  auto Result = Worker.computeDependencies(Input, CWD, Compilations, Consumer);
90
61
  if (Result)
91
3
    return std::move(Result);
92
58
  std::string Output;
93
58
  Consumer.printDependencies(Output);
94
58
  return Output;
95
58
}
96
97
llvm::Expected<FullDependenciesResult>
98
DependencyScanningTool::getFullDependencies(
99
    const tooling::CompilationDatabase &Compilations, StringRef CWD,
100
4
    const llvm::StringSet<> &AlreadySeen) {
101
4
  class FullDependencyPrinterConsumer : public DependencyConsumer {
102
4
  public:
103
4
    FullDependencyPrinterConsumer(const llvm::StringSet<> &AlreadySeen)
104
4
        : AlreadySeen(AlreadySeen) {}
105
4
106
4
    void handleFileDependency(const DependencyOutputOptions &Opts,
107
4
                              StringRef File) override {
108
4
      Dependencies.push_back(std::string(File));
109
4
    }
110
4
111
5
    void handleModuleDependency(ModuleDeps MD) override {
112
5
      ClangModuleDeps[MD.ContextHash + MD.ModuleName] = std::move(MD);
113
5
    }
114
4
115
4
    void handleContextHash(std::string Hash) override {
116
4
      ContextHash = std::move(Hash);
117
4
    }
118
4
119
4
    FullDependenciesResult getFullDependencies() const {
120
4
      FullDependencies FD;
121
4
122
4
      FD.ContextHash = std::move(ContextHash);
123
4
124
4
      FD.FileDeps.assign(Dependencies.begin(), Dependencies.end());
125
4
126
5
      for (auto &&M : ClangModuleDeps) {
127
5
        auto &MD = M.second;
128
5
        if (MD.ImportedByMainFile)
129
4
          FD.ClangModuleDeps.push_back({MD.ModuleName, ContextHash});
130
5
      }
131
4
132
4
      FullDependenciesResult FDR;
133
4
134
5
      for (auto &&M : ClangModuleDeps) {
135
5
        // TODO: Avoid handleModuleDependency even being called for modules
136
5
        //   we've already seen.
137
5
        if (AlreadySeen.count(M.first))
138
0
          continue;
139
5
        FDR.DiscoveredModules.push_back(std::move(M.second));
140
5
      }
141
4
142
4
      FDR.FullDeps = std::move(FD);
143
4
      return FDR;
144
4
    }
145
4
146
4
  private:
147
4
    std::vector<std::string> Dependencies;
148
4
    std::unordered_map<std::string, ModuleDeps> ClangModuleDeps;
149
4
    std::string ContextHash;
150
4
    std::vector<std::string> OutputPaths;
151
4
    const llvm::StringSet<> &AlreadySeen;
152
4
  };
153
4
154
4
  // We expect a single command here because if a source file occurs multiple
155
4
  // times in the original CDB, then `computeDependencies` would run the
156
4
  // `DependencyScanningAction` once for every time the input occured in the
157
4
  // CDB. Instead we split up the CDB into single command chunks to avoid this
158
4
  // behavior.
159
4
  assert(Compilations.getAllCompileCommands().size() == 1 &&
160
4
         "Expected a compilation database with a single command!");
161
4
  std::string Input = Compilations.getAllCompileCommands().front().Filename;
162
4
163
4
  FullDependencyPrinterConsumer Consumer(AlreadySeen);
164
4
  llvm::Error Result =
165
4
      Worker.computeDependencies(Input, CWD, Compilations, Consumer);
166
4
  if (Result)
167
0
    return std::move(Result);
168
4
  return Consumer.getFullDependencies();
169
4
}
170
171
} // end namespace dependencies
172
} // end namespace tooling
173
} // end namespace clang