[clangd] Fix hover crashing on null types

Summary: Fixes https://github.com/clangd/clangd/issues/225

Reviewers: sammccall, ilya-biryukov

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

Tags: #clang

Differential Revision: https://reviews.llvm.org/D71403
This commit is contained in:
Kadir Cetinkaya 2019-12-12 10:41:27 +01:00
parent d7357c52a4
commit 75b04c7af9
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
2 changed files with 21 additions and 2 deletions

View File

@ -242,12 +242,13 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
// FIXME: handle variadics.
}
llvm::Optional<std::string> printExprValue(const Expr *E, const ASTContext &Ctx) {
llvm::Optional<std::string> printExprValue(const Expr *E,
const ASTContext &Ctx) {
Expr::EvalResult Constant;
// Evaluating [[foo]]() as "&foo" isn't useful, and prevents us walking up
// to the enclosing call.
QualType T = E->getType();
if (T->isFunctionType() || T->isFunctionPointerType() ||
if (T.isNull() || T->isFunctionType() || T->isFunctionPointerType() ||
T->isFunctionReferenceType())
return llvm::None;
// Attempt to evaluate. If expr is dependent, evaluation crashes!

View File

@ -506,6 +506,24 @@ void foo())cpp";
HI.NamespaceScope = "";
HI.Value = "&\"1234\"[0]";
}},
{R"cpp(// Should not crash
template <typename T>
struct Tmpl {
Tmpl(int name);
};
template <typename A>
void boom(int name) {
new Tmpl<A>([[na^me]]);
})cpp",
[](HoverInfo &HI) {
HI.Name = "name";
HI.Definition = "int name";
HI.Kind = index::SymbolKind::Parameter;
HI.Type = "int";
HI.NamespaceScope = "";
HI.LocalScope = "boom::";
}},
};
for (const auto &Case : Cases) {
SCOPED_TRACE(Case.Code);