Skip to content

Commit 5c92cca

Browse files
authored
Merge pull request #39628 from apple/es-m4
[Module aliasing] Mangle symbols using the module real name (binary name), which can be different from the module name appearing in source files if '-module-alias' is used. Resolves rdar://83632604.
2 parents 47d02d2 + e130fde commit 5c92cca

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,8 +2175,16 @@ void ASTMangler::appendModule(const ModuleDecl *module,
21752175
StringRef useModuleName) {
21762176
assert(!module->getParent() && "cannot mangle nested modules!");
21772177

2178-
StringRef ModName =
2179-
DWARFMangling ? module->getName().str() : module->getABIName().str();
2178+
// Use the module real name in mangling; this is the physical name
2179+
// of the module on-disk, which can be different if -module-alias is
2180+
// used.
2181+
// For example, if a module Foo has 'import Bar', and '-module-alias Bar=Baz'
2182+
// was passed, the name 'Baz' will be used for mangling besides loading.
2183+
StringRef ModName = module->getRealName().str();
2184+
if (!DWARFMangling &&
2185+
module->getABIName() != module->getName()) { // check if the ABI name is set
2186+
ModName = module->getABIName().str();
2187+
}
21802188

21812189
// Try the special 'swift' substitution.
21822190
if (ModName == STDLIB_NAME) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Test mangling with module aliasing
2+
3+
// RUN: %empty-directory(%t)
4+
5+
/// Create a module Bar
6+
// RUN: echo 'public class Klass {}' > %t/FileBar.swift
7+
// RUN: %target-swift-frontend -module-name Bar %t/FileBar.swift -emit-module -emit-module-path %t/Bar.swiftmodule
8+
9+
/// Check Bar.swiftmodule is created
10+
// RUN: test -f %t/Bar.swiftmodule
11+
12+
/// Create a module Foo that imports Cat with -module-alias Cat=Bar
13+
// RUN: echo 'import Cat' > %t/FileFoo.swift
14+
// RUN: echo 'public func meow() -> Cat.Klass? { return nil }' >> %t/FileFoo.swift
15+
// RUN: %target-swift-frontend -module-name Foo %t/FileFoo.swift -module-alias Cat=Bar -I %t -emit-module -emit-module-path %t/Foo.swiftmodule -c -o %t/ResultFoo.o
16+
17+
/// Check Foo.swiftmodule is created
18+
// RUN: test -f %t/Foo.swiftmodule
19+
20+
/// Check the mangled name for func meow contains the real module name Bar
21+
// RUN: llvm-objdump -t %t/ResultFoo.o | %FileCheck %s -check-prefix=CHECK-A
22+
// CHECK-A: s3Foo4meow3Bar5KlassCSgyF
23+
24+
/// Check demangling shows the real module name Bar
25+
// RUN: llvm-objdump -t %t/ResultFoo.o | swift-demangle | %FileCheck %s -check-prefix=CHECK-B
26+
// CHECK-B: Foo.meow() -> Bar.Klass?
27+

0 commit comments

Comments
 (0)