Skip to content

Commit b6a58cd

Browse files
authored
Name translation: Allow type name translation when cursor points to constructor call. rdar://33163114 (#10872) (#10886)
1 parent 5ed6c42 commit b6a58cd

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

test/SourceKit/NameTranslation/basic.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class MyDerived: FooClassDerived {
2727
// REQUIRES: objc_interop
2828
// RUN: %sourcekitd-test -req=translate -objc-name FooClassDerived2 -pos=5:30 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK1 %s
2929
// RUN: %sourcekitd-test -req=translate -objc-selector FooClassDerived2 -pos=3:23 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK11 %s
30+
// RUN: %sourcekitd-test -req=translate -objc-name FooClassDerived2 -pos=3:23 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK11-1 %s
3031
// RUN: %sourcekitd-test -req=translate -objc-name fooProperty2 -pos=6:16 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK2 %s
3132
// RUN: %sourcekitd-test -req=translate -objc-selector fooInstanceFunc1 -pos=7:16 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK3 %s
3233
// RUN: %sourcekitd-test -req=translate -objc-selector fooInstanceFunc1: -pos=7:16 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK3 %s
@@ -66,6 +67,7 @@ class MyDerived: FooClassDerived {
6667
// CHECK9: fooProperty11
6768
// CHECK10: init(nstanceFunc21:)
6869
// CHECK11: init(lassDerived2:)
70+
// CHECK11-1: FooClassDerived2
6971
// CHECK12: init(float2:)
7072
// CHECK13: init(_:)
7173
// CHECK14: init

test/SourceKit/NameTranslation/swiftnames.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class C3: NSObject {
7474

7575
// RUN: %sourcekitd-test -req=translate -swift-name "init(a1:b2:)" -pos=10:16 %s -- -F %S/Inputs/mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK10 %s
7676
// RUN: %sourcekitd-test -req=translate -swift-name "init(_:_:)" -pos=10:16 %s -- -F %S/Inputs/mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK11 %s
77+
// RUN: %sourcekitd-test -req=translate -swift-name "C11" -pos=10:16 %s -- -F %S/Inputs/mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK9 %s
7778
// RUN: %sourcekitd-test -req=translate -swift-name "foo(a1:_:)" -pos=10:16 %s -- -F %S/Inputs/mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK12 %s
7879

7980
// RUN: %sourcekitd-test -req=translate -swift-name "A2" -pos=27:10 %s -- -F %S/Inputs/mock-sdk -I %t.tmp %mcp_opt %s | %FileCheck -check-prefix=CHECK13 %s

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,18 @@ static DeclName getSwiftDeclName(const ValueDecl *VD,
952952
}
953953

954954
/// Returns true for failure to resolve.
955-
static bool passNameInfoForDecl(const ValueDecl *VD, NameTranslatingInfo &Info,
955+
static bool passNameInfoForDecl(SemaToken SemaTok, NameTranslatingInfo &Info,
956956
std::function<void(const NameTranslatingInfo &)> Receiver) {
957+
auto *VD = SemaTok.ValueD;
958+
959+
// If the given name is not a function name, and the cursor points to
960+
// a contructor call, we use the type declaration instead of the init
961+
// declaration to translate the name.
962+
if (Info.ArgNames.empty() && !Info.IsZeroArgSelector) {
963+
if (auto *TD = SemaTok.CtorTyRef) {
964+
VD = TD;
965+
}
966+
}
957967
switch (SwiftLangSupport::getNameKindForUID(Info.NameKind)) {
958968
case NameKind::Swift: {
959969
NameTranslatingInfo Result;
@@ -993,6 +1003,7 @@ static bool passNameInfoForDecl(const ValueDecl *VD, NameTranslatingInfo &Info,
9931003

9941004
const clang::NamedDecl *Named = nullptr;
9951005
auto *BaseDecl = VD;
1006+
9961007
while (!Named && BaseDecl) {
9971008
Named = dyn_cast_or_null<clang::NamedDecl>(BaseDecl->getClangDecl());
9981009
BaseDecl = BaseDecl->getOverriddenDecl();
@@ -1273,7 +1284,7 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
12731284
return;
12741285

12751286
case SemaTokenKind::ValueRef: {
1276-
bool Failed = passNameInfoForDecl(SemaTok.ValueD, Input, Receiver);
1287+
bool Failed = passNameInfoForDecl(SemaTok, Input, Receiver);
12771288
if (Failed) {
12781289
if (!getPreviousASTSnaps().empty()) {
12791290
// Attempt again using the up-to-date AST.
@@ -1502,10 +1513,8 @@ getNameInfo(StringRef InputFile, unsigned Offset, NameTranslatingInfo &Input,
15021513
if (Entity.Mod) {
15031514
// Module is ignored
15041515
} else {
1505-
NameTranslatingInfo NewInput = Input;
15061516
// FIXME: Should pass the main module for the interface but currently
15071517
// it's not necessary.
1508-
passNameInfoForDecl(Entity.Dcl, NewInput, Receiver);
15091518
}
15101519
} else {
15111520
Receiver({});

0 commit comments

Comments
 (0)