[PATCH] D71545: [clangd] Improve hover for auto on template instantiations

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 16 06:33:14 PST 2019


kadircet created this revision.
kadircet added a reviewer: ilya-biryukov.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay.
Herald added a project: clang.
kadircet added a parent revision: D71544: [clangd] Improve printing of lambda names.

Follow-up to D71543 <https://reviews.llvm.org/D71543>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71545

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -373,7 +373,7 @@
         }
         )cpp",
        [](HoverInfo &HI) {
-         HI.Name = "Foo";
+         HI.Name = "Foo<int>";
          HI.Kind = index::SymbolKind::Class;
        }},
       // auto on specialized template
@@ -543,6 +543,19 @@
             HI.Name = "Foo";
             HI.Kind = index::SymbolKind::Class;
           }},
+      {
+          R"cpp(
+          template <typename T, typename C = int> class Foo {};
+          class X;
+          void foo() {
+            [[^auto]] x = Foo<X>();
+          }
+          )cpp",
+          [](HoverInfo &HI) {
+            // FIXME: Drop default arguments.
+            HI.Name = "Foo<X, int>";
+            HI.Kind = index::SymbolKind::Class;
+          }},
   };
   for (const auto &Case : Cases) {
     SCOPED_TRACE(Case.Code);
@@ -1193,7 +1206,7 @@
           )cpp",
           [](HoverInfo &HI) {
             // FIXME: Print template instantiation parameters.
-            HI.Name = "initializer_list";
+            HI.Name = "initializer_list<int>";
             HI.Kind = index::SymbolKind::Class;
           }},
       {
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -24,6 +24,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/PrettyPrinter.h"
 #include "clang/Index/IndexSymbol.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -416,11 +417,22 @@
 
   if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
     // Find the corresponding decl to populate kind and fetch documentation.
-    DeclRelationSet Rel = DeclRelation::TemplatePattern | DeclRelation::Alias;
+    DeclRelationSet Rel = DeclRelation::TemplateInstantiation |
+                          DeclRelation::TemplatePattern | DeclRelation::Alias;
     auto Decls =
         targetDecl(ast_type_traits::DynTypedNode::create(*Deduced), Rel);
-    HI = getHoverContents(*Deduced, Decls.empty() ? nullptr : Decls.front(),
-                          AST.getASTContext(), Index);
+    // Select the target decl, biased towards instantiations.
+    auto *D = [&Decls]() -> const Decl * {
+      if (Decls.empty())
+        return nullptr;
+      auto *Spec = llvm::find_if(Decls, [](const Decl *D) {
+        return llvm::isa<ClassTemplateSpecializationDecl>(D);
+      });
+      if (Spec != Decls.end())
+        return *Spec;
+      return Decls.front();
+    }();
+    HI = getHoverContents(*Deduced, D, AST.getASTContext(), Index);
   } else if (auto M = locateMacroAt(SourceLocationBeg, AST.getPreprocessor())) {
     HI = getHoverContents(*M, AST);
   } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71545.234047.patch
Type: text/x-patch
Size: 2983 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191216/1f7b8c0c/attachment.bin>


More information about the cfe-commits mailing list