[clangd] Fix a crash when presenting values for Hover
authorIlya Biryukov <ibiryukov@google.com>
Fri, 2 Aug 2019 15:23:04 +0000 (15:23 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Fri, 2 Aug 2019 15:23:04 +0000 (15:23 +0000)
Summary:
We should pass the expression type, not a variable type when printing
the resulting value. Variable type may be different from what the
pretty-printing function expects, e.g. have references.

Reviewers: sammccall

Reviewed By: sammccall

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

Tags: #clang

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

llvm-svn: 367687

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

index 7d2f04c3e3232f1b16c224d1fadd6f9518570c1f..b520c4daebad0698597a1fa29f7f112ffd44d909 100644 (file)
@@ -715,7 +715,7 @@ static HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
         HI.Value.emplace();
         llvm::raw_string_ostream ValueOS(*HI.Value);
         Result.Val.printPretty(ValueOS, const_cast<ASTContext &>(Ctx),
-                               Var->getType());
+                               Init->getType());
       }
     }
   }
index 3439bc7d15d37f0206e39ad76368d767656c1f2e..4a95a9935f2f69eead21833a6d7cb2590e811e9b 100644 (file)
@@ -1793,6 +1793,16 @@ TEST(Hover, All) {
           "int\n"
           "]",
       },
+      {
+          R"cpp(// Should not crash when evaluating the initializer.
+            struct Test {};
+            void test() { Test && te^st = {}; }
+          )cpp",
+          "text[Declared in]code[test]\n"
+          "codeblock(cpp) [\n"
+          "struct Test &&test = {}\n"
+          "]",
+      },
   };
 
   // Create a tiny index, so tests above can verify documentation is fetched.