Skip to content

Commit 1ce6949

Browse files
committed
Address code review comments.
1 parent 15726fc commit 1ce6949

File tree

3 files changed

+64
-59
lines changed

3 files changed

+64
-59
lines changed

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ struct SwiftInterfaceInfo {
639639
/// then prune the extra args, and finally obtain expandedName again.
640640
/// Pruning always happens before the first time we call getExpandedName().
641641
/// Therefore it is correct to calculate expandedName only once.
642-
class InterfaceModuleNameExpander {
642+
class InterfaceModuleOutputPathResolver {
643643
public:
644644
using ArgListTy = std::vector<std::string>;
645645
struct ResultTy {
@@ -660,19 +660,19 @@ class InterfaceModuleNameExpander {
660660
// and see pruneExtraArgs to see how it can be updated.
661661
ArgListTy extraArgs;
662662

663-
std::unique_ptr<ResultTy> expandedName;
663+
std::unique_ptr<ResultTy> resolvedOutputPath;
664664

665665
std::string getHash();
666666

667667
public:
668-
InterfaceModuleNameExpander(const StringRef moduleName,
669-
const StringRef interfacePath,
670-
const StringRef sdkPath,
671-
const CompilerInvocation &CI)
668+
InterfaceModuleOutputPathResolver(const StringRef moduleName,
669+
const StringRef interfacePath,
670+
const StringRef sdkPath,
671+
const CompilerInvocation &CI)
672672
: moduleName(moduleName), interfacePath(interfacePath), sdkPath(sdkPath),
673673
CI(CI), extraArgs(CI.getClangImporterOptions()
674674
.getReducedExtraArgsForSwiftModuleDependency()) {}
675-
ResultTy getExpandedName();
675+
ResultTy getOutputPath();
676676
void pruneExtraArgs(std::function<void(ArgListTy &)> filter);
677677
};
678678

@@ -751,7 +751,7 @@ struct InterfaceSubContextDelegateImpl : InterfaceSubContextDelegate {
751751
~InterfaceSubContextDelegateImpl() = default;
752752

753753
/// includes a hash of relevant key data.
754-
InterfaceModuleNameExpander::ResultTy
754+
InterfaceModuleOutputPathResolver::ResultTy
755755
getCachedOutputPath(StringRef moduleName, StringRef interfacePath,
756756
StringRef sdkPath);
757757
};

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class ExplicitModuleDependencyResolver {
9393
auto swiftTextualDeps = resolvingDepInfo.getAsSwiftInterfaceModule();
9494
auto &interfacePath = swiftTextualDeps->swiftInterfaceFile;
9595
auto SDKPath = instance.getASTContext().SearchPathOpts.getSDKPath();
96-
nameExpander = std::make_unique<InterfaceModuleNameExpander>(
96+
outputPathResolver = std::make_unique<InterfaceModuleOutputPathResolver>(
9797
moduleID.ModuleName, interfacePath, SDKPath,
9898
instance.getInvocation());
9999
}
@@ -178,13 +178,13 @@ class ExplicitModuleDependencyResolver {
178178
}
179179
}
180180

181-
pruneUnusedVFSOverlay();
182-
183181
// It is necessary to update the command line to take
184182
// the pruned unused VFS overlay into account before remapping the
185183
// command line.
186-
if (resolvingDepInfo.isSwiftInterfaceModule())
187-
updateSwiftInterfaceCommandLine();
184+
if (resolvingDepInfo.isSwiftInterfaceModule()) {
185+
pruneUnusedVFSOverlay();
186+
updateSwiftInterfaceModuleOutputPath();
187+
}
188188

189189
// Update the dependency in the cache with the modified command-line.
190190
if (resolvingDepInfo.isSwiftInterfaceModule() ||
@@ -236,8 +236,8 @@ class ExplicitModuleDependencyResolver {
236236
return err;
237237
}
238238

239-
if (nameExpander) {
240-
auto expandedName = nameExpander->getExpandedName();
239+
if (outputPathResolver) {
240+
auto expandedName = outputPathResolver->getOutputPath();
241241
depInfo.updateModuleOutputPath(expandedName.outputPath.c_str());
242242
depInfo.updateContextHash(expandedName.hash.str());
243243
}
@@ -427,43 +427,45 @@ class ExplicitModuleDependencyResolver {
427427
// Filter the extra args that we use to calculate the hash of the module.
428428
// The key assumption is that these args are clang importer args, so we
429429
// do not need to check for -Xcc.
430-
auto extraArgsFilter = [&](InterfaceModuleNameExpander::ArgListTy &args) {
431-
bool skip = false;
432-
InterfaceModuleNameExpander::ArgListTy newArgs;
433-
for (auto it = args.begin(), end = args.end(); it != end; it++) {
434-
if (skip) {
435-
skip = false;
436-
continue;
437-
}
438-
439-
if ((it + 1) != end && isVFSOverlayFlag(*it)) {
440-
if (!usedVFSOverlayPaths.contains(*(it + 1))) {
441-
skip = true;
442-
continue;
430+
auto extraArgsFilter =
431+
[&](InterfaceModuleOutputPathResolver::ArgListTy &args) {
432+
bool skip = false;
433+
InterfaceModuleOutputPathResolver::ArgListTy newArgs;
434+
for (auto it = args.begin(), end = args.end(); it != end; it++) {
435+
if (skip) {
436+
skip = false;
437+
continue;
438+
}
439+
440+
if ((it + 1) != end && isVFSOverlayFlag(*it)) {
441+
if (!usedVFSOverlayPaths.contains(*(it + 1))) {
442+
skip = true;
443+
continue;
444+
}
445+
}
446+
447+
newArgs.push_back(*it);
443448
}
444-
}
445-
446-
newArgs.push_back(*it);
447-
}
448-
args = std::move(newArgs);
449-
return;
450-
};
449+
args = std::move(newArgs);
450+
return;
451+
};
451452

452-
if (nameExpander)
453-
nameExpander->pruneExtraArgs(extraArgsFilter);
453+
if (outputPathResolver)
454+
outputPathResolver->pruneExtraArgs(extraArgsFilter);
454455

455456
commandline = std::move(resolvedCommandLine);
456457
}
457458

458-
void updateSwiftInterfaceCommandLine() {
459+
void updateSwiftInterfaceModuleOutputPath() {
459460
// The command line needs update once we prune the unused VFS overlays.
460461
// The update consists of two steps.
461462
// 1. Obtain the output path, which includes the module hash that takes the
462463
// VFS pruning into account.
463464
// 2. Update `-o `'s value on the command line with the new output path.
464465

465-
assert(nameExpander && "Can only update if we hae a nameExpander.");
466-
const auto &expandedName = nameExpander->getExpandedName();
466+
assert(outputPathResolver &&
467+
"Can only update if we have an outputPathResolver.");
468+
const auto &expandedName = outputPathResolver->getOutputPath();
467469

468470
StringRef outputName = expandedName.outputPath.str();
469471

@@ -603,7 +605,7 @@ class ExplicitModuleDependencyResolver {
603605
ModuleDependenciesCache &cache;
604606
CompilerInstance &instance;
605607
const ModuleDependencyInfo &resolvingDepInfo;
606-
std::unique_ptr<InterfaceModuleNameExpander> nameExpander;
608+
std::unique_ptr<InterfaceModuleOutputPathResolver> outputPathResolver;
607609

608610
std::optional<SwiftDependencyTracker> tracker;
609611
std::vector<std::string> rootIDs;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,9 +1098,9 @@ class ModuleInterfaceLoaderImpl {
10981098
requiresOSSAModules);
10991099

11001100
// Compute the output path if we're loading or emitting a cached module.
1101-
auto expandedName = astDelegate.getCachedOutputPath(
1101+
auto resolvedOutputPath = astDelegate.getCachedOutputPath(
11021102
moduleName, interfacePath, ctx.SearchPathOpts.getSDKPath());
1103-
auto &cachedOutputPath = expandedName.outputPath;
1103+
auto &cachedOutputPath = resolvedOutputPath.outputPath;
11041104

11051105
// Try to find the right module for this interface, either alongside it,
11061106
// in the cache, or in the prebuilt cache.
@@ -2005,13 +2005,13 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
20052005

20062006
/// Calculate an output filename in \p genericSubInvocation's cache path that
20072007
/// includes a hash of relevant key data.
2008-
InterfaceModuleNameExpander::ResultTy
2008+
InterfaceModuleOutputPathResolver::ResultTy
20092009
InterfaceSubContextDelegateImpl::getCachedOutputPath(StringRef moduleName,
20102010
StringRef interfacePath,
20112011
StringRef sdkPath) {
2012-
InterfaceModuleNameExpander expander(moduleName, interfacePath, sdkPath,
2013-
genericSubInvocation);
2014-
return expander.getExpandedName();
2012+
InterfaceModuleOutputPathResolver resolver(moduleName, interfacePath, sdkPath,
2013+
genericSubInvocation);
2014+
return resolver.getOutputPath();
20152015
}
20162016

20172017
std::error_code
@@ -2090,11 +2090,12 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
20902090
}
20912091

20922092
// Calculate output path of the module.
2093-
auto expandedName = getCachedOutputPath(moduleName, interfacePath, sdkPath);
2093+
auto resolvedOutputPath =
2094+
getCachedOutputPath(moduleName, interfacePath, sdkPath);
20942095

20952096
// If no specific output path is given, use the hashed output path.
20962097
if (outputPath.empty()) {
2097-
outputPath = expandedName.outputPath;
2098+
outputPath = resolvedOutputPath.outputPath;
20982099
}
20992100

21002101
// Configure the outputs in front-end options. There must be an equal number of
@@ -2148,7 +2149,7 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
21482149
}
21492150

21502151
info.BuildArguments = BuildArgs;
2151-
info.Hash = expandedName.hash;
2152+
info.Hash = resolvedOutputPath.hash;
21522153

21532154
// Run the action under the sub compiler instance.
21542155
return action(info);
@@ -2743,22 +2744,22 @@ std::unique_ptr<ExplicitCASModuleLoader> ExplicitCASModuleLoader::create(
27432744
return result;
27442745
}
27452746

2746-
InterfaceModuleNameExpander::ResultTy
2747-
InterfaceModuleNameExpander::getExpandedName() {
2748-
if (!expandedName) {
2749-
expandedName = std::make_unique<ResultTy>();
2750-
auto &outputPath = expandedName->outputPath;
2747+
InterfaceModuleOutputPathResolver::ResultTy
2748+
InterfaceModuleOutputPathResolver::getOutputPath() {
2749+
if (!resolvedOutputPath) {
2750+
resolvedOutputPath = std::make_unique<ResultTy>();
2751+
auto &outputPath = resolvedOutputPath->outputPath;
27512752
outputPath = CI.getClangModuleCachePath();
27522753
llvm::sys::path::append(outputPath, moduleName);
27532754
outputPath.append("-");
27542755
auto hashStart = outputPath.size();
27552756
outputPath.append(getHash());
2756-
expandedName->hash = outputPath.str().substr(hashStart);
2757+
resolvedOutputPath->hash = outputPath.str().substr(hashStart);
27572758
outputPath.append(".");
27582759
auto outExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
27592760
outputPath.append(outExt);
27602761
}
2761-
return *expandedName;
2762+
return *resolvedOutputPath;
27622763
}
27632764

27642765
/// Construct a key for the .swiftmodule being generated. There is a
@@ -2771,7 +2772,7 @@ InterfaceModuleNameExpander::getExpandedName() {
27712772
/// -- rather than making a new one and potentially filling up the cache
27722773
/// with dead entries -- when other factors change, such as the contents of
27732774
/// the .swiftinterface input or its dependencies.
2774-
std::string InterfaceModuleNameExpander::getHash() {
2775+
std::string InterfaceModuleOutputPathResolver::getHash() {
27752776
// When doing dependency scanning for explicit module, use strict context hash
27762777
// to ensure sound module hash.
27772778
bool useStrictCacheHash = CI.getFrontendOptions().RequestedAction ==
@@ -2841,7 +2842,9 @@ std::string InterfaceModuleNameExpander::getHash() {
28412842
return llvm::toString(llvm::APInt(64, H), 36, /*Signed=*/false);
28422843
}
28432844

2844-
void InterfaceModuleNameExpander::pruneExtraArgs(
2845+
void InterfaceModuleOutputPathResolver::pruneExtraArgs(
28452846
std::function<void(ArgListTy &)> filter) {
2847+
assert(!resolvedOutputPath &&
2848+
"Cannot prune again after the output path has been resolved.");
28462849
filter(extraArgs);
28472850
}

0 commit comments

Comments
 (0)