Coverage Report

Created: 2021-01-19 06:58

/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
21
4
  dependencies::detail::appendCommonModuleArguments(
22
4
      ClangModuleDeps, LookupPCMPath, LookupModuleDeps, Ret);
23
24
4
  return Ret;
25
4
}
26
27
DependencyScanningTool::DependencyScanningTool(
28
    DependencyScanningService &Service)
29
59
    : Worker(Service) {}
30
31
llvm::Expected<std::string> DependencyScanningTool::getDependencyFile(
32
71
    const tooling::CompilationDatabase &Compilations, StringRef CWD) {
33
  /// Prints out all of the gathered dependencies into a string.
34
71
  class MakeDependencyPrinterConsumer : public DependencyConsumer {
35
71
  public:
36
71
    void handleFileDependency(const DependencyOutputOptions &Opts,
37
195
                              StringRef File) override {
38
195
      if (!this->Opts)
39
69
        this->Opts = std::make_unique<DependencyOutputOptions>(Opts);
40
195
      Dependencies.push_back(std::string(File));
41
195
    }
42
43
0
    void handleModuleDependency(ModuleDeps MD) override {
44
      // These are ignored for the make format as it can't support the full
45
      // set of deps, and handleFileDependency handles enough for implicitly
46
      // built modules to work.
47
0
    }
48
49
0
    void handleContextHash(std::string Hash) override {}
50
51
65
    void printDependencies(std::string &S) {
52
65
      if (!Opts)
53
0
        return;
54
55
65
      class DependencyPrinter : public DependencyFileGenerator {
56
65
      public:
57
65
        DependencyPrinter(DependencyOutputOptions &Opts,
58
65
                          ArrayRef<std::string> Dependencies)
59
65
            : DependencyFileGenerator(Opts) {
60
65
          for (const auto &Dep : Dependencies)
61
193
            addDependency(Dep);
62
65
        }
63
64
68
        void printDependencies(std::string &S) {
65
68
          llvm::raw_string_ostream OS(S);
66
68
          outputDependencyFile(OS);
67
68
        }
68
65
      };
69
70
65
      DependencyPrinter Generator(*Opts, Dependencies);
71
65
      Generator.printDependencies(S);
72
65
    }
73
74
71
  private:
75
71
    std::unique_ptr<DependencyOutputOptions> Opts;
76
71
    std::vector<std::string> Dependencies;
77
71
  };
78
79
  // We expect a single command here because if a source file occurs multiple
80
  // times in the original CDB, then `computeDependencies` would run the
81
  // `DependencyScanningAction` once for every time the input occured in the
82
  // CDB. Instead we split up the CDB into single command chunks to avoid this
83
  // behavior.
84
71
  assert(Compilations.getAllCompileCommands().size() == 1 &&
85
71
         "Expected a compilation database with a single command!");
86
71
  std::string Input = Compilations.getAllCompileCommands().front().Filename;
87
88
71
  MakeDependencyPrinterConsumer Consumer;
89
71
  auto Result = Worker.computeDependencies(Input, CWD, Compilations, Consumer);
90
71
  if (Result)
91
3
    return std::move(Result);
92
68
  std::string Output;
93
68
  Consumer.printDependencies(Output);
94
68
  return Output;
95
68
}
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
106
4
    void handleFileDependency(const DependencyOutputOptions &Opts,
107
4
                              StringRef File) override {
108
4
      Dependencies.push_back(std::string(File));
109
4
    }
110
111
5
    void handleModuleDependency(ModuleDeps MD) override {
112
5
      ClangModuleDeps[MD.ContextHash + MD.ModuleName] = std::move(MD);
113
5
    }
114
115
4
    void handleContextHash(std::string Hash) override {
116
4
      ContextHash = std::move(Hash);
117
4
    }
118
119
4
    FullDependenciesResult getFullDependencies() const {
120
4
      FullDependencies FD;
121
122
4
      FD.ContextHash = std::move(ContextHash);
123
124
4
      FD.FileDeps.assign(Dependencies.begin(), Dependencies.end());
125
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
132
4
      FullDependenciesResult FDR;
133
134
5
      for (auto &&M : ClangModuleDeps) {
135
        // TODO: Avoid handleModuleDependency even being called for modules
136
        //   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
142
4
      FDR.FullDeps = std::move(FD);
143
4
      return FDR;
144
4
    }
145
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
154
  // We expect a single command here because if a source file occurs multiple
155
  // times in the original CDB, then `computeDependencies` would run the
156
  // `DependencyScanningAction` once for every time the input occured in the
157
  // CDB. Instead we split up the CDB into single command chunks to avoid this
158
  // 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
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