@@ -81,13 +81,22 @@ namespace {
8181class ExplicitModuleDependencyResolver {
8282public:
8383 ExplicitModuleDependencyResolver (
84- ModuleDependencyID moduleID, ModuleDependenciesCache &cache,
84+ const ModuleDependencyID & moduleID, ModuleDependenciesCache &cache,
8585 CompilerInstance &instance, std::optional<SwiftDependencyTracker> tracker)
8686 : moduleID(moduleID), cache(cache), instance(instance),
8787 resolvingDepInfo (cache.findKnownDependency(moduleID)),
8888 tracker(std::move(tracker)) {
8989 // Copy commandline.
9090 commandline = resolvingDepInfo.getCommandline ();
91+
92+ if (resolvingDepInfo.isSwiftInterfaceModule ()) {
93+ auto swiftTextualDeps = resolvingDepInfo.getAsSwiftInterfaceModule ();
94+ auto &interfacePath = swiftTextualDeps->swiftInterfaceFile ;
95+ auto SDKPath = instance.getASTContext ().SearchPathOpts .getSDKPath ();
96+ nameExpander = std::make_unique<InterfaceModuleNameExpander>(
97+ moduleID.ModuleName , interfacePath, SDKPath,
98+ instance.getInvocation ());
99+ }
91100 }
92101
93102 llvm::Error
@@ -171,6 +180,9 @@ class ExplicitModuleDependencyResolver {
171180
172181 pruneUnusedVFSOverlay ();
173182
183+ if (resolvingDepInfo.isSwiftInterfaceModule ())
184+ updateSwiftInterfaceCommandLine ();
185+
174186 // Update the dependency in the cache with the modified command-line.
175187 if (resolvingDepInfo.isSwiftInterfaceModule () ||
176188 resolvingDepInfo.isClangModule ()) {
@@ -221,6 +233,12 @@ class ExplicitModuleDependencyResolver {
221233 return err;
222234 }
223235
236+ if (nameExpander) {
237+ auto expandedName = nameExpander->getExpandedName ();
238+ depInfo.updateModuleOutputPath (expandedName.outputPath .c_str ());
239+ depInfo.updateContextHash (expandedName.hash .str ());
240+ }
241+
224242 return llvm::Error::success ();
225243 }
226244
@@ -402,9 +420,62 @@ class ExplicitModuleDependencyResolver {
402420 }
403421 resolvedCommandLine.push_back (*it);
404422 }
423+
424+ // Filter the extra args that we use to calculate the hash of the module.
425+ // The key assumption is that these args are clang importer args, so we
426+ // do not need to check for -Xcc.
427+ auto extraArgsFilter = [&](InterfaceModuleNameExpander::ArgListTy &args) {
428+ bool skip = false ;
429+ InterfaceModuleNameExpander::ArgListTy newArgs;
430+ for (auto it = args.begin (), end = args.end (); it != end; it++) {
431+ if (skip) {
432+ skip = false ;
433+ continue ;
434+ }
435+
436+ if ((it + 1 ) != end && isVFSOverlayFlag (*it)) {
437+ if (!usedVFSOverlayPaths.contains (*(it + 1 ))) {
438+ skip = true ;
439+ continue ;
440+ }
441+ }
442+
443+ newArgs.push_back (*it);
444+ }
445+ args = newArgs;
446+ return ;
447+ };
448+
449+ if (nameExpander)
450+ nameExpander->pruneExtraArgs (extraArgsFilter);
451+
405452 commandline = std::move (resolvedCommandLine);
406453 }
407454
455+ void updateSwiftInterfaceCommandLine () {
456+ // The command line needs upadte once we prune the unused VFS overlays.
457+ // The update consists of two steps.
458+ // 1. Recompute the output path, which includes the module hash.
459+ // 2. Update `-o `'s value on the command line with the new output path.
460+
461+ assert (nameExpander && " Can only update if we hae a nameExpander." );
462+ auto expandedName = nameExpander->getExpandedName ();
463+
464+ StringRef outputName = expandedName.outputPath .str ();
465+
466+ bool isOutputPath = false ;
467+ for (auto &A : commandline) {
468+ if (isOutputPath) {
469+ A = outputName.str ();
470+ break ;
471+ } else if (A == " -o" ) {
472+ isOutputPath = true ;
473+ }
474+ }
475+
476+ return ;
477+ }
478+
408479 llvm::Error collectCASDependencies (ModuleDependencyInfo &dependencyInfoCopy) {
409480 if (!instance.getInvocation ().getCASOptions ().EnableCaching )
410481 return llvm::Error::success ();
@@ -524,10 +595,11 @@ class ExplicitModuleDependencyResolver {
524595 }
525596
526597private:
527- ModuleDependencyID moduleID;
598+ const ModuleDependencyID & moduleID;
528599 ModuleDependenciesCache &cache;
529600 CompilerInstance &instance;
530601 const ModuleDependencyInfo &resolvingDepInfo;
602+ std::unique_ptr<InterfaceModuleNameExpander> nameExpander;
531603
532604 std::optional<SwiftDependencyTracker> tracker;
533605 std::vector<std::string> rootIDs;
@@ -540,7 +612,7 @@ class ExplicitModuleDependencyResolver {
540612};
541613
542614static llvm::Error resolveExplicitModuleInputs (
543- ModuleDependencyID moduleID,
615+ const ModuleDependencyID & moduleID,
544616 const std::set<ModuleDependencyID> &dependencies,
545617 ModuleDependenciesCache &cache, CompilerInstance &instance,
546618 std::optional<std::set<ModuleDependencyID>> bridgingHeaderDeps,
0 commit comments