[clangd] Fix hover crash on invalid decls

Summary: This also changes the way we display Size and Offset to be independent.

Reviewers: sammccall

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

Tags: #clang

Differential Revision: https://reviews.llvm.org/D83143
This commit is contained in:
Kadir Cetinkaya 2020-07-03 20:52:41 +02:00
parent 0939e04e41
commit 50ba9f994c
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
2 changed files with 19 additions and 4 deletions

View File

@ -672,11 +672,10 @@ void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
if (Record)
Record = Record->getDefinition();
if (Record && !Record->isDependentType()) {
uint64_t OffsetBits = Ctx.getFieldOffset(FD);
if (auto Size = Ctx.getTypeSizeInCharsIfKnown(FD->getType())) {
if (auto Size = Ctx.getTypeSizeInCharsIfKnown(FD->getType()))
HI.Size = Size->getQuantity();
HI.Offset = OffsetBits / 8;
}
if (!FD->isInvalidDecl())
HI.Offset = Ctx.getFieldOffset(FD) / 8;
}
return;
}

View File

@ -772,6 +772,22 @@ class Foo {})cpp";
HI.CallPassType->PassBy = PassMode::Value;
HI.CallPassType->Converted = false;
}},
{// Dont crash on invalid decl
R"cpp(
// error-ok
struct Foo {
Bar [[x^x]];
};)cpp",
[](HoverInfo &HI) {
HI.Name = "xx";
HI.Kind = index::SymbolKind::Field;
HI.NamespaceScope = "";
HI.Definition = "int xx";
HI.LocalScope = "Foo::";
HI.Size = 4;
HI.Type = "int";
HI.AccessSpecifier = "public";
}},
};
for (const auto &Case : Cases) {
SCOPED_TRACE(Case.Code);