[Serialization|NFC] Extract logic loading dependencies out of associateWithFileContext

This commit is contained in:
Alexis Laferrière
2023-03-22 15:55:06 -07:00
parent 1a3cbfab7c
commit 3c52406355
2 changed files with 71 additions and 45 deletions

View File

@@ -123,53 +123,13 @@ bool ModuleFile::allowCompilerErrors() const {
return getContext().LangOpts.AllowModuleWithCompilerErrors;
}
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
bool recoverFromIncompatibility) {
PrettyStackTraceModuleFile stackEntry(*this);
assert(!hasError() && "error already detected; should not call this");
assert(!FileContext && "already associated with an AST module");
FileContext = file;
Status status = Status::Valid;
ModuleDecl *M = file->getParentModule();
// The real (on-disk) name of the module should be checked here as that's the
// actually loaded module. In case module aliasing is used when building the main
// module, e.g. -module-name MyModule -module-alias Foo=Bar, the loaded module
// that maps to 'Foo' is actually Bar.swiftmodule|.swiftinterface (applies to swift
// modules only), which is retrieved via M->getRealName(). If no module aliasing is
// used, M->getRealName() will return the same value as M->getName(), which is 'Foo'.
if (M->getRealName().str() != Core->Name) {
return error(Status::NameMismatch);
}
Status
ModuleFile::loadDependenciesForFileContext(const FileUnit *file,
SourceLoc diagLoc,
bool forTestable) {
ASTContext &ctx = getContext();
llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
status = Status::TargetIncompatible;
if (!recoverFromIncompatibility)
return error(status);
} else if (ctx.LangOpts.EnableTargetOSChecking && !M->isResilient() &&
isTargetTooNew(moduleTarget, ctx.LangOpts.Target)) {
status = Status::TargetTooNew;
if (!recoverFromIncompatibility)
return error(status);
}
StringRef SDKPath = ctx.SearchPathOpts.getSDKPath();
if (SDKPath.empty() ||
!Core->ModuleInputBuffer->getBufferIdentifier().startswith(SDKPath)) {
for (const auto &searchPath : Core->SearchPaths) {
ctx.addSearchPath(
ctx.SearchPathOpts.SearchPathRemapper.remapPath(searchPath.Path),
searchPath.IsFramework,
searchPath.IsSystem);
}
}
auto clangImporter = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
ModuleDecl *M = file->getParentModule();
bool missingDependency = false;
for (auto &dependency : Dependencies) {
@@ -259,6 +219,59 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
return error(Status::MissingDependency);
}
return Status::Valid;
}
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
bool recoverFromIncompatibility) {
PrettyStackTraceModuleFile stackEntry(*this);
assert(!hasError() && "error already detected; should not call this");
assert(!FileContext && "already associated with an AST module");
FileContext = file;
Status status = Status::Valid;
ModuleDecl *M = file->getParentModule();
// The real (on-disk) name of the module should be checked here as that's the
// actually loaded module. In case module aliasing is used when building the main
// module, e.g. -module-name MyModule -module-alias Foo=Bar, the loaded module
// that maps to 'Foo' is actually Bar.swiftmodule|.swiftinterface (applies to swift
// modules only), which is retrieved via M->getRealName(). If no module aliasing is
// used, M->getRealName() will return the same value as M->getName(), which is 'Foo'.
if (M->getRealName().str() != Core->Name) {
return error(Status::NameMismatch);
}
ASTContext &ctx = getContext();
llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
status = Status::TargetIncompatible;
if (!recoverFromIncompatibility)
return error(status);
} else if (ctx.LangOpts.EnableTargetOSChecking && !M->isResilient() &&
isTargetTooNew(moduleTarget, ctx.LangOpts.Target)) {
status = Status::TargetTooNew;
if (!recoverFromIncompatibility)
return error(status);
}
StringRef SDKPath = ctx.SearchPathOpts.getSDKPath();
if (SDKPath.empty() ||
!Core->ModuleInputBuffer->getBufferIdentifier().startswith(SDKPath)) {
for (const auto &searchPath : Core->SearchPaths) {
ctx.addSearchPath(
ctx.SearchPathOpts.SearchPathRemapper.remapPath(searchPath.Path),
searchPath.IsFramework,
searchPath.IsSystem);
}
}
Status res = loadDependenciesForFileContext(file, diagLoc,
/*forTestable=*/false);
if (res != Status::Valid) return res;
if (Core->Bits.HasEntryPoint) {
FileContext->getParentModule()->registerEntryPointFile(FileContext,
SourceLoc(),