diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp index b68e5c6c1e8999..8419043f4462aa 100644 --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -94,6 +94,16 @@ const NamedDecl *getTemplatePattern(const NamedDecl *D) { return nullptr; } +// Returns true if the `TypedefNameDecl` should not be reported. +bool shouldSkipTypedef(const TypedefNameDecl *TD) { + // These should be treated as keywords rather than decls - the typedef is an + // odd implementation detail. + if (TD == TD->getASTContext().getObjCInstanceTypeDecl() || + TD == TD->getASTContext().getObjCIdDecl()) + return true; + return false; +} + // TargetFinder locates the entities that an AST node refers to. // // Typically this is (possibly) one declaration and (possibly) one type, but @@ -395,6 +405,8 @@ struct TargetFinder { } } void VisitTypedefType(const TypedefType *TT) { + if (shouldSkipTypedef(TT->getDecl())) + return; Outer.add(TT->getDecl(), Flags); } void @@ -903,6 +915,8 @@ refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) { } void VisitTypedefTypeLoc(TypedefTypeLoc L) { + if (shouldSkipTypedef(L.getTypedefNameDecl())) + return; Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false, diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp index bccf98ee07ab47..14cb15a7644b0a 100644 --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -995,6 +995,20 @@ TEST_F(TargetDeclTest, ObjC) { } )cpp"; EXPECT_DECLS("ObjCPropertyRefExpr", "+ (id)sharedInstance"); + + Code = R"cpp( + @interface Foo + + ([[id]])sharedInstance; + @end + )cpp"; + EXPECT_DECLS("TypedefTypeLoc"); + + Code = R"cpp( + @interface Foo + + ([[instancetype]])sharedInstance; + @end + )cpp"; + EXPECT_DECLS("TypedefTypeLoc"); } class FindExplicitReferencesTest : public ::testing::Test { diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 646c6ec1846520..1a9220e911e32d 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -661,13 +661,15 @@ sizeof...($TemplateParameter[[Elements]]); @interface $Class_decl[[Foo]] @end @interface $Class_decl[[Bar]] : $Class[[Foo]] - -($Class[[id]]) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]]; + -(id) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]]; + +(instancetype)$StaticMethod_decl_static[[sharedInstance]]; +(void) $StaticMethod_decl_static[[explode]]; @end @implementation $Class_decl[[Bar]] - -($Class[[id]]) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]] { + -(id) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]] { return self; } + +(instancetype)$StaticMethod_decl_static[[sharedInstance]] { return 0; } +(void) $StaticMethod_decl_static[[explode]] {} @end