diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -139,7 +139,13 @@ const NamedDecl *getTemplatePattern(const NamedDecl *D) { if (const CXXRecordDecl *CRD = dyn_cast(D)) { - return CRD->getTemplateInstantiationPattern(); + if (const auto *Result = CRD->getTemplateInstantiationPattern()) + return Result; + // getTemplateInstantiationPattern returns null if the Specialization is + // incomplete (e.g. the type didn't need to be complete), fall back to the + // primary template. + if (const auto *Spec = dyn_cast(CRD)) + return Spec->getSpecializedTemplate()->getTemplatedDecl(); } else if (const FunctionDecl *FD = dyn_cast(D)) { return FD->getTemplateInstantiationPattern(); } else if (auto *VD = dyn_cast(D)) { @@ -412,7 +418,6 @@ void VisitTemplateSpecializationType(const TemplateSpecializationType *TST) { // Have to handle these case-by-case. - // templated type aliases: there's no specialized/instantiated using // decl to point to. So try to find a decl for the underlying type // (after substitution), and failing that point to the (templated) using diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -309,6 +309,16 @@ {"template<> class Foo<42>", Rel::TemplateInstantiation}, {"class Foo", Rel::TemplatePattern}); + Code = R"cpp( + template class Foo {}; + // The "Foo" SpecializationDecl is incomplete, there is no + // instantiation happening. + void func([[Foo]] *); + )cpp"; + EXPECT_DECLS("TemplateSpecializationTypeLoc", + {"class Foo", Rel::TemplatePattern}, + {"template<> class Foo", Rel::TemplateInstantiation}); + Code = R"cpp( // Explicit specialization. template class Foo{}; 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 @@ -954,6 +954,12 @@ class [[Fo^o]] {}; void func([[Foo]]); )cpp", + + R"cpp( + template + class [[Foo]] {}; + void func([[Fo^o]]); + )cpp", }; for (const char *Test : Tests) { Annotations T(Test);