diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp index 7dc7fd906be9d6..1ab80b72a90b1f 100644 --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -448,8 +448,15 @@ llvm::SmallVector refInDecl(const Decl *D) { // FIXME: decide on how to surface destructors when we need them. if (llvm::isa(ND)) return; - Refs.push_back(ReferenceLoc{ - getQualifierLoc(*ND), ND->getLocation(), /*IsDecl=*/true, {ND}}); + // Filter anonymous decls, name location will point outside the name token + // and the clients are not prepared to handle that. + if (ND->getDeclName().isIdentifier() && + !ND->getDeclName().getAsIdentifierInfo()) + return; + Refs.push_back(ReferenceLoc{getQualifierLoc(*ND), + ND->getLocation(), + /*IsDecl=*/true, + {ND}}); } }; diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp index f7095aebd868e0..6b0e51b228b803 100644 --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -852,8 +852,8 @@ TEST_F(FindExplicitReferencesTest, All) { }; // delegating initializer class $10^Foo { - $11^Foo(int$12^); - $13^Foo(): $14^Foo(111) {} + $11^Foo(int); + $12^Foo(): $13^Foo(111) {} }; } )cpp", @@ -869,11 +869,19 @@ TEST_F(FindExplicitReferencesTest, All) { "9: targets = {Base}\n" "10: targets = {Foo}, decl\n" "11: targets = {foo()::Foo::Foo}, decl\n" - // FIXME: we should exclude the built-in type. - "12: targets = {}, decl\n" - "13: targets = {foo()::Foo::Foo}, decl\n" - "14: targets = {Foo}\n"}, - + "12: targets = {foo()::Foo::Foo}, decl\n" + "13: targets = {Foo}\n"}, + // Anonymous entities should not be reported. + { + R"cpp( + void foo() { + class {} $0^x; + int (*$1^fptr)(int $2^a, int) = nullptr; + } + )cpp", + "0: targets = {x}, decl\n" + "1: targets = {fptr}, decl\n" + "2: targets = {a}, decl\n"}, }; for (const auto &C : Cases) {