Skip to content

Commit 8f1aec4

Browse files
authored
Merge pull request swiftlang#40384 from apple/es-scan
Module aliasing: Resolve deps graph with real module names when scanning dependencies. Resolves rdar://85991587.
2 parents ac6bb68 + 48a483a commit 8f1aec4

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

lib/AST/ModuleDependencies.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ void ModuleDependencies::addModuleDependencies(
100100
if (!importDecl)
101101
continue;
102102

103-
addModuleDependency(importDecl->getModulePath(), &alreadyAddedModules);
103+
ImportPath::Builder scratch;
104+
auto realPath = importDecl->getRealModulePath(scratch);
105+
addModuleDependency(realPath, &alreadyAddedModules);
104106
}
105107

106108
auto fileName = sf.getFilename();

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
16851685
if (subInstance.setup(subInvocation)) {
16861686
return std::make_error_code(std::errc::not_supported);
16871687
}
1688+
16881689
info.BuildArguments = BuildArgs;
16891690
info.Hash = CacheHash;
16901691
auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
@@ -1826,7 +1827,12 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
18261827

18271828
bool ExplicitSwiftModuleLoader::canImportModule(
18281829
ImportPath::Element mID, llvm::VersionTuple version, bool underlyingVersion) {
1829-
StringRef moduleName = mID.Item.str();
1830+
// Look up the module with the real name (physical name on disk);
1831+
// in case `-module-alias` is used, the name appearing in source files
1832+
// and the real module name are different. For example, '-module-alias Foo=Bar'
1833+
// maps Foo appearing in source files, e.g. 'import Foo', to the real module
1834+
// name Bar (on-disk name), which should be searched for loading.
1835+
StringRef moduleName = Ctx.getRealModuleName(mID.Item).str();
18301836
auto it = Impl.ExplicitModuleMap.find(moduleName);
18311837
// If no provided explicit module matches the name, then it cannot be imported.
18321838
if (it == Impl.ExplicitModuleMap.end()) {

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ std::error_code PlaceholderSwiftModuleScanner::findModuleFilesInDirectory(
7878
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
7979
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
8080
bool skipBuildingInterface, bool IsFramework) {
81-
StringRef moduleName = ModuleID.Item.str();
81+
StringRef moduleName = Ctx.getRealModuleName(ModuleID.Item).str();
8282
auto it = PlaceholderDependencyModuleMap.find(moduleName);
8383
// If no placeholder module stub path is given matches the name, return with an
8484
// error code.
@@ -106,11 +106,12 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
106106
// FIXME: Query the module interface loader to determine an appropriate
107107
// name for the module, which includes an appropriate hash.
108108
auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
109-
llvm::SmallString<32> modulePath = moduleName.str();
109+
auto realModuleName = Ctx.getRealModuleName(moduleName);
110+
llvm::SmallString<32> modulePath = realModuleName.str();
110111
llvm::sys::path::replace_extension(modulePath, newExt);
111112
Optional<ModuleDependencies> Result;
112113
std::error_code code =
113-
astDelegate.runInSubContext(moduleName.str(),
114+
astDelegate.runInSubContext(realModuleName.str(),
114115
moduleInterfacePath.str(),
115116
StringRef(),
116117
SourceLoc(),
@@ -119,7 +120,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
119120
ArrayRef<StringRef> PCMArgs, StringRef Hash) {
120121
assert(mainMod);
121122
std::string InPath = moduleInterfacePath.str();
122-
auto compiledCandidates = getCompiledCandidates(Ctx, moduleName.str(),
123+
auto compiledCandidates = getCompiledCandidates(Ctx, realModuleName.str(),
123124
InPath);
124125
Result = ModuleDependencies::forSwiftInterfaceModule(InPath,
125126
compiledCandidates,
@@ -136,7 +137,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
136137

137138
// Create a source file.
138139
unsigned bufferID = Ctx.SourceMgr.addNewSourceBuffer(std::move(interfaceBuf.get()));
139-
auto moduleDecl = ModuleDecl::create(moduleName, Ctx);
140+
auto moduleDecl = ModuleDecl::create(realModuleName, Ctx);
140141
auto sourceFile = new (Ctx) SourceFile(
141142
*moduleDecl, SourceFileKind::Interface, bufferID);
142143

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Test -scan-dependencies module aliasing.
2+
///
3+
/// Module 'Lib' imports module 'XLogging' via module aliasing and with various import attributes.
4+
/// Module 'User' imports 'Lib'.
5+
6+
// RUN: %empty-directory(%t)
7+
// RUN: %{python} %utils/split_file.py -o %t %s
8+
9+
/// Create AppleLogging.swiftmodule by aliasing XLogging
10+
// RUN: %target-swift-frontend -module-name AppleLogging -module-alias XLogging=AppleLogging %t/FileLogging.swift -emit-module -emit-module-path %t/AppleLogging.swiftmodule
11+
// RUN: test -f %t/AppleLogging.swiftmodule
12+
13+
/// Scaned dependencies should contain real name AppleLogging
14+
// RUN: %target-swift-frontend -scan-dependencies %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t > %t/scandump.output
15+
// RUN: %FileCheck %s -check-prefix=CHECK-REAL-NAME -input-file %t/scandump.output
16+
// CHECK-REAL-NAME-NOT: "swiftPrebuiltExternal": "XLogging"
17+
// CHECK-REAL-NAME-NOT: "compiledModulePath":{{.*}}XLogging.swiftmodule",
18+
// CHECK-REAL-NAME: "swiftPrebuiltExternal": "AppleLogging"
19+
// CHECK-REAL-NAME: "compiledModulePath":{{.*}}AppleLogging.swiftmodule",
20+
21+
/// Create AppleLoggingIF.swiftinterface by aliasing XLogging
22+
///
23+
// RUN: %target-swift-frontend -module-name AppleLoggingIF %t/FileLogging.swift -module-alias XLogging=AppleLoggingIF -I %t -emit-module -emit-module-interface-path %t/AppleLoggingIF.swiftinterface -swift-version 5 -enable-library-evolution -I %t
24+
// RUN: test -f %t/AppleLoggingIF.swiftinterface
25+
26+
/// Scaned dependencies should contain real name AppleLoggingIF
27+
// RUN: %target-swift-frontend -scan-dependencies %t/FileLib.swift -module-alias XLogging=AppleLoggingIF -I %t > %t/scandumpIF.output
28+
// RUN: %FileCheck %s -check-prefix=CHECK-REAL-NAME-IF -input-file %t/scandumpIF.output
29+
// CHECK-REAL-NAME-IF-NOT: "swift": "XLogging"
30+
// CHECK-REAL-NAME-IF-NOT: "moduleInterfacePath":{{.*}}XLogging.swiftinterface
31+
// CHECK-REAL-NAME-IF: "swift": "AppleLoggingIF"
32+
// CHECK-REAL-NAME-IF: "moduleInterfacePath":{{.*}}AppleLoggingIF.swiftinterface
33+
34+
// BEGIN FileLogging.swift
35+
public struct Logger {
36+
public init() {}
37+
public func startLogging() {}
38+
}
39+
public func setup() -> XLogging.Logger? {
40+
return Logger()
41+
}
42+
43+
// BEGIN FileLib.swift
44+
import XLogging
45+
46+
public func start() {
47+
let it = Logger()
48+
it.startLogging()
49+
}
50+

0 commit comments

Comments
 (0)