[clangd] Allow hover over 128-bit variable without crashing (#71415)

When hovering over variables larger than 64 bits, with more than 64
active bits, there were assertion failures since Hover is trying to
print the value as a 64-bit hex value.

There is already protection avoiding to call printHex if there is more
than 64 significant bits. And we already truncate and print negative
values using only 32 bits, when possible. So we can simply truncate
values with more than 64 bits to avoid the assert when using
getZExtValue. The result will be that for example a negative 128 bit
variable is printed using 64 bits, when possible.

There is still no support for printing more than 64 bits. That would
involve more changes since for example llvm::FormatterNumber is limited
to 64 bits.

This is a second attempt at landing this patch. Now with protection
to ensure we use a triple that supports __int128_t.
This commit is contained in:
Björn Pettersson 2023-11-08 11:30:03 +01:00 committed by Bjorn Pettersson
parent fdf99b21f3
commit d5cfdcaacb
2 changed files with 17 additions and 1 deletions

View File

@ -408,7 +408,9 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
// -2 => 0xfffffffe
// -2^32 => 0xffffffff00000000
static llvm::FormattedNumber printHex(const llvm::APSInt &V) {
uint64_t Bits = V.getZExtValue();
assert(V.getSignificantBits() <= 64 && "Can't print more than 64 bits.");
uint64_t Bits =
V.getBitWidth() > 64 ? V.trunc(64).getZExtValue() : V.getZExtValue();
if (V.isNegative() && V.getSignificantBits() <= 32)
return llvm::format_hex(uint32_t(Bits), 0);
return llvm::format_hex(Bits, 0);

View File

@ -3349,6 +3349,20 @@ TEST(Hover, NoCrashAPInt64) {
getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
}
TEST(Hover, NoCrashInt128) {
Annotations T(R"cpp(
constexpr __int128_t value = -4;
void foo() { va^lue; }
)cpp");
auto TU = TestTU::withCode(T.code());
// Need a triple that support __int128_t.
TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu");
auto AST = TU.build();
auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
ASSERT_TRUE(H);
EXPECT_EQ(H->Value, "-4 (0xfffffffc)");
}
TEST(Hover, DocsFromMostSpecial) {
Annotations T(R"cpp(
// doc1