[PATCH] D92977: [clangd] Improve hover and goToDefinition on auto and dectype

Quentin Chateau via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 9 15:32:01 PST 2020


qchateau created this revision.
Herald added subscribers: usaxena95, kadircet, arphaman.
qchateau requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

Make the hover content on auto and decltype
look like the hover content we would get on the
deduced type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92977

Files:
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/XRefs.cpp


Index: clang-tools-extra/clangd/XRefs.cpp
===================================================================
--- clang-tools-extra/clangd/XRefs.cpp
+++ clang-tools-extra/clangd/XRefs.cpp
@@ -667,21 +667,44 @@
     return {};
   }
 
-  const syntax::Token *TouchedIdentifier =
-      syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
-  if (TouchedIdentifier)
-    if (auto Macro =
-            locateMacroReferent(*TouchedIdentifier, AST, *MainFilePath))
-      // Don't look at the AST or index if we have a macro result.
-      // (We'd just return declarations referenced from the macro's
-      // expansion.)
-      return {*std::move(Macro)};
-
-  ASTNodeKind NodeKind;
-  auto ASTResults = locateASTReferent(*CurLoc, TouchedIdentifier, AST,
-                                      *MainFilePath, Index, &NodeKind);
-  if (!ASTResults.empty())
-    return ASTResults;
+  auto TokensTouchingCursor =
+      syntax::spelledTokensTouching(*CurLoc, AST.getTokens());
+  for (const auto &Tok : TokensTouchingCursor) {
+    if (Tok.kind() == tok::identifier) {
+      if (auto Macro = locateMacroReferent(Tok, AST, *MainFilePath))
+        // Don't look at the AST or index if we have a macro result.
+        // (We'd just return declarations referenced from the macro's
+        // expansion.)
+        return {*std::move(Macro)};
+
+      ASTNodeKind NodeKind;
+      auto ASTResults = locateASTReferent(*CurLoc, &Tok, AST, *MainFilePath,
+                                          Index, &NodeKind);
+      if (!ASTResults.empty())
+        return ASTResults;
+    } else if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype) {
+      if (auto Deduced = getDeducedType(AST.getASTContext(), Tok.location())) {
+        const NamedDecl *D = Deduced->getTypePtr()->getAsTagDecl();
+        if (!D)
+          continue;
+
+        D = getPreferredDecl(D);
+        auto Loc = makeLocation(AST.getASTContext(), nameLocation(*D, SM),
+                                *MainFilePath);
+        if (!Loc)
+          continue;
+
+        LocatedSymbol LocSym;
+        LocSym.Name = printName(AST.getASTContext(), *D);
+        LocSym.PreferredDeclaration = *Loc;
+        if (const NamedDecl *Def = getDefinition(D))
+          LocSym.Definition = makeLocation(
+              AST.getASTContext(), nameLocation(*Def, SM), *MainFilePath);
+
+        return {std::move(LocSym)};
+      }
+    }
+  }
 
   // If the cursor can't be resolved directly, try fallback strategies.
   auto Word =
@@ -695,7 +718,7 @@
             Word->Text);
         return {*std::move(Macro)};
       }
-      ASTResults =
+      auto ASTResults =
           locateASTReferent(NearbyIdent->location(), NearbyIdent, AST,
                             *MainFilePath, Index, /*NodeKind=*/nullptr);
       if (!ASTResults.empty()) {
@@ -708,6 +731,7 @@
       }
     }
     // No nearby word, or it didn't refer to anything either. Try the index.
+    ASTNodeKind NodeKind;
     auto TextualResults =
         locateSymbolTextually(*Word, AST, Index, *MainFilePath, NodeKind);
     if (!TextualResults.empty())
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -556,12 +556,7 @@
   HoverInfo HI;
 
   if (const auto *D = T->getAsTagDecl()) {
-    HI.Name = printName(ASTCtx, *D);
-    HI.Kind = index::getSymbolInfo(D).Kind;
-
-    const auto *CommentD = getDeclForComment(D);
-    HI.Documentation = getDeclComment(ASTCtx, *CommentD);
-    enhanceFromIndex(HI, *CommentD, Index);
+    return getHoverContents(D, Index);
   } else {
     // Builtin types
     auto Policy = printingPolicyForDecls(ASTCtx.getPrintingPolicy());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92977.310686.patch
Type: text/x-patch
Size: 3757 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201209/5f3b2478/attachment.bin>


More information about the cfe-commits mailing list