Skip to content

Commit 864c126

Browse files
authored
Merge pull request #32935 from nkcsgexi/refactor-candidate-compiled-modules
ModuleInterface: refactor a utility function out for collecting candidate compiled modules. NFC
2 parents 3b8b632 + a5ad5eb commit 864c126

File tree

1 file changed

+58
-43
lines changed

1 file changed

+58
-43
lines changed

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,8 @@ class ModuleInterfaceLoaderImpl {
590590
return path.startswith(resourceDir);
591591
}
592592

593-
llvm::ErrorOr<DiscoveredModule>
594-
discoverUpToDateCompiledModuleForInterface(SmallVectorImpl<FileDependency> &deps,
595-
std::string &UsableModulePath) {
596-
auto notFoundError =
597-
std::make_error_code(std::errc::no_such_file_or_directory);
598-
593+
std::pair<std::string, std::string> getCompiledModuleCandidates() {
594+
std::pair<std::string, std::string> result;
599595
// Keep track of whether we should attempt to load a .swiftmodule adjacent
600596
// to the .swiftinterface.
601597
bool shouldLoadAdjacentModule = true;
@@ -604,7 +600,7 @@ class ModuleInterfaceLoaderImpl {
604600
case ModuleLoadingMode::OnlyInterface:
605601
// Always skip both the caches and adjacent modules, and always build the
606602
// module interface.
607-
return notFoundError;
603+
return {};
608604
case ModuleLoadingMode::PreferInterface:
609605
// If we're in the load mode that prefers .swiftinterfaces, specifically
610606
// skip the module adjacent to the interface, but use the caches if
@@ -627,16 +623,48 @@ class ModuleInterfaceLoaderImpl {
627623
// diagnose it.
628624

629625
if (shouldLoadAdjacentModule) {
630-
auto adjacentModuleBuffer = fs.getBufferForFile(modulePath);
626+
if (fs.exists(modulePath)) {
627+
result.first = modulePath;
628+
}
629+
}
630+
631+
// If we have a prebuilt cache path, check that too if the interface comes
632+
// from the SDK.
633+
if (!prebuiltCacheDir.empty()) {
634+
llvm::SmallString<256> scratch;
635+
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
636+
Optional<StringRef> path = computePrebuiltModulePath(scratch);
637+
if (!path) {
638+
// Hack: deal with prebuilds of modules that still use the target-based
639+
// names.
640+
path = computeFallbackPrebuiltModulePath(scratch);
641+
}
642+
if (path) {
643+
if (fs.exists(*path)) {
644+
result.second = *path;
645+
}
646+
}
647+
}
648+
649+
return result;
650+
}
651+
652+
llvm::ErrorOr<DiscoveredModule>
653+
discoverUpToDateCompiledModuleForInterface(SmallVectorImpl<FileDependency> &deps,
654+
std::string &UsableModulePath) {
655+
std::string adjacentMod, prebuiltMod;
656+
std::tie(adjacentMod, prebuiltMod) = getCompiledModuleCandidates();
657+
if (!adjacentMod.empty()) {
658+
auto adjacentModuleBuffer = fs.getBufferForFile(adjacentMod);
631659
if (adjacentModuleBuffer) {
632-
if (serializedASTBufferIsUpToDate(modulePath, *adjacentModuleBuffer.get(),
660+
if (serializedASTBufferIsUpToDate(adjacentMod, *adjacentModuleBuffer.get(),
633661
deps)) {
634662
LLVM_DEBUG(llvm::dbgs() << "Found up-to-date module at "
635-
<< modulePath
663+
<< adjacentMod
636664
<< "; deferring to serialized module loader\n");
637-
UsableModulePath = modulePath.str();
665+
UsableModulePath = adjacentMod;
638666
return std::make_error_code(std::errc::not_supported);
639-
} else if (isInResourceDir(modulePath) &&
667+
} else if (isInResourceDir(adjacentMod) &&
640668
loadMode == ModuleLoadingMode::PreferSerialized) {
641669
// Special-case here: If we're loading a .swiftmodule from the resource
642670
// dir adjacent to the compiler, defer to the serialized loader instead
@@ -647,53 +675,40 @@ class ModuleInterfaceLoaderImpl {
647675
// we used to.
648676
LLVM_DEBUG(llvm::dbgs() << "Found out-of-date module in the "
649677
"resource-dir at "
650-
<< modulePath
678+
<< adjacentMod
651679
<< "; deferring to serialized module loader "
652680
"to diagnose\n");
653681
return std::make_error_code(std::errc::not_supported);
654682
} else {
655683
LLVM_DEBUG(llvm::dbgs() << "Found out-of-date module at "
656-
<< modulePath << "\n");
657-
rebuildInfo.setModuleKind(modulePath,
684+
<< adjacentMod << "\n");
685+
rebuildInfo.setModuleKind(adjacentMod,
658686
ModuleRebuildInfo::ModuleKind::Normal);
659687
}
660-
} else if (adjacentModuleBuffer.getError() != notFoundError) {
688+
} else {
661689
LLVM_DEBUG(llvm::dbgs() << "Found unreadable module at "
662-
<< modulePath
690+
<< adjacentMod
663691
<< "; deferring to serialized module loader\n");
664692
return std::make_error_code(std::errc::not_supported);
665693
}
666694
}
667695

668-
// If we have a prebuilt cache path, check that too if the interface comes
669-
// from the SDK.
670-
if (!prebuiltCacheDir.empty()) {
671-
llvm::SmallString<256> scratch;
696+
if(!prebuiltMod.empty()) {
672697
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
673-
Optional<StringRef> path = computePrebuiltModulePath(scratch);
674-
if (!path) {
675-
// Hack: deal with prebuilds of modules that still use the target-based
676-
// names.
677-
path = computeFallbackPrebuiltModulePath(scratch);
678-
}
679-
if (path) {
680-
if (swiftModuleIsUpToDate(*path, deps, moduleBuffer)) {
681-
LLVM_DEBUG(llvm::dbgs() << "Found up-to-date prebuilt module at "
682-
<< path->str() << "\n");
683-
UsableModulePath = path->str();
684-
return DiscoveredModule::prebuilt(*path, std::move(moduleBuffer));
685-
} else {
686-
LLVM_DEBUG(llvm::dbgs() << "Found out-of-date prebuilt module at "
687-
<< path->str() << "\n");
688-
rebuildInfo.setModuleKind(*path,
689-
ModuleRebuildInfo::ModuleKind::Prebuilt);
690-
}
698+
if (swiftModuleIsUpToDate(prebuiltMod, deps, moduleBuffer)) {
699+
LLVM_DEBUG(llvm::dbgs() << "Found up-to-date prebuilt module at "
700+
<< prebuiltMod << "\n");
701+
UsableModulePath = prebuiltMod;
702+
return DiscoveredModule::prebuilt(prebuiltMod, std::move(moduleBuffer));
703+
} else {
704+
LLVM_DEBUG(llvm::dbgs() << "Found out-of-date prebuilt module at "
705+
<< prebuiltMod << "\n");
706+
rebuildInfo.setModuleKind(prebuiltMod,
707+
ModuleRebuildInfo::ModuleKind::Prebuilt);
691708
}
692709
}
693-
694-
// Couldn't find an up-to-date .swiftmodule, will need to build module from
695-
// interface.
696-
return notFoundError;
710+
// We cannot find any proper compiled module to use.
711+
return std::make_error_code(std::errc::no_such_file_or_directory);
697712
}
698713

699714
/// Finds the most appropriate .swiftmodule, whose dependencies are up to

0 commit comments

Comments
 (0)