Move getRealImportPath to cpp

Remove unnecessary setter
Update tests
This commit is contained in:
Ellie Shin
2021-11-09 14:33:44 -08:00
parent d3e18f919a
commit e2631c9ca8
4 changed files with 46 additions and 42 deletions

View File

@@ -1143,9 +1143,6 @@ class ImportDecl final : public Decl,
ImportDecl(DeclContext *DC, SourceLoc ImportLoc, ImportKind K,
SourceLoc KindLoc, ImportPath Path);
// Sets the real module name corresponding to this import decl in
// case module aliasing is used. Called in \c ImportDecl::create.
void setRealModuleName(Identifier name) { RealModuleName = name; };
public:
static ImportDecl *create(ASTContext &C, DeclContext *DC,
SourceLoc ImportLoc, ImportKind Kind,
@@ -1193,23 +1190,7 @@ public:
/// '-module-alias Foo=Bar', this import path will include 'Bar'. This
/// return value may be owned by \p scratch, so it should not be used
/// after \p scratch is destroyed.
ImportPath getRealImportPath(ImportPath::Builder &scratch) const {
assert(scratch.empty() && "non-empty scratch ImportPath::Builder?");
auto path = getImportPath();
if (RealModuleName.empty())
return path;
for (auto elem : path) {
if (scratch.empty()) {
// Add the real module name instead of its alias
scratch.push_back(RealModuleName);
} else {
// Add the rest if any (access path elements)
scratch.push_back(elem.Item);
}
}
return scratch.get();
}
ImportPath getRealImportPath(ImportPath::Builder &scratch) const;
/// Retrieves the part of the import path that contains the module name,
/// as written in the source code.
@@ -1238,7 +1219,6 @@ public:
/// after \p scratch is destroyed.
ImportPath::Module getRealModulePath(ImportPath::Builder &scratch) const {
return getRealImportPath(scratch).getModulePath(getImportKind());
return getImportPath().getModulePath(getImportKind());
}
ImportPath::Access getAccessPath() const {

View File

@@ -1066,8 +1066,10 @@ ImportDecl *ImportDecl::create(ASTContext &Ctx, DeclContext *DC,
auto D = new (ptr) ImportDecl(DC, ImportLoc, Kind, KindLoc, Path);
if (ClangN)
D->setClangNode(ClangN);
auto realName = Ctx.getRealModuleName(Path.front().Item);
D->setRealModuleName(realName);
auto realNameIfExists = Ctx.getRealModuleName(Path.front().Item), ModuleAliasLookupOption::realFromAlias);
if (!realNameIfExists.empty()) {
D->RealModuleName = realNameIfExists;
}
return D;
}
@@ -1160,6 +1162,24 @@ ImportDecl::findBestImportKind(ArrayRef<ValueDecl *> Decls) {
return FirstKind;
}
ImportPath ImportDecl::getRealImportPath(ImportPath::Builder &scratch) const {
assert(scratch.empty() && "scratch ImportPath::Builder must be initially empty");
auto path = getImportPath();
if (RealModuleName.empty())
return path;
for (auto elem : path) {
if (scratch.empty()) {
// Add the real module name instead of its alias
scratch.push_back(RealModuleName);
} else {
// Add the rest if any (access path elements)
scratch.push_back(elem.Item);
}
}
return scratch.get();
}
ArrayRef<ValueDecl *> ImportDecl::getDecls() const {
// If this isn't a scoped import, there's nothing to do.
if (getImportKind() == ImportKind::Module)

View File

@@ -9,12 +9,17 @@
// RUN: test -f %t/AppleLogging.swiftmodule
/// Verify emitted imported modules contains AppleLogging as a module name
// RUN: %target-swift-frontend -emit-imported-modules %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t > %t/result.output
// RUN: %target-swift-frontend -emit-imported-modules %t/FileLib1.swift -module-alias XLogging=AppleLogging -I %t > %t/result1.output
// RUN: %FileCheck %s -input-file %t/result.output -check-prefix CHECK-AST
// CHECK-AST: AppleLogging
// RUN: not %FileCheck %s -input-file %t/result.output -check-prefix CHECK-NOT-AST
// CHECK-NOT-AST: XLogging
// RUN: %FileCheck %s -input-file %t/result1.output -check-prefix CHECK-AST1
// CHECK-AST1-NOT: XLogging
// CHECK-AST1: AppleLogging
// RUN: %target-swift-frontend -emit-imported-modules %t/FileLib2.swift -module-alias XLogging=AppleLogging -I %t > %t/result2.output
// RUN: %FileCheck %s -input-file %t/result2.output -check-prefix CHECK-AST2
// CHECK-AST2-NOT: XLogging
// CHECK-AST2: AppleLogging
// BEGIN FileLogging.swift
@@ -25,7 +30,7 @@ public func setup() -> XLogging.Logger? {
return Logger()
}
// BEGIN FileLib.swift
// BEGIN FileLib1.swift
import XLogging
public func start() -> XLogging.Logger? {
@@ -35,3 +40,9 @@ public func start() -> XLogging.Logger? {
public func end(_ arg: XLogging.Logger) {
}
// BEGIN FileLib2.swift
import struct XLogging.Logger
public func start() -> XLogging.Logger? {
return XLogging.Logger()
}

View File

@@ -1,4 +1,3 @@
/// Test various import attributes with module aliasing.
///
/// Module 'Lib' imports module 'XLogging' via module aliasing and with various import attributes.
@@ -24,9 +23,8 @@
// RUN: test -f %t/Lib2.swiftmodule
// RUN: llvm-bcanalyzer -dump %t/Lib2.swiftmodule > %t/Lib2.dump.txt
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME %s < %t/Lib2.dump.txt
// CHECK-REAL-NAME-NOT: XLogging
// CHECK-REAL-NAME: AppleLogging
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS %s < %t/Lib2.dump.txt
// CHECK-ALIAS: XLogging
/// Test @_implementationOnly: Should fail
// 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 @@
// RUN: %target-swift-frontend -module-name Lib4 %t/FileLib4.swift -module-alias XLogging=AppleLoggingEnablePrivate -I %t -emit-module -emit-module-path %t/Lib4.swiftmodule
// RUN: llvm-bcanalyzer -dump %t/Lib4.swiftmodule > %t/Lib4.dump.txt
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME4 %s < %t/Lib4.dump.txt
// CHECK-REAL-NAME4-NOT: XLogging
// CHECK-REAL-NAME4: AppleLoggingEnablePrivate
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS4 %s < %t/Lib4.dump.txt
// CHECK-ALIAS4: XLogging
/// Test @testable: Should pass
@@ -56,25 +53,22 @@
// RUN: %target-swift-frontend -module-name Lib5 %t/FileLib5.swift -module-alias XLogging=AppleLoggingEnableTesting -I %t -emit-module -emit-module-path %t/Lib5.swiftmodule
// RUN: llvm-bcanalyzer -dump %t/Lib5.swiftmodule > %t/Lib5.dump.txt
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME5 %s < %t/Lib5.dump.txt
// CHECK-REAL-NAME5-NOT: XLogging
// CHECK-REAL-NAME5: AppleLoggingEnableTesting
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS5 %s < %t/Lib5.dump.txt
// CHECK-ALIAS5: XLogging
/// Test import struct: Should pass with correct module name reference
// 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
// RUN: llvm-nm --defined-only %t/Lib6.o > %t/Lib6.dump.txt
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME6 %s < %t/Lib6.dump.txt
// CHECK-REAL-NAME6-NOT: XLogging
// CHECK-REAL-NAME6: s4Lib65start12AppleLogging6LoggerVyF
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS6 %s < %t/Lib6.dump.txt
// CHECK-ALIAS6: XLogging
/// Test canImport
// 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
// RUN: %FileCheck -check-prefix=CHECK-LOAD %s < %t/Lib7.dump.txt
// CHECK-LOAD-NOT: XLogging.swiftmodule
// CHECK-LOAD: AppleLogging.swiftmodule
// RUN: not %FileCheck -check-prefix=CHECK-NOT-LOAD %s < %t/Lib7.dump.txt
// CHECK-NOT-LOAD: XLogging.swiftmodule
/// Test @_exported: Should pass with correct module name reference
// 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 @@
// RUN: llvm-nm --defined-only %t/User.o > %t/User.dump.txt
// RUN: %FileCheck -check-prefix=CHECK-REAL-NAME-USER %s < %t/User.dump.txt
// CHECK-REAL-NAME-USER-NOT: XLogging
// CHECK-REAL-NAME-USER: s4User04MainA0V3use12AppleLogging6LoggerVyF
// RUN: not %FileCheck -check-prefix=CHECK-ALIAS-USER %s < %t/User.dump.txt
// CHECK-ALIAS-USER: XLogging
// BEGIN FileLogging.swift
public struct Logger {