[clangd] Fix a crash when presenting values for Hover

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

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@367687 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/clangd/XRefs.cpp b/clangd/XRefs.cpp
index 7d2f04c..b520c4d 100644
--- a/clangd/XRefs.cpp
+++ b/clangd/XRefs.cpp
@@ -715,7 +715,7 @@
         HI.Value.emplace();
         llvm::raw_string_ostream ValueOS(*HI.Value);
         Result.Val.printPretty(ValueOS, const_cast<ASTContext &>(Ctx),
-                               Var->getType());
+                               Init->getType());
       }
     }
   }
diff --git a/clangd/unittests/XRefsTests.cpp b/clangd/unittests/XRefsTests.cpp
index 3439bc7..4a95a99 100644
--- a/clangd/unittests/XRefsTests.cpp
+++ b/clangd/unittests/XRefsTests.cpp
@@ -1793,6 +1793,16 @@
           "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.