[clangd] Fix a hover crash on unsigned 64bit value

This patch adapts to D140059, which makes an assumption that the
caller of `APSInt::getExtValue` promises no narrowing conversion
happens, i.e., from unsigned int64 to signed int64.

It also fixes clangd/clangd#1557.

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D146874
This commit is contained in:
Younan Zhang 2023-03-25 23:15:54 +08:00
parent 12e8463b14
commit 0b103edf5b
2 changed files with 11 additions and 2 deletions

View File

@ -400,9 +400,9 @@ void fillFunctionTypeAndParams(HoverInfo &HI, const Decl *D,
// 100 => 0x64 // 100 => 0x64
// Negative numbers are sign-extended to 32/64 bits // Negative numbers are sign-extended to 32/64 bits
// -2 => 0xfffffffe // -2 => 0xfffffffe
// -2^32 => 0xfffffffeffffffff // -2^32 => 0xffffffff00000000
static llvm::FormattedNumber printHex(const llvm::APSInt &V) { static llvm::FormattedNumber printHex(const llvm::APSInt &V) {
uint64_t Bits = V.getExtValue(); uint64_t Bits = V.getZExtValue();
if (V.isNegative() && V.getSignificantBits() <= 32) if (V.isNegative() && V.getSignificantBits() <= 32)
return llvm::format_hex(uint32_t(Bits), 0); return llvm::format_hex(uint32_t(Bits), 0);
return llvm::format_hex(Bits, 0); return llvm::format_hex(Bits, 0);

View File

@ -3045,6 +3045,15 @@ TEST(Hover, NoCrash) {
getHover(AST, P, format::getLLVMStyle(), nullptr); getHover(AST, P, format::getLLVMStyle(), nullptr);
} }
TEST(Hover, NoCrashAPInt64) {
Annotations T(R"cpp(
constexpr unsigned long value = -1; // wrap around
void foo() { va^lue; }
)cpp");
auto AST = TestTU::withCode(T.code()).build();
getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
}
TEST(Hover, DocsFromMostSpecial) { TEST(Hover, DocsFromMostSpecial) {
Annotations T(R"cpp( Annotations T(R"cpp(
// doc1 // doc1