Skip to content

Commit

Permalink
[clangd] Fix hover crash on InitListExpr.
Browse files Browse the repository at this point in the history
  • Loading branch information
hokein committed Jul 10, 2020
1 parent 22c8a08 commit 015a0fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion clang-tools-extra/clangd/Hover.cpp
Expand Up @@ -329,13 +329,23 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,

llvm::Optional<std::string> printExprValue(const Expr *E,
const ASTContext &Ctx) {
Expr::EvalResult Constant;
// InitListExpr has two forms, syntactic and semantic. They are the same thing
// (refer to a same AST node) in most cases.
// When they are different, RAV returns the syntactic form, and we should feed
// the semantic form to EvaluateAsRValue.
if (const auto *ILE = llvm::dyn_cast<InitListExpr>(E)) {
if (!ILE->isSemanticForm())
E = ILE->getSemanticForm();
}

// Evaluating [[foo]]() as "&foo" isn't useful, and prevents us walking up
// to the enclosing call.
QualType T = E->getType();
if (T.isNull() || T->isFunctionType() || T->isFunctionPointerType() ||
T->isFunctionReferenceType())
return llvm::None;

Expr::EvalResult Constant;
// Attempt to evaluate. If expr is dependent, evaluation crashes!
if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx) ||
// Disable printing for record-types, as they are usually confusing and
Expand Down
15 changes: 15 additions & 0 deletions clang-tools-extra/clangd/unittests/HoverTests.cpp
Expand Up @@ -801,6 +801,21 @@ class Foo {})cpp";
HI.LocalScope = "Foo::";
HI.Type = "int";
HI.AccessSpecifier = "public";
}},
{// No crash on InitListExpr.
R"cpp(
struct Foo {
int a[10];
};
constexpr Foo k2 = {
^[[{]]1} // FIXME: why the hover range is 1 character?
};
)cpp",
[](HoverInfo &HI) {
HI.Name = "expression";
HI.Kind = index::SymbolKind::Unknown;
HI.Type = "int [10]";
HI.Value = "{1}";
}}};
for (const auto &Case : Cases) {
SCOPED_TRACE(Case.Code);
Expand Down

0 comments on commit 015a0fa

Please sign in to comment.