diff --git a/include/swift/AST/ModuleDependencies.h b/include/swift/AST/ModuleDependencies.h index 3739d7465eaf1..eb15df008e582 100644 --- a/include/swift/AST/ModuleDependencies.h +++ b/include/swift/AST/ModuleDependencies.h @@ -23,9 +23,11 @@ #include "swift/Basic/Assertions.h" #include "swift/Basic/CXXStdlibKind.h" #include "swift/Basic/LLVM.h" +#include "swift/ClangImporter/ClangImporter.h" #include "swift/Serialization/Validation.h" #include "clang/CAS/CASOptions.h" #include "clang/Tooling/DependencyScanning/DependencyScanningService.h" +#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h" #include "clang/Tooling/DependencyScanning/ModuleDepCollector.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" @@ -111,6 +113,18 @@ void registerCxxInteropLibraries( const llvm::Triple &Target, StringRef mainModuleName, bool hasStaticCxx, bool hasStaticCxxStdlib, CXXStdlibKind cxxStdlibKind, std::function RegistrationCallback); + +using RemapPathCallback = llvm::function_ref; +using LookupModuleOutputCallback = + llvm::function_ref; + +ModuleDependencyInfo +bridgeClangModuleDependency( + const ASTContext &ctx, + const clang::tooling::dependencies::ModuleDeps &clangDependency, + LookupModuleOutputCallback LookupModuleOutput, + RemapPathCallback remapPath = nullptr); } // namespace dependencies struct ScannerImportStatementInfo { @@ -1107,8 +1121,11 @@ class ModuleDependenciesCache { ModuleDependencyInfo dependencies); /// Record dependencies for the given collection of Clang modules. - void recordClangDependencies(ModuleDependencyVector moduleDependencies, - DiagnosticEngine &diags); + void recordClangDependencies( + const clang::tooling::dependencies::ModuleDepsGraph &dependencies, + const ASTContext &ctx, + dependencies::LookupModuleOutputCallback LookupModuleOutput, + dependencies::RemapPathCallback remapPath = nullptr); /// Update stored dependencies for the given module. void updateDependency(ModuleDependencyID moduleID, diff --git a/include/swift/ClangImporter/ClangImporter.h b/include/swift/ClangImporter/ClangImporter.h index a20c0505c1dc0..a23e05124fd7f 100644 --- a/include/swift/ClangImporter/ClangImporter.h +++ b/include/swift/ClangImporter/ClangImporter.h @@ -492,19 +492,6 @@ class ClangImporter final : public ClangModuleLoader { void verifyAllModules() override; - using RemapPathCallback = llvm::function_ref; - using LookupModuleOutputCallback = - llvm::function_ref; - - static llvm::SmallVector, 1> - bridgeClangModuleDependencies( - const ASTContext &ctx, - clang::tooling::dependencies::DependencyScanningTool &clangScanningTool, - clang::tooling::dependencies::ModuleDepsGraph &clangDependencies, - LookupModuleOutputCallback LookupModuleOutput, - RemapPathCallback remapPath = nullptr); - static void getBridgingHeaderOptions( const ASTContext &ctx, const clang::tooling::dependencies::TranslationUnitDeps &deps, diff --git a/include/swift/DependencyScan/ModuleDependencyScanner.h b/include/swift/DependencyScan/ModuleDependencyScanner.h index 6bc8a4ddc3c59..b73165e606c4d 100644 --- a/include/swift/DependencyScan/ModuleDependencyScanner.h +++ b/include/swift/DependencyScan/ModuleDependencyScanner.h @@ -40,7 +40,8 @@ class ModuleDependencyScanningWorker { private: /// Retrieve the module dependencies for the Clang module with the given name. - ClangModuleScannerQueryResult scanFilesystemForClangModuleDependency( + std::optional + scanFilesystemForClangModuleDependency( Identifier moduleName, const llvm::DenseSet &alreadySeenModules); diff --git a/include/swift/Serialization/ScanningLoaders.h b/include/swift/Serialization/ScanningLoaders.h index f80c80c0fe4cf..2ae4a5d01576f 100644 --- a/include/swift/Serialization/ScanningLoaders.h +++ b/include/swift/Serialization/ScanningLoaders.h @@ -40,18 +40,6 @@ struct SwiftModuleScannerQueryResult { std::vector incompatibleCandidates; }; -/// Result of looking up a Clang module on the current filesystem -/// search paths. -struct ClangModuleScannerQueryResult { - ClangModuleScannerQueryResult(const ModuleDependencyVector &dependencyModuleGraph, - const std::vector &visibleModuleIdentifiers) - : foundDependencyModuleGraph(dependencyModuleGraph), - visibleModuleIdentifiers(visibleModuleIdentifiers) {} - - ModuleDependencyVector foundDependencyModuleGraph; - std::vector visibleModuleIdentifiers; -}; - /// A module "loader" that looks for .swiftinterface and .swiftmodule files /// for the purpose of determining dependencies, but does not attempt to /// load the module files. diff --git a/lib/AST/ModuleDependencies.cpp b/lib/AST/ModuleDependencies.cpp index ae2ba45e722af..7c5d565922cdd 100644 --- a/lib/AST/ModuleDependencies.cpp +++ b/lib/AST/ModuleDependencies.cpp @@ -23,6 +23,7 @@ #include "swift/AST/SourceFile.h" #include "swift/Frontend/Frontend.h" #include "swift/Strings.h" +#include "clang/Lex/HeaderSearchOptions.h" #include "llvm/Config/config.h" #include "llvm/Support/Path.h" using namespace swift; @@ -598,6 +599,120 @@ void swift::dependencies::registerCxxInteropLibraries( } } +ModuleDependencyInfo swift::dependencies::bridgeClangModuleDependency( + const ASTContext &ctx, + const clang::tooling::dependencies::ModuleDeps &clangModuleDep, + LookupModuleOutputCallback lookupModuleOutput, RemapPathCallback callback) { + auto remapPath = [&](StringRef path) { + if (callback) + return callback(path); + return path.str(); + }; + + // File dependencies for this module. + std::vector fileDeps; + clangModuleDep.forEachFileDep( + [&fileDeps](StringRef fileDep) { fileDeps.emplace_back(fileDep); }); + + std::vector swiftArgs; + auto addClangArg = [&](Twine arg) { + swiftArgs.push_back("-Xcc"); + swiftArgs.push_back(arg.str()); + }; + + // We are using Swift frontend mode. + swiftArgs.push_back("-frontend"); + + // Swift frontend action: -emit-pcm + swiftArgs.push_back("-emit-pcm"); + swiftArgs.push_back("-module-name"); + swiftArgs.push_back(clangModuleDep.ID.ModuleName); + + auto pcmPath = lookupModuleOutput( + clangModuleDep, + clang::tooling::dependencies::ModuleOutputKind::ModuleFile); + swiftArgs.push_back("-o"); + swiftArgs.push_back(pcmPath); + + // Ensure that the resulting PCM build invocation uses Clang frontend + // directly + swiftArgs.push_back("-direct-clang-cc1-module-build"); + + // Swift frontend option for input file path (Foo.modulemap). + swiftArgs.push_back(remapPath(clangModuleDep.ClangModuleMapFile)); + + auto invocation = clangModuleDep.getUnderlyingCompilerInvocation(); + // Clear some options from clang scanner. + invocation.getMutFrontendOpts().ModuleCacheKeys.clear(); + invocation.getMutFrontendOpts().PathPrefixMappings.clear(); + invocation.getMutFrontendOpts().OutputFile.clear(); + + // Reset CASOptions since that should be coming from swift. + invocation.getMutCASOpts() = clang::CASOptions(); + invocation.getMutFrontendOpts().CASIncludeTreeID.clear(); + + // FIXME: workaround for rdar://105684525: find the -ivfsoverlay option + // from clang scanner and pass to swift. + if (!ctx.CASOpts.EnableCaching) { + auto &overlayFiles = invocation.getMutHeaderSearchOpts().VFSOverlayFiles; + for (auto overlay : overlayFiles) { + swiftArgs.push_back("-vfsoverlay"); + swiftArgs.push_back(overlay); + } + } + + // Add args reported by the scanner. + auto clangArgs = invocation.getCC1CommandLine(); + llvm::for_each(clangArgs, addClangArg); + + // CASFileSystemRootID. + std::string RootID = clangModuleDep.CASFileSystemRootID + ? clangModuleDep.CASFileSystemRootID->toString() + : ""; + + std::string IncludeTree = + clangModuleDep.IncludeTreeID ? *clangModuleDep.IncludeTreeID : ""; + + ctx.CASOpts.enumerateCASConfigurationFlags( + [&](StringRef Arg) { swiftArgs.push_back(Arg.str()); }); + + if (!IncludeTree.empty()) { + swiftArgs.push_back("-clang-include-tree-root"); + swiftArgs.push_back(IncludeTree); + } + std::string mappedPCMPath = remapPath(pcmPath); + + std::vector LinkLibraries; + for (const auto &ll : clangModuleDep.LinkLibraries) + LinkLibraries.emplace_back(ll.Library, + ll.IsFramework ? LibraryKind::Framework + : LibraryKind::Library, + /*static=*/false); + + // Module-level dependencies. + llvm::StringSet<> alreadyAddedModules; + auto bridgedDependencyInfo = ModuleDependencyInfo::forClangModule( + pcmPath, mappedPCMPath, clangModuleDep.ClangModuleMapFile, + clangModuleDep.ID.ContextHash, swiftArgs, fileDeps, LinkLibraries, RootID, + IncludeTree, /*module-cache-key*/ "", clangModuleDep.IsSystem); + + std::vector directDependencyIDs; + for (const auto &moduleName : clangModuleDep.ClangModuleDeps) { + // FIXME: This assumes, conservatively, that all Clang module imports + // are exported. We need to fix this once the clang scanner gains the + // appropriate API to query this. + bridgedDependencyInfo.addModuleImport( + moduleName.ModuleName, /* isExported */ true, AccessLevel::Public, + &alreadyAddedModules); + // It is safe to assume that all dependencies of a Clang module are Clang + // modules. + directDependencyIDs.push_back( + {moduleName.ModuleName, ModuleDependencyKind::Clang}); + } + bridgedDependencyInfo.setImportedClangDependencies(directDependencyIDs); + return bridgedDependencyInfo; +} + void swift::dependencies::registerBackDeployLibraries( const IRGenOptions &IRGenOpts, @@ -779,16 +894,19 @@ void ModuleDependenciesCache::recordDependency( } void ModuleDependenciesCache::recordClangDependencies( - ModuleDependencyVector dependencies, DiagnosticEngine &diags) { + const clang::tooling::dependencies::ModuleDepsGraph &dependencies, + const ASTContext &ctx, + dependencies::LookupModuleOutputCallback lookupModuleOutput, + dependencies::RemapPathCallback remapPath) { for (const auto &dep : dependencies) { - ASSERT(dep.first.Kind == ModuleDependencyKind::Clang); - auto newClangModuleDetails = dep.second.getAsClangModule(); - if (hasDependency(dep.first)) { + auto depID = + ModuleDependencyID{dep.ID.ModuleName, ModuleDependencyKind::Clang}; + if (hasDependency(depID)) { auto priorClangModuleDetails = - findKnownDependency(dep.first).getAsClangModule(); - DEBUG_ASSERT(priorClangModuleDetails && newClangModuleDetails); + findKnownDependency(depID).getAsClangModule(); + DEBUG_ASSERT(priorClangModuleDetails); auto priorContextHash = priorClangModuleDetails->contextHash; - auto newContextHash = newClangModuleDetails->contextHash; + auto newContextHash = dep.ID.ContextHash; if (priorContextHash != newContextHash) { // This situation means that within the same scanning action, Clang // Dependency Scanner has produced two different variants of the same @@ -799,28 +917,33 @@ void ModuleDependenciesCache::recordClangDependencies( // // Emit a failure diagnostic here that is hopefully more actionable // for the time being. - diags.diagnose(SourceLoc(), diag::dependency_scan_unexpected_variant, - dep.first.ModuleName); - diags.diagnose( + ctx.Diags.diagnose(SourceLoc(), + diag::dependency_scan_unexpected_variant, + dep.ID.ModuleName); + ctx.Diags.diagnose( SourceLoc(), diag::dependency_scan_unexpected_variant_context_hash_note, priorContextHash, newContextHash); - diags.diagnose( + ctx.Diags.diagnose( SourceLoc(), diag::dependency_scan_unexpected_variant_module_map_note, - priorClangModuleDetails->moduleMapFile, - newClangModuleDetails->moduleMapFile); + priorClangModuleDetails->moduleMapFile, dep.ClangModuleMapFile); + + auto newClangModuleDetails = + dependencies::bridgeClangModuleDependency( + ctx, dep, lookupModuleOutput, remapPath) + .getAsClangModule(); auto diagnoseExtraCommandLineFlags = - [&diags](const ClangModuleDependencyStorage *checkModuleDetails, - const ClangModuleDependencyStorage *baseModuleDetails, - bool isNewlyDiscovered) -> void { + [&ctx](const ClangModuleDependencyStorage *checkModuleDetails, + const ClangModuleDependencyStorage *baseModuleDetails, + bool isNewlyDiscovered) -> void { std::unordered_set baseCommandLineSet( baseModuleDetails->buildCommandLine.begin(), baseModuleDetails->buildCommandLine.end()); for (const auto &checkArg : checkModuleDetails->buildCommandLine) if (baseCommandLineSet.find(checkArg) == baseCommandLineSet.end()) - diags.diagnose( + ctx.Diags.diagnose( SourceLoc(), diag::dependency_scan_unexpected_variant_extra_arg_note, isNewlyDiscovered, checkArg); @@ -831,9 +954,10 @@ void ModuleDependenciesCache::recordClangDependencies( priorClangModuleDetails, false); } } else { - recordDependency(dep.first.ModuleName, dep.second); - addSeenClangModule(clang::tooling::dependencies::ModuleID{ - dep.first.ModuleName, newClangModuleDetails->contextHash}); + recordDependency(dep.ID.ModuleName, + dependencies::bridgeClangModuleDependency( + ctx, dep, lookupModuleOutput, remapPath)); + addSeenClangModule(dep.ID); } } } diff --git a/lib/ClangImporter/ClangModuleDependencyScanner.cpp b/lib/ClangImporter/ClangModuleDependencyScanner.cpp index e10edaf691364..6eb7e99d9ac76 100644 --- a/lib/ClangImporter/ClangModuleDependencyScanner.cpp +++ b/lib/ClangImporter/ClangModuleDependencyScanner.cpp @@ -82,126 +82,6 @@ std::vector ClangImporter::getClangDepScanningInvocationArguments( return commandLineArgs; } -ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies( - const ASTContext &ctx, - clang::tooling::dependencies::DependencyScanningTool &clangScanningTool, - clang::tooling::dependencies::ModuleDepsGraph &clangDependencies, - LookupModuleOutputCallback lookupModuleOutput, - RemapPathCallback callback) { - ModuleDependencyVector result; - - auto remapPath = [&](StringRef path) { - if (callback) - return callback(path); - return path.str(); - }; - - for (auto &clangModuleDep : clangDependencies) { - // File dependencies for this module. - std::vector fileDeps; - clangModuleDep.forEachFileDep( - [&fileDeps](StringRef fileDep) { fileDeps.emplace_back(fileDep); }); - - std::vector swiftArgs; - auto addClangArg = [&](Twine arg) { - swiftArgs.push_back("-Xcc"); - swiftArgs.push_back(arg.str()); - }; - - // We are using Swift frontend mode. - swiftArgs.push_back("-frontend"); - - // Swift frontend action: -emit-pcm - swiftArgs.push_back("-emit-pcm"); - swiftArgs.push_back("-module-name"); - swiftArgs.push_back(clangModuleDep.ID.ModuleName); - - auto pcmPath = lookupModuleOutput(clangModuleDep, - ModuleOutputKind::ModuleFile); - swiftArgs.push_back("-o"); - swiftArgs.push_back(pcmPath); - - // Ensure that the resulting PCM build invocation uses Clang frontend - // directly - swiftArgs.push_back("-direct-clang-cc1-module-build"); - - // Swift frontend option for input file path (Foo.modulemap). - swiftArgs.push_back(remapPath(clangModuleDep.ClangModuleMapFile)); - - auto invocation = clangModuleDep.getUnderlyingCompilerInvocation(); - // Clear some options from clang scanner. - invocation.getMutFrontendOpts().ModuleCacheKeys.clear(); - invocation.getMutFrontendOpts().PathPrefixMappings.clear(); - invocation.getMutFrontendOpts().OutputFile.clear(); - - // Reset CASOptions since that should be coming from swift. - invocation.getMutCASOpts() = clang::CASOptions(); - invocation.getMutFrontendOpts().CASIncludeTreeID.clear(); - - // FIXME: workaround for rdar://105684525: find the -ivfsoverlay option - // from clang scanner and pass to swift. - if (!ctx.CASOpts.EnableCaching) { - auto &overlayFiles = invocation.getMutHeaderSearchOpts().VFSOverlayFiles; - for (auto overlay : overlayFiles) { - swiftArgs.push_back("-vfsoverlay"); - swiftArgs.push_back(overlay); - } - } - - // Add args reported by the scanner. - auto clangArgs = invocation.getCC1CommandLine(); - llvm::for_each(clangArgs, addClangArg); - - // CASFileSystemRootID. - std::string RootID = clangModuleDep.CASFileSystemRootID - ? clangModuleDep.CASFileSystemRootID->toString() - : ""; - - std::string IncludeTree = - clangModuleDep.IncludeTreeID ? *clangModuleDep.IncludeTreeID : ""; - - ctx.CASOpts.enumerateCASConfigurationFlags( - [&](StringRef Arg) { swiftArgs.push_back(Arg.str()); }); - - if (!IncludeTree.empty()) { - swiftArgs.push_back("-clang-include-tree-root"); - swiftArgs.push_back(IncludeTree); - } - std::string mappedPCMPath = remapPath(pcmPath); - - std::vector LinkLibraries; - for (const auto &ll : clangModuleDep.LinkLibraries) - LinkLibraries.emplace_back( - ll.Library, - ll.IsFramework ? LibraryKind::Framework : LibraryKind::Library, - /*static=*/false); - - // Module-level dependencies. - llvm::StringSet<> alreadyAddedModules; - auto dependencies = ModuleDependencyInfo::forClangModule( - pcmPath, mappedPCMPath, clangModuleDep.ClangModuleMapFile, - clangModuleDep.ID.ContextHash, swiftArgs, fileDeps, - LinkLibraries, RootID, IncludeTree, /*module-cache-key*/ "", - clangModuleDep.IsSystem); - - std::vector directDependencyIDs; - for (const auto &moduleName : clangModuleDep.ClangModuleDeps) { - // FIXME: This assumes, conservatively, that all Clang module imports - // are exported. We need to fix this once the clang scanner gains the appropriate - // API to query this. - dependencies.addModuleImport(moduleName.ModuleName, /* isExported */ true, - AccessLevel::Public, &alreadyAddedModules); - // It is safe to assume that all dependencies of a Clang module are Clang modules. - directDependencyIDs.push_back({moduleName.ModuleName, ModuleDependencyKind::Clang}); - } - dependencies.setImportedClangDependencies(directDependencyIDs); - result.push_back(std::make_pair(ModuleDependencyID{clangModuleDep.ID.ModuleName, - ModuleDependencyKind::Clang}, - dependencies)); - } - return result; -} - void ClangImporter::getBridgingHeaderOptions( const ASTContext &ctx, const clang::tooling::dependencies::TranslationUnitDeps &deps, diff --git a/lib/DependencyScan/ModuleDependencyScanner.cpp b/lib/DependencyScan/ModuleDependencyScanner.cpp index 858281081b0e3..ce087340ca5b6 100644 --- a/lib/DependencyScan/ModuleDependencyScanner.cpp +++ b/lib/DependencyScan/ModuleDependencyScanner.cpp @@ -305,7 +305,7 @@ ModuleDependencyScanningWorker::scanFilesystemForSwiftModuleDependency( isTestableImport); } -ClangModuleScannerQueryResult +std::optional ModuleDependencyScanningWorker::scanFilesystemForClangModuleDependency( Identifier moduleName, const llvm::DenseSet @@ -332,15 +332,10 @@ ModuleDependencyScanningWorker::scanFilesystemForClangModuleDependency( "' not found") == std::string::npos) workerASTContext->Diags.diagnose( SourceLoc(), diag::clang_dependency_scan_error, errorStr); - return ClangModuleScannerQueryResult({}, {}); + return std::nullopt; } - return ClangModuleScannerQueryResult( - ClangImporter::bridgeClangModuleDependencies( - *workerASTContext, clangScanningTool, - clangModuleDependencies->ModuleGraph, lookupModuleOutput, - [&](StringRef path) { return remapPath(PrefixMapper, path); }), - clangModuleDependencies->VisibleModules); + return clangModuleDependencies.get(); } bool ModuleDependencyScanningWorker::scanHeaderDependenciesOfSwiftModule( @@ -374,10 +369,9 @@ bool ModuleDependencyScanningWorker::scanHeaderDependenciesOfSwiftModule( return dependencies.takeError(); // Record module dependencies for each new module we found. - auto bridgedDeps = ClangImporter::bridgeClangModuleDependencies( - ctx, clangScanningTool, dependencies->ModuleGraph, lookupModuleOutput, + cache.recordClangDependencies( + dependencies->ModuleGraph, ctx, lookupModuleOutput, [this](StringRef path) { return remapPath(PrefixMapper, path); }); - cache.recordClangDependencies(bridgedDeps, ctx.Diags); visibleClangModules = dependencies->VisibleModules; llvm::copy(dependencies->FileDeps, std::back_inserter(headerFileInputs)); @@ -612,11 +606,10 @@ ModuleDependencyScanner::ModuleDependencyScanner( /// Find all of the imported Clang modules starting with the given module name. static void findAllImportedClangModules(StringRef moduleName, const ModuleDependenciesCache &cache, - std::vector &allModules, - llvm::StringSet<> &knownModules) { - if (!knownModules.insert(moduleName).second) + llvm::StringSet<> &allModules) { + if (!allModules.insert(moduleName).second) return; - allModules.push_back(moduleName.str()); + auto moduleID = ModuleDependencyID{moduleName.str(), ModuleDependencyKind::Clang}; auto optionalDependencies = cache.findDependency(moduleID); @@ -624,8 +617,7 @@ static void findAllImportedClangModules(StringRef moduleName, return; for (const auto &dep : cache.getClangDependencies(moduleID)) - findAllImportedClangModules(dep.ModuleName, cache, allModules, - knownModules); + findAllImportedClangModules(dep.ModuleName, cache, allModules); } static std::set @@ -1115,9 +1107,11 @@ void ModuleDependencyScanner::resolveAllClangModuleDependencies( } // Module lookup result collection - llvm::StringMap moduleLookupResult; + llvm::StringMap< + std::optional> + moduleLookupResult; const llvm::DenseSet - seenClangModules = cache.getAlreadySeenClangModules(); + seenClangModules = cache.getAlreadySeenClangModules(); std::mutex resultAccessLock; auto scanForClangModuleDependency = [this, &moduleLookupResult, &resultAccessLock, &seenClangModules]( @@ -1157,16 +1151,28 @@ void ModuleDependencyScanner::resolveAllClangModuleDependencies( ASSERT(moduleLookupResult.contains(moduleImport.importIdentifier)); const auto &lookupResult = moduleLookupResult.at(moduleImport.importIdentifier); - // Cache discovered module dependencies. - if (!lookupResult.foundDependencyModuleGraph.empty() || - !lookupResult.visibleModuleIdentifiers.empty()) { - if (!lookupResult.foundDependencyModuleGraph.empty()) { - cache.recordClangDependencies(lookupResult.foundDependencyModuleGraph, - IssueReporter.Diagnostics); - // Add the full transitive dependency set - for (const auto &dep : lookupResult.foundDependencyModuleGraph) - allDiscoveredClangModules.insert(dep.first); - } + if (lookupResult.has_value()) { + auto lookupModuleOutput = + [this](const clang::tooling::dependencies::ModuleDeps &MD, + const clang::tooling::dependencies::ModuleOutputKind MOK) + -> std::string { + return moduleCacheRelativeLookupModuleOutput( + MD, MOK, + ScanCompilerInvocation.getFrontendOptions() + .ExplicitModulesOutputPath, + ScanCompilerInvocation.getFrontendOptions() + .ExplicitSDKModulesOutputPath, + ScanASTContext.SearchPathOpts.RuntimeResourcePath); + }; + + cache.recordClangDependencies( + lookupResult->ModuleGraph, ScanASTContext, lookupModuleOutput, + [this](StringRef path) { return remapPath(path); }); + + // Add the full transitive dependency set + for (const auto &dep : lookupResult->ModuleGraph) + allDiscoveredClangModules.insert( + {dep.ID.ModuleName, ModuleDependencyKind::Clang}); importedClangDependencies.insert( {moduleImport.importIdentifier, ModuleDependencyKind::Clang}); @@ -1174,7 +1180,7 @@ void ModuleDependencyScanner::resolveAllClangModuleDependencies( // Add visible Clang modules for this query to the depending // Swift module cache.addVisibleClangModules(moduleID, - lookupResult.visibleModuleIdentifiers); + lookupResult->VisibleModules); } else if (!optionalImport) { // Otherwise, we failed to resolve this dependency. We will try // again using the cache after all other imports have been @@ -1481,13 +1487,11 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependenciesForModule( ModuleDependencyIDSetVector &swiftOverlayDependencies) { PrettyStackTraceStringAction trace( "Resolving Swift Overlay dependencies of module", moduleID.ModuleName); - std::vector allClangDependencies; - llvm::StringSet<> knownModules; + llvm::StringSet<> allClangDependencies; // Find all of the discovered Clang modules that this module depends on. for (const auto &dep : cache.getClangDependencies(moduleID)) - findAllImportedClangModules(dep.ModuleName, cache, allClangDependencies, - knownModules); + findAllImportedClangModules(dep.ModuleName, cache, allClangDependencies); llvm::StringMap swiftOverlayLookupResult; std::mutex lookupResultLock; @@ -1520,7 +1524,7 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependenciesForModule( // Enque asynchronous lookup tasks for (const auto &clangDep : allClangDependencies) ScanningThreadPool.async(scanForSwiftDependency, - getModuleImportIdentifier(clangDep)); + getModuleImportIdentifier(clangDep.getKey().str())); ScanningThreadPool.wait(); // Aggregate both previously-cached and freshly-scanned module results @@ -1548,7 +1552,7 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependenciesForModule( } }; for (const auto &clangDep : allClangDependencies) - recordResult(clangDep); + recordResult(clangDep.getKey().str()); // C++ Interop requires additional handling bool lookupCxxStdLibOverlay = ScanCompilerInvocation.getLangOptions().EnableCXXInterop; @@ -1570,7 +1574,8 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependenciesForModule( if (lookupCxxStdLibOverlay) { for (const auto &clangDepNameEntry : allClangDependencies) { - auto clangDepName = clangDepNameEntry; + auto clangDepName = clangDepNameEntry.getKey().str(); + // If this Clang module is a part of the C++ stdlib, and we haven't // loaded the overlay for it so far, it is a split libc++ module (e.g. // std_vector). Load the CxxStdlib overlay explicitly. @@ -1970,13 +1975,12 @@ ModuleDependencyScanner::attemptToFindResolvingSerializedSearchPath( binaryDepID, swiftResult.foundDependencyInfo->getModuleDefiningPath()); - ClangModuleScannerQueryResult clangResult = + auto clangResult = ScanningWorker->scanFilesystemForClangModuleDependency( importIdentifier, {}); - if (!clangResult.foundDependencyModuleGraph.empty()) - return std::make_pair(binaryDepID, - clangResult.foundDependencyModuleGraph[0] - .second.getModuleDefiningPath()); + if (clangResult) + return std::make_pair( + binaryDepID, clangResult->ModuleGraph[0].ClangModuleMapFile); return std::nullopt; }); if (result)