[clang-tools-extra] [clangd] Fix crash on hover over `UL` enums with MSB set (PR #173187)
Mythreya Kuricheti via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 21 05:01:10 PST 2025
https://github.com/MythreyaK updated https://github.com/llvm/llvm-project/pull/173187
>From e23269bbf04f18883a000b6e647fe7795ce86489 Mon Sep 17 00:00:00 2001
From: Mythreya <git at mythreya.dev>
Date: Sun, 21 Dec 2025 05:00:58 -0800
Subject: [PATCH] [clangd] Fix crash on hover over large `UL` enums
---
clang-tools-extra/clangd/Hover.cpp | 4 +-
.../clangd/unittests/HoverTests.cpp | 59 +++++++++++++++++++
2 files changed, 61 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index 34369e188d4ec..7ec7e6d19105a 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -451,7 +451,7 @@ std::optional<std::string> printExprValue(const Expr *E,
// Show enums symbolically, not numerically like APValue::printPretty().
if (T->isEnumeralType() && Constant.Val.isInt() &&
- Constant.Val.getInt().getSignificantBits() <= 64) {
+ Constant.Val.getInt().isRepresentableByInt64()) {
// Compare to int64_t to avoid bit-width match requirements.
int64_t Val = Constant.Val.getInt().getExtValue();
for (const EnumConstantDecl *ECD : T->castAsEnumDecl()->enumerators())
@@ -462,7 +462,7 @@ std::optional<std::string> printExprValue(const Expr *E,
}
// Show hex value of integers if they're at least 10 (or negative!)
if (T->isIntegralOrEnumerationType() && Constant.Val.isInt() &&
- Constant.Val.getInt().getSignificantBits() <= 64 &&
+ Constant.Val.getInt().isRepresentableByInt64() &&
Constant.Val.getInt().uge(10))
return llvm::formatv("{0} ({1})", Constant.Val.getAsString(Ctx, T),
printHex(Constant.Val.getInt()))
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index eb858ff616e90..0c8423bd5fb2a 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -5015,6 +5015,65 @@ TEST(Hover, FunctionParameters) {
}
}
+TEST(Hover, GH2381) {
+ Annotations Code(R"cpp(
+ struct Foo {
+ enum Bar {
+ A = -42UL,
+ B = ~0UL,
+ C = 0xFFFFFFFFFFFFFFFFUL,
+ };
+ };
+ constexpr auto va$a^ = Foo::A;
+ constexpr auto vb$b^ = Foo::B;
+ constexpr auto vc$c^ = Foo::C;
+ )cpp");
+
+ TestTU TU = TestTU::withCode(Code.code());
+ auto AST = TU.build();
+
+ {
+ auto H = getHover(AST, Code.point("a"), format::getLLVMStyle(), nullptr);
+
+ ASSERT_TRUE(H);
+ EXPECT_EQ(H->Name, "va");
+ EXPECT_EQ(H->Kind, index::SymbolKind::Variable);
+ EXPECT_EQ(H->NamespaceScope, "");
+ EXPECT_EQ(H->LocalScope, "");
+ EXPECT_EQ(H->Type, "const Foo::Bar");
+ EXPECT_EQ(H->Definition, "constexpr auto va = Foo::A");
+ // FIXME: Should be "A (FFFFFFFFFFFFFFD6)
+ EXPECT_EQ(H->Value, "18446744073709551574");
+ }
+
+ {
+ auto H = getHover(AST, Code.point("b"), format::getLLVMStyle(), nullptr);
+
+ ASSERT_TRUE(H);
+ EXPECT_EQ(H->Name, "vb");
+ EXPECT_EQ(H->Kind, index::SymbolKind::Variable);
+ EXPECT_EQ(H->NamespaceScope, "");
+ EXPECT_EQ(H->LocalScope, "");
+ EXPECT_EQ(H->Type, "const Foo::Bar");
+ EXPECT_EQ(H->Definition, "constexpr auto vb = Foo::B");
+ // FIXME: Should be "B (0xFFFFFFFFFFFFFFFF)");
+ EXPECT_EQ(H->Value, "18446744073709551615");
+ }
+
+ {
+ auto H = getHover(AST, Code.point("c"), format::getLLVMStyle(), nullptr);
+
+ ASSERT_TRUE(H);
+ EXPECT_EQ(H->Name, "vc");
+ EXPECT_EQ(H->Kind, index::SymbolKind::Variable);
+ EXPECT_EQ(H->NamespaceScope, "");
+ EXPECT_EQ(H->LocalScope, "");
+ EXPECT_EQ(H->Type, "const Foo::Bar");
+ EXPECT_EQ(H->Definition, "constexpr auto vc = Foo::C");
+ // FIXME: Should be "C (0xFFFFFFFFFFFFFFFF)");
+ EXPECT_EQ(H->Value, "18446744073709551615");
+ }
+}
} // namespace
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list