Skip to content

Commit c75b331

Browse files
committed
[clang][deps] Remove ModuleDeps::ImportedByMainFile
This information is already exposed via `TranslationUnitDeps::ClangModuleDeps` on the `DependencyScanningTool` level, and this patch also adds it on the `DependencyScanningWorker` level via `DependencyConsumer::handleDirectModuleDependency()`. Besides being redundant, this bit of information is misleading for clients that share single `ModuleDeps` instance between multiple TUs (by using the `AlreadySeen` set). The module can be imported directly in some TUs but transitively in others. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D156563
1 parent 8a077cf commit c75b331

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ class FullDependencyConsumer : public DependencyConsumer {
162162
ClangModuleDeps[MD.ID] = std::move(MD);
163163
}
164164

165+
void handleDirectModuleDependency(ModuleID ID) override {
166+
DirectModuleDeps.push_back(ID);
167+
}
168+
165169
void handleContextHash(std::string Hash) override {
166170
ContextHash = std::move(Hash);
167171
}
@@ -173,6 +177,7 @@ class FullDependencyConsumer : public DependencyConsumer {
173177
std::vector<std::string> Dependencies;
174178
std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
175179
llvm::MapVector<ModuleID, ModuleDeps> ClangModuleDeps;
180+
std::vector<ModuleID> DirectModuleDeps;
176181
std::vector<Command> Commands;
177182
std::string ContextHash;
178183
std::vector<std::string> OutputPaths;

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class DependencyConsumer {
5656

5757
virtual void handleModuleDependency(ModuleDeps MD) = 0;
5858

59+
virtual void handleDirectModuleDependency(ModuleID MD) = 0;
60+
5961
virtual void handleContextHash(std::string Hash) = 0;
6062
};
6163

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ struct ModuleDeps {
136136
/// determined that the differences are benign for this compilation.
137137
std::vector<ModuleID> ClangModuleDeps;
138138

139-
// Used to track which modules that were discovered were directly imported by
140-
// the primary TU.
141-
bool ImportedByMainFile = false;
142-
143139
/// Compiler invocation that can be used to build this module. Does not
144140
/// include argv[0].
145141
std::vector<std::string> BuildArguments;
@@ -172,8 +168,6 @@ class ModuleDepCollectorPP final : public PPCallbacks {
172168
private:
173169
/// The parent dependency collector.
174170
ModuleDepCollector &MDC;
175-
/// Working set of direct modular dependencies.
176-
llvm::SetVector<const Module *> DirectModularDeps;
177171

178172
void handleImport(const Module *Imported);
179173

@@ -243,6 +237,8 @@ class ModuleDepCollector final : public DependencyCollector {
243237
llvm::DenseMap<ModuleID, ModuleDeps *> ModuleDepsByID;
244238
/// Direct modular dependencies that have already been built.
245239
llvm::MapVector<const Module *, PrebuiltModuleDep> DirectPrebuiltModularDeps;
240+
/// Working set of direct modular dependencies.
241+
llvm::SetVector<const Module *> DirectModularDeps;
246242
/// Options that control the dependency output generation.
247243
std::unique_ptr<DependencyOutputOptions> Opts;
248244
/// The original Clang invocation passed to dependency scanner.

clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@ class MakeDependencyPrinterConsumer : public DependencyConsumer {
3434
Dependencies.push_back(std::string(File));
3535
}
3636

37-
void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override {
38-
// Same as `handleModuleDependency`.
39-
}
40-
41-
void handleModuleDependency(ModuleDeps MD) override {
42-
// These are ignored for the make format as it can't support the full
43-
// set of deps, and handleFileDependency handles enough for implicitly
44-
// built modules to work.
45-
}
46-
37+
// These are ignored for the make format as it can't support the full
38+
// set of deps, and handleFileDependency handles enough for implicitly
39+
// built modules to work.
40+
void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override {}
41+
void handleModuleDependency(ModuleDeps MD) override {}
42+
void handleDirectModuleDependency(ModuleID ID) override {}
4743
void handleContextHash(std::string Hash) override {}
4844

4945
void printDependencies(std::string &S) {
@@ -179,14 +175,13 @@ TranslationUnitDeps FullDependencyConsumer::takeTranslationUnitDeps() {
179175

180176
for (auto &&M : ClangModuleDeps) {
181177
auto &MD = M.second;
182-
if (MD.ImportedByMainFile)
183-
TU.ClangModuleDeps.push_back(MD.ID);
184178
// TODO: Avoid handleModuleDependency even being called for modules
185179
// we've already seen.
186180
if (AlreadySeen.count(M.first))
187181
continue;
188182
TU.ModuleGraph.push_back(std::move(MD));
189183
}
184+
TU.ClangModuleDeps = std::move(DirectModuleDeps);
190185

191186
return TU;
192187
}

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void ModuleDepCollector::applyDiscoveredDependencies(CompilerInvocation &CI) {
242242

243243
SmallVector<ModuleID> DirectDeps;
244244
for (const auto &KV : ModularDeps)
245-
if (KV.second->ImportedByMainFile)
245+
if (DirectModularDeps.contains(KV.first))
246246
DirectDeps.push_back(KV.second->ID);
247247

248248
// TODO: Report module maps the same way it's done for modular dependencies.
@@ -364,7 +364,7 @@ void ModuleDepCollectorPP::handleImport(const Module *Imported) {
364364
MDC.DirectPrebuiltModularDeps.insert(
365365
{TopLevelModule, PrebuiltModuleDep{TopLevelModule}});
366366
else
367-
DirectModularDeps.insert(TopLevelModule);
367+
MDC.DirectModularDeps.insert(TopLevelModule);
368368
}
369369

370370
void ModuleDepCollectorPP::EndOfMainFile() {
@@ -394,9 +394,9 @@ void ModuleDepCollectorPP::EndOfMainFile() {
394394
for (const Module *M :
395395
MDC.ScanInstance.getPreprocessor().getAffectingClangModules())
396396
if (!MDC.isPrebuiltModule(M))
397-
DirectModularDeps.insert(M);
397+
MDC.DirectModularDeps.insert(M);
398398

399-
for (const Module *M : DirectModularDeps)
399+
for (const Module *M : MDC.DirectModularDeps)
400400
handleTopLevelModule(M);
401401

402402
MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts);
@@ -408,6 +408,13 @@ void ModuleDepCollectorPP::EndOfMainFile() {
408408
for (auto &&I : MDC.ModularDeps)
409409
MDC.Consumer.handleModuleDependency(*I.second);
410410

411+
for (const Module *M : MDC.DirectModularDeps) {
412+
auto It = MDC.ModularDeps.find(M);
413+
// Only report direct dependencies that were successfully handled.
414+
if (It != MDC.ModularDeps.end())
415+
MDC.Consumer.handleDirectModuleDependency(MDC.ModularDeps[M]->ID);
416+
}
417+
411418
for (auto &&I : MDC.FileDeps)
412419
MDC.Consumer.handleFileDependency(I);
413420

@@ -435,7 +442,6 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
435442
ModuleDeps &MD = *ModI.first->second;
436443

437444
MD.ID.ModuleName = M->getFullModuleName();
438-
MD.ImportedByMainFile = DirectModularDeps.contains(M);
439445
MD.IsSystem = M->IsSystem;
440446

441447
ModuleMap &ModMapInfo =

0 commit comments

Comments
 (0)