[PATCH] D92041: Add hover info for `this` expr

xndcn via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 09:07:26 PST 2020


xndcn created this revision.
xndcn added reviewers: sammccall, kadircet.
xndcn added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, usaxena95, arphaman.
Herald added a project: clang.
xndcn requested review of this revision.

How about add hover information for `this` expr?
It seems useful to show related information about the class for `this` expr sometimes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92041

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
@@ -2019,6 +2019,23 @@
             HI.NamespaceScope = "";
             HI.Definition = "@interface MYObject\n at end";
           }},
+      {
+          R"cpp(// this expr
+           namespace ns {
+             class Foo {
+               Foo* bar() {
+                 return [[t^his]];
+               }
+             };
+           };
+          )cpp",
+          [](HoverInfo &HI) {
+            HI.Name = "this";
+            HI.Type = "ns::Foo *";
+            HI.Kind = index::SymbolKind::Unknown;
+            HI.NamespaceScope = "ns::";
+            HI.Definition = "class Foo {}";
+          }},
   };
 
   // Create a tiny index, so tests above can verify documentation is fetched.
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -606,6 +606,17 @@
   return HI;
 }
 
+/// Generate a \p Hover object given the \p this pointer.
+HoverInfo getHoverContents(const CXXThisExpr *CTE, const SymbolIndex *Index) {
+  const NamedDecl *D = CTE->getType()->getPointeeType()->getAsCXXRecordDecl();
+  HoverInfo HI = getHoverContents(D, Index);
+  HI.Name = "this";
+  // TODO: determine the symbol kind.
+  HI.Kind = index::SymbolKind::Unknown;
+  HI.Type = printType(CTE->getType(), D->getASTContext().getPrintingPolicy());
+  return HI;
+}
+
 bool isLiteral(const Expr *E) {
   // Unfortunately there's no common base Literal classes inherits from
   // (apart from Expr), therefore these exclusions.
@@ -860,6 +871,8 @@
         if (!HI->Value)
           HI->Value = printExprValue(N, AST.getASTContext());
         maybeAddCalleeArgInfo(N, *HI, AST.getASTContext().getPrintingPolicy());
+      } else if (const CXXThisExpr *CTE = N->ASTNode.get<CXXThisExpr>()) {
+        HI = getHoverContents(CTE, Index);
       } else if (const Expr *E = N->ASTNode.get<Expr>()) {
         HI = getHoverContents(E, AST);
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92041.307379.patch
Type: text/x-patch
Size: 2198 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201124/4ca22b8a/attachment-0001.bin>


More information about the cfe-commits mailing list