[clang-tools-extra] e51484a - [clangd] Fix hover 'local scope' to include class template params

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 19 01:52:51 PST 2019


Author: Sam McCall
Date: 2019-11-19T10:52:38+01:00
New Revision: e51484abd402016a385e17896e87877b77bf4c7b

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

LOG: [clangd] Fix hover 'local scope' to include class template params

Summary: Fixes the last part of https://github.com/clangd/clangd/issues/76

Reviewers: kadircet

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

Tags: #clang

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/XRefs.cpp
    clang-tools-extra/clangd/unittests/XRefsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index ce8e59553622..3165633e60f9 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -414,12 +414,12 @@ static PrintingPolicy printingPolicyForDecls(PrintingPolicy Base) {
 static std::string getLocalScope(const Decl *D) {
   std::vector<std::string> Scopes;
   const DeclContext *DC = D->getDeclContext();
-  auto GetName = [](const Decl *D) {
-    const NamedDecl *ND = dyn_cast<NamedDecl>(D);
-    std::string Name = ND->getNameAsString();
-    // FIXME(sammccall): include template params/specialization args?.
-    if (!Name.empty())
-      return Name;
+  auto GetName = [](const TypeDecl *D) {
+    if (!D->getDeclName().isEmpty()) {
+      PrintingPolicy Policy = D->getASTContext().getPrintingPolicy();
+      Policy.SuppressScope = true;
+      return declaredType(D).getAsString(Policy);
+    }
     if (auto RD = dyn_cast<RecordDecl>(D))
       return ("(anonymous " + RD->getKindName() + ")").str();
     return std::string("");

diff  --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index 9817ba643894..483f216ca666 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -910,13 +910,13 @@ void foo())cpp";
        }},
       // Constructor of partially-specialized class template
       {R"cpp(
-          template<typename> struct X;
+          template<typename, typename=void> struct X;
           template<typename T> struct X<T*>{ [[^X]](); };
           )cpp",
        [](HoverInfo &HI) {
          HI.NamespaceScope = "";
          HI.Name = "X";
-         HI.LocalScope = "X::";        // FIXME: Should be X<T *>::
+         HI.LocalScope = "X<T *>::"; // FIXME: X<T *, void>::
          HI.Kind = SymbolKind::Constructor;
          HI.ReturnType = "X<T *>";
          HI.Definition = "X()";
@@ -1029,8 +1029,8 @@ void foo())cpp";
          HI.Type = "enum Color";
          HI.Value = "1";
        }},
-      // FIXME: We should use the Decl referenced, even if it comes from an
-      // implicit instantiation.
+      // FIXME: We should use the Decl referenced, even if from an implicit
+      // instantiation. Then the scope would be Add<1, 2> and the value 3.
       {R"cpp(
         template<int a, int b> struct Add {
           static constexpr int result = a + b;
@@ -1043,7 +1043,7 @@ void foo())cpp";
          HI.Kind = SymbolKind::Property;
          HI.Type = "const int";
          HI.NamespaceScope = "";
-         HI.LocalScope = "Add::";
+         HI.LocalScope = "Add<a, b>::";
        }},
       {R"cpp(
         const char *[[ba^r]] = "1234";


        


More information about the cfe-commits mailing list