Skip to content

Commit 06af749

Browse files
author
Simon Barinka
committed
[SourceKit] Report type decl info instead of hidden inits
1 parent b7ee6e4 commit 06af749

File tree

4 files changed

+70
-55
lines changed

4 files changed

+70
-55
lines changed

test/SourceKit/CursorInfo/cursor_info.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -814,38 +814,40 @@ var a: A = 42
814814
// CHECK93-NEXT: <decl.enumelement><syntaxtype.keyword>case</syntaxtype.keyword> <decl.name>b</decl.name> = <syntaxtype.string>&quot;f&quot;</syntaxtype.string></decl.enumelement>
815815

816816
// RUN: %sourcekitd-test -req=cursor -pos=227:14 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK94 %s
817-
// CHECK94: source.lang.swift.ref.function.constructor
818-
// CHECK94-NEXT: init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
819-
// CHECK94-NEXT: s:SS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfc
817+
// CHECK94: source.lang.swift.ref.struct
818+
// CHECK94-NEXT: String
819+
// CHECK94-NEXT: s:SS
820820
// CHECK94-NEXT: source.lang.swift
821-
// CHECK94-NEXT: (String.Type) -> (Builtin.RawPointer, Builtin.Word, Builtin.Int1) -> String
822-
// CHECK94-NEXT: $s21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcD
821+
// CHECK94-NEXT: String.Type
822+
// CHECK94-NEXT: $sSSmD
823823
// CHECK94-NEXT: Swift
824824
// CHECK94-NEXT: <Group>String</Group>
825825
// CHECK94-NEXT: SYSTEM
826+
// CHECK94-NEXT: <Declaration>@frozen @_eagerMove struct String</Declaration>
826827

827828
// RUN: %sourcekitd-test -req=cursor -pos=231:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK95 %s
828829
// CHECK95: <Declaration>func checkAnyIsAKeyword(x: Any)</Declaration>
829830
// CHECK95-NEXT: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>checkAnyIsAKeyword</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>x</decl.var.parameter.argument_label>: <decl.var.parameter.type><syntaxtype.keyword>Any</syntaxtype.keyword></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>
830831

831832
// RUN: %sourcekitd-test -req=cursor -pos=23:23 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK96 %s
832-
// CHECK96: source.lang.swift.ref.function.constructor
833-
// CHECK96-NEXT: init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
834-
// CHECK96-NEXT: s:SS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfc
833+
// CHECK96: source.lang.swift.ref.struct
834+
// CHECK96-NEXT: String
835+
// CHECK96-NEXT: s:SS
835836
// CHECK96-NEXT: source.lang.swift
836-
// CHECK96-NEXT: (String.Type) -> (Builtin.RawPointer, Builtin.Word, Builtin.Int1) -> String
837-
// CHECK96-NEXT: $s21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcD
837+
// CHECK96-NEXT: String.Type
838+
// CHECK96-NEXT: $sSSmD
838839
// CHECK96-NEXT: Swift
839840
// CHECK96-NEXT: <Group>String</Group>
840841
// CHECK96-NEXT: SYSTEM
842+
// CHECK96-NEXT: <Declaration>@frozen @_eagerMove struct String</Declaration>
841843

842844
// RUN: %sourcekitd-test -req=cursor -pos=233:19 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK97 %s
843-
// CHECK97: source.lang.swift.ref.function.constructor
844-
// CHECK97-NEXT: init(_builtinIntegerLiteral:)
845-
// CHECK97-NEXT: s:Si22_builtinIntegerLiteralSiBI_tcfc
845+
// CHECK97: source.lang.swift.ref.struct
846+
// CHECK97-NEXT: Int
847+
// CHECK97-NEXT: s:Si
846848
// CHECK97-NEXT: source.lang.swift
847-
// CHECK97-NEXT: (Int.Type) -> (Builtin.IntLiteral) -> Int
848-
// CHECK97-NEXT: $s22_builtinIntegerLiteralSiBI_tcD
849+
// CHECK97-NEXT: Int.Type
850+
// CHECK97-NEXT: $sSimD
849851
// CHECK97-NEXT: Swift
850852
// CHECK97-NEXT: <Group>Math/Integers</Group>
851853
// CHECK97-NEXT: SYSTEM

test/SourceKit/CursorInfo/cursor_stdlib.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,15 @@ let dictNonCost = [1:2, 3:d]
237237
// CHECK-BOOL1: s:Sb
238238

239239
// RUN: %sourcekitd-test -req=cursor -pos=29:29 %s -- %s -target %target-triple %clang-importer-sdk-nosource -I %t | %FileCheck -check-prefix=CHECK-OBJ-LITERAL %s
240-
// CHECK-OBJ-LITERAL: source.lang.swift.ref.function.constructor
241-
// CHECK-OBJ-LITERAL-NEXT: init(_colorLiteralRed:green:blue:alpha:)
242-
// CHECK-OBJ-LITERAL-NEXT: s:13cursor_stdlib7MyColorV16_colorLiteralRed5green4blue5alphaACSf_S3ftcfc
240+
// CHECK-OBJ-LITERAL: source.lang.swift.ref.struct (24:8-24:15)
241+
// CHECK-OBJ-LITERAL-NEXT: MyColor
242+
// CHECK-OBJ-LITERAL-NEXT: s:13cursor_stdlib7MyColorV
243243
// CHECK-OBJ-LITERAL-NEXT: source.lang.swift
244-
// CHECK-OBJ-LITERAL-NEXT: (MyColor.Type) -> (Float, Float, Float, Float) -> MyColor
245-
// CHECK-OBJ-LITERAL-NEXT: $s16_colorLiteralRed5green4blue5alpha13cursor_stdlib7MyColorVSf_S3ftcD
244+
// CHECK-OBJ-LITERAL-NEXT: MyColor.Type
245+
// CHECK-OBJ-LITERAL-NEXT: $s13cursor_stdlib7MyColorVmD
246246
// CHECK-OBJ-LITERAL-NEXT: cursor_stdlib
247+
// CHECK-OBJ-LITERAL-NEXT: <Declaration>struct MyColor : <Type usr="s:s26_ExpressibleByColorLiteralP">_ExpressibleByColorLiteral</Type></Declaration>
248+
// CHECK-OBJ-LITERAL-NEXT: <decl.struct><syntaxtype.keyword>struct</syntaxtype.keyword> <decl.name>MyColor</decl.name> : <ref.protocol usr="s:s26_ExpressibleByColorLiteralP">_ExpressibleByColorLiteral</ref.protocol></decl.struct>
247249

248250
// RUN: %sourcekitd-test -req=cursor -pos=31:18 %s -- %s -target %target-triple %clang-importer-sdk-nosource -I %t | %FileCheck -check-prefix=CHECK-ARRAY1 %s
249251
// CHECK-ARRAY1: source.lang.swift.ref.function.constructor

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@ struct CursorInfoData {
685685
/// \c CursorInfoData.
686686
llvm::BumpPtrAllocator Allocator;
687687

688+
bool isEmpty() const {
689+
return InternalDiagnostic.empty() && Symbols.empty() &&
690+
AvailableActions.empty();
691+
}
692+
688693
void print(llvm::raw_ostream &OS, std::string Indentation) const {
689694
OS << Indentation << "CursorInfoData" << '\n';
690695
OS << Indentation << " Symbols:" << '\n';

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,39 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
11511151
return llvm::Error::success();
11521152
}
11531153

1154+
// If E is a literal, returns the declaration that should be reported by
1155+
// cursor info for that initializer.
1156+
static ValueDecl *getCursorInfoDeclForLiteral(Expr *E) {
1157+
if (auto *CollectionLit = dyn_cast<CollectionExpr>(E)) {
1158+
return CollectionLit->getInitializer().getDecl();
1159+
}
1160+
1161+
LiteralExpr* LitExpr = dyn_cast<LiteralExpr>(E);
1162+
if (!LitExpr) {
1163+
return nullptr;
1164+
}
1165+
1166+
bool IsObjectLiteral = dyn_cast<ObjectLiteralExpr>(E);
1167+
if (!IsObjectLiteral && LitExpr->getInitializer().getDecl()) {
1168+
return LitExpr->getInitializer().getDecl();
1169+
}
1170+
1171+
// We shouldn’t report the builtin initializer to the user because it’s
1172+
// underscored and not visible. Instead, return the type of the literal.
1173+
if (IsObjectLiteral || dyn_cast<BuiltinLiteralExpr>(E)) {
1174+
Type Ty = E->getType();
1175+
if (!Ty) {
1176+
return nullptr;
1177+
}
1178+
auto NominalTy = Ty->getAs<NominalOrBoundGenericNominalType>();
1179+
if (!NominalTy) {
1180+
return nullptr;
1181+
}
1182+
return NominalTy->getDecl();
1183+
}
1184+
return nullptr;
1185+
}
1186+
11541187
static bool addCursorInfoForLiteral(
11551188
CursorInfoData &Data, Expr *LitExpr, SwiftLangSupport &Lang,
11561189
const CompilerInvocation &CompInvoc, SourceLoc CursorLoc,
@@ -1159,22 +1192,7 @@ static bool addCursorInfoForLiteral(
11591192
return false;
11601193
}
11611194

1162-
// Tries to retrieve the referenced initializer.
1163-
auto getInitializer = [](auto *Expr) -> ValueDecl * {
1164-
if (auto *LitExpr = dyn_cast<LiteralExpr>(Expr)) {
1165-
if (LitExpr->getInitializer().getDecl()) {
1166-
return LitExpr->getInitializer().getDecl();
1167-
}
1168-
if (auto *BuiltinLit = dyn_cast<BuiltinLiteralExpr>(LitExpr)) {
1169-
return BuiltinLit->getBuiltinInitializer().getDecl();
1170-
}
1171-
} else if (auto *CollectionLit = dyn_cast<CollectionExpr>(Expr)) {
1172-
return CollectionLit->getInitializer().getDecl();
1173-
}
1174-
return nullptr;
1175-
};
1176-
1177-
ValueDecl *InitDecl = getInitializer(LitExpr);
1195+
ValueDecl *InitDecl = getCursorInfoDeclForLiteral(LitExpr);
11781196
if (!InitDecl) {
11791197
return false;
11801198
}
@@ -1183,9 +1201,11 @@ static bool addCursorInfoForLiteral(
11831201
DeclInfo Info(InitDecl, nullptr, true, false, {}, CompInvoc);
11841202
auto Err = fillSymbolInfo(Symbol, Info, CursorLoc, false, Lang, CompInvoc,
11851203
PreviousSnaps, Data.Allocator);
1204+
11861205
bool Success = true;
11871206
llvm::handleAllErrors(std::move(Err), [&](const llvm::StringError &E) {
11881207
Data.InternalDiagnostic = copyCString(E.getMessage(), Data.Allocator);
1208+
Data.Symbols.pop_back();
11891209
Success = false;
11901210
});
11911211
return Success;
@@ -1678,18 +1698,10 @@ static void resolveCursor(
16781698
return nullptr;
16791699
}
16801700
Expr *E = ExprInfo->getTrailingExpr();
1681-
if (dyn_cast<LiteralExpr>(E)) {
1682-
return E;
1683-
}
1684-
switch (E->getKind()) {
1685-
case ExprKind::Array:
1686-
case ExprKind::Dictionary:
1687-
case ExprKind::KeyPath: {
1701+
if (dyn_cast<LiteralExpr>(E) || dyn_cast<CollectionExpr>(E)) {
16881702
return E;
16891703
}
1690-
default:
1691-
return nullptr;
1692-
}
1704+
return nullptr;
16931705
};
16941706

16951707
CursorInfoData Data;
@@ -1702,17 +1714,11 @@ static void resolveCursor(
17021714

17031715
// Handle literal expression.
17041716
if (auto *LitExpr = tryGetLiteralExpr(CursorInfo)) {
1705-
bool Success = addCursorInfoForLiteral(Data, LitExpr, Lang, CompInvok,
1706-
CursorInfo->getLoc(),
1707-
getPreviousASTSnaps());
1708-
if (!Success) {
1709-
Data.Symbols.clear();
1710-
Data.AvailableActions.clear();
1711-
}
1717+
addCursorInfoForLiteral(Data, LitExpr, Lang, CompInvok,
1718+
CursorInfo->getLoc(), getPreviousASTSnaps());
17121719
}
17131720

1714-
if (Data.InternalDiagnostic.empty() && Data.AvailableActions.empty() &&
1715-
Data.Symbols.empty()) {
1721+
if (Data.isEmpty()) {
17161722
Data.InternalDiagnostic =
17171723
"Resolved to incomplete expression or statement.";
17181724
}

0 commit comments

Comments
 (0)