diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -341,7 +341,10 @@ T->isFunctionReferenceType()) return llvm::None; // Attempt to evaluate. If expr is dependent, evaluation crashes! - if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx)) + if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx) || + // Disable printing for record-types, as they are usually confusing and + // might make clang crash while printing the expressions. + Constant.Val.isStruct() || Constant.Val.isUnion()) return llvm::None; // Show enums symbolically, not numerically like APValue::printPretty(). @@ -353,7 +356,7 @@ if (ECD->getInitVal() == Val) return llvm::formatv("{0} ({1})", ECD->getNameAsString(), Val).str(); } - return Constant.Val.getAsString(Ctx, E->getType()); + return Constant.Val.getAsString(Ctx, T); } llvm::Optional printExprValue(const SelectionTree::Node *N, diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -696,6 +696,18 @@ HI.Parameters->back().Name = "v"; HI.AccessSpecifier = "public"; }}, + {// Field type initializer. + R"cpp( + struct X { int x = 2; }; + X ^[[x]]; + )cpp", + [](HoverInfo &HI) { + HI.Name = "x"; + HI.Kind = index::SymbolKind::Variable; + HI.NamespaceScope = ""; + HI.Definition = "X x"; + HI.Type = "struct X"; + }}, }; for (const auto &Case : Cases) { SCOPED_TRACE(Case.Code); @@ -978,13 +990,14 @@ HI.LocalScope = "Foo::"; HI.Type = "int"; HI.Definition = "int x"; - HI.Value = "{1}"; + // FIXME: Initializer for x is a DesignatedInitListExpr, hence it is + // of struct type and omitted. }}, { R"cpp(// Field, field designator - struct Foo { int x; }; + struct Foo { int x; int y; }; int main() { - Foo bar = { .^[[x]] = 2 }; + Foo bar = { .^[[x]] = 2, .y = 2 }; } )cpp", [](HoverInfo &HI) { @@ -994,7 +1007,6 @@ HI.LocalScope = "Foo::"; HI.Type = "int"; HI.Definition = "int x"; - HI.Value = "{2}"; }}, { R"cpp(// Method call @@ -1592,7 +1604,6 @@ HI.LocalScope = "test::"; HI.Type = "struct Test &&"; HI.Definition = "Test &&test = {}"; - HI.Value = "{}"; }}, { R"cpp(// auto on alias @@ -1651,7 +1662,6 @@ HI.NamespaceScope = ""; HI.Name = "foo"; HI.Type = "cls>>"; - HI.Value = "{}"; }}, { R"cpp(// type of nested templates.