This revision was automatically updated to reflect the committed changes.
Closed by commit rGe326f5243041: [clangd] Fix the incomplete template 
specialization in findTarget. (authored by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D74900?vs=245800&id=245801#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74900/new/

https://reviews.llvm.org/D74900

Files:
  clang-tools-extra/clangd/FindTarget.cpp
  clang-tools-extra/clangd/unittests/FindTargetTests.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -954,6 +954,12 @@
        class [[Fo^o]] {};
        void func([[Foo]]<int>);
       )cpp",
+
+      R"cpp(
+       template <typename T>
+       class [[Foo]] {};
+       void func([[Fo^o]]<int>);
+      )cpp",
   };
   for (const char *Test : Tests) {
     Annotations T(Test);
Index: clang-tools-extra/clangd/unittests/FindTargetTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -310,6 +310,16 @@
                {"class Foo", Rel::TemplatePattern});
 
   Code = R"cpp(
+    template<typename T> class Foo {};
+    // The "Foo<int>" SpecializationDecl is incomplete, there is no
+    // instantiation happening.
+    void func([[Foo<int>]] *);
+  )cpp";
+  EXPECT_DECLS("TemplateSpecializationTypeLoc",
+               {"class Foo", Rel::TemplatePattern},
+               {"template<> class Foo<int>", Rel::TemplateInstantiation});
+
+  Code = R"cpp(
     // Explicit specialization.
     template<int x> class Foo{};
     template<> class Foo<42>{};
Index: clang-tools-extra/clangd/FindTarget.cpp
===================================================================
--- clang-tools-extra/clangd/FindTarget.cpp
+++ clang-tools-extra/clangd/FindTarget.cpp
@@ -31,6 +31,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/OperatorKinds.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/Specifiers.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
@@ -139,7 +140,14 @@
 
 const NamedDecl *getTemplatePattern(const NamedDecl *D) {
   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(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 (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
+      if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
+        return Spec->getSpecializedTemplate()->getTemplatedDecl();
   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     return FD->getTemplateInstantiationPattern();
   } else if (auto *VD = dyn_cast<VarDecl>(D)) {


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -954,6 +954,12 @@
        class [[Fo^o]] {};
        void func([[Foo]]<int>);
       )cpp",
+
+      R"cpp(
+       template <typename T>
+       class [[Foo]] {};
+       void func([[Fo^o]]<int>);
+      )cpp",
   };
   for (const char *Test : Tests) {
     Annotations T(Test);
Index: clang-tools-extra/clangd/unittests/FindTargetTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -310,6 +310,16 @@
                {"class Foo", Rel::TemplatePattern});
 
   Code = R"cpp(
+    template<typename T> class Foo {};
+    // The "Foo<int>" SpecializationDecl is incomplete, there is no
+    // instantiation happening.
+    void func([[Foo<int>]] *);
+  )cpp";
+  EXPECT_DECLS("TemplateSpecializationTypeLoc",
+               {"class Foo", Rel::TemplatePattern},
+               {"template<> class Foo<int>", Rel::TemplateInstantiation});
+
+  Code = R"cpp(
     // Explicit specialization.
     template<int x> class Foo{};
     template<> class Foo<42>{};
Index: clang-tools-extra/clangd/FindTarget.cpp
===================================================================
--- clang-tools-extra/clangd/FindTarget.cpp
+++ clang-tools-extra/clangd/FindTarget.cpp
@@ -31,6 +31,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/OperatorKinds.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/Specifiers.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
@@ -139,7 +140,14 @@
 
 const NamedDecl *getTemplatePattern(const NamedDecl *D) {
   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(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 (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
+      if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
+        return Spec->getSpecializedTemplate()->getTemplatedDecl();
   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     return FD->getTemplateInstantiationPattern();
   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to