Skip to content

Commit 782ba98

Browse files
committed
[Module aliasing] Mangle symbols with module real names
1 parent b69eac9 commit 782ba98

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 and Bar.swiftmodule is loaded
18+
// RUN: test -f %t/Foo.swiftmodule
19+
20+
// RUN: llvm-objdump -t %t/ResultFoo.o | %FileCheck %s -check-prefix=CHECK-A
21+
// CHECK-A: _$s3Foo4meow3Bar5KlassCSgyF
22+
23+
// RUN: llvm-objdump -t %t/ResultFoo.o | swift-demangle | %FileCheck %s -check-prefix=CHECK-B
24+
// CHECK-B: Foo.meow() -> Bar.Klass?
25+

0 commit comments

Comments
 (0)