Skip to content

Commit 3c52406

Browse files
committed
[Serialization|NFC] Extract logic loading dependencies out of associateWithFileContext
1 parent 1a3cbfa commit 3c52406

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
lines changed

lib/Serialization/ModuleFile.cpp

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -123,53 +123,13 @@ bool ModuleFile::allowCompilerErrors() const {
123123
return getContext().LangOpts.AllowModuleWithCompilerErrors;
124124
}
125125

126-
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
127-
bool recoverFromIncompatibility) {
128-
PrettyStackTraceModuleFile stackEntry(*this);
129-
130-
assert(!hasError() && "error already detected; should not call this");
131-
assert(!FileContext && "already associated with an AST module");
132-
FileContext = file;
133-
Status status = Status::Valid;
134-
135-
ModuleDecl *M = file->getParentModule();
136-
// The real (on-disk) name of the module should be checked here as that's the
137-
// actually loaded module. In case module aliasing is used when building the main
138-
// module, e.g. -module-name MyModule -module-alias Foo=Bar, the loaded module
139-
// that maps to 'Foo' is actually Bar.swiftmodule|.swiftinterface (applies to swift
140-
// modules only), which is retrieved via M->getRealName(). If no module aliasing is
141-
// used, M->getRealName() will return the same value as M->getName(), which is 'Foo'.
142-
if (M->getRealName().str() != Core->Name) {
143-
return error(Status::NameMismatch);
144-
}
145-
126+
Status
127+
ModuleFile::loadDependenciesForFileContext(const FileUnit *file,
128+
SourceLoc diagLoc,
129+
bool forTestable) {
146130
ASTContext &ctx = getContext();
147-
148-
llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
149-
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
150-
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
151-
status = Status::TargetIncompatible;
152-
if (!recoverFromIncompatibility)
153-
return error(status);
154-
} else if (ctx.LangOpts.EnableTargetOSChecking && !M->isResilient() &&
155-
isTargetTooNew(moduleTarget, ctx.LangOpts.Target)) {
156-
status = Status::TargetTooNew;
157-
if (!recoverFromIncompatibility)
158-
return error(status);
159-
}
160-
161-
StringRef SDKPath = ctx.SearchPathOpts.getSDKPath();
162-
if (SDKPath.empty() ||
163-
!Core->ModuleInputBuffer->getBufferIdentifier().startswith(SDKPath)) {
164-
for (const auto &searchPath : Core->SearchPaths) {
165-
ctx.addSearchPath(
166-
ctx.SearchPathOpts.SearchPathRemapper.remapPath(searchPath.Path),
167-
searchPath.IsFramework,
168-
searchPath.IsSystem);
169-
}
170-
}
171-
172131
auto clangImporter = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
132+
ModuleDecl *M = file->getParentModule();
173133

174134
bool missingDependency = false;
175135
for (auto &dependency : Dependencies) {
@@ -259,6 +219,59 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
259219
return error(Status::MissingDependency);
260220
}
261221

222+
return Status::Valid;
223+
}
224+
225+
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
226+
bool recoverFromIncompatibility) {
227+
PrettyStackTraceModuleFile stackEntry(*this);
228+
229+
assert(!hasError() && "error already detected; should not call this");
230+
assert(!FileContext && "already associated with an AST module");
231+
FileContext = file;
232+
Status status = Status::Valid;
233+
234+
ModuleDecl *M = file->getParentModule();
235+
// The real (on-disk) name of the module should be checked here as that's the
236+
// actually loaded module. In case module aliasing is used when building the main
237+
// module, e.g. -module-name MyModule -module-alias Foo=Bar, the loaded module
238+
// that maps to 'Foo' is actually Bar.swiftmodule|.swiftinterface (applies to swift
239+
// modules only), which is retrieved via M->getRealName(). If no module aliasing is
240+
// used, M->getRealName() will return the same value as M->getName(), which is 'Foo'.
241+
if (M->getRealName().str() != Core->Name) {
242+
return error(Status::NameMismatch);
243+
}
244+
245+
ASTContext &ctx = getContext();
246+
247+
llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
248+
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
249+
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
250+
status = Status::TargetIncompatible;
251+
if (!recoverFromIncompatibility)
252+
return error(status);
253+
} else if (ctx.LangOpts.EnableTargetOSChecking && !M->isResilient() &&
254+
isTargetTooNew(moduleTarget, ctx.LangOpts.Target)) {
255+
status = Status::TargetTooNew;
256+
if (!recoverFromIncompatibility)
257+
return error(status);
258+
}
259+
260+
StringRef SDKPath = ctx.SearchPathOpts.getSDKPath();
261+
if (SDKPath.empty() ||
262+
!Core->ModuleInputBuffer->getBufferIdentifier().startswith(SDKPath)) {
263+
for (const auto &searchPath : Core->SearchPaths) {
264+
ctx.addSearchPath(
265+
ctx.SearchPathOpts.SearchPathRemapper.remapPath(searchPath.Path),
266+
searchPath.IsFramework,
267+
searchPath.IsSystem);
268+
}
269+
}
270+
271+
Status res = loadDependenciesForFileContext(file, diagLoc,
272+
/*forTestable=*/false);
273+
if (res != Status::Valid) return res;
274+
262275
if (Core->Bits.HasEntryPoint) {
263276
FileContext->getParentModule()->registerEntryPointFile(FileContext,
264277
SourceLoc(),

lib/Serialization/ModuleFile.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,19 @@ class ModuleFile
649649
Status associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
650650
bool recoverFromIncompatibility);
651651

652+
/// Load dependencies of this module.
653+
///
654+
/// \param file The FileUnit that represents this file's place in the AST.
655+
/// \param diagLoc A location used for diagnostics that occur during loading.
656+
/// This does not include diagnostics about \e this file failing to load,
657+
/// but rather other things that might be imported as part of bringing the
658+
/// file into the AST.
659+
///
660+
/// \returns any error that occurred during loading dependencies.
661+
Status
662+
loadDependenciesForFileContext(const FileUnit *file, SourceLoc diagLoc,
663+
bool forTestable);
664+
652665
/// How should \p dependency be loaded for a transitive import via \c this?
653666
ModuleLoadingBehavior
654667
getTransitiveLoadingBehavior(const Dependency &dependency) const;

0 commit comments

Comments
 (0)