Coverage Report

Created: 2021-08-24 07:12

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ModuleDepCollector.cpp - Callbacks to collect deps -------*- C++ -*-===//
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/ModuleDepCollector.h"
10
11
#include "clang/Frontend/CompilerInstance.h"
12
#include "clang/Lex/Preprocessor.h"
13
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
14
#include "llvm/Support/StringSaver.h"
15
16
using namespace clang;
17
using namespace tooling;
18
using namespace dependencies;
19
20
CompilerInvocation ModuleDepCollector::makeInvocationForModuleBuildWithoutPaths(
21
29
    const ModuleDeps &Deps) const {
22
  // Make a deep copy of the original Clang invocation.
23
29
  CompilerInvocation CI(OriginalInvocation);
24
25
  // Remove options incompatible with explicit module build.
26
29
  CI.getFrontendOpts().Inputs.clear();
27
29
  CI.getFrontendOpts().OutputFile.clear();
28
29
29
  CI.getFrontendOpts().ProgramAction = frontend::GenerateModule;
30
29
  CI.getLangOpts()->ModuleName = Deps.ID.ModuleName;
31
29
  CI.getFrontendOpts().IsSystemModule = Deps.IsSystem;
32
33
29
  CI.getLangOpts()->ImplicitModules = false;
34
35
  // Report the prebuilt modules this module uses.
36
29
  for (const auto &PrebuiltModule : Deps.PrebuiltModuleDeps) {
37
1
    CI.getFrontendOpts().ModuleFiles.push_back(PrebuiltModule.PCMFile);
38
1
    CI.getFrontendOpts().ModuleMapFiles.push_back(PrebuiltModule.ModuleMapFile);
39
1
  }
40
41
29
  CI.getPreprocessorOpts().ImplicitPCHInclude.clear();
42
43
29
  return CI;
44
29
}
45
46
static std::vector<std::string>
47
22
serializeCompilerInvocation(const CompilerInvocation &CI) {
48
  // Set up string allocator.
49
22
  llvm::BumpPtrAllocator Alloc;
50
22
  llvm::StringSaver Strings(Alloc);
51
1.18k
  auto SA = [&Strings](const Twine &Arg) { return Strings.save(Arg).data(); };
52
53
  // Synthesize full command line from the CompilerInvocation, including "-cc1".
54
22
  SmallVector<const char *, 32> Args{"-cc1"};
55
22
  CI.generateCC1CommandLine(Args, SA);
56
57
  // Convert arguments to the return type.
58
22
  return std::vector<std::string>{Args.begin(), Args.end()};
59
22
}
60
61
std::vector<std::string> ModuleDeps::getCanonicalCommandLine(
62
    std::function<StringRef(ModuleID)> LookupPCMPath,
63
15
    std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const {
64
15
  CompilerInvocation CI(Invocation);
65
15
  FrontendOptions &FrontendOpts = CI.getFrontendOpts();
66
67
15
  InputKind ModuleMapInputKind(FrontendOpts.DashX.getLanguage(),
68
15
                               InputKind::Format::ModuleMap);
69
15
  FrontendOpts.Inputs.emplace_back(ClangModuleMapFile, ModuleMapInputKind);
70
15
  FrontendOpts.OutputFile = std::string(LookupPCMPath(ID));
71
72
15
  dependencies::detail::collectPCMAndModuleMapPaths(
73
15
      ClangModuleDeps, LookupPCMPath, LookupModuleDeps,
74
15
      FrontendOpts.ModuleFiles, FrontendOpts.ModuleMapFiles);
75
76
15
  return serializeCompilerInvocation(CI);
77
15
}
78
79
std::vector<std::string>
80
7
ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const {
81
7
  return serializeCompilerInvocation(Invocation);
82
7
}
83
84
void dependencies::detail::collectPCMAndModuleMapPaths(
85
    llvm::ArrayRef<ModuleID> Modules,
86
    std::function<StringRef(ModuleID)> LookupPCMPath,
87
    std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps,
88
29
    std::vector<std::string> &PCMPaths, std::vector<std::string> &ModMapPaths) {
89
29
  llvm::StringSet<> AlreadyAdded;
90
91
29
  std::function<void(llvm::ArrayRef<ModuleID>)> AddArgs =
92
51
      [&](llvm::ArrayRef<ModuleID> Modules) {
93
51
        for (const ModuleID &MID : Modules) {
94
22
          if (!AlreadyAdded.insert(MID.ModuleName + MID.ContextHash).second)
95
0
            continue;
96
22
          const ModuleDeps &M = LookupModuleDeps(MID);
97
          // Depth first traversal.
98
22
          AddArgs(M.ClangModuleDeps);
99
22
          PCMPaths.push_back(LookupPCMPath(MID).str());
100
22
          if (!M.ClangModuleMapFile.empty())
101
22
            ModMapPaths.push_back(M.ClangModuleMapFile);
102
22
        }
103
51
      };
104
105
29
  AddArgs(Modules);
106
29
}
107
108
void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
109
                                       FileChangeReason Reason,
110
                                       SrcMgr::CharacteristicKind FileType,
111
133
                                       FileID PrevFID) {
112
133
  if (Reason != PPCallbacks::EnterFile)
113
65
    return;
114
  
115
  // This has to be delayed as the context hash can change at the start of
116
  // `CompilerInstance::ExecuteAction`.
117
68
  if (MDC.ContextHash.empty()) {
118
22
    MDC.ContextHash = Instance.getInvocation().getModuleHash();
119
22
    MDC.Consumer.handleContextHash(MDC.ContextHash);
120
22
  }
121
122
68
  SourceManager &SM = Instance.getSourceManager();
123
124
  // Dependency generation really does want to go all the way to the
125
  // file entry for a source location to find out what is depended on.
126
  // We do not want #line markers to affect dependency generation!
127
68
  if (Optional<StringRef> Filename =
128
68
          SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc))))
129
25
    MDC.FileDeps.push_back(
130
25
        std::string(llvm::sys::path::remove_leading_dotslash(*Filename)));
131
68
}
132
133
void ModuleDepCollectorPP::InclusionDirective(
134
    SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
135
    bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
136
    StringRef SearchPath, StringRef RelativePath, const Module *Imported,
137
29
    SrcMgr::CharacteristicKind FileType) {
138
29
  if (!File && 
!Imported0
) {
139
    // This is a non-modular include that HeaderSearch failed to find. Add it
140
    // here as `FileChanged` will never see it.
141
0
    MDC.FileDeps.push_back(std::string(FileName));
142
0
  }
143
29
  handleImport(Imported);
144
29
}
145
146
void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc,
147
                                        ModuleIdPath Path,
148
0
                                        const Module *Imported) {
149
0
  handleImport(Imported);
150
0
}
151
152
29
void ModuleDepCollectorPP::handleImport(const Module *Imported) {
153
29
  if (!Imported)
154
2
    return;
155
156
27
  const Module *TopLevelModule = Imported->getTopLevelModule();
157
158
27
  if (MDC.isPrebuiltModule(TopLevelModule))
159
1
    DirectPrebuiltModularDeps.insert(TopLevelModule);
160
26
  else
161
26
    DirectModularDeps.insert(TopLevelModule);
162
27
}
163
164
22
void ModuleDepCollectorPP::EndOfMainFile() {
165
22
  FileID MainFileID = Instance.getSourceManager().getMainFileID();
166
22
  MDC.MainFile = std::string(
167
22
      Instance.getSourceManager().getFileEntryForID(MainFileID)->getName());
168
169
22
  if (!Instance.getPreprocessorOpts().ImplicitPCHInclude.empty())
170
2
    MDC.FileDeps.push_back(Instance.getPreprocessorOpts().ImplicitPCHInclude);
171
172
26
  for (const Module *M : DirectModularDeps) {
173
    // A top-level module might not be actually imported as a module when
174
    // -fmodule-name is used to compile a translation unit that imports this
175
    // module. In that case it can be skipped. The appropriate header
176
    // dependencies will still be reported as expected.
177
26
    if (!M->getASTFile())
178
1
      continue;
179
25
    handleTopLevelModule(M);
180
25
  }
181
182
22
  MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts);
183
184
22
  for (auto &&I : MDC.ModularDeps)
185
30
    MDC.Consumer.handleModuleDependency(I.second);
186
187
22
  for (auto &&I : MDC.FileDeps)
188
27
    MDC.Consumer.handleFileDependency(I);
189
190
22
  for (auto &&I : DirectPrebuiltModularDeps)
191
1
    MDC.Consumer.handlePrebuiltModuleDependency(PrebuiltModuleDep{I});
192
22
}
193
194
30
ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
195
30
  assert(M == M->getTopLevelModule() && "Expected top level module!");
196
197
  // If this module has been handled already, just return its ID.
198
0
  auto ModI = MDC.ModularDeps.insert({M, ModuleDeps{}});
199
30
  if (!ModI.second)
200
0
    return ModI.first->second.ID;
201
202
30
  ModuleDeps &MD = ModI.first->second;
203
204
30
  MD.ID.ModuleName = M->getFullModuleName();
205
30
  MD.ImportedByMainFile = DirectModularDeps.contains(M);
206
30
  MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
207
30
  MD.IsSystem = M->IsSystem;
208
209
30
  const FileEntry *ModuleMap = Instance.getPreprocessor()
210
30
                                   .getHeaderSearchInfo()
211
30
                                   .getModuleMap()
212
30
                                   .getModuleMapFileForUniquing(M);
213
30
  MD.ClangModuleMapFile = std::string(ModuleMap ? 
ModuleMap->getName()28
:
""2
);
214
215
30
  serialization::ModuleFile *MF =
216
30
      MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
217
30
  MDC.Instance.getASTReader()->visitInputFiles(
218
61
      *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
219
        // __inferred_module.map is the result of the way in which an implicit
220
        // module build handles inferred modules. It adds an overlay VFS with
221
        // this file in the proper directory and relies on the rest of Clang to
222
        // handle it like normal. With explicitly built modules we don't need
223
        // to play VFS tricks, so replace it with the correct module map.
224
61
        if (IF.getFile()->getName().endswith("__inferred_module.map")) {
225
2
          MD.FileDeps.insert(ModuleMap->getName());
226
2
          return;
227
2
        }
228
59
        MD.FileDeps.insert(IF.getFile()->getName());
229
59
      });
230
231
  // Add direct prebuilt module dependencies now, so that we can use them when
232
  // creating a CompilerInvocation and computing context hash for this
233
  // ModuleDeps instance.
234
30
  addDirectPrebuiltModuleDeps(M, MD);
235
236
30
  MD.Invocation = MDC.makeInvocationForModuleBuildWithoutPaths(MD);
237
30
  MD.ID.ContextHash = MD.Invocation.getModuleHash();
238
239
30
  llvm::DenseSet<const Module *> AddedModules;
240
30
  addAllSubmoduleDeps(M, MD, AddedModules);
241
242
30
  return MD.ID;
243
30
}
244
245
void ModuleDepCollectorPP::addDirectPrebuiltModuleDeps(const Module *M,
246
30
                                                       ModuleDeps &MD) {
247
30
  for (const Module *Import : M->Imports)
248
6
    if (Import->getTopLevelModule() != M->getTopLevelModule())
249
6
      if (MDC.isPrebuiltModule(Import))
250
1
        MD.PrebuiltModuleDeps.emplace_back(Import);
251
30
}
252
253
void ModuleDepCollectorPP::addAllSubmoduleDeps(
254
    const Module *M, ModuleDeps &MD,
255
32
    llvm::DenseSet<const Module *> &AddedModules) {
256
32
  addModuleDep(M, MD, AddedModules);
257
258
32
  for (const Module *SubM : M->submodules())
259
2
    addAllSubmoduleDeps(SubM, MD, AddedModules);
260
32
}
261
262
void ModuleDepCollectorPP::addModuleDep(
263
    const Module *M, ModuleDeps &MD,
264
32
    llvm::DenseSet<const Module *> &AddedModules) {
265
32
  for (const Module *Import : M->Imports) {
266
6
    if (Import->getTopLevelModule() != M->getTopLevelModule() &&
267
6
        !MDC.isPrebuiltModule(Import)) {
268
5
      ModuleID ImportID = handleTopLevelModule(Import->getTopLevelModule());
269
5
      if (AddedModules.insert(Import->getTopLevelModule()).second)
270
5
        MD.ClangModuleDeps.push_back(ImportID);
271
5
    }
272
6
  }
273
32
}
274
275
ModuleDepCollector::ModuleDepCollector(
276
    std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
277
    DependencyConsumer &C, CompilerInvocation &&OriginalCI)
278
    : Instance(I), Consumer(C), Opts(std::move(Opts)),
279
23
      OriginalInvocation(std::move(OriginalCI)) {}
280
281
23
void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
282
23
  PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(Instance, *this));
283
23
}
284
285
22
void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
286
287
39
bool ModuleDepCollector::isPrebuiltModule(const Module *M) {
288
39
  std::string Name(M->getTopLevelModuleName());
289
39
  const auto &PrebuiltModuleFiles =
290
39
      Instance.getHeaderSearchOpts().PrebuiltModuleFiles;
291
39
  auto PrebuiltModuleFileIt = PrebuiltModuleFiles.find(Name);
292
39
  if (PrebuiltModuleFileIt == PrebuiltModuleFiles.end())
293
36
    return false;
294
3
  assert("Prebuilt module came from the expected AST file" &&
295
3
         PrebuiltModuleFileIt->second == M->getASTFile()->getName());
296
0
  return true;
297
39
}