diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h @@ -162,6 +162,10 @@ ClangModuleDeps[MD.ID] = std::move(MD); } + void handleDirectModuleDependency(ModuleID ID) override { + DirectModuleDeps.push_back(ID); + } + void handleContextHash(std::string Hash) override { ContextHash = std::move(Hash); } @@ -173,6 +177,7 @@ std::vector Dependencies; std::vector PrebuiltModuleDeps; llvm::MapVector ClangModuleDeps; + std::vector DirectModuleDeps; std::vector Commands; std::string ContextHash; std::vector OutputPaths; diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h @@ -56,6 +56,8 @@ virtual void handleModuleDependency(ModuleDeps MD) = 0; + virtual void handleDirectModuleDependency(ModuleID MD) = 0; + virtual void handleContextHash(std::string Hash) = 0; }; diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h --- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h +++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h @@ -136,10 +136,6 @@ /// determined that the differences are benign for this compilation. std::vector ClangModuleDeps; - // Used to track which modules that were discovered were directly imported by - // the primary TU. - bool ImportedByMainFile = false; - /// Compiler invocation that can be used to build this module. Does not /// include argv[0]. std::vector BuildArguments; @@ -172,8 +168,6 @@ private: /// The parent dependency collector. ModuleDepCollector &MDC; - /// Working set of direct modular dependencies. - llvm::SetVector DirectModularDeps; void handleImport(const Module *Imported); @@ -243,6 +237,8 @@ llvm::DenseMap ModuleDepsByID; /// Direct modular dependencies that have already been built. llvm::MapVector DirectPrebuiltModularDeps; + /// Working set of direct modular dependencies. + llvm::SetVector DirectModularDeps; /// Options that control the dependency output generation. std::unique_ptr Opts; /// The original Clang invocation passed to dependency scanner. diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp @@ -34,16 +34,12 @@ Dependencies.push_back(std::string(File)); } - void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override { - // Same as `handleModuleDependency`. - } - - void handleModuleDependency(ModuleDeps MD) override { - // These are ignored for the make format as it can't support the full - // set of deps, and handleFileDependency handles enough for implicitly - // built modules to work. - } - + // These are ignored for the make format as it can't support the full + // set of deps, and handleFileDependency handles enough for implicitly + // built modules to work. + void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override {} + void handleModuleDependency(ModuleDeps MD) override {} + void handleDirectModuleDependency(ModuleID ID) override {} void handleContextHash(std::string Hash) override {} void printDependencies(std::string &S) { @@ -179,14 +175,13 @@ for (auto &&M : ClangModuleDeps) { auto &MD = M.second; - if (MD.ImportedByMainFile) - TU.ClangModuleDeps.push_back(MD.ID); // TODO: Avoid handleModuleDependency even being called for modules // we've already seen. if (AlreadySeen.count(M.first)) continue; TU.ModuleGraph.push_back(std::move(MD)); } + TU.ClangModuleDeps = std::move(DirectModuleDeps); return TU; } diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -242,7 +242,7 @@ SmallVector DirectDeps; for (const auto &KV : ModularDeps) - if (KV.second->ImportedByMainFile) + if (DirectModularDeps.contains(KV.first)) DirectDeps.push_back(KV.second->ID); // TODO: Report module maps the same way it's done for modular dependencies. @@ -364,7 +364,7 @@ MDC.DirectPrebuiltModularDeps.insert( {TopLevelModule, PrebuiltModuleDep{TopLevelModule}}); else - DirectModularDeps.insert(TopLevelModule); + MDC.DirectModularDeps.insert(TopLevelModule); } void ModuleDepCollectorPP::EndOfMainFile() { @@ -394,9 +394,9 @@ for (const Module *M : MDC.ScanInstance.getPreprocessor().getAffectingClangModules()) if (!MDC.isPrebuiltModule(M)) - DirectModularDeps.insert(M); + MDC.DirectModularDeps.insert(M); - for (const Module *M : DirectModularDeps) + for (const Module *M : MDC.DirectModularDeps) handleTopLevelModule(M); MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts); @@ -408,6 +408,13 @@ for (auto &&I : MDC.ModularDeps) MDC.Consumer.handleModuleDependency(*I.second); + for (const Module *M : MDC.DirectModularDeps) { + auto It = MDC.ModularDeps.find(M); + // Only report direct dependencies that were successfully handled. + if (It != MDC.ModularDeps.end()) + MDC.Consumer.handleDirectModuleDependency(MDC.ModularDeps[M]->ID); + } + for (auto &&I : MDC.FileDeps) MDC.Consumer.handleFileDependency(I); @@ -435,7 +442,6 @@ ModuleDeps &MD = *ModI.first->second; MD.ID.ModuleName = M->getFullModuleName(); - MD.ImportedByMainFile = DirectModularDeps.contains(M); MD.IsSystem = M->IsSystem; ModuleMap &ModMapInfo =