[clang] [clang] Allow fixed-point to bool enumeration conversions (PR #245853)
Sendao Yan via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 19:24:16 PST 2026
https://github.com/sendaoYan updated https://github.com/llvm/llvm-project/pull/245853
>From cba21bc29d6a82f96fad7df7586a1111e72449a1 Mon Sep 17 00:00:00 2001
From: Sendao Yan <yansendao at 126.com>
Date: Wed, 25 Nov 2026 10:24:15 +0800
Subject: [PATCH] [clang] Allow fixed-point to bool enumeration conversions
Since #244494, Type::getScalarTypeKind() returns STK_Bool for an
enumeration with a fixed bool underlying type, so converting a
fixed-point value to such an enumeration selects CK_FixedPointToBoolean.
VisitCastExpr() still asserted that the destination of that cast kind is
bool, which crashed an assertions build:
enum E : bool { F, T };
enum E to_enum(_Accum x) { return (enum E)x; }
EmitScalarConversion() already treats such enumerations as bool, so only
the assertion needs relaxing.
Fixes https://github.com/llvm/llvm-project/issues/245850
---
clang/lib/CodeGen/CGExprScalar.cpp | 3 ++-
clang/test/CodeGen/enum-bool.c | 5 +++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 3d00ddff57ff79..1378db5715fbf2 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3051,7 +3051,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
case CK_FixedPointToBoolean:
assert(E->getType()->isFixedPointType() &&
"Expected src type to be fixed point type");
- assert(DestTy->isBooleanType() && "Expected dest type to be boolean type");
+ assert(hasBoolConversionSemantics(DestTy) &&
+ "Expected dest type to be a boolean type");
return EmitScalarConversion(Visit(E), E->getType(), DestTy,
CE->getExprLoc());
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
index e98ecce22ee8df..872834d573f1bb 100644
--- a/clang/test/CodeGen/enum-bool.c
+++ b/clang/test/CodeGen/enum-bool.c
@@ -90,3 +90,8 @@ void o(_Atomic(enum E) *p) { ++*p; }
// CHECK-LABEL: define{{.*}} void @o(
// CHECK-NOT: atomicrmw add
// CHECK: store atomic i8 1, ptr %{{.*}} seq_cst
+
+enum E r(_Accum x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @r(
+// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
+// CHECK: ret i1 %tobool
More information about the cfe-commits
mailing list