[clang-tools-extra] 6a69d3c - [clangd] Handle DeducedTemplateSpecializationType in TargetFinder

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 9 13:14:25 PST 2020


Author: Nathan Ridge
Date: 2020-01-09T16:14:11-05:00
New Revision: 6a69d3c6b3da3d4e7709d11fd52b8e5540265280

URL: https://github.com/llvm/llvm-project/commit/6a69d3c6b3da3d4e7709d11fd52b8e5540265280
DIFF: https://github.com/llvm/llvm-project/commit/6a69d3c6b3da3d4e7709d11fd52b8e5540265280.diff

LOG: [clangd] Handle DeducedTemplateSpecializationType in TargetFinder

Summary:
This is a workaround for https://bugs.llvm.org/show_bug.cgi?id=42914.
Once that is fixed, the handling in VisitDeducedTyped() should be sufficient.

Fixes https://github.com/clangd/clangd/issues/242

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D72119

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp
index fb3001fca94c..119b932956be 100644
--- a/clang-tools-extra/clangd/FindTarget.cpp
+++ b/clang-tools-extra/clangd/FindTarget.cpp
@@ -358,6 +358,17 @@ struct TargetFinder {
         // 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
index 8c9727fb64eb..f34205a81f30 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -315,6 +315,20 @@ TEST_F(TargetDeclTest, ClassTemplate) {
   EXPECT_DECLS("TemplateSpecializationTypeLoc",
                {"template<> class Foo<int *>", Rel::TemplateInstantiation},
                {"template <typename T> class Foo<T *>", Rel::TemplatePattern});
+
+  Code = R"cpp(
+    // Class template argument deduction
+    template <typename T>
+    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 @@ class FindExplicitReferencesTest : public ::testing::Test {
     // 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 @@ TEST_F(FindExplicitReferencesTest, All) {
                 };
             )cpp",
            "0: targets = {size}, decl\n"
-           "1: targets = {E}\n"}};
+           "1: targets = {E}\n"},
+       // Class template argument deduction
+       {
+           R"cpp(
+                template <typename T>
+                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;


        


More information about the cfe-commits mailing list