Skip to content

Commit 4bc7435

Browse files
author
Nathan Hawes
committed
[indexer] provide names for pseudo accessors too
1 parent 8c45e20 commit 4bc7435

File tree

7 files changed

+77
-34
lines changed

7 files changed

+77
-34
lines changed

lib/Index/Index.cpp

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,19 @@ static SymbolSubKind getSubKindForAccessor(AccessorKind AK) {
4242
}
4343

4444
static bool
45-
printArtificialName(const swift::ValueDecl *VD, llvm::raw_ostream &OS) {
46-
auto *FD = dyn_cast<FuncDecl>(VD);
47-
if (!FD)
48-
return true;
49-
switch (FD->getAccessorKind()) {
45+
printArtificialName(const swift::AbstractStorageDecl *ASD, AccessorKind AK, llvm::raw_ostream &OS) {
46+
switch (AK) {
5047
case AccessorKind::IsGetter:
51-
OS << "getter:" << FD->getAccessorStorageDecl()->getFullName();
48+
OS << "getter:" << ASD->getFullName();
5249
return false;
5350
case AccessorKind::IsSetter:
54-
OS << "setter:" << FD->getAccessorStorageDecl()->getFullName();
51+
OS << "setter:" << ASD->getFullName();
5552
return false;
5653
case AccessorKind::IsDidSet:
57-
OS << "didSet:" << FD->getAccessorStorageDecl()->getFullName();
54+
OS << "didSet:" << ASD->getFullName();
5855
return false;
5956
case AccessorKind::IsWillSet:
60-
OS << "willSet:" << FD->getAccessorStorageDecl()->getFullName() ;
57+
OS << "willSet:" << ASD->getFullName() ;
6158
return false;
6259

6360
case AccessorKind::NotAccessor:
@@ -71,8 +68,12 @@ printArtificialName(const swift::ValueDecl *VD, llvm::raw_ostream &OS) {
7168
}
7269

7370
static bool printDisplayName(const swift::ValueDecl *D, llvm::raw_ostream &OS) {
74-
if (!D->hasName())
75-
return printArtificialName(D, OS);
71+
if (!D->hasName()) {
72+
auto *FD = dyn_cast<FuncDecl>(D);
73+
if (!FD || FD->getAccessorKind() == AccessorKind::NotAccessor)
74+
return true;
75+
return printArtificialName(FD->getAccessorStorageDecl(), FD->getAccessorKind(), OS);
76+
}
7677

7778
OS << D->getFullName();
7879
return false;
@@ -146,7 +147,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
146147
};
147148
typedef llvm::PointerIntPair<Decl *, 3> DeclAccessorPair;
148149
llvm::DenseMap<Decl *, NameAndUSR> nameAndUSRCache;
149-
llvm::DenseMap<DeclAccessorPair, StringRef> accessorUSRCache;
150+
llvm::DenseMap<DeclAccessorPair, NameAndUSR> accessorNameAndUSRCache;
150151
StringScratchSpace stringStorage;
151152

152153
bool getNameAndUSR(ValueDecl *D, StringRef &name, StringRef &USR) {
@@ -173,6 +174,33 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
173174
return false;
174175
}
175176

177+
bool getPseudoAccessorNameAndUSR(AbstractStorageDecl *D, AccessorKind AK, StringRef &Name, StringRef &USR) {
178+
assert(AK != AccessorKind::NotAccessor);
179+
assert(static_cast<int>(AK) < 0x111 && "AccessorKind too big for pair");
180+
DeclAccessorPair key(D, static_cast<int>(AK));
181+
auto &result = accessorNameAndUSRCache[key];
182+
if (result.USR.empty()) {
183+
SmallString<128> storage;
184+
{
185+
llvm::raw_svector_ostream OS(storage);
186+
if(ide::printAccessorUSR(D, AK, OS))
187+
return true;
188+
result.USR = stringStorage.copyString(OS.str());
189+
}
190+
191+
storage.clear();
192+
{
193+
llvm::raw_svector_ostream OS(storage);
194+
printArtificialName(D, AK, OS);
195+
result.name = stringStorage.copyString(OS.str());
196+
}
197+
}
198+
199+
Name = result.name;
200+
USR = result.USR;
201+
return false;
202+
}
203+
176204
bool addRelation(IndexSymbol &Info, SymbolRoleSet RelationRoles, ValueDecl *D) {
177205
assert(D);
178206
StringRef Name, USR;
@@ -191,19 +219,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
191219
return false;
192220
}
193221

194-
StringRef getAccessorUSR(AbstractStorageDecl *D, AccessorKind AK) {
195-
assert(AK != AccessorKind::NotAccessor);
196-
assert(static_cast<int>(AK) < 0x111 && "AccessorKind too big for pair");
197-
DeclAccessorPair key(D, static_cast<int>(AK));
198-
auto &result = accessorUSRCache[key];
199-
if (result.empty()) {
200-
SmallString<128> storage;
201-
llvm::raw_svector_ostream OS(storage);
202-
ide::printAccessorUSR(D, AK, OS);
203-
result = stringStorage.copyString(OS.str());
204-
}
205-
return result;
206-
}
222+
207223

208224
public:
209225
IndexSwiftASTWalker(IndexDataConsumer &IdxConsumer, ASTContext &Ctx,
@@ -652,27 +668,30 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
652668
return true; // continue walking.
653669

654670
auto updateInfo = [this, D, AccKind](IndexSymbol &Info) {
671+
if (getPseudoAccessorNameAndUSR(D, AccKind, Info.name, Info.USR))
672+
return true;
655673
Info.kind = SymbolKind::Accessor;
656674
Info.subKinds |= getSubKindForAccessor(AccKind);
657-
Info.name = "";
658-
Info.USR = getAccessorUSR(D, AccKind);
675+
Info.roles |= (SymbolRoleSet)SymbolRole::Implicit;
659676
Info.group = "";
677+
return false;
660678
};
661679

662680
if (IsRef) {
663681
IndexSymbol Info;
664682
if (initCallRefIndexSymbol(ExprStack.back(), getParentExpr(), D, Loc, Info))
665683
return true; // continue walking.
684+
if (updateInfo(Info))
685+
return true;
666686

667-
updateInfo(Info);
668687
if (!IdxConsumer.startSourceEntity(Info) || !IdxConsumer.finishSourceEntity(Info.kind, Info.subKinds, Info.roles))
669688
Cancelled = true;
670689
} else {
671690
IndexSymbol Info;
672691
if (initIndexSymbol(D, Loc, IsRef, Info))
673692
return true; // continue walking.
674-
675-
updateInfo(Info);
693+
if (updateInfo(Info))
694+
return true;
676695
if (addRelation(Info, (SymbolRoleSet) SymbolRole::RelationAccessorOf, D))
677696
return true;
678697

test/Index/roles.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ var z: Int {
4040
// Write + Read of z
4141
z = z + 1
4242
// CHECK: [[@LINE-1]]:1 | variable/Swift | z | s:v14swift_ide_test1zSi | Ref,Writ | rel: 0
43-
// CHECK: [[@LINE-2]]:1 | accessor(set)/Swift | | s:F14swift_ide_tests1zSi | Ref,Call,RelCall | rel: 1
43+
// CHECK: [[@LINE-2]]:1 | accessor(set)/Swift | setter:z | s:F14swift_ide_tests1zSi | Ref,Call,Impl,RelCall | rel: 1
4444
// CHECK-NEXT: RelCall | z | s:v14swift_ide_test1zSi
4545
// CHECK: [[@LINE-4]]:5 | variable/Swift | z | s:v14swift_ide_test1zSi | Ref,Read | rel: 0
46-
// CHECK-NEXT: [[@LINE-5]]:5 | accessor(get)/Swift | | s:F14swift_ide_testg1zSi | Ref,Call,RelCall | rel: 1
46+
// CHECK-NEXT: [[@LINE-5]]:5 | accessor(get)/Swift | getter:z | s:F14swift_ide_testg1zSi | Ref,Call,Impl,RelCall | rel: 1
4747

4848
// Call
4949
func aCalledFunction() {}
@@ -65,10 +65,10 @@ struct aStruct {
6565
mutating func aMethod() {
6666
x += 1
6767
// CHECK: [[@LINE-1]]:5 | instance-property/Swift | x | s:vV14swift_ide_test7aStruct1xSi | Ref,Read,Writ | rel: 0
68-
// CHECK: [[@LINE-2]]:5 | accessor(get)/Swift | | s:FV14swift_ide_test7aStructg1xSi | Ref,Call,RelRec,RelCall | rel: 2
68+
// CHECK: [[@LINE-2]]:5 | accessor(get)/Swift | getter:x | s:FV14swift_ide_test7aStructg1xSi | Ref,Call,Impl,RelRec,RelCall | rel: 2
6969
// CHECK-NEXT: RelCall | x | s:vV14swift_ide_test7aStruct1xSi
7070
// CHECK-NEXT: RelRec | aStruct | s:V14swift_ide_test7aStruct
71-
// CHECK: [[@LINE-5]]:5 | accessor(set)/Swift | | s:FV14swift_ide_test7aStructs1xSi | Ref,Call,RelRec,RelCall | rel: 2
71+
// CHECK: [[@LINE-5]]:5 | accessor(set)/Swift | setter:x | s:FV14swift_ide_test7aStructs1xSi | Ref,Call,Impl,RelRec,RelCall | rel: 2
7272
// CHECK-NEXT: RelCall | x | s:vV14swift_ide_test7aStruct1xSi
7373
// CHECK-NEXT: RelRec | aStruct | s:V14swift_ide_test7aStruct
7474
// CHECK: [[@LINE-8]]:7 | infix-operator/Swift | +=(_:_:) | s:Fsoi2peFTRSiSi_T_ | Ref,Call,RelCall | rel: 1

test/SourceKit/Indexing/index.swift.response

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
key.entities: [
2020
{
2121
key.kind: source.lang.swift.decl.function.accessor.getter,
22+
key.name: "getter:globV",
2223
key.usr: "s:F5indexg5globVSi",
2324
key.line: 4,
2425
key.column: 5
2526
},
2627
{
2728
key.kind: source.lang.swift.decl.function.accessor.setter,
29+
key.name: "setter:globV",
2830
key.usr: "s:F5indexs5globVSi",
2931
key.line: 4,
3032
key.column: 5
@@ -266,6 +268,7 @@
266268
key.entities: [
267269
{
268270
key.kind: source.lang.swift.ref.function.accessor.setter,
271+
key.name: "setter:globV",
269272
key.usr: "s:F5indexs5globVSi",
270273
key.line: 37,
271274
key.column: 3
@@ -288,6 +291,7 @@
288291
key.entities: [
289292
{
290293
key.kind: source.lang.swift.ref.function.accessor.getter,
294+
key.name: "getter:instV",
291295
key.usr: "s:FC5index2CCg5instVS0_",
292296
key.line: 38,
293297
key.column: 9,
@@ -501,12 +505,14 @@
501505
key.entities: [
502506
{
503507
key.kind: source.lang.swift.decl.function.accessor.getter,
508+
key.name: "getter:globV2",
504509
key.usr: "s:F5indexg6globV2CS_5SubCC",
505510
key.line: 57,
506511
key.column: 5
507512
},
508513
{
509514
key.kind: source.lang.swift.decl.function.accessor.setter,
515+
key.name: "setter:globV2",
510516
key.usr: "s:F5indexs6globV2CS_5SubCC",
511517
key.line: 57,
512518
key.column: 5
@@ -750,6 +756,7 @@
750756
key.entities: [
751757
{
752758
key.kind: source.lang.swift.ref.function.accessor.getter,
759+
key.name: "getter:value",
753760
key.usr: "s:FC5index16ComputedPropertyg5valueSi",
754761
key.line: 92,
755762
key.column: 14,
@@ -767,6 +774,7 @@
767774
key.entities: [
768775
{
769776
key.kind: source.lang.swift.ref.function.accessor.getter,
777+
key.name: "getter:readOnly",
770778
key.usr: "s:FC5index16ComputedPropertyg8readOnlySi",
771779
key.line: 93,
772780
key.column: 10,
@@ -784,6 +792,7 @@
784792
key.entities: [
785793
{
786794
key.kind: source.lang.swift.ref.function.accessor.setter,
795+
key.name: "setter:value",
787796
key.usr: "s:FC5index16ComputedPropertys5valueSi",
788797
key.line: 94,
789798
key.column: 6,
@@ -808,6 +817,7 @@
808817
key.entities: [
809818
{
810819
key.kind: source.lang.swift.ref.function.accessor.getter,
820+
key.name: "getter:value",
811821
key.usr: "s:FC5index16ComputedPropertyg5valueSi",
812822
key.line: 95,
813823
key.column: 8,
@@ -816,6 +826,7 @@
816826
},
817827
{
818828
key.kind: source.lang.swift.ref.function.accessor.setter,
829+
key.name: "setter:value",
819830
key.usr: "s:FC5index16ComputedPropertys5valueSi",
820831
key.line: 95,
821832
key.column: 8,
@@ -833,6 +844,7 @@
833844
key.entities: [
834845
{
835846
key.kind: source.lang.swift.ref.function.accessor.getter,
847+
key.name: "getter:subscript(_:)",
836848
key.usr: "s:FC5index3CC2g9subscriptFSiSi",
837849
key.line: 96,
838850
key.column: 10,
@@ -850,6 +862,7 @@
850862
key.entities: [
851863
{
852864
key.kind: source.lang.swift.ref.function.accessor.setter,
865+
key.name: "setter:subscript(_:)",
853866
key.usr: "s:FC5index3CC2s9subscriptFSiSi",
854867
key.line: 97,
855868
key.column: 6,
@@ -874,6 +887,7 @@
874887
key.entities: [
875888
{
876889
key.kind: source.lang.swift.ref.function.accessor.getter,
890+
key.name: "getter:subscript(_:)",
877891
key.usr: "s:FC5index3CC2g9subscriptFSiSi",
878892
key.line: 98,
879893
key.column: 8,
@@ -882,6 +896,7 @@
882896
},
883897
{
884898
key.kind: source.lang.swift.ref.function.accessor.setter,
899+
key.name: "setter:subscript(_:)",
885900
key.usr: "s:FC5index3CC2s9subscriptFSiSi",
886901
key.line: 98,
887902
key.column: 8,
@@ -956,6 +971,7 @@
956971
key.entities: [
957972
{
958973
key.kind: source.lang.swift.ref.function.accessor.getter,
974+
key.name: "getter:globReadOnly",
959975
key.usr: "s:F5indexg12globReadOnlyVS_2S2",
960976
key.line: 112,
961977
key.column: 3

test/SourceKit/Indexing/index_bad_modulename.swift.response

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
key.entities: [
2020
{
2121
key.kind: source.lang.swift.decl.function.accessor.getter,
22+
key.name: "getter:v",
2223
key.usr: "s:F4maing1vGSqCSo8NSObject_",
2324
key.line: 7,
2425
key.column: 5
2526
},
2627
{
2728
key.kind: source.lang.swift.decl.function.accessor.setter,
29+
key.name: "setter:v",
2830
key.usr: "s:F4mains1vGSqCSo8NSObject_",
2931
key.line: 7,
3032
key.column: 5

test/SourceKit/Indexing/index_big_array.swift.response

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
key.entities: [
2020
{
2121
key.kind: source.lang.swift.decl.function.accessor.getter,
22+
key.name: "getter:gCubeVertexData",
2223
key.usr: "s:F9big_arrayg15gCubeVertexDataGSaSf_",
2324
key.line: 1,
2425
key.column: 5
2526
},
2627
{
2728
key.kind: source.lang.swift.decl.function.accessor.setter,
29+
key.name: "setter:gCubeVertexData",
2830
key.usr: "s:F9big_arrays15gCubeVertexDataGSaSf_",
2931
key.line: 1,
3032
key.column: 5

test/SourceKit/Indexing/index_enum_case.swift.response

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@
104104
key.entities: [
105105
{
106106
key.kind: source.lang.swift.decl.function.accessor.getter,
107+
key.name: "getter:e",
107108
key.usr: "s:F15index_enum_caseg1eOS_1E",
108109
key.line: 21,
109110
key.column: 5
110111
},
111112
{
112113
key.kind: source.lang.swift.decl.function.accessor.setter,
114+
key.name: "setter:e",
113115
key.usr: "s:F15index_enum_cases1eOS_1E",
114116
key.line: 21,
115117
key.column: 5

test/SourceKit/Indexing/index_forbid_typecheck.swift.response

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
key.entities: [
2020
{
2121
key.kind: source.lang.swift.decl.function.accessor.getter,
22+
key.name: "getter:globalPrim",
2223
key.usr: "s:F18forbid_typecheck_2g10globalPrimSi",
2324
key.line: 1,
2425
key.column: 5
2526
},
2627
{
2728
key.kind: source.lang.swift.decl.function.accessor.setter,
29+
key.name: "setter:globalPrim",
2830
key.usr: "s:F18forbid_typecheck_2s10globalPrimSi",
2931
key.line: 1,
3032
key.column: 5

0 commit comments

Comments
 (0)