@@ -377,7 +377,7 @@ bool ModuleDependencyScanningWorker::scanHeaderDependenciesOfSwiftModule(
377377 auto bridgedDeps = ClangImporter::bridgeClangModuleDependencies (
378378 ctx, clangScanningTool, dependencies->ModuleGraph , lookupModuleOutput,
379379 [this ](StringRef path) { return remapPath (PrefixMapper, path); });
380- cache.recordDependencies (bridgedDeps, ctx.Diags );
380+ cache.recordClangDependencies (bridgedDeps, ctx.Diags );
381381 visibleClangModules = dependencies->VisibleModules ;
382382
383383 llvm::copy (dependencies->FileDeps , std::back_inserter (headerFileInputs));
@@ -1057,7 +1057,7 @@ void ModuleDependencyScanner::resolveAllClangModuleDependencies(
10571057 continue ;
10581058 } else {
10591059 // We need to query the Clang dependency scanner for this module's
1060- // unresolved imports
1060+ // non-Swift imports
10611061 llvm::StringSet<> resolvedImportIdentifiers;
10621062 for (const auto &resolvedDep :
10631063 moduleDependencyInfo.getImportedSwiftDependencies ())
@@ -1095,46 +1095,23 @@ void ModuleDependencyScanner::resolveAllClangModuleDependencies(
10951095 }
10961096 }
10971097
1098- // Prepare the module lookup result collection
1099- llvm::StringMap<std::optional<ClangModuleScannerQueryResult>>
1100- moduleLookupResult;
1101- for (const auto &unresolvedIdentifier : unresolvedImportIdentifiers)
1102- moduleLookupResult.insert (
1103- std::make_pair (unresolvedIdentifier.getKey (), std::nullopt ));
1104-
1105- // We need a copy of the shared already-seen module set, which will be shared
1106- // amongst all the workers. In `recordDependencies`, each worker will
1107- // contribute its results back to the shared set for future lookups.
1098+ // Module lookup result collection
1099+ llvm::StringMap<ClangModuleScannerQueryResult> moduleLookupResult;
11081100 const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
1109- seenClangModules = cache.getAlreadySeenClangModules ();
1110- std::mutex cacheAccessLock ;
1111- auto scanForClangModuleDependency = [this , &cache, & moduleLookupResult,
1112- &cacheAccessLock , &seenClangModules](
1101+ seenClangModules = cache.getAlreadySeenClangModules ();
1102+ std::mutex resultAccessLock ;
1103+ auto scanForClangModuleDependency = [this , &moduleLookupResult,
1104+ &resultAccessLock , &seenClangModules](
11131105 Identifier moduleIdentifier) {
1114- auto moduleName = moduleIdentifier.str ();
1115- {
1116- std::lock_guard<std::mutex> guard (cacheAccessLock);
1117- if (cache.hasDependency (moduleName, ModuleDependencyKind::Clang))
1118- return ;
1119- }
1120-
11211106 auto scanResult = withDependencyScanningWorker (
11221107 [&seenClangModules,
11231108 moduleIdentifier](ModuleDependencyScanningWorker *ScanningWorker) {
11241109 return ScanningWorker->scanFilesystemForClangModuleDependency (
11251110 moduleIdentifier, seenClangModules);
11261111 });
1127-
1128- // Update the `moduleLookupResult` and cache all discovered dependencies
1129- // so that subsequent queries do not have to call into the scanner
1130- // if looking for a module that was discovered as a transitive
1131- // dependency in this scan.
11321112 {
1133- std::lock_guard<std::mutex> guard (cacheAccessLock);
1134- moduleLookupResult.insert_or_assign (moduleName, scanResult);
1135- if (!scanResult.foundDependencyModuleGraph .empty ())
1136- cache.recordDependencies (scanResult.foundDependencyModuleGraph ,
1137- IssueReporter.Diagnostics );
1113+ std::lock_guard<std::mutex> guard (resultAccessLock);
1114+ moduleLookupResult.insert_or_assign (moduleIdentifier.str (), scanResult);
11381115 }
11391116 };
11401117
@@ -1154,35 +1131,37 @@ void ModuleDependencyScanner::resolveAllClangModuleDependencies(
11541131 std::vector<ScannerImportStatementInfo> failedToResolveImports;
11551132 ModuleDependencyIDSetVector importedClangDependencies;
11561133 auto recordResolvedClangModuleImport =
1157- [&moduleLookupResult, &importedClangDependencies, &cache,
1134+ [this , &moduleLookupResult, &importedClangDependencies, &cache,
11581135 &allDiscoveredClangModules, moduleID, &failedToResolveImports](
11591136 const ScannerImportStatementInfo &moduleImport,
11601137 bool optionalImport) {
1161- auto lookupResult = moduleLookupResult[moduleImport.importIdentifier ];
1162- // The imported module was found in the cache
1163- if (lookupResult == std::nullopt ) {
1164- importedClangDependencies.insert (
1165- {moduleImport.importIdentifier , ModuleDependencyKind::Clang});
1166- } else {
1167- // Cache discovered module dependencies.
1168- if (!lookupResult.value ().foundDependencyModuleGraph .empty ()) {
1169- importedClangDependencies.insert (
1170- {moduleImport.importIdentifier , ModuleDependencyKind::Clang});
1138+ ASSERT (moduleLookupResult.contains (moduleImport.importIdentifier ));
1139+ const auto &lookupResult =
1140+ moduleLookupResult.at (moduleImport.importIdentifier );
1141+ // Cache discovered module dependencies.
1142+ if (!lookupResult.foundDependencyModuleGraph .empty () ||
1143+ !lookupResult.visibleModuleIdentifiers .empty ()) {
1144+ if (!lookupResult.foundDependencyModuleGraph .empty ()) {
1145+ cache.recordClangDependencies (lookupResult.foundDependencyModuleGraph ,
1146+ IssueReporter.Diagnostics );
11711147 // Add the full transitive dependency set
1172- for (const auto &dep :
1173- lookupResult.value ().foundDependencyModuleGraph )
1148+ for (const auto &dep : lookupResult.foundDependencyModuleGraph )
11741149 allDiscoveredClangModules.insert (dep.first );
1175- // Add visible Clang modules for this query to the depending
1176- // Swift module
1177- cache.addVisibleClangModules (
1178- moduleID, lookupResult->visibleModuleIdentifiers );
1179- } else if (!optionalImport) {
1180- // Otherwise, we failed to resolve this dependency. We will try
1181- // again using the cache after all other imports have been
1182- // resolved. If that fails too, a scanning failure will be
1183- // diagnosed.
1184- failedToResolveImports.push_back (moduleImport);
11851150 }
1151+
1152+ importedClangDependencies.insert (
1153+ {moduleImport.importIdentifier , ModuleDependencyKind::Clang});
1154+
1155+ // Add visible Clang modules for this query to the depending
1156+ // Swift module
1157+ cache.addVisibleClangModules (moduleID,
1158+ lookupResult.visibleModuleIdentifiers );
1159+ } else if (!optionalImport) {
1160+ // Otherwise, we failed to resolve this dependency. We will try
1161+ // again using the cache after all other imports have been
1162+ // resolved. If that fails too, a scanning failure will be
1163+ // diagnosed.
1164+ failedToResolveImports.push_back (moduleImport);
11861165 }
11871166 };
11881167
0 commit comments