Skip to content

[SourceKit][CursorInfo] Add a field for the source language the symbol was originally defined in #36486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions test/SourceKit/CursorInfo/cursor_info.swift

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions test/SourceKit/CursorInfo/cursor_info_source_lang.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foo
import Module

class FromSwift {}
@objc class StillFromSwift {}
func test(fromObjC: FooClassDerived,
_: FromSwiftMod,
_: FromSwiftModObjC) {}

// SWIFT: source.lang.swift{{$}}
// OBJC: source.lang.objc{{$}}

// REQUIRES: objc_interop
// RUN: %empty-directory(%t)
// RUN: echo '// swift-interface-format-version: 1.0' > %t/Module.swiftinterface
// RUN: echo '// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name Module' >> %t/Module.swiftinterface
// RUN: echo ' public class FromSwiftMod {}' >> %t/Module.swiftinterface
// RUN: echo ' @objc public class FromSwiftModObjC {}' >> %t/Module.swiftinterface

// RUN: %sourcekitd-test -req=cursor -pos=4:7 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %s | %FileCheck -check-prefix=SWIFT %s
// RUN: %sourcekitd-test -req=cursor -pos=5:13 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %s | %FileCheck -check-prefix=SWIFT %s
// RUN: %sourcekitd-test -req=cursor -pos=6:21 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %s | %FileCheck -check-prefix=OBJC %s
// RUN: %sourcekitd-test -req=cursor -pos=7:14 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %s | %FileCheck -check-prefix=SWIFT %s
// RUN: %sourcekitd-test -req=cursor -pos=8:14 %s -- -I %t -F %S/../Inputs/libIDE-mock-sdk %s | %FileCheck -check-prefix=SWIFT %s
2 changes: 2 additions & 0 deletions test/SourceKit/CursorInfo/cursor_opaque_result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func test(value: MyStruct) {

// OPAQUE: foo()
// OPAQUE-NEXT: s:8MyModule0A6StructV3fooQryF
// OPAQUE-NEXT: source.lang.swift
// OPAQUE-NEXT: (MyStruct) -> () -> some Comparable
// OPAQUE-NEXT: $sQrycD
// OPAQUE-NEXT: <Container>$s8MyModule0A6StructVD</Container>
Expand All @@ -28,6 +29,7 @@ func test(value: MyStruct) {

// ASSOC: bar()
// ASSOC-NEXT: s:8MyModule1PPAAE3bar5AssocQzyF
// ASSOC-NEXT: source.lang.swift
// ASSOC-NEXT: <Self where Self : P> (Self) -> () -> Self.Assoc
// ASSOC-NEXT: $s5AssocQzycD
// ASSOC-NEXT: <Container>$s8MyModule0A6StructVD</Container>
Expand Down
3 changes: 3 additions & 0 deletions test/SourceKit/CursorInfo/cursor_some_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func test(value: S) {
// DECLSITE: source.lang.swift.decl.function.method.instance (13:15-13:27)
// DECLSITE-NEXT: foo(x:)
// DECLSITE-NEXT: s:4Test1SV3foo1xQrx_tlF
// DECLSITE-NEXT: source.lang.swift
// DECLSITE-NEXT: <T> (S) -> (T) -> some Base & Proto
// DECLSITE-NEXT: $s1xQrx_tcluD
// DECLSITE-NEXT: <Declaration>public func foo&lt;T&gt;(x: <Type usr=[[T_USR:.*]]>T</Type>) -&gt; some <Type usr=[[Base_USR:.*]]>Base</Type> &amp; <Type usr=[[Proto_USR:.*]]>Proto</Type></Declaration>
Expand All @@ -32,6 +33,7 @@ func test(value: S) {
// PROTO_AFTER_SOME: source.lang.swift.ref.protocol (1:17-1:22)
// PROTO_AFTER_SOME-NEXT: Proto
// PROTO_AFTER_SOME-NEXT: s:4Test5ProtoP
// PROTO_AFTER_SOME-NEXT: source.lang.swift
// PROTO_AFTER_SOME-NEXT: Proto.Protocol
// PROTO_AFTER_SOME-NEXT: $s4Test5Proto_pmD
// PROTO_AFTER_SOME-NEXT: <Declaration>public protocol Proto</Declaration>
Expand All @@ -41,6 +43,7 @@ func test(value: S) {
// USESITE: source.lang.swift.ref.function.method.instance (13:15-13:27)
// USESITE-NEXT: foo(x:)
// USESITE-NEXT: s:4Test1SV3foo1xQrx_tlF
// USESITE-NEXT: source.lang.swift
// USESITE-NEXT: <T> (S) -> (T) -> some Base & Proto
// USESITE-NEXT: $s1xQrx_tcluD
// USESITE-NEXT: <Container>$s4Test1SVD</Container>
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/CursorInfo/cursor_stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func foo3(a: Float, b: Bool) {}
// CHECK-OVERLAY: source.lang.swift.ref.var.global
// CHECK-OVERLAY-NEXT: NSUTF8StringEncoding
// CHECK-OVERLAY-NEXT: s:10Foundation20NSUTF8StringEncodingSuv
// CHECK-OVERLAY-NEXT: source.lang.swift
// CHECK-OVERLAY-NEXT: UInt
// CHECK-OVERLAY-NEXT: $sSuD
// CHECK-OVERLAY-NEXT: Foundation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func test() {
// CHECK: source.lang.swift.ref.function.free ()
// CHECK-NEXT: someFunc()
// CHECK-NEXT: s:10SomeModule8someFuncyyF
// CHECK-NEXT: source.lang.swift
// CHECK-NEXT: () -> ()
// CHECK-NEXT: $syycD
// CHECK-NEXT: SomeModule
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/CursorInfo/cursor_usr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func foo(x: FooStruct1) -> S1 {}
// CHECK_SANITY1: source.lang.swift.decl.struct (5:8-5:10)
// CHECK_SANITY1-NEXT: S1
// CHECK_SANITY1-NEXT: s:10cursor_usr2S1
// CHECK_SANITY1-NEXT: source.lang.swift
// CHECK_SANITY1-NEXT: S1.Type
// CHECK_SANITY1-NEXT: $s10cursor_usr2S1VmD
// CHECK_SANITY1-NEXT: <Declaration>struct S1</Declaration>
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/CursorInfo/rdar_34348776.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public typealias Aliased = Alias
// CHECK: source.lang.swift.decl.typealias (3:18-3:25)
// CHECK-NEXT: Aliased
// CHECK-NEXT: s:13rdar_343487767Aliaseda
// CHECK-NEXT: source.lang.swift
// CHECK-NEXT: Alias.Type
// CHECK-NEXT: $s13rdar_343487765AliasamD
// CHECK-NEXT: <Declaration>public typealias Aliased = <Type usr="s:13rdar_343487765Aliasa">Alias</Type></Declaration>
Expand Down
7 changes: 7 additions & 0 deletions test/SourceKit/CursorInfo/result_builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// ATTR_NAME: source.lang.swift.ref.struct (19:8-19:21)
// ATTR_NAME-NEXT: TaggedBuilder
// ATTR_NAME-NEXT: s:11BuilderTest06TaggedA0V
// ATTR_NAME-NEXT: source.lang.swift
// ATTR_NAME-NEXT: TaggedBuilder<Tag>.Type
// ATTR_NAME-NEXT: $s11BuilderTest06TaggedA0VyxGmD
// ATTR_NAME-NEXT: <Declaration>@resultBuilder struct TaggedBuilder&lt;Tag&gt;</Declaration>
Expand All @@ -58,6 +59,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// ATTR_GENERICARG: source.lang.swift.ref.enum (31:6-31:11)
// ATTR_GENERICARG-NEXT: Color
// ATTR_GENERICARG-NEXT: s:11BuilderTest5ColorO
// ATTR_GENERICARG-NEXT: source.lang.swift
// ATTR_GENERICARG-NEXT: Color.Type
// ATTR_GENERICARG-NEXT: $s11BuilderTest5ColorOmD
// ATTR_GENERICARG-NEXT: <Declaration>enum Color</Declaration>
Expand All @@ -68,6 +70,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// CALL_BUILDERFUNC: source.lang.swift.ref.function.free (35:6-35:78)
// CALL_BUILDERFUNC-NEXT: acceptColorTagged(body:)
// CALL_BUILDERFUNC-NEXT: s:11BuilderTest17acceptColorTagged4bodyyxAA0D0OXE_tlF
// CALL_BUILDERFUNC-NEXT: source.lang.swift
// CALL_BUILDERFUNC-NEXT: <Result> (body: (Color) -> Result) -> ()
// CALL_BUILDERFUNC-NEXT: $s4bodyyx11BuilderTest5ColorOXE_tcluD
// CALL_BUILDERFUNC-NEXT: <Declaration>func acceptColorTagged&lt;Result&gt;(@<Type usr="s:11BuilderTest06TaggedA0V">TaggedBuilder</Type>&lt;<Type usr="s:11BuilderTest5ColorO">Color</Type>&gt; body: (<Type usr="s:11BuilderTest5ColorO">Color</Type>) -&gt; <Type usr="s:11BuilderTest17acceptColorTagged4bodyyxAA0D0OXE_tlF6ResultL_xmfp">Result</Type>)</Declaration>
Expand All @@ -78,6 +81,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// CLOSUREPARAM_DECL: source.lang.swift.decl.var.parameter (40:23-40:28)
// CLOSUREPARAM_DECL-NEXT: color
// CLOSUREPARAM_DECL-NEXT: s:11BuilderTest21testAcceptColorTagged1i1sySi_SStFAA0F0VyAA0E0OSiG_AFyAHSSGtAHXEfU_5colorL_AHvp
// CLOSUREPARAM_DECL-NEXT: source.lang.swift
// CLOSUREPARAM_DECL-NEXT: Color
// CLOSUREPARAM_DECL-NEXT: $s11BuilderTest5ColorOD
// CLOSUREPARAM_DECL-NEXT: <Declaration>let color: <Type usr="s:11BuilderTest5ColorO">Color</Type></Declaration>
Expand All @@ -88,6 +92,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// CLOSUREPARAM_USER: source.lang.swift.ref.var.local (40:23-40:28)
// CLOSUREPARAM_USER-NEXT: color
// CLOSUREPARAM_USER-NEXT: s:11BuilderTest21testAcceptColorTagged1i1sySi_SStFAA0F0VyAA0E0OSiG_AFyAHSSGtAHXEfU_5colorL_AHvp
// CLOSUREPARAM_USER-NEXT: source.lang.swift
// CLOSUREPARAM_USER-NEXT: Color
// CLOSUREPARAM_USER-NEXT: $s11BuilderTest5ColorOD
// CLOSUREPARAM_USER-NEXT: <Declaration>let color: <Type usr="s:11BuilderTest5ColorO">Color</Type></Declaration>
Expand All @@ -98,6 +103,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// CAPTURED_VALUE: source.lang.swift.ref.var.local (39:28-39:29)
// CAPTURED_VALUE-NEXT: i
// CAPTURED_VALUE-NEXT: s:11BuilderTest21testAcceptColorTagged1i1sySi_SStFACL_Sivp
// CAPTURED_VALUE-NEXT: source.lang.swift
// CAPTURED_VALUE-NEXT: Int
// CAPTURED_VALUE-NEXT: $sSiD
// CAPTURED_VALUE-NEXT: <Declaration>let i: <Type usr="s:Si">Int</Type></Declaration>
Expand All @@ -108,6 +114,7 @@ func testAcceptColorTagged(i: Int, s: String) {
// CAPTURED_VALUE_METHOD: source.lang.swift.ref.function.method.instance (10:8-10:28)
// CAPTURED_VALUE_METHOD: tag(_:)
// CAPTURED_VALUE_METHOD: s:11BuilderTest8TaggablePAAE3tagyAA6TaggedVyqd__xGqd__lF
// CAPTURED_VALUE_METHOD: source.lang.swift
// CAPTURED_VALUE_METHOD: <Self, Tag where Self : Taggable> (Self) -> (Tag) -> Tagged<Tag, Self>
// CAPTURED_VALUE_METHOD: $sy11BuilderTest6TaggedVyqd__xGqd__cluD
// CAPTURED_VALUE_METHOD: <Container>$sSiD</Container>
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/InterfaceGen/gen_stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var x: Int
// CHECK1: source.lang.swift.ref.struct ()
// CHECK1-NEXT: Int
// CHECK1-NEXT: s:Si
// CHECK1-NEXT: source.lang.swift
// CHECK1-NEXT: Int.Type
// CHECK1-NEXT: $s
// CHECK1-NEXT: Swift{{$}}
Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ struct CursorInfoData {
StringRef InternalDiagnostic;

UIdent Kind;
UIdent DeclarationLang;
StringRef Name;
StringRef USR;
StringRef TypeName;
Expand Down
6 changes: 6 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ void SwiftLangSupport::dependencyUpdated() {
CompletionInst->markCachedCompilerInstanceShouldBeInvalidated();
}

UIdent SwiftLangSupport::getUIDForDeclLanguage(const swift::Decl *D) {
if (D->hasClangNode())
return KindObjC;
return KindSwift;
}

UIdent SwiftLangSupport::getUIDForDecl(const Decl *D, bool IsRef) {
return UIdentVisitor(IsRef).visit(const_cast<Decl*>(D));
}
Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class SwiftLangSupport : public LangSupport {
getFileSystem(const Optional<VFSOptions> &vfsOptions,
Optional<StringRef> primaryFile, std::string &error);

static SourceKit::UIdent getUIDForDeclLanguage(const swift::Decl *D);
static SourceKit::UIdent getUIDForDecl(const swift::Decl *D,
bool IsRef = false);
static SourceKit::UIdent getUIDForExtensionOfDecl(const swift::Decl *D);
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ static bool passCursorInfoForDecl(SourceFile* SF,
if (auto IFaceGenRef = Lang.getIFaceGenContexts().find(ModuleName, Invoc))
ModuleInterfaceName = IFaceGenRef->getDocumentName();

UIdent DeclLang = SwiftLangSupport::getUIDForDeclLanguage(VD);
UIdent Kind = SwiftLangSupport::getUIDForDecl(VD, IsRef);
StringRef Name = StringRef(SS.begin()+NameBegin, NameEnd-NameBegin);
StringRef USR = StringRef(SS.begin()+USRBegin, USREnd-USRBegin);
Expand Down Expand Up @@ -1040,6 +1041,7 @@ static bool passCursorInfoForDecl(SourceFile* SF,
std::string TypeInterface;

CursorInfoData Info;
Info.DeclarationLang = DeclLang;
Info.Kind = Kind;
Info.Name = Name;
Info.USR = USR;
Expand Down
8 changes: 8 additions & 0 deletions tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,12 @@ static void printCursorInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
KeyModuleName);
const char *GroupName = sourcekitd_variant_dictionary_get_string(Info,
KeyGroupName);

sourcekitd_uid_t LangUID =
sourcekitd_variant_dictionary_get_uid(Info, KeyDeclarationLang);
const char *DeclLang = nullptr;
if (LangUID)
DeclLang = sourcekitd_uid_get_string_ptr(LangUID);

const char *LocalizationKey =
sourcekitd_variant_dictionary_get_string(Info, KeyLocalizationKey);
Expand Down Expand Up @@ -1693,6 +1699,8 @@ static void printCursorInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
OS << Name << '\n';
if (USR)
OS << USR << '\n';
if (DeclLang)
OS << DeclLang << "\n";
if (Typename)
OS << Typename << '\n';
if (TypeUsr)
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,8 @@ static void reportCursorInfo(const RequestResult<CursorInfoData> &Result,
auto Elem = RespBuilder.getDictionary();
Elem.set(KeyKind, Info.Kind);
Elem.set(KeyName, Info.Name);
if (Info.DeclarationLang.isValid())
Elem.set(KeyDeclarationLang, Info.DeclarationLang);
if (!Info.USR.empty())
Elem.set(KeyUSR, Info.USR);
if (!Info.TypeName.empty())
Expand Down
3 changes: 3 additions & 0 deletions utils/gyb_sourcekit_support/UIDs.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def __init__(self, internal_name, external_name):
KEY('AnnotatedTypename', 'key.annotated.typename'),
KEY('CompileOperation', 'key.compile_operation'),
KEY('EffectiveAccess', 'key.effective_access'),
KEY('DeclarationLang', 'key.decl_lang'),
]


Expand Down Expand Up @@ -452,4 +453,6 @@ def __init__(self, internal_name, external_name):
KIND('SyntaxTreeOff', 'source.syntaxtree.transfer.off'),
KIND('SyntaxTreeIncremental', 'source.syntaxtree.transfer.incremental'),
KIND('SyntaxTreeFull', 'source.syntaxtree.transfer.full'),
KIND('Swift', 'source.lang.swift'),
KIND('ObjC', 'source.lang.objc'),
]