Skip to content

Commit 478cd5a

Browse files
committed
Only give overrides on ObjC method defs
1 parent 4f88a70 commit 478cd5a

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,16 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
439439
continue;
440440
}
441441
}
442-
// Special case: - (void)^method; should jump to overrides. Note that an
443-
// Objective-C method can override a parent class or protocol.
442+
// Special case: - (void)^method {} should jump to overrides, but the decl
443+
// shouldn't, only the definition. Note that an Objective-C method can
444+
// override a parent class or protocol.
444445
//
445446
// FIXME: Support jumping from a protocol decl to overrides on go-to
446447
// definition.
447448
if (const auto *OMD = llvm::dyn_cast<ObjCMethodDecl>(D)) {
448449
if (TouchedIdentifier &&
449-
objcMethodIsTouched(SM, OMD, TouchedIdentifier->location())) {
450+
objcMethodIsTouched(SM, OMD, TouchedIdentifier->location()) &&
451+
OMD->isThisDeclarationADefinition()) {
450452
llvm::SmallVector<const ObjCMethodDecl *, 4> Overrides;
451453
OMD->getOverriddenMethods(Overrides);
452454
if (!Overrides.empty()) {

clang-tools-extra/clangd/unittests/XRefsTests.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ TEST(LocateSymbol, FindOverrides) {
411411
sym("foo", Code.range("2"), std::nullopt)));
412412
}
413413

414-
TEST(LocateSymbol, FindOverridesObjC) {
414+
TEST(LocateSymbol, FindOverridesFromDefObjC) {
415415
auto Code = Annotations(R"objc(
416416
@protocol Fooey
417417
- (void)foo;
@@ -439,6 +439,33 @@ TEST(LocateSymbol, FindOverridesObjC) {
439439
sym("foo", Code.range("2"), Code.range("3"))));
440440
}
441441

442+
TEST(LocateSymbol, NoOverridesFromDeclObjC) {
443+
auto Code = Annotations(R"objc(
444+
@protocol Fooey
445+
- (void)foo;
446+
@end
447+
@interface Base
448+
- (void)foo;
449+
@end
450+
@interface Foo : Base<Fooey>
451+
- (void)foo;
452+
@end
453+
454+
@interface Bar : Foo
455+
- (void)$2[[fo^o]];
456+
@end
457+
@implementation Bar
458+
- (void)$3[[foo]] {}
459+
@end
460+
)objc");
461+
TestTU TU = TestTU::withCode(Code.code());
462+
TU.ExtraArgs.push_back("-xobjective-c++");
463+
auto AST = TU.build();
464+
EXPECT_THAT(
465+
locateSymbolAt(AST, Code.point(), TU.index().get()),
466+
UnorderedElementsAre(sym("foo", Code.range("2"), Code.range("3"))));
467+
}
468+
442469
TEST(LocateSymbol, ObjCNoOverridesOnUsage) {
443470
auto Code = Annotations(R"objc(
444471
@interface Foo

0 commit comments

Comments
 (0)