Skip to content

Commit 7a3a032

Browse files
committed
Use module real name in ASTPrinter
1 parent e3d5ae0 commit 7a3a032

File tree

5 files changed

+157
-109
lines changed

5 files changed

+157
-109
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,13 +2337,16 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
23372337
llvm::interleave(decl->getImportPath(),
23382338
[&](const ImportPath::Element &Elem) {
23392339
if (!Mods.empty()) {
2340-
Identifier Name = Elem.Item;
2340+
// Should print the module real name in case module
2341+
// aliasing is used (see -module-alias), since that's
2342+
// the actual binary name.
2343+
Identifier Name = decl->getASTContext().getRealModuleName(Elem.Item);
23412344
if (Options.MapCrossImportOverlaysToDeclaringModule) {
23422345
if (auto *MD = Mods.front().getAsSwiftModule()) {
23432346
ModuleDecl *Declaring = const_cast<ModuleDecl*>(MD)
23442347
->getDeclaringModuleIfCrossImportOverlay();
23452348
if (Declaring)
2346-
Name = Declaring->getName();
2349+
Name = Declaring->getRealName();
23472350
}
23482351
}
23492352
Printer.printModuleRef(Mods.front(), Name);
@@ -4321,7 +4324,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43214324
Mod = Declaring;
43224325
}
43234326

4324-
Identifier Name = Mod->getName();
4327+
// Should use the module real (binary) name here and everywhere else the
4328+
// module is printed in case module aliasing is used (see -module-alias)
4329+
Identifier Name = Mod->getRealName();
43254330
if (Options.UseExportedModuleNames && !ExportedModuleName.empty()) {
43264331
Name = Mod->getASTContext().getIdentifier(ExportedModuleName);
43274332
}
@@ -4342,7 +4347,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43424347
bool isLLDBExpressionModule(ModuleDecl *M) {
43434348
if (!M)
43444349
return false;
4345-
return M->getName().str().startswith(LLDB_EXPRESSIONS_MODULE_NAME_PREFIX);
4350+
return M->getRealName().str().startswith(LLDB_EXPRESSIONS_MODULE_NAME_PREFIX);
43464351
}
43474352

43484353
bool shouldPrintFullyQualified(TypeBase *T) {
@@ -4373,7 +4378,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43734378

43744379
// Don't print qualifiers for types from the standard library.
43754380
if (M->isStdlibModule() ||
4376-
M->getName() == M->getASTContext().Id_ObjectiveC ||
4381+
M->getRealName() == M->getASTContext().Id_ObjectiveC ||
43774382
M->isSystemModule() ||
43784383
isLLDBExpressionModule(M))
43794384
return false;
@@ -4636,7 +4641,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
46364641

46374642
void visitModuleType(ModuleType *T) {
46384643
Printer << "module<";
4639-
Printer.printModuleRef(T->getModule(), T->getModule()->getName());
4644+
// Should print the module real name in case module aliasing is
4645+
// used (see -module-alias), since that's the actual binary name.
4646+
Printer.printModuleRef(T->getModule(), T->getModule()->getRealName());
46404647
Printer << ">";
46414648
}
46424649

@@ -5696,13 +5703,13 @@ void ProtocolConformance::printName(llvm::raw_ostream &os,
56965703
case ProtocolConformanceKind::Normal: {
56975704
auto normal = cast<NormalProtocolConformance>(this);
56985705
os << normal->getProtocol()->getName()
5699-
<< " module " << normal->getDeclContext()->getParentModule()->getName();
5706+
<< " module " << normal->getDeclContext()->getParentModule()->getRealName();
57005707
break;
57015708
}
57025709
case ProtocolConformanceKind::Self: {
57035710
auto self = cast<SelfProtocolConformance>(this);
57045711
os << self->getProtocol()->getName()
5705-
<< " module " << self->getDeclContext()->getParentModule()->getName();
5712+
<< " module " << self->getDeclContext()->getParentModule()->getRealName();
57065713
break;
57075714
}
57085715
case ProtocolConformanceKind::Specialized: {

test/Frontend/module-alias-emit-sil-reingest.swift

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
/// Round trip test for SIL with module aliasing
2+
13
// RUN: %empty-directory(%t)
24
// RUN: %{python} %utils/split_file.py -o %t %s
35

6+
/// Create module Tea
47
// RUN: %target-swift-frontend -module-name Tea %t/FileTea.swift -emit-module -emit-module-path %t/Tea.swiftmodule
58

6-
// RUN: %target-swift-frontend -emit-sil %t/FileBar.swift -module-alias Coffee=Tea -I %t -o %t/Bar-output.sil
7-
// RUN: %FileCheck %s -input-file %t/Bar-output.sil
9+
/// Emit SIL with module aliasing
10+
// RUN: %target-swift-frontend -emit-sil %t/FileBar.swift -module-alias Coffee=Tea -I %t -o %t/Bar-output1.sil
11+
12+
/// Verify the module real name 'Tea' is contained in the generated SIL
13+
// RUN: %FileCheck %s -input-file %t/Bar-output1.sil
814

9-
// RUN: %target-sil-opt -enable-sil-verify-all %t/Bar-import-real.sil -I %t -o %t/Bar-re-output.sil
10-
// RUN: %FileCheck %s -input-file %t/Bar-re-output.sil -check-prefix RE-CHECK
15+
/// Reingest the SIL file and verify it contains the same result
16+
// RUN: %target-sil-opt -enable-sil-verify-all %t/Bar-output1.sil -I %t -o %t/Bar-output2.sil
17+
// RUN: %FileCheck %s -input-file %t/Bar-output2.sil -check-prefix CHECK
1118

1219
// CHECK: sil_stage canonical
1320

1421
// CHECK: import Builtin
1522
// CHECK: import Swift
1623
// CHECK: import SwiftShims
1724

18-
// CHECK: import Coffee
25+
// CHECK: import Tea
1926

2027
// CHECK: public func drink() -> Mild?
2128

@@ -39,66 +46,6 @@
3946
// CHECK: // brew()
4047
// CHECK: sil [noinline] @$s3Tea4brewAA4MildVSgyF : $@convention(thin) () -> Optional<Mild>
4148

42-
// RE-CHECK: sil_stage canonical
43-
44-
// RE-CHECK: import Builtin
45-
// RE-CHECK: import Swift
46-
// RE-CHECK: import SwiftShims
47-
48-
// RE-CHECK: import Tea
49-
50-
// RE-CHECK: public func drink() -> Mild?
51-
52-
// RE-CHECK: // main
53-
// RE-CHECK: sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
54-
// RE-CHECK: bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
55-
// RE-CHECK: %2 = integer_literal $Builtin.Int32, 0 // user: %3
56-
// RE-CHECK: %3 = struct $Int32 (%2 : $Builtin.Int32) // user: %4
57-
// RE-CHECK: return %3 : $Int32 // id: %4
58-
// RE-CHECK: } // end sil function 'main'
59-
60-
// RE-CHECK: // drink()
61-
// RE-CHECK: sil @$s4main5drink3Tea4MildVSgyF : $@convention(thin) () -> Optional<Mild> {
62-
// RE-CHECK: bb0:
63-
// RE-CHECK: // function_ref brew()
64-
// RE-CHECK: %0 = function_ref @$s3Tea4brewAA4MildVSgyF : $@convention(thin) () -> Optional<Mild> // user: %1
65-
// RE-CHECK: %1 = apply %0() : $@convention(thin) () -> Optional<Mild> // user: %2
66-
// RE-CHECK: return %1 : $Optional<Mild> // id: %2
67-
// RE-CHECK: } // end sil function '$s4main5drink3Tea4MildVSgyF'
68-
69-
// RE-CHECK: // brew()
70-
// RE-CHECK: sil [noinline] @$s3Tea4brewAA4MildVSgyF : $@convention(thin) () -> Optional<Mild>
71-
72-
// BEGIN Bar-import-real.sil
73-
sil_stage canonical
74-
75-
import Builtin
76-
import Swift
77-
import SwiftShims
78-
79-
import Tea
80-
81-
public func drink() -> Mild?
82-
83-
// main
84-
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
85-
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
86-
%2 = integer_literal $Builtin.Int32, 0 // user: %3
87-
%3 = struct $Int32 (%2 : $Builtin.Int32) // user: %4
88-
return %3 : $Int32 // id: %4
89-
} // end sil function 'main'
90-
91-
// drink()
92-
sil @$s4main5drink3Tea4MildVSgyF : $@convention(thin) () -> Optional<Mild> {
93-
bb0:
94-
// function_ref brew()
95-
%0 = function_ref @$s3Tea4brewAA4MildVSgyF : $@convention(thin) () -> Optional<Mild> // user: %1
96-
%1 = apply %0() : $@convention(thin) () -> Optional<Mild> // user: %2
97-
return %1 : $Optional<Mild> // id: %2
98-
} // end sil function '$s4main5drink3Tea4MildVSgyF'
99-
100-
// brew()
101-
sil [noinline] @$s3Tea4brewAA4MildVSgyF : $@convention(thin) () -> Optional<Mild>
10249

10350

10451
// BEGIN FileTea.swift
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/// Test emit sil with module aliasing.
2+
///
3+
/// Module 'Lib' imports module 'XLogging', and 'XLogging' is aliased 'AppleLogging'.
4+
5+
// RUN: %empty-directory(%t)
6+
// RUN: %{python} %utils/split_file.py -o %t %s
7+
8+
/// Create AppleLogging.swiftmodule by aliasing XLogging
9+
// RUN: %target-swift-frontend -module-name AppleLogging -module-alias XLogging=AppleLogging %t/FileLogging.swift -emit-module -emit-module-path %t/AppleLogging.swiftmodule
10+
// RUN: test -f %t/AppleLogging.swiftmodule
11+
12+
/// Verify generated SIL only contains AppleLogging
13+
// RUN: %target-swift-frontend -emit-sil %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t > %t/result-sil.output
14+
// RUN: %FileCheck %s -input-file %t/result-sil.output
15+
// RUN: not %FileCheck %s -input-file %t/result-sil.output -check-prefix CHECK-NOT-FOUND
16+
// CHECK-NOT-FOUND: XLogging
17+
18+
19+
// CHECK: sil_stage canonical
20+
21+
// CHECK: import Builtin
22+
// CHECK: import Swift
23+
// CHECK: import SwiftShims
24+
25+
// CHECK: import AppleLogging
26+
27+
// CHECK: public struct MyLib : Loggable {
28+
// CHECK: public var verbosity: Int { get }
29+
// CHECK: init()
30+
// CHECK: }
31+
32+
// CHECK: public func start() -> Loggable?
33+
34+
// CHECK: public func end(_ arg: Logger)
35+
36+
// CHECK: // main
37+
// CHECK: sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
38+
// CHECK: bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
39+
// CHECK: %2 = integer_literal $Builtin.Int32, 0 // user: %3
40+
// CHECK: %3 = struct $Int32 (%2 : $Builtin.Int32) // user: %4
41+
// CHECK: return %3 : $Int32 // id: %4
42+
// CHECK: // end sil function 'main'
43+
44+
// CHECK: // MyLib.verbosity.getter
45+
// CHECK: sil @$s7FileLib02MyB0V9verbositySivg : $@convention(method) (MyLib) -> Int {
46+
// CHECK: // %0 "self" // user: %1
47+
// CHECK: bb0(%0 : $MyLib):
48+
// CHECK: debug_value %0 : $MyLib, let, name "self", argno 1, implicit // id: %1
49+
// CHECK: %2 = integer_literal $Builtin.Int64, 3 // user: %3
50+
// CHECK: %3 = struct $Int (%2 : $Builtin.Int64) // user: %4
51+
// CHECK: return %3 : $Int // id: %4
52+
// CHECK: } // end sil function '$s7FileLib02MyB0V9verbositySivg'
53+
54+
// CHECK: // Int.init(_builtinIntegerLiteral:)
55+
// CHECK: sil public_external [transparent] @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int {
56+
// CHECK: // %0 // user: %2
57+
// CHECK: bb0(%0 : $Builtin.IntLiteral, %1 : $@thin Int.Type):
58+
// CHECK: %2 = builtin "s_to_s_checked_trunc_IntLiteral_Int64"(%0 : $Builtin.IntLiteral) : $(Builtin.Int64, Builtin.Int1) // user: %3
59+
// CHECK: %3 = tuple_extract %2 : $(Builtin.Int64, Builtin.Int1), 0 // user: %4
60+
// CHECK: %4 = struct $Int (%3 : $Builtin.Int64) // user: %5
61+
// CHECK: return %4 : $Int // id: %5
62+
// CHECK: } // end sil function '$sSi22_builtinIntegerLiteralSiBI_tcfC'
63+
64+
// CHECK: // protocol witness for Loggable.verbosity.getter in conformance MyLib
65+
// CHECK: sil shared [transparent] [thunk] @$s7FileLib02MyB0V12AppleLogging8LoggableAadEP9verbositySivgTW : $@convention(witness_method: Loggable) (@in_guaranteed MyLib) -> Int {
66+
// CHECK: // %0 // user: %1
67+
// CHECK: bb0(%0 : $*MyLib):
68+
// CHECK: %1 = load %0 : $*MyLib // user: %3
69+
// CHECK: // function_ref MyLib.verbosity.getter
70+
// CHECK: %2 = function_ref @$s7FileLib02MyB0V9verbositySivg : $@convention(method) (MyLib) -> Int // user: %3
71+
// CHECK: %3 = apply %2(%1) : $@convention(method) (MyLib) -> Int // user: %4
72+
// CHECK: return %3 : $Int // id: %4
73+
// CHECK: } // end sil function '$s7FileLib02MyB0V12AppleLogging8LoggableAadEP9verbositySivgTW'
74+
75+
// CHECK: // start()
76+
// CHECK: sil @$s7FileLib5start12AppleLogging8Loggable_pSgyF : $@convention(thin) () -> @out Optional<Loggable> {
77+
// CHECK: // %0 "$return_value" // user: %2
78+
// CHECK: bb0(%0 : $*Optional<Loggable>):
79+
// CHECK: // function_ref setup()
80+
// CHECK: %1 = function_ref @$s12AppleLogging5setupAA8Loggable_pSgyF : $@convention(thin) () -> @out Optional<Loggable> // user: %2
81+
// CHECK: %2 = apply %1(%0) : $@convention(thin) () -> @out Optional<Loggable>
82+
// CHECK: %3 = tuple () // user: %4
83+
// CHECK: return %3 : $() // id: %4
84+
// CHECK: } // end sil function '$s7FileLib5start12AppleLogging8Loggable_pSgyF'
85+
86+
// CHECK: // setup()
87+
// CHECK: sil @$s12AppleLogging5setupAA8Loggable_pSgyF : $@convention(thin) () -> @out Optional<Loggable>
88+
89+
// CHECK: // end(_:)
90+
// CHECK: sil @$s7FileLib3endyy12AppleLogging6LoggerVF : $@convention(thin) (Logger) -> () {
91+
// CHECK: // %0 "arg" // user: %1
92+
// CHECK: bb0(%0 : $Logger):
93+
// CHECK: debug_value %0 : $Logger, let, name "arg", argno 1 // id: %1
94+
// CHECK: %2 = tuple () // user: %3
95+
// CHECK: return %2 : $() // id: %3
96+
// CHECK: } // end sil function '$s7FileLib3endyy12AppleLogging6LoggerVF'
97+
98+
// CHECK: sil_witness_table MyLib: Loggable module FileLib {
99+
// CHECK: method #Loggable.verbosity!getter: <Self where Self : Loggable> (Self) -> () -> Int : @$s7FileLib02MyB0V12AppleLogging8LoggableAadEP9verbositySivgTW // protocol witness for Loggable.verbosity.getter in conformance MyLib
100+
// CHECK: }
101+
102+
// CHECK: sil_property #MyLib.verbosity ()
103+
104+
105+
106+
// BEGIN FileLogging.swift
107+
public protocol Loggable {
108+
var verbosity: Int { get }
109+
}
110+
public struct Logger {
111+
public init() {}
112+
}
113+
public func setup() -> XLogging.Loggable? {
114+
return nil
115+
}
116+
117+
// BEGIN FileLib.swift
118+
import XLogging
119+
120+
public struct MyLib: Loggable {
121+
public var verbosity: Int {
122+
return 3
123+
}
124+
}
125+
public func start() -> XLogging.Loggable? {
126+
return XLogging.setup()
127+
}
128+
129+
public func end(_ arg: XLogging.Logger) {
130+
}

test/Frontend/module-alias-sil.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)