Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang-tools-extra/clangd/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/clangd/unittests/HoverTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3349,6 +3349,17 @@ 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 AST = TestTU::withCode(T.code()).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
Expand Down