Skip to content

[Index] Record relations for pseudo accessors #75241

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
Jul 15, 2024
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
22 changes: 21 additions & 1 deletion lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,14 +1056,34 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
return {{line, col, inGeneratedBuffer}};
}

bool shouldIndexImplicitDecl(const ValueDecl *D, bool IsRef) const {
// We index implicit constructors.
if (isa<ConstructorDecl>(D))
return true;

// Allow references to implicit getter & setter AccessorDecls on
// non-implicit storage, these are "pseudo accessors".
if (auto *AD = dyn_cast<AccessorDecl>(D)) {
if (!IsRef)
return false;

auto Kind = AD->getAccessorKind();
if (Kind != AccessorKind::Get && Kind != AccessorKind::Set)
return false;

return shouldIndex(AD->getStorage(), IsRef);
}
return false;
}

bool shouldIndex(const ValueDecl *D, bool IsRef) const {
if (D->isImplicit() && isa<VarDecl>(D) && IsRef) {
// Bypass the implicit VarDecls introduced in CaseStmt bodies by using the
// canonical VarDecl for these checks instead.
D = cast<VarDecl>(D)->getCanonicalVarDecl();
}

if (D->isImplicit() && !isa<ConstructorDecl>(D))
if (D->isImplicit() && !shouldIndexImplicitDecl(D, IsRef))
return false;

// Do not handle non-public imported decls.
Expand Down
25 changes: 25 additions & 0 deletions test/Index/roles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,28 @@ func containerFunc() {
// CHECK: [[@LINE-16]]:15 | function/acc-get/Swift | getter:y | {{.*}} | Ref,Call,Impl,RelCall,RelCont | rel: 1
// CHECK-NEXT: RelCall,RelCont | function/Swift | containerFunc()
}

// rdar://131749546 - Make sure we record the override relation for the
// pseudo accessor for 'x'.
class BaseClass {
var x = 0
// CHECK: [[@LINE-1]]:7 | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp | Def,RelChild | rel: 1
// CHECK: [[@LINE-2]]:7 | instance-method/acc-get/Swift | getter:x | s:14swift_ide_test9BaseClassC1xSivg | Def,Dyn,Impl,RelChild,RelAcc | rel: 1
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp
// CHECK: [[@LINE-4]]:7 | instance-method/acc-set/Swift | setter:x | s:14swift_ide_test9BaseClassC1xSivs | Def,Dyn,Impl,RelChild,RelAcc | rel: 1
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp
}
class Subclass: BaseClass {
override var x: Int {
// CHECK: [[@LINE-1]]:16 | instance-property/Swift | x | s:14swift_ide_test8SubclassC1xSivp | Def,RelChild,RelOver | rel: 2
// CHECK-NEXT: RelOver | instance-property/Swift | x | s:14swift_ide_test9BaseClassC1xSivp
get { 0 }
// CHECK: [[@LINE-1]]:5 | instance-method/acc-get/Swift | getter:x | s:14swift_ide_test8SubclassC1xSivg | Def,Dyn,RelChild,RelOver,RelAcc | rel: 2
// CHECK-NEXT: RelOver | instance-method/acc-get/Swift | getter:x | s:14swift_ide_test9BaseClassC1xSivg
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test8SubclassC1xSivp
set {}
// CHECK: [[@LINE-1]]:5 | instance-method/acc-set/Swift | setter:x | s:14swift_ide_test8SubclassC1xSivs | Def,Dyn,RelChild,RelOver,RelAcc | rel: 2
// CHECK-NEXT: RelOver | instance-method/acc-set/Swift | setter:x | s:14swift_ide_test9BaseClassC1xSivs
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | x | s:14swift_ide_test8SubclassC1xSivp
}
}