Skip to content

Commit 3dc5ae3

Browse files
committed
SourceKit: Use Demangle::getTypeDeclForUSR() to implement 'CursorInfo for USR' request
Fixes <rdar://problem/47819208>.
1 parent c42c98c commit 3dc5ae3

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

test/SourceKit/CursorInfo/cursor_usr.swift

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import Foo
33
import FooSwiftModule
44

5-
var global: Int
6-
75
struct S1 {}
86

97
func foo(x: FooStruct1) -> S1 {}
@@ -13,18 +11,18 @@ func foo(x: FooStruct1) -> S1 {}
1311
// RUN: %swiftc_driver -emit-module -o %t/FooSwiftModule.swiftmodule %S/Inputs/FooSwiftModule.swift
1412

1513
// Sanity check that we have identical responses when things work.
16-
// RUN: %sourcekitd-test -req=cursor -pos=5:5 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s > %t.from_offset.txt
17-
// RUN: %sourcekitd-test -req=cursor -usr "s:10cursor_usr6globalSivp" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s > %t.from_usr.txt
14+
// RUN: %sourcekitd-test -req=cursor -pos=5:8 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s > %t.from_offset.txt
15+
// RUN: %sourcekitd-test -req=cursor -usr "s:10cursor_usr2S1V" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s > %t.from_usr.txt
1816
// RUN: %FileCheck %s -check-prefix=CHECK_SANITY1 < %t.from_offset.txt
1917
// RUN: %FileCheck %s -check-prefix=CHECK_SANITY1 < %t.from_usr.txt
2018
// RUN: diff -u %t.from_usr.txt %t.from_offset.txt
21-
// CHECK_SANITY1: source.lang.swift.decl.var.global (5:5-5:11)
22-
// CHECK_SANITY1-NEXT: global
23-
// CHECK_SANITY1-NEXT: s:10cursor_usr6globalSiv
24-
// CHECK_SANITY1-NEXT: Int
25-
// CHECK_SANITY1-NEXT: $sSiD
26-
// CHECK_SANITY1-NEXT: <Declaration>var global: <Type usr="s:Si">Int</Type></Declaration>
27-
// CHECK_SANITY1-NEXT: <decl.var.global><syntaxtype.keyword>var</syntaxtype.keyword> <decl.name>global</decl.name>: <decl.var.type><ref.struct usr="s:Si">Int</ref.struct></decl.var.type></decl.var.global>
19+
// CHECK_SANITY1: source.lang.swift.decl.struct (5:8-5:10)
20+
// CHECK_SANITY1-NEXT: S1
21+
// CHECK_SANITY1-NEXT: s:10cursor_usr2S1
22+
// CHECK_SANITY1-NEXT: S1.Type
23+
// CHECK_SANITY1-NEXT: $s10cursor_usr2S1VmD
24+
// CHECK_SANITY1-NEXT: <Declaration>struct S1</Declaration>
25+
// CHECK_SANITY1-NEXT: <decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>S1</decl.name></decl.struct>
2826

2927
// Bogus USR.
3028
// RUN: %sourcekitd-test -req=cursor -usr "s:blahblahblah" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s | %FileCheck %s -check-prefix=EMPTY
@@ -33,26 +31,8 @@ func foo(x: FooStruct1) -> S1 {}
3331
// FIXME: no support for clang USRs.
3432
// RUN: %sourcekitd-test -req=cursor -usr "c:@S@FooStruct1" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s | %FileCheck %s -check-prefix=EMPTY
3533
// EMPTY: <empty cursor info>
36-
37-
// FIXME: missing symbol shows up as some other part of the USR (the type here).
38-
// RUN: %sourcekitd-test -req=cursor -usr "s:10cursor_usr11global_noneSivp" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s | %FileCheck %s -check-prefix=SHOULD_BE_EMPTY
39-
// SHOULD_BE_EMPTY: source.lang.swift.decl.struct ()
40-
// SHOULD_BE_EMPTY: Int
41-
4234
// RUN: %sourcekitd-test -req=cursor -usr "s:10cursor_usr2S1V" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s | %FileCheck %s -check-prefix=CHECK1
43-
// CHECK1: source.lang.swift.decl.struct (7:8-7:10)
35+
// CHECK1: source.lang.swift.decl.struct (5:8-5:10)
4436
// CHECK1: s1
4537
// CHECK1: <decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>S1</decl.name></decl.struct>
4638

47-
// RUN: %sourcekitd-test -req=cursor -usr "s:14FooSwiftModule03fooB4FuncSiyF" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s | %FileCheck %s -check-prefix=CHECK2
48-
// CHECK2: source.lang.swift.decl.function.free ()
49-
// CHECK2: fooSwiftFunc()
50-
// CHECK2: () -> Int
51-
// CHECK2: FooSwiftModule
52-
// CHECK2: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>fooSwiftFunc</decl.name>() -&gt; <decl.function.returntype><ref.struct usr="s:Si">Int</ref.struct></decl.function.returntype></decl.function.free>
53-
54-
// RUN: %sourcekitd-test -req=cursor -usr "s:10cursor_usr3fooyAA2S1VSC10FooStruct1VF" %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %mcp_opt %s | %FileCheck %s -check-prefix=CHECK3
55-
// CHECK3: source.lang.swift.decl.function.free (9:6-9:24)
56-
// CHECK3: foo(x:)
57-
// CHECK3: (FooStruct1) -> S1
58-
// CHECK3: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>x</decl.var.parameter.argument_label>: <decl.var.parameter.type><ref.struct usr="c:@S@FooStruct1">FooStruct1</ref.struct></decl.var.parameter.type></decl.var.parameter>) -&gt; <decl.function.returntype><ref.struct usr="s:10cursor_usr2S1V">S1</ref.struct></decl.function.returntype></decl.function.free>

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "SourceKit/Support/ImmutableTextBuffer.h"
1717
#include "SourceKit/Support/Logging.h"
1818

19+
#include "swift/AST/ASTDemangler.h"
1920
#include "swift/AST/ASTPrinter.h"
2021
#include "swift/AST/Decl.h"
2122
#include "swift/AST/NameLookup.h"
@@ -1724,8 +1725,7 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
17241725
}
17251726

17261727
auto &context = CompIns.getASTContext();
1727-
std::string error;
1728-
Decl *D = ide::getDeclFromUSR(context, USR, error);
1728+
TypeDecl *D = Demangle::getTypeDeclForUSR(context, USR);
17291729

17301730
if (!D) {
17311731
Receiver(CursorInfoData());
@@ -1738,15 +1738,15 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
17381738
if (auto *M = dyn_cast<ModuleDecl>(D)) {
17391739
passCursorInfoForModule(M, Lang.getIFaceGenContexts(), CompInvok,
17401740
Receiver);
1741-
} else if (auto *VD = dyn_cast<ValueDecl>(D)) {
1742-
auto *DC = VD->getDeclContext();
1741+
} else {
1742+
auto *DC = D->getDeclContext();
17431743
Type selfTy;
17441744
if (DC->isTypeContext()) {
17451745
selfTy = DC->getSelfInterfaceType();
1746-
selfTy = VD->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
1746+
selfTy = D->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
17471747
}
17481748
bool Failed =
1749-
passCursorInfoForDecl(/*SourceFile*/nullptr, VD, MainModule, selfTy,
1749+
passCursorInfoForDecl(/*SourceFile*/nullptr, D, MainModule, selfTy,
17501750
/*IsRef=*/false, false, ResolvedCursorInfo(),
17511751
BufferID, SourceLoc(), {}, Lang, CompInvok,
17521752
PreviousASTSnaps, Receiver);

0 commit comments

Comments
 (0)