Skip to content

Commit 98ffddb

Browse files
committed
[Index] Record relations for pseudo accessors
I recently accidentally broke this, make sure we carve out an exception for pseudo accessors in `shouldIndex` such that we record e.g override relations for them. rdar://131749546
1 parent 7b71100 commit 98ffddb

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/Index/Index.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -1056,14 +1056,34 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
10561056
return {{line, col, inGeneratedBuffer}};
10571057
}
10581058

1059+
bool shouldIndexImplicitDecl(const ValueDecl *D, bool IsRef) const {
1060+
// We index implicit constructors.
1061+
if (isa<ConstructorDecl>(D))
1062+
return true;
1063+
1064+
// Allow references to implicit getter & setter AccessorDecls on
1065+
// non-implicit storage, these are "pseudo accessors".
1066+
if (auto *AD = dyn_cast<AccessorDecl>(D)) {
1067+
if (!IsRef)
1068+
return false;
1069+
1070+
auto Kind = AD->getAccessorKind();
1071+
if (Kind != AccessorKind::Get && Kind != AccessorKind::Set)
1072+
return false;
1073+
1074+
return shouldIndex(AD->getStorage(), IsRef);
1075+
}
1076+
return false;
1077+
}
1078+
10591079
bool shouldIndex(const ValueDecl *D, bool IsRef) const {
10601080
if (D->isImplicit() && isa<VarDecl>(D) && IsRef) {
10611081
// Bypass the implicit VarDecls introduced in CaseStmt bodies by using the
10621082
// canonical VarDecl for these checks instead.
10631083
D = cast<VarDecl>(D)->getCanonicalVarDecl();
10641084
}
10651085

1066-
if (D->isImplicit() && !isa<ConstructorDecl>(D))
1086+
if (D->isImplicit() && !shouldIndexImplicitDecl(D, IsRef))
10671087
return false;
10681088

10691089
// Do not handle non-public imported decls.

test/Index/roles.swift

+25
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,28 @@ func containerFunc() {
539539
// CHECK: [[@LINE-16]]:15 | function/acc-get/Swift | getter:y | {{.*}} | Ref,Call,Impl,RelCall,RelCont | rel: 1
540540
// CHECK-NEXT: RelCall,RelCont | function/Swift | containerFunc()
541541
}
542+
543+
// rdar://131749546 - Make sure we record the override relation for the
544+
// pseudo accessor for 'x'.
545+
class BaseClass {
546+
var x = 0
547+
// CHECK: [[@LINE-1]]:7 | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp | Def,RelChild | rel: 1
548+
// CHECK: [[@LINE-2]]:7 | instance-method/acc-get/Swift | getter:x | s:14swift_ide_test9BaseClassC1xSivg | Def,Dyn,Impl,RelChild,RelAcc | rel: 1
549+
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp
550+
// CHECK: [[@LINE-4]]:7 | instance-method/acc-set/Swift | setter:x | s:14swift_ide_test9BaseClassC1xSivs | Def,Dyn,Impl,RelChild,RelAcc | rel: 1
551+
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp
552+
}
553+
class Subclass: BaseClass {
554+
override var x: Int {
555+
// CHECK: [[@LINE-1]]:16 | instance-property/Swift | x | s:14swift_ide_test8SubclassC1xSivp | Def,RelChild,RelOver | rel: 2
556+
// CHECK-NEXT: RelOver | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp
557+
get { 0 }
558+
// CHECK: [[@LINE-1]]:5 | instance-method/acc-get/Swift | getter:x | s:14swift_ide_test8SubclassC1xSivg | Def,Dyn,RelChild,RelOver,RelAcc | rel: 2
559+
// CHECK-NEXT: RelOver | instance-method/acc-get/Swift | getter:x | s:14swift_ide_test9BaseClassC1xSivg
560+
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test8SubclassC1xSivp
561+
set {}
562+
// CHECK: [[@LINE-1]]:5 | instance-method/acc-set/Swift | setter:x | s:14swift_ide_test8SubclassC1xSivs | Def,Dyn,RelChild,RelOver,RelAcc | rel: 2
563+
// CHECK-NEXT: RelOver | instance-method/acc-set/Swift | setter:x | s:14swift_ide_test9BaseClassC1xSivs
564+
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test8SubclassC1xSivp
565+
}
566+
}

0 commit comments

Comments
 (0)