Coverage Report

Created: 2020-09-19 12:23

/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
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
11
12
#include "clang/Frontend/CompilerInstance.h"
13
#include "clang/Lex/Preprocessor.h"
14
#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h"
15
16
using namespace clang;
17
using namespace tooling;
18
using namespace dependencies;
19
20
std::vector<std::string> ModuleDeps::getFullCommandLine(
21
    std::function<StringRef(ClangModuleDep)> LookupPCMPath,
22
3
    std::function<const ModuleDeps &(ClangModuleDep)> LookupModuleDeps) const {
23
3
  std::vector<std::string> Ret = NonPathCommandLine;
24
3
25
  // TODO: Build full command line. That also means capturing the original
26
  //       command line into NonPathCommandLine.
27
3
28
3
  dependencies::detail::appendCommonModuleArguments(
29
3
      ClangModuleDeps, LookupPCMPath, LookupModuleDeps, Ret);
30
3
31
3
  return Ret;
32
3
}
33
34
void dependencies::detail::appendCommonModuleArguments(
35
    llvm::ArrayRef<ClangModuleDep> Modules,
36
    std::function<StringRef(ClangModuleDep)> LookupPCMPath,
37
    std::function<const ModuleDeps &(ClangModuleDep)> LookupModuleDeps,
38
7
    std::vector<std::string> &Result) {
39
7
  llvm::StringSet<> AlreadyAdded;
40
7
41
7
  std::function<void(llvm::ArrayRef<ClangModuleDep>)> AddArgs =
42
13
      [&](llvm::ArrayRef<ClangModuleDep> Modules) {
43
6
        for (const ClangModuleDep &CMD : Modules) {
44
6
          if (!AlreadyAdded.insert(CMD.ModuleName + CMD.ContextHash).second)
45
0
            continue;
46
6
          const ModuleDeps &M = LookupModuleDeps(CMD);
47
          // Depth first traversal.
48
6
          AddArgs(M.ClangModuleDeps);
49
6
          Result.push_back(("-fmodule-file=" + LookupPCMPath(CMD)).str());
50
6
          if (!M.ClangModuleMapFile.empty()) {
51
6
            Result.push_back("-fmodule-map-file=" + M.ClangModuleMapFile);
52
6
          }
53
6
        }
54
13
      };
55
7
56
7
  Result.push_back("-fno-implicit-modules");
57
7
  Result.push_back("-fno-implicit-module-maps");
58
7
  AddArgs(Modules);
59
7
}
60
61
void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
62
                                       FileChangeReason Reason,
63
                                       SrcMgr::CharacteristicKind FileType,
64
24
                                       FileID PrevFID) {
65
24
  if (Reason != PPCallbacks::EnterFile)
66
12
    return;
67
12
  
68
  // This has to be delayed as the context hash can change at the start of
69
  // `CompilerInstance::ExecuteAction`.
70
12
  if (MDC.ContextHash.empty()) {
71
4
    MDC.ContextHash = Instance.getInvocation().getModuleHash();
72
4
    MDC.Consumer.handleContextHash(MDC.ContextHash);
73
4
  }
74
12
75
12
  SourceManager &SM = Instance.getSourceManager();
76
12
77
  // Dependency generation really does want to go all the way to the
78
  // file entry for a source location to find out what is depended on.
79
  // We do not want #line markers to affect dependency generation!
80
12
  Optional<FileEntryRef> File =
81
12
      SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(Loc)));
82
12
  if (!File)
83
8
    return;
84
4
85
4
  StringRef FileName =
86
4
      llvm::sys::path::remove_leading_dotslash(File->getName());
87
4
88
4
  MDC.MainDeps.push_back(std::string(FileName));
89
4
}
90
91
void ModuleDepCollectorPP::InclusionDirective(
92
    SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
93
    bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
94
    StringRef SearchPath, StringRef RelativePath, const Module *Imported,
95
4
    SrcMgr::CharacteristicKind FileType) {
96
4
  if (!File && 
!Imported0
) {
97
    // This is a non-modular include that HeaderSearch failed to find. Add it
98
    // here as `FileChanged` will never see it.
99
0
    MDC.MainDeps.push_back(std::string(FileName));
100
0
  }
101
4
  handleImport(Imported);
102
4
}
103
104
void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc,
105
                                        ModuleIdPath Path,
106
0
                                        const Module *Imported) {
107
0
  handleImport(Imported);
108
0
}
109
110
4
void ModuleDepCollectorPP::handleImport(const Module *Imported) {
111
4
  if (!Imported)
112
0
    return;
113
4
114
4
  MDC.Deps[MDC.ContextHash + Imported->getTopLevelModule()->getFullModuleName()]
115
4
      .ImportedByMainFile = true;
116
4
  DirectDeps.insert(Imported->getTopLevelModule());
117
4
}
118
119
4
void ModuleDepCollectorPP::EndOfMainFile() {
120
4
  FileID MainFileID = Instance.getSourceManager().getMainFileID();
121
4
  MDC.MainFile = std::string(
122
4
      Instance.getSourceManager().getFileEntryForID(MainFileID)->getName());
123
4
124
4
  for (const Module *M : DirectDeps) {
125
4
    handleTopLevelModule(M);
126
4
  }
127
4
128
4
  for (auto &&I : MDC.Deps)
129
5
    MDC.Consumer.handleModuleDependency(I.second);
130
4
131
4
  for (auto &&I : MDC.MainDeps)
132
4
    MDC.Consumer.handleFileDependency(*MDC.Opts, I);
133
4
}
134
135
5
void ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
136
5
  assert(M == M->getTopLevelModule() && "Expected top level module!");
137
5
138
5
  auto ModI = MDC.Deps.insert(
139
5
      std::make_pair(MDC.ContextHash + M->getFullModuleName(), ModuleDeps{}));
140
5
141
5
  if (!ModI.first->second.ModuleName.empty())
142
0
    return;
143
5
144
5
  ModuleDeps &MD = ModI.first->second;
145
5
146
5
  const FileEntry *ModuleMap = Instance.getPreprocessor()
147
5
                                   .getHeaderSearchInfo()
148
5
                                   .getModuleMap()
149
5
                                   .getContainingModuleMapFile(M);
150
5
151
5
  MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : 
""0
);
152
5
  MD.ModuleName = M->getFullModuleName();
153
5
  MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
154
5
  MD.ContextHash = MDC.ContextHash;
155
5
  serialization::ModuleFile *MF =
156
5
      MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
157
5
  MDC.Instance.getASTReader()->visitInputFiles(
158
10
      *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
159
10
        MD.FileDeps.insert(IF.getFile()->getName());
160
10
      });
161
5
162
5
  llvm::DenseSet<const Module *> AddedModules;
163
5
  addAllSubmoduleDeps(M, MD, AddedModules);
164
5
}
165
166
void ModuleDepCollectorPP::addAllSubmoduleDeps(
167
    const Module *M, ModuleDeps &MD,
168
5
    llvm::DenseSet<const Module *> &AddedModules) {
169
5
  addModuleDep(M, MD, AddedModules);
170
5
171
5
  for (const Module *SubM : M->submodules())
172
0
    addAllSubmoduleDeps(SubM, MD, AddedModules);
173
5
}
174
175
void ModuleDepCollectorPP::addModuleDep(
176
    const Module *M, ModuleDeps &MD,
177
5
    llvm::DenseSet<const Module *> &AddedModules) {
178
1
  for (const Module *Import : M->Imports) {
179
1
    if (Import->getTopLevelModule() != M->getTopLevelModule()) {
180
1
      if (AddedModules.insert(Import->getTopLevelModule()).second)
181
1
        MD.ClangModuleDeps.push_back(
182
1
            {std::string(Import->getTopLevelModuleName()),
183
1
             Instance.getInvocation().getModuleHash()});
184
1
      handleTopLevelModule(Import->getTopLevelModule());
185
1
    }
186
1
  }
187
5
}
188
189
ModuleDepCollector::ModuleDepCollector(
190
    std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
191
    DependencyConsumer &C)
192
4
    : Instance(I), Consumer(C), Opts(std::move(Opts)) {}
193
194
4
void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
195
4
  PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(Instance, *this));
196
4
}
197
198
2
void ModuleDepCollector::attachToASTReader(ASTReader &R) {}