Skip to content

Commit 0067c41

Browse files
committed
Factor out reading in Binary module dependency imports from 'SerializedModuleLoaderBase::scanModuleFile'.
1 parent c1d5118 commit 0067c41

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
namespace swift {
2222
class ModuleFile;
23+
class PathObfuscator;
24+
enum class ModuleLoadingBehavior;
2325
namespace file_types {
2426
enum ID : uint8_t;
2527
}
@@ -146,6 +148,16 @@ class SerializedModuleLoaderBase : public ModuleLoader {
146148
/// Scan the given serialized module file to determine dependencies.
147149
llvm::ErrorOr<ModuleDependencyInfo> scanModuleFile(Twine modulePath, bool isFramework);
148150

151+
static llvm::ErrorOr<llvm::StringSet<>>
152+
getModuleImportsOfModule(Twine modulePath,
153+
ModuleLoadingBehavior transitiveBehavior,
154+
bool isFramework,
155+
bool isRequiredOSSAModules,
156+
StringRef SDKName,
157+
StringRef packageName,
158+
llvm::vfs::FileSystem *fileSystem,
159+
PathObfuscator &recoverer);
160+
149161
/// Load the module file into a buffer and also collect its module name.
150162
static std::unique_ptr<llvm::MemoryBuffer>
151163
getModuleName(ASTContext &Ctx, StringRef modulePath, std::string &Name);

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -392,47 +392,35 @@ std::error_code SerializedModuleLoaderBase::openModuleFile(
392392
return std::error_code();
393393
}
394394

395-
llvm::ErrorOr<ModuleDependencyInfo> SerializedModuleLoaderBase::scanModuleFile(
396-
Twine modulePath, bool isFramework) {
397-
// Open the module file
398-
auto &fs = *Ctx.SourceMgr.getFileSystem();
399-
auto moduleBuf = fs.getBufferForFile(modulePath);
395+
llvm::ErrorOr<llvm::StringSet<>>
396+
SerializedModuleLoaderBase::getModuleImportsOfModule(
397+
Twine modulePath, ModuleLoadingBehavior transitiveBehavior,
398+
bool isFramework, bool isRequiredOSSAModules, StringRef SDKName,
399+
StringRef packageName, llvm::vfs::FileSystem *fileSystem,
400+
PathObfuscator &recoverer) {
401+
auto moduleBuf = fileSystem->getBufferForFile(modulePath);
400402
if (!moduleBuf)
401403
return moduleBuf.getError();
402404

405+
llvm::StringSet<> importedModuleNames;
403406
// Load the module file without validation.
404407
std::shared_ptr<const ModuleFileSharedCore> loadedModuleFile;
405408
serialization::ValidationInfo loadInfo = ModuleFileSharedCore::load(
406409
"", "", std::move(moduleBuf.get()), nullptr, nullptr, isFramework,
407-
isRequiredOSSAModules(), Ctx.LangOpts.SDKName,
408-
Ctx.SearchPathOpts.DeserializedPathRecoverer, loadedModuleFile);
410+
isRequiredOSSAModules, SDKName, recoverer, loadedModuleFile);
409411

410-
const std::string moduleDocPath;
411-
const std::string sourceInfoPath;
412-
// Map the set of dependencies over to the "module dependencies".
413-
auto dependencies = ModuleDependencyInfo::forSwiftBinaryModule(modulePath.str(),
414-
moduleDocPath,
415-
sourceInfoPath,
416-
isFramework);
417-
llvm::StringSet<> addedModuleNames;
418412
for (const auto &dependency : loadedModuleFile->getDependencies()) {
419413
// FIXME: Record header dependency?
420414
if (dependency.isHeader())
421415
continue;
422416

423-
// Some transitive dependencies of binary modules are not required to be
424-
// imported during normal builds.
425-
// TODO: This is worth revisiting for debugger purposes where
426-
// loading the module is optional, and implementation-only imports
427-
// from modules with testing enabled where the dependency is
428-
// optional.
429-
ModuleLoadingBehavior transitiveBehavior =
430-
loadedModuleFile->getTransitiveLoadingBehavior(dependency,
431-
/*debuggerMode*/false,
432-
/*isPartialModule*/false,
433-
/*package*/Ctx.LangOpts.PackageName,
434-
loadedModuleFile->isTestable());
435-
if (transitiveBehavior != ModuleLoadingBehavior::Required)
417+
ModuleLoadingBehavior dependencyTransitiveBehavior =
418+
loadedModuleFile->getTransitiveLoadingBehavior(
419+
dependency,
420+
/*debuggerMode*/ false,
421+
/*isPartialModule*/ false, packageName,
422+
loadedModuleFile->isTestable());
423+
if (dependencyTransitiveBehavior != transitiveBehavior)
436424
continue;
437425

438426
// Find the top-level module name.
@@ -442,9 +430,40 @@ llvm::ErrorOr<ModuleDependencyInfo> SerializedModuleLoaderBase::scanModuleFile(
442430
if (dotPos != std::string::npos)
443431
moduleName = moduleName.slice(0, dotPos);
444432

445-
dependencies.addModuleImport(moduleName, &addedModuleNames);
433+
importedModuleNames.insert(moduleName);
446434
}
447435

436+
return importedModuleNames;
437+
}
438+
439+
llvm::ErrorOr<ModuleDependencyInfo>
440+
SerializedModuleLoaderBase::scanModuleFile(Twine modulePath, bool isFramework) {
441+
const std::string moduleDocPath;
442+
const std::string sourceInfoPath;
443+
// Map the set of dependencies over to the "module dependencies".
444+
auto dependencies = ModuleDependencyInfo::forSwiftBinaryModule(
445+
modulePath.str(), moduleDocPath, sourceInfoPath, isFramework);
446+
// Some transitive dependencies of binary modules are not required to be
447+
// imported during normal builds.
448+
// TODO: This is worth revisiting for debugger purposes where
449+
// loading the module is optional, and implementation-only imports
450+
// from modules with testing enabled where the dependency is
451+
// optional.
452+
ModuleLoadingBehavior transitiveLoadingBehavior =
453+
ModuleLoadingBehavior::Required;
454+
auto importedModuleNames = getModuleImportsOfModule(
455+
modulePath, transitiveLoadingBehavior, isFramework,
456+
isRequiredOSSAModules(), Ctx.LangOpts.SDKName, Ctx.LangOpts.PackageName,
457+
Ctx.SourceMgr.getFileSystem().get(),
458+
Ctx.SearchPathOpts.DeserializedPathRecoverer);
459+
if (!importedModuleNames)
460+
return importedModuleNames.getError();
461+
462+
llvm::StringSet<> addedModuleNames;
463+
for (const auto &importedModuleName : *importedModuleNames)
464+
dependencies.addModuleImport(importedModuleName.getKey(),
465+
&addedModuleNames);
466+
448467
return std::move(dependencies);
449468
}
450469

0 commit comments

Comments
 (0)