Coverage Report

Created: 2022-05-17 06:19

/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
static void optimizeHeaderSearchOpts(HeaderSearchOptions &Opts,
21
                                     ASTReader &Reader,
22
7
                                     const serialization::ModuleFile &MF) {
23
  // Only preserve search paths that were used during the dependency scan.
24
7
  std::vector<HeaderSearchOptions::Entry> Entries = Opts.UserEntries;
25
7
  Opts.UserEntries.clear();
26
27
7
  llvm::BitVector SearchPathUsage(Entries.size());
28
7
  llvm::DenseSet<const serialization::ModuleFile *> Visited;
29
7
  std::function<void(const serialization::ModuleFile *)> VisitMF =
30
9
      [&](const serialization::ModuleFile *MF) {
31
9
        SearchPathUsage |= MF->SearchPathUsage;
32
9
        Visited.insert(MF);
33
9
        for (const serialization::ModuleFile *Import : MF->Imports)
34
2
          if (!Visited.contains(Import))
35
2
            VisitMF(Import);
36
9
      };
37
7
  VisitMF(&MF);
38
39
7
  for (auto Idx : SearchPathUsage.set_bits())
40
20
    Opts.UserEntries.push_back(Entries[Idx]);
41
7
}
42
43
CompilerInvocation ModuleDepCollector::makeInvocationForModuleBuildWithoutPaths(
44
    const ModuleDeps &Deps,
45
75
    llvm::function_ref<void(CompilerInvocation &)> Optimize) const {
46
  // Make a deep copy of the original Clang invocation.
47
75
  CompilerInvocation CI(OriginalInvocation);
48
49
75
  CI.getLangOpts()->resetNonModularOptions();
50
75
  CI.getPreprocessorOpts().resetNonModularOptions();
51
52
  // Remove options incompatible with explicit module build or are likely to
53
  // differ between identical modules discovered from different translation
54
  // units.
55
75
  CI.getFrontendOpts().Inputs.clear();
56
75
  CI.getFrontendOpts().OutputFile.clear();
57
75
  CI.getCodeGenOpts().MainFileName.clear();
58
75
  CI.getCodeGenOpts().DwarfDebugFlags.clear();
59
60
75
  CI.getFrontendOpts().ProgramAction = frontend::GenerateModule;
61
75
  CI.getLangOpts()->ModuleName = Deps.ID.ModuleName;
62
75
  CI.getFrontendOpts().IsSystemModule = Deps.IsSystem;
63
64
75
  CI.getLangOpts()->ImplicitModules = false;
65
75
  CI.getHeaderSearchOpts().ImplicitModuleMaps = false;
66
75
  CI.getHeaderSearchOpts().ModuleCachePath.clear();
67
68
  // Report the prebuilt modules this module uses.
69
75
  for (const auto &PrebuiltModule : Deps.PrebuiltModuleDeps)
70
3
    CI.getFrontendOpts().ModuleFiles.push_back(PrebuiltModule.PCMFile);
71
72
75
  CI.getFrontendOpts().ModuleMapFiles = Deps.ModuleMapFileDeps;
73
74
75
  Optimize(CI);
75
76
  // The original invocation probably didn't have strict context hash enabled.
77
  // We will use the context hash of this invocation to distinguish between
78
  // multiple incompatible versions of the same module and will use it when
79
  // reporting dependencies to the clients. Let's make sure we're using
80
  // **strict** context hash in order to prevent accidental sharing of
81
  // incompatible modules (e.g. with differences in search paths).
82
75
  CI.getHeaderSearchOpts().ModulesStrictContextHash = true;
83
84
75
  return CI;
85
75
}
86
87
static std::vector<std::string>
88
67
serializeCompilerInvocation(const CompilerInvocation &CI) {
89
  // Set up string allocator.
90
67
  llvm::BumpPtrAllocator Alloc;
91
67
  llvm::StringSaver Strings(Alloc);
92
3.57k
  auto SA = [&Strings](const Twine &Arg) { return Strings.save(Arg).data(); };
93
94
  // Synthesize full command line from the CompilerInvocation, including "-cc1".
95
67
  SmallVector<const char *, 32> Args{"-cc1"};
96
67
  CI.generateCC1CommandLine(Args, SA);
97
98
  // Convert arguments to the return type.
99
67
  return std::vector<std::string>{Args.begin(), Args.end()};
100
67
}
101
102
std::vector<std::string> ModuleDeps::getCanonicalCommandLine(
103
40
    std::function<StringRef(ModuleID)> LookupPCMPath) const {
104
40
  CompilerInvocation CI(BuildInvocation);
105
40
  FrontendOptions &FrontendOpts = CI.getFrontendOpts();
106
107
40
  InputKind ModuleMapInputKind(FrontendOpts.DashX.getLanguage(),
108
40
                               InputKind::Format::ModuleMap);
109
40
  FrontendOpts.Inputs.emplace_back(ClangModuleMapFile, ModuleMapInputKind);
110
40
  FrontendOpts.OutputFile = std::string(LookupPCMPath(ID));
111
112
40
  for (ModuleID MID : ClangModuleDeps)
113
20
    FrontendOpts.ModuleFiles.emplace_back(LookupPCMPath(MID));
114
115
40
  return serializeCompilerInvocation(CI);
116
40
}
117
118
std::vector<std::string>
119
27
ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const {
120
27
  return serializeCompilerInvocation(BuildInvocation);
121
27
}
122
123
void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
124
                                       FileChangeReason Reason,
125
                                       SrcMgr::CharacteristicKind FileType,
126
250
                                       FileID PrevFID) {
127
250
  if (Reason != PPCallbacks::EnterFile)
128
121
    return;
129
130
  // This has to be delayed as the context hash can change at the start of
131
  // `CompilerInstance::ExecuteAction`.
132
129
  if (MDC.ContextHash.empty()) {
133
44
    MDC.ContextHash = MDC.ScanInstance.getInvocation().getModuleHash();
134
44
    MDC.Consumer.handleContextHash(MDC.ContextHash);
135
44
  }
136
137
129
  SourceManager &SM = MDC.ScanInstance.getSourceManager();
138
139
  // Dependency generation really does want to go all the way to the
140
  // file entry for a source location to find out what is depended on.
141
  // We do not want #line markers to affect dependency generation!
142
129
  if (Optional<StringRef> Filename =
143
129
          SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc))))
144
45
    MDC.FileDeps.push_back(
145
45
        std::string(llvm::sys::path::remove_leading_dotslash(*Filename)));
146
129
}
147
148
void ModuleDepCollectorPP::InclusionDirective(
149
    SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
150
    bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
151
    StringRef SearchPath, StringRef RelativePath, const Module *Imported,
152
50
    SrcMgr::CharacteristicKind FileType) {
153
50
  if (!File && 
!Imported0
) {
154
    // This is a non-modular include that HeaderSearch failed to find. Add it
155
    // here as `FileChanged` will never see it.
156
0
    MDC.FileDeps.push_back(std::string(FileName));
157
0
  }
158
50
  handleImport(Imported);
159
50
}
160
161
void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc,
162
                                        ModuleIdPath Path,
163
5
                                        const Module *Imported) {
164
5
  handleImport(Imported);
165
5
}
166
167
55
void ModuleDepCollectorPP::handleImport(const Module *Imported) {
168
55
  if (!Imported)
169
3
    return;
170
171
52
  const Module *TopLevelModule = Imported->getTopLevelModule();
172
173
52
  if (MDC.isPrebuiltModule(TopLevelModule))
174
2
    DirectPrebuiltModularDeps.insert(TopLevelModule);
175
50
  else
176
50
    DirectModularDeps.insert(TopLevelModule);
177
52
}
178
179
47
void ModuleDepCollectorPP::EndOfMainFile() {
180
47
  FileID MainFileID = MDC.ScanInstance.getSourceManager().getMainFileID();
181
47
  MDC.MainFile = std::string(MDC.ScanInstance.getSourceManager()
182
47
                                 .getFileEntryForID(MainFileID)
183
47
                                 ->getName());
184
185
47
  if (!MDC.ScanInstance.getPreprocessorOpts().ImplicitPCHInclude.empty())
186
6
    MDC.FileDeps.push_back(
187
6
        MDC.ScanInstance.getPreprocessorOpts().ImplicitPCHInclude);
188
189
50
  for (const Module *M : DirectModularDeps) {
190
    // A top-level module might not be actually imported as a module when
191
    // -fmodule-name is used to compile a translation unit that imports this
192
    // module. In that case it can be skipped. The appropriate header
193
    // dependencies will still be reported as expected.
194
50
    if (!M->getASTFile())
195
1
      continue;
196
49
    handleTopLevelModule(M);
197
49
  }
198
199
47
  MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts);
200
201
47
  for (auto &&I : MDC.ModularDeps)
202
75
    MDC.Consumer.handleModuleDependency(I.second);
203
204
47
  for (auto &&I : MDC.FileDeps)
205
53
    MDC.Consumer.handleFileDependency(I);
206
207
47
  for (auto &&I : DirectPrebuiltModularDeps)
208
2
    MDC.Consumer.handlePrebuiltModuleDependency(PrebuiltModuleDep{I});
209
47
}
210
211
75
ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
212
75
  assert(M == M->getTopLevelModule() && "Expected top level module!");
213
214
  // If this module has been handled already, just return its ID.
215
0
  auto ModI = MDC.ModularDeps.insert({M, ModuleDeps{}});
216
75
  if (!ModI.second)
217
0
    return ModI.first->second.ID;
218
219
75
  ModuleDeps &MD = ModI.first->second;
220
221
75
  MD.ID.ModuleName = M->getFullModuleName();
222
75
  MD.ImportedByMainFile = DirectModularDeps.contains(M);
223
75
  MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
224
75
  MD.IsSystem = M->IsSystem;
225
226
75
  const FileEntry *ModuleMap = MDC.ScanInstance.getPreprocessor()
227
75
                                   .getHeaderSearchInfo()
228
75
                                   .getModuleMap()
229
75
                                   .getModuleMapFileForUniquing(M);
230
231
75
  if (ModuleMap) {
232
75
    StringRef Path = ModuleMap->tryGetRealPathName();
233
75
    if (Path.empty())
234
0
      Path = ModuleMap->getName();
235
75
    MD.ClangModuleMapFile = std::string(Path);
236
75
  }
237
238
75
  serialization::ModuleFile *MF =
239
75
      MDC.ScanInstance.getASTReader()->getModuleManager().lookup(
240
75
          M->getASTFile());
241
75
  MDC.ScanInstance.getASTReader()->visitInputFiles(
242
172
      *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
243
        // __inferred_module.map is the result of the way in which an implicit
244
        // module build handles inferred modules. It adds an overlay VFS with
245
        // this file in the proper directory and relies on the rest of Clang to
246
        // handle it like normal. With explicitly built modules we don't need
247
        // to play VFS tricks, so replace it with the correct module map.
248
172
        if (IF.getFile()->getName().endswith("__inferred_module.map")) {
249
2
          MD.FileDeps.insert(ModuleMap->getName());
250
2
          return;
251
2
        }
252
170
        MD.FileDeps.insert(IF.getFile()->getName());
253
170
      });
254
255
  // We usually don't need to list the module map files of our dependencies when
256
  // building a module explicitly: their semantics will be deserialized from PCM
257
  // files.
258
  //
259
  // However, some module maps loaded implicitly during the dependency scan can
260
  // describe anti-dependencies. That happens when this module, let's call it
261
  // M1, is marked as '[no_undeclared_includes]' and tries to access a header
262
  // "M2/M2.h" from another module, M2, but doesn't have a 'use M2;'
263
  // declaration. The explicit build needs the module map for M2 so that it
264
  // knows that textually including "M2/M2.h" is not allowed.
265
  // E.g., '__has_include("M2/M2.h")' should return false, but without M2's
266
  // module map the explicit build would return true.
267
  //
268
  // An alternative approach would be to tell the explicit build what its
269
  // textual dependencies are, instead of having it re-discover its
270
  // anti-dependencies. For example, we could create and use an `-ivfs-overlay`
271
  // with `fall-through: false` that explicitly listed the dependencies.
272
  // However, that's more complicated to implement and harder to reason about.
273
75
  if (M->NoUndeclaredIncludes) {
274
    // We don't have a good way to determine which module map described the
275
    // anti-dependency (let alone what's the corresponding top-level module
276
    // map). We simply specify all the module maps in the order they were loaded
277
    // during the implicit build during scan.
278
    // TODO: Resolve this by serializing and only using Module::UndeclaredUses.
279
1
    MDC.ScanInstance.getASTReader()->visitTopLevelModuleMaps(
280
2
        *MF, [&](const FileEntry *FE) {
281
2
          if (FE->getName().endswith("__inferred_module.map"))
282
0
            return;
283
          // The top-level modulemap of this module will be the input file. We
284
          // don't need to specify it as a module map.
285
2
          if (FE == ModuleMap)
286
1
            return;
287
1
          MD.ModuleMapFileDeps.push_back(FE->getName().str());
288
1
        });
289
1
  }
290
291
  // Add direct prebuilt module dependencies now, so that we can use them when
292
  // creating a CompilerInvocation and computing context hash for this
293
  // ModuleDeps instance.
294
75
  llvm::DenseSet<const Module *> SeenModules;
295
75
  addAllSubmodulePrebuiltDeps(M, MD, SeenModules);
296
297
75
  MD.BuildInvocation = MDC.makeInvocationForModuleBuildWithoutPaths(
298
75
      MD, [&](CompilerInvocation &BuildInvocation) {
299
75
        if (MDC.OptimizeArgs)
300
7
          optimizeHeaderSearchOpts(BuildInvocation.getHeaderSearchOpts(),
301
7
                                   *MDC.ScanInstance.getASTReader(), *MF);
302
75
      });
303
75
  MD.ID.ContextHash = MD.BuildInvocation.getModuleHash();
304
305
75
  llvm::DenseSet<const Module *> AddedModules;
306
75
  addAllSubmoduleDeps(M, MD, AddedModules);
307
308
75
  return MD.ID;
309
75
}
310
311
void ModuleDepCollectorPP::addAllSubmodulePrebuiltDeps(
312
    const Module *M, ModuleDeps &MD,
313
79
    llvm::DenseSet<const Module *> &SeenSubmodules) {
314
79
  addModulePrebuiltDeps(M, MD, SeenSubmodules);
315
316
79
  for (const Module *SubM : M->submodules())
317
4
    addAllSubmodulePrebuiltDeps(SubM, MD, SeenSubmodules);
318
79
}
319
320
void ModuleDepCollectorPP::addModulePrebuiltDeps(
321
    const Module *M, ModuleDeps &MD,
322
79
    llvm::DenseSet<const Module *> &SeenSubmodules) {
323
79
  for (const Module *Import : M->Imports)
324
29
    if (Import->getTopLevelModule() != M->getTopLevelModule())
325
29
      if (MDC.isPrebuiltModule(Import->getTopLevelModule()))
326
3
        if (SeenSubmodules.insert(Import->getTopLevelModule()).second)
327
3
          MD.PrebuiltModuleDeps.emplace_back(Import->getTopLevelModule());
328
79
}
329
330
void ModuleDepCollectorPP::addAllSubmoduleDeps(
331
    const Module *M, ModuleDeps &MD,
332
79
    llvm::DenseSet<const Module *> &AddedModules) {
333
79
  addModuleDep(M, MD, AddedModules);
334
335
79
  for (const Module *SubM : M->submodules())
336
4
    addAllSubmoduleDeps(SubM, MD, AddedModules);
337
79
}
338
339
void ModuleDepCollectorPP::addModuleDep(
340
    const Module *M, ModuleDeps &MD,
341
79
    llvm::DenseSet<const Module *> &AddedModules) {
342
79
  for (const Module *Import : M->Imports) {
343
29
    if (Import->getTopLevelModule() != M->getTopLevelModule() &&
344
29
        !MDC.isPrebuiltModule(Import)) {
345
26
      ModuleID ImportID = handleTopLevelModule(Import->getTopLevelModule());
346
26
      if (AddedModules.insert(Import->getTopLevelModule()).second)
347
26
        MD.ClangModuleDeps.push_back(ImportID);
348
26
    }
349
29
  }
350
79
}
351
352
ModuleDepCollector::ModuleDepCollector(
353
    std::unique_ptr<DependencyOutputOptions> Opts,
354
    CompilerInstance &ScanInstance, DependencyConsumer &C,
355
    CompilerInvocation &&OriginalCI, bool OptimizeArgs)
356
    : ScanInstance(ScanInstance), Consumer(C), Opts(std::move(Opts)),
357
48
      OriginalInvocation(std::move(OriginalCI)), OptimizeArgs(OptimizeArgs) {}
358
359
45
void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
360
45
  PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(*this));
361
45
}
362
363
46
void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
364
365
110
bool ModuleDepCollector::isPrebuiltModule(const Module *M) {
366
110
  std::string Name(M->getTopLevelModuleName());
367
110
  const auto &PrebuiltModuleFiles =
368
110
      ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles;
369
110
  auto PrebuiltModuleFileIt = PrebuiltModuleFiles.find(Name);
370
110
  if (PrebuiltModuleFileIt == PrebuiltModuleFiles.end())
371
101
    return false;
372
9
  assert("Prebuilt module came from the expected AST file" &&
373
9
         PrebuiltModuleFileIt->second == M->getASTFile()->getName());
374
0
  return true;
375
110
}