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

xndcn via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 08:25:51 PST 2020


xndcn updated this revision to Diff 307622.
xndcn added a comment.

Update the diff with `getHoverContents(const NamedDecl ..)` overload function.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92041/new/

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,22 @@
             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 = "Foo";
+            HI.Kind = index::SymbolKind::Class;
+            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
@@ -630,7 +630,8 @@
 
 // Generates hover info for evaluatable expressions.
 // FIXME: Support hover for literals (esp user-defined)
-llvm::Optional<HoverInfo> getHoverContents(const Expr *E, ParsedAST &AST) {
+llvm::Optional<HoverInfo> getHoverContents(const Expr *E, ParsedAST &AST,
+                                           const SymbolIndex *Index) {
   // There's not much value in hovering over "42" and getting a hover card
   // saying "42 is an int", similar for other literals.
   if (isLiteral(E))
@@ -647,6 +648,10 @@
     HI.Value = *Val;
     HI.Name = std::string(getNameForExpr(E));
     return HI;
+  } else if (const CXXThisExpr *CTE = dyn_cast<CXXThisExpr>(E)) {
+    const NamedDecl *D = CTE->getType()->getPointeeType()->getAsCXXRecordDecl();
+    HI = getHoverContents(D, Index);
+    return HI;
   }
   return llvm::None;
 }
@@ -861,7 +866,7 @@
           HI->Value = printExprValue(N, AST.getASTContext());
         maybeAddCalleeArgInfo(N, *HI, AST.getASTContext().getPrintingPolicy());
       } else if (const Expr *E = N->ASTNode.get<Expr>()) {
-        HI = getHoverContents(E, AST);
+        HI = getHoverContents(E, AST, Index);
       }
       // FIXME: support hovers for other nodes?
       //  - built-in types


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92041.307622.patch
Type: text/x-patch
Size: 2324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201125/2a8efd12/attachment.bin>


More information about the cfe-commits mailing list