diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -767,7 +767,12 @@ index::IndexDataConsumer::ASTNodeInfo ASTNode) override { assert(D->isCanonicalDecl() && "expect D to be a canonical declaration"); const SourceManager &SM = AST.getSourceManager(); - if (!CanonicalTargets.count(D) || !isInsideMainFile(Loc, SM)) + // For references to template specializations, `D` will contain the + // template and `ASTNode.OrigD` the specialization. We want to find + // references to specializations, to check `ASTNode.OrigD` as well. + bool ReferencesCanonicalTarget = + CanonicalTargets.count(D) || CanonicalTargets.count(ASTNode.OrigD); + if (!ReferencesCanonicalTarget || !isInsideMainFile(Loc, SM)) return true; const auto &TB = AST.getTokens(); Loc = SM.getFileLoc(Loc); @@ -1142,7 +1147,7 @@ // We also show references to the targets of using-decls, so we include // DeclRelation::Underlying. - DeclRelationSet Relations = DeclRelation::TemplatePattern | + DeclRelationSet Relations = DeclRelation::TemplateInstantiation | DeclRelation::Alias | DeclRelation::Underlying; auto Decls = getDeclAtPosition(AST, *CurLoc, Relations); diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -1570,7 +1570,7 @@ R"cpp( template - class [[Foo]] {}; + class Foo {}; void func([[Fo^o]]); )cpp", R"cpp(// Not touching any identifiers. @@ -1582,6 +1582,13 @@ f.[[^~]]Foo(); } )cpp", + R"cpp(// Temlate specialization + template class Vector {}; + using [[^X]] = [[Vector]]; + [[X]] x1; + [[Vector]] x2; + Vector y; + )cpp", }; for (const char *Test : Tests) { Annotations T(Test);