Skip to content

Commit e2631c9

Browse files
committed
Move getRealImportPath to cpp
Remove unnecessary setter Update tests
1 parent d3e18f9 commit e2631c9

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,6 @@ class ImportDecl final : public Decl,
11431143

11441144
ImportDecl(DeclContext *DC, SourceLoc ImportLoc, ImportKind K,
11451145
SourceLoc KindLoc, ImportPath Path);
1146-
// Sets the real module name corresponding to this import decl in
1147-
// case module aliasing is used. Called in \c ImportDecl::create.
1148-
void setRealModuleName(Identifier name) { RealModuleName = name; };
11491146
public:
11501147
static ImportDecl *create(ASTContext &C, DeclContext *DC,
11511148
SourceLoc ImportLoc, ImportKind Kind,
@@ -1193,23 +1190,7 @@ class ImportDecl final : public Decl,
11931190
/// '-module-alias Foo=Bar', this import path will include 'Bar'. This
11941191
/// return value may be owned by \p scratch, so it should not be used
11951192
/// after \p scratch is destroyed.
1196-
ImportPath getRealImportPath(ImportPath::Builder &scratch) const {
1197-
assert(scratch.empty() && "non-empty scratch ImportPath::Builder?");
1198-
auto path = getImportPath();
1199-
if (RealModuleName.empty())
1200-
return path;
1201-
1202-
for (auto elem : path) {
1203-
if (scratch.empty()) {
1204-
// Add the real module name instead of its alias
1205-
scratch.push_back(RealModuleName);
1206-
} else {
1207-
// Add the rest if any (access path elements)
1208-
scratch.push_back(elem.Item);
1209-
}
1210-
}
1211-
return scratch.get();
1212-
}
1193+
ImportPath getRealImportPath(ImportPath::Builder &scratch) const;
12131194

12141195
/// Retrieves the part of the import path that contains the module name,
12151196
/// as written in the source code.
@@ -1238,7 +1219,6 @@ class ImportDecl final : public Decl,
12381219
/// after \p scratch is destroyed.
12391220
ImportPath::Module getRealModulePath(ImportPath::Builder &scratch) const {
12401221
return getRealImportPath(scratch).getModulePath(getImportKind());
1241-
return getImportPath().getModulePath(getImportKind());
12421222
}
12431223

12441224
ImportPath::Access getAccessPath() const {

lib/AST/Decl.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,8 +1066,10 @@ ImportDecl *ImportDecl::create(ASTContext &Ctx, DeclContext *DC,
10661066
auto D = new (ptr) ImportDecl(DC, ImportLoc, Kind, KindLoc, Path);
10671067
if (ClangN)
10681068
D->setClangNode(ClangN);
1069-
auto realName = Ctx.getRealModuleName(Path.front().Item);
1070-
D->setRealModuleName(realName);
1069+
auto realNameIfExists = Ctx.getRealModuleName(Path.front().Item), ModuleAliasLookupOption::realFromAlias);
1070+
if (!realNameIfExists.empty()) {
1071+
D->RealModuleName = realNameIfExists;
1072+
}
10711073
return D;
10721074
}
10731075

@@ -1160,6 +1162,24 @@ ImportDecl::findBestImportKind(ArrayRef<ValueDecl *> Decls) {
11601162
return FirstKind;
11611163
}
11621164

1165+
ImportPath ImportDecl::getRealImportPath(ImportPath::Builder &scratch) const {
1166+
assert(scratch.empty() && "scratch ImportPath::Builder must be initially empty");
1167+
auto path = getImportPath();
1168+
if (RealModuleName.empty())
1169+
return path;
1170+
1171+
for (auto elem : path) {
1172+
if (scratch.empty()) {
1173+
// Add the real module name instead of its alias
1174+
scratch.push_back(RealModuleName);
1175+
} else {
1176+
// Add the rest if any (access path elements)
1177+
scratch.push_back(elem.Item);
1178+
}
1179+
}
1180+
return scratch.get();
1181+
}
1182+
11631183
ArrayRef<ValueDecl *> ImportDecl::getDecls() const {
11641184
// If this isn't a scoped import, there's nothing to do.
11651185
if (getImportKind() == ImportKind::Module)

test/Frontend/module-alias-emit-imported-modules.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
// RUN: test -f %t/AppleLogging.swiftmodule
1010

1111
/// Verify emitted imported modules contains AppleLogging as a module name
12-
// RUN: %target-swift-frontend -emit-imported-modules %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t > %t/result.output
12+
// RUN: %target-swift-frontend -emit-imported-modules %t/FileLib1.swift -module-alias XLogging=AppleLogging -I %t > %t/result1.output
1313

14-
// RUN: %FileCheck %s -input-file %t/result.output -check-prefix CHECK-AST
15-
// CHECK-AST: AppleLogging
16-
// RUN: not %FileCheck %s -input-file %t/result.output -check-prefix CHECK-NOT-AST
17-
// CHECK-NOT-AST: XLogging
14+
// RUN: %FileCheck %s -input-file %t/result1.output -check-prefix CHECK-AST1
15+
// CHECK-AST1-NOT: XLogging
16+
// CHECK-AST1: AppleLogging
17+
18+
// RUN: %target-swift-frontend -emit-imported-modules %t/FileLib2.swift -module-alias XLogging=AppleLogging -I %t > %t/result2.output
19+
20+
// RUN: %FileCheck %s -input-file %t/result2.output -check-prefix CHECK-AST2
21+
// CHECK-AST2-NOT: XLogging
22+
// CHECK-AST2: AppleLogging
1823

1924

2025
// BEGIN FileLogging.swift
@@ -25,7 +30,7 @@ public func setup() -> XLogging.Logger? {
2530
return Logger()
2631
}
2732

28-
// BEGIN FileLib.swift
33+
// BEGIN FileLib1.swift
2934
import XLogging
3035

3136
public func start() -> XLogging.Logger? {
@@ -35,3 +40,9 @@ public func start() -> XLogging.Logger? {
3540
public func end(_ arg: XLogging.Logger) {
3641
}
3742

43+
// BEGIN FileLib2.swift
44+
import struct XLogging.Logger
45+
46+
public func start() -> XLogging.Logger? {
47+
return XLogging.Logger()
48+
}

test/Frontend/module-alias-import-attrs.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/// Test various import attributes with module aliasing.
32
///
43
/// Module 'Lib' imports module 'XLogging' via module aliasing and with various import attributes.
@@ -24,9 +23,8 @@
2423
// RUN: test -f %t/Lib2.swiftmodule
2524
// RUN: llvm-bcanalyzer -dump %t/Lib2.swiftmodule > %t/Lib2.dump.txt
2625
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME %s < %t/Lib2.dump.txt
26+
// CHECK-REAL-NAME-NOT: XLogging
2727
// CHECK-REAL-NAME: AppleLogging
28-
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS %s < %t/Lib2.dump.txt
29-
// CHECK-ALIAS: XLogging
3028

3129
/// Test @_implementationOnly: Should fail
3230
// RUN: not %target-swift-frontend -module-name Lib3 %t/FileLib3.swift -module-alias XLogging=AppleLogging -I %t -emit-module -emit-module-path %t/Lib3.swiftmodule 2> %t/result-Lib3.output
@@ -43,9 +41,8 @@
4341
// RUN: %target-swift-frontend -module-name Lib4 %t/FileLib4.swift -module-alias XLogging=AppleLoggingEnablePrivate -I %t -emit-module -emit-module-path %t/Lib4.swiftmodule
4442
// RUN: llvm-bcanalyzer -dump %t/Lib4.swiftmodule > %t/Lib4.dump.txt
4543
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME4 %s < %t/Lib4.dump.txt
44+
// CHECK-REAL-NAME4-NOT: XLogging
4645
// CHECK-REAL-NAME4: AppleLoggingEnablePrivate
47-
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS4 %s < %t/Lib4.dump.txt
48-
// CHECK-ALIAS4: XLogging
4946

5047
/// Test @testable: Should pass
5148

@@ -56,25 +53,22 @@
5653
// RUN: %target-swift-frontend -module-name Lib5 %t/FileLib5.swift -module-alias XLogging=AppleLoggingEnableTesting -I %t -emit-module -emit-module-path %t/Lib5.swiftmodule
5754
// RUN: llvm-bcanalyzer -dump %t/Lib5.swiftmodule > %t/Lib5.dump.txt
5855
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME5 %s < %t/Lib5.dump.txt
56+
// CHECK-REAL-NAME5-NOT: XLogging
5957
// CHECK-REAL-NAME5: AppleLoggingEnableTesting
60-
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS5 %s < %t/Lib5.dump.txt
61-
// CHECK-ALIAS5: XLogging
6258

6359
/// Test import struct: Should pass with correct module name reference
6460
// RUN: %target-swift-frontend -module-name Lib6 %t/FileLib6.swift -module-alias XLogging=AppleLogging -I %t -emit-module -emit-module-path %t/Lib6.swiftmodule -c -o %t/Lib6.o
6561
// RUN: llvm-nm --defined-only %t/Lib6.o > %t/Lib6.dump.txt
6662
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME6 %s < %t/Lib6.dump.txt
63+
// CHECK-REAL-NAME6-NOT: XLogging
6764
// CHECK-REAL-NAME6: s4Lib65start12AppleLogging6LoggerVyF
68-
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS6 %s < %t/Lib6.dump.txt
69-
// CHECK-ALIAS6: XLogging
7065

7166
/// Test canImport
7267
// RUN: %target-swift-frontend -module-name Lib7 %t/FileLib7.swift -module-alias XLogging=AppleLogging -I %t -emit-module -emit-module-path %t/Lib7.swiftmodule -Rmodule-loading 2> %t/Lib7.dump.txt
7368

7469
// RUN: %FileCheck -check-prefix=CHECK-LOAD %s < %t/Lib7.dump.txt
70+
// CHECK-LOAD-NOT: XLogging.swiftmodule
7571
// CHECK-LOAD: AppleLogging.swiftmodule
76-
// RUN: not %FileCheck -check-prefix=CHECK-NOT-LOAD %s < %t/Lib7.dump.txt
77-
// CHECK-NOT-LOAD: XLogging.swiftmodule
7872

7973
/// Test @_exported: Should pass with correct module name reference
8074
// RUN: %target-swift-frontend -module-name Lib %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t -emit-module -emit-module-path %t/Lib.swiftmodule
@@ -84,9 +78,8 @@
8478

8579
// RUN: llvm-nm --defined-only %t/User.o > %t/User.dump.txt
8680
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME-USER %s < %t/User.dump.txt
81+
// CHECK-REAL-NAME-USER-NOT: XLogging
8782
// CHECK-REAL-NAME-USER: s4User04MainA0V3use12AppleLogging6LoggerVyF
88-
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS-USER %s < %t/User.dump.txt
89-
// CHECK-ALIAS-USER: XLogging
9083

9184
// BEGIN FileLogging.swift
9285
public struct Logger {

0 commit comments

Comments
 (0)