[clang-tools-extra] 3d15605 - [clangd][NFC] Make use of TagDecl inside type for hover on auto

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 17 07:34:27 PST 2019


Author: Kadir Cetinkaya
Date: 2019-12-17T16:33:22+01:00
New Revision: 3d15605358ebadcbd7740a61c92b4fea4d6241e5

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

LOG: [clangd][NFC] Make use of TagDecl inside type for hover on auto

Summary:
We were traversing AST twice to get the Decl in case of sugared
types(auto, decltype). They seem to be same in practice, so this patch gets rid
of the second traversal and makes use of TagDecl inside QualType instead.

Reviewers: ilya-biryukov

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

Tags: #clang

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index f55a9c3d1f67..af43af8fcc32 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -342,15 +342,15 @@ HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
 }
 
 /// Generate a \p Hover object given the type \p T.
-HoverInfo getHoverContents(QualType T, const Decl *D, ASTContext &ASTCtx,
-                                  const SymbolIndex *Index) {
+HoverInfo getHoverContents(QualType T, ASTContext &ASTCtx,
+                           const SymbolIndex *Index) {
   HoverInfo HI;
   llvm::raw_string_ostream OS(HI.Name);
   PrintingPolicy Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy());
   T.print(OS, Policy);
   OS.flush();
 
-  if (D) {
+  if (const auto *D = T->getAsTagDecl()) {
     HI.Kind = index::getSymbolInfo(D).Kind;
     enhanceFromIndex(HI, D, Index);
   }
@@ -396,12 +396,7 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
       getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
 
   if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
-    // Find the corresponding decl to populate kind and fetch documentation.
-    DeclRelationSet Rel = 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);
+    HI = getHoverContents(*Deduced, AST.getASTContext(), Index);
   } else if (auto M = locateMacroAt(SourceLocationBeg, AST.getPreprocessor())) {
     HI = getHoverContents(*M, AST);
   } else {

diff  --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 783599ad7ac0..aa44d3bda72f 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -1396,6 +1396,32 @@ TEST(Hover, All) {
             HI.Definition = "struct Test &&test = {}";
             HI.Value = "{}";
           }},
+      {
+          R"cpp(// auto on alias
+          typedef int int_type;
+          ^[[auto]] x = int_type();
+          )cpp",
+          [](HoverInfo &HI) { HI.Name = "int"; }},
+      {
+          R"cpp(// auto on alias
+          struct cls {};
+          typedef cls cls_type;
+          ^[[auto]] y = cls_type();
+          )cpp",
+          [](HoverInfo &HI) {
+            HI.Name = "struct cls";
+            HI.Kind = index::SymbolKind::Struct;
+          }},
+      {
+          R"cpp(// auto on alias
+          template <class>
+          struct templ {};
+          ^[[auto]] z = templ<int>();
+          )cpp",
+          [](HoverInfo &HI) {
+            HI.Name = "struct templ<int>";
+            HI.Kind = index::SymbolKind::Struct;
+          }},
   };
 
   // Create a tiny index, so tests above can verify documentation is fetched.


        


More information about the cfe-commits mailing list