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 @@ -358,6 +358,17 @@ // TypeLoc never has a deduced type. https://llvm.org/PR42914 Outer.add(DT->getDeducedType(), Flags | Rel::Underlying); } + void VisitDeducedTemplateSpecializationType( + const DeducedTemplateSpecializationType *DTST) { + // FIXME: This is a workaround for https://llvm.org/PR42914, + // which is causing DTST->getDeducedType() to be empty. We + // fall back to the template pattern and miss the instantiation + // even when it's known in principle. Once that bug is fixed, + // this method can be removed (the existing handling in + // VisitDeducedType() is sufficient). + if (auto *TD = DTST->getTemplateName().getAsTemplateDecl()) + Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern); + } void VisitTypedefType(const TypedefType *TT) { Outer.add(TT->getDecl(), Flags); } 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 @@ -315,6 +315,20 @@ EXPECT_DECLS("TemplateSpecializationTypeLoc", {"template<> class Foo", Rel::TemplateInstantiation}, {"template class Foo", Rel::TemplatePattern}); + + Code = R"cpp( + // Class template argument deduction + template + struct Test { + Test(T); + }; + void foo() { + [[Test]] a(5); + } + )cpp"; + Flags.push_back("-std=c++17"); + EXPECT_DECLS("DeducedTemplateSpecializationTypeLoc", + {"struct Test", Rel::TemplatePattern}); } TEST_F(TargetDeclTest, FunctionTemplate) { @@ -549,6 +563,7 @@ // FIXME: Auto-completion in a template requires disabling delayed template // parsing. TU.ExtraArgs.push_back("-fno-delayed-template-parsing"); + TU.ExtraArgs.push_back("-std=c++17"); auto AST = TU.build(); @@ -937,7 +952,20 @@ }; )cpp", "0: targets = {size}, decl\n" - "1: targets = {E}\n"}}; + "1: targets = {E}\n"}, + // Class template argument deduction + { + R"cpp( + template + struct Test { + Test(T); + }; + void foo() { + $0^Test $1^a(5); + } + )cpp", + "0: targets = {Test}\n" + "1: targets = {a}, decl\n"}}; for (const auto &C : Cases) { llvm::StringRef ExpectedCode = C.first;