[clang] f3a8393 - [clang] Convert to a C23 enum with bool underlying type like bool (#244494)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 08:52:00 PST 2026
Author: chessMan
Date: 2026-11-24T08:51:54-08:00
New Revision: f3a8393d4cf38b46d8806cb934ec26f8d69d363c
URL: https://github.com/llvm/llvm-project/commit/f3a8393d4cf38b46d8806cb934ec26f8d69d363c
DIFF: https://github.com/llvm/llvm-project/commit/f3a8393d4cf38b46d8806cb934ec26f8d69d363c.diff
LOG: [clang] Convert to a C23 enum with bool underlying type like bool (#244494)
In C23, conversion to an enum with a fixed underlying type has the
semantics of conversion to the underlying type (6.7.3.3p16). For `enum E
: bool` clang truncated instead of comparing against zero, so `(enum E :
bool)2` was 0, and converting a double like 2.5 emitted `fptoui double
to i1` (poison), giving different results at -O0 and -O3. Compound
assignment and increment had the same problem: `e += 2` with e == 0 gave
0, and `e++` with e == 1 gave 0.
`Type::getScalarTypeKind()` now returns `STK_Bool` for such an enum, so
casts get the `*ToBoolean` cast kinds, as C++ `static_cast` already
does. In CodeGen, `EmitScalarConversion` and
`EmitComplexToScalarConversion` treat the enum as bool when it is the
destination, which covers compound assignment, and the bool special case
for `++` (plain and atomic) covers it too.
Fixes #241289
Added:
clang/test/CodeGen/enum-bool.c
Modified:
clang/docs/ReleaseNotes.md
clang/lib/AST/Type.cpp
clang/lib/CodeGen/CGExprScalar.cpp
clang/test/C/C23/n3030.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 006b643e582342..c1860216d38d74 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -990,6 +990,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed conversion from `float` to `bool` in `__builtin_convertvector`. (#GH228649)
- Fixed `-print-dependency-directives-minimized-source` output for object-like macros starting with '(' so that it is not incorrectly turned into a function-like macro.
- Fixed an exponential time-increase bug when collecting argument dependent lookup candidates when the same type is used several times at each level.
+- Fixed conversion to a C23 enumeration with an underlying type of `bool`, which truncated the value instead of converting it like `bool` (e.g. `(enum E : bool)2` was 0 instead of 1). This also affected compound assignment and increment. (#GH241289)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 7eafa13962e793..349b818e419b4f 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2606,7 +2606,12 @@ Type::ScalarTypeKind Type::getScalarTypeKind() const {
} else if (isa<MemberPointerType>(T)) {
return STK_MemberPointer;
} else if (isa<EnumType>(T)) {
- assert(T->castAsEnumDecl()->isComplete());
+ const EnumDecl *ED = T->castAsEnumDecl();
+ assert(ED->isComplete());
+ // C23 6.7.3.3p16: conversion to an enumerated type with a fixed underlying
+ // type has the same semantics as conversion to the underlying type.
+ if (ED->isFixed() && ED->getIntegerType()->isBooleanType())
+ return STK_Bool;
return STK_Integral;
} else if (const auto *CT = dyn_cast<ComplexType>(T)) {
if (CT->getElementType()->isRealFloatingType())
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 7b698208cacfdb..3d00ddff57ff79 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -176,6 +176,15 @@ static bool MustVisitNullValue(const Expr *E) {
return E->getType()->isNullPtrType();
}
+/// Return true if conversion to \p T has the semantics of conversion to bool:
+/// \p T is bool or an enumeration with a fixed underlying type of bool
+/// (C23 6.7.3.3p16).
+static bool hasBoolConversionSemantics(QualType T) {
+ if (const auto *ED = T->getAsEnumDecl())
+ return ED->isFixed() && ED->getIntegerType()->isBooleanType();
+ return T->isBooleanType();
+}
+
/// If \p E is a widened promoted integer, get its base (unpromoted) type.
static std::optional<QualType> getUnwidenedIntegerType(const ASTContext &Ctx,
const Expr *E) {
@@ -1644,7 +1653,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
// TODO(leonardchan): When necessary, add another if statement checking for
// conversions to fixed point types from other types.
if (SrcType->isFixedPointType()) {
- if (DstType->isBooleanType())
+ if (hasBoolConversionSemantics(DstType))
// It is important that we check this before checking if the dest type is
// an integer because booleans are technically integer types.
// We do not need to check the padding bit on unsigned types if unsigned
@@ -1680,7 +1689,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
llvm::Type *SrcTy = Src->getType();
// Handle conversions to bool first, they are special: comparisons against 0.
- if (DstType->isBooleanType())
+ if (hasBoolConversionSemantics(DstType))
return EmitConversionToBool(Src, SrcType);
llvm::Type *DstTy = ConvertType(DstType);
@@ -1888,7 +1897,7 @@ Value *ScalarExprEmitter::EmitComplexToScalarConversion(
SrcTy = SrcTy->castAs<ComplexType>()->getElementType();
// Handle conversions to bool first, they are special: comparisons against 0.
- if (DstTy->isBooleanType()) {
+ if (hasBoolConversionSemantics(DstTy)) {
// Complex != 0 -> (Real != 0) | (Imag != 0)
Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy, Loc);
Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy, Loc);
@@ -3354,7 +3363,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
type = atomicTy->getValueType();
- if (isInc && type->isBooleanType()) {
+ if (isInc && hasBoolConversionSemantics(type)) {
llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type);
if (isPre) {
Builder.CreateStore(True, LV.getAddress(), LV.isVolatileQualified())
@@ -3370,7 +3379,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
// Special case for atomic increment / decrement on integers, emit
// atomicrmw instructions. We skip this if we want to be doing overflow
// checking, and fall into the slow path with the atomic cmpxchg loop.
- if (!type->isBooleanType() && type->isIntegerType() &&
+ if (!hasBoolConversionSemantics(type) && type->isIntegerType() &&
!(type->isUnsignedIntegerType() &&
CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
CGF.getLangOpts().getSignedOverflowBehavior() !=
@@ -3426,7 +3435,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
// -> bool = ((int)bool + 1 != 0)
// An interesting aspect of this is that increment is always true.
// Decrement does not have this property.
- if (isInc && type->isBooleanType()) {
+ if (isInc && hasBoolConversionSemantics(type)) {
value = Builder.getTrue();
// Most common case by far: integer increment.
diff --git a/clang/test/C/C23/n3030.c b/clang/test/C/C23/n3030.c
index 94ea7037edd117..d38184b50aee6c 100644
--- a/clang/test/C/C23/n3030.c
+++ b/clang/test/C/C23/n3030.c
@@ -59,6 +59,14 @@ constexpr int b = _Generic((enum underlying)b0, int: 2, unsigned char: 1, defaul
static_assert(a == 1);
static_assert(b == 1);
+// Conversion to an enumeration with an underlying type of bool has the
+// semantics of conversion to bool (C23 6.7.3.3p16).
+enum eb : bool { eb_false, eb_true };
+static_assert((enum eb)2 == eb_true);
+static_assert((enum eb)-1 == eb_true);
+static_assert((enum eb)0.5 == eb_true);
+static_assert((enum eb)0 == eb_false);
+
void f1(enum a : long b); // expected-error {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}}
// expected-warning at -1 {{declaration of 'enum a' will not be visible outside of this function}}
void f2(enum c : long{x} d);
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
new file mode 100644
index 00000000000000..e98ecce22ee8df
--- /dev/null
+++ b/clang/test/CodeGen/enum-bool.c
@@ -0,0 +1,92 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c23 -ffixed-point -emit-llvm -o - %s | FileCheck %s
+
+// C23 6.7.3.3p16: conversion to an enumerated type with a fixed underlying
+// type has the same semantics as conversion to the underlying type. For an
+// underlying type of bool, any nonzero value converts to true, so the value
+// has to be compared against zero rather than truncated.
+
+enum E : bool { Zero, One };
+
+enum E a(int x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @a(
+// CHECK-NOT: trunc
+// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
+// CHECK: ret i1 %tobool
+
+enum E b(int x) { return x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @b(
+// CHECK-NOT: trunc
+// CHECK: %tobool = icmp ne i32 %{{.*}}, 0
+// CHECK: ret i1 %tobool
+
+enum E c(double x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @c(
+// CHECK-NOT: fptoui
+// CHECK: %tobool = fcmp une double %{{.*}}, 0.000000e+00
+// CHECK: ret i1 %tobool
+
+enum E d(double x) { enum E e = x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @d(
+// CHECK-NOT: fptoui
+// CHECK: %tobool = fcmp une double %{{.*}}, 0.000000e+00
+
+enum E f(long long x) { enum E e; e = x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @f(
+// CHECK-NOT: trunc
+// CHECK: %tobool = icmp ne i64 %{{.*}}, 0
+
+enum E g(_Complex double x) { return (enum E)x; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @g(
+// CHECK-NOT: fptoui
+// CHECK: fcmp une double
+// CHECK: fcmp une double
+// CHECK: %[[OR:.*]] = or i1
+// CHECK: ret i1 %[[OR]]
+
+// Compound assignment converts the result back to the enumeration type.
+enum E h(enum E e, int x) { e += x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @h(
+// CHECK: %[[ADD:.*]] = add nsw i32
+// CHECK-NOT: trunc
+// CHECK: icmp ne i32 %[[ADD]], 0
+
+enum E i(enum E e, double x) { e += x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @i(
+// CHECK: %[[ADD:.*]] = fadd double
+// CHECK-NOT: fptoui
+// CHECK: fcmp une double %[[ADD]], 0.000000e+00
+
+enum E j(enum E e, _Complex double x) { e += x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @j(
+// CHECK-NOT: fptoui
+// CHECK: fcmp une double
+// CHECK: fcmp une double
+// CHECK: or i1
+
+enum E k(enum E e, _Accum x) { e += x; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @k(
+// CHECK: add i47
+// CHECK: %[[SUM:.*]] = trunc i47 %{{.*}} to i32
+// CHECK: icmp ne i32 %[[SUM]], 0
+
+// Like bool, increment always gives true; decrement flips the value.
+enum E l(enum E e) { return ++e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @l(
+// CHECK-NOT: = add
+// CHECK: store i8 1, ptr
+// CHECK: ret i1 true
+
+enum E m(enum E e) { e++; return e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @m(
+// CHECK-NOT: = add
+// CHECK: store i8 1, ptr
+
+enum E n(enum E e) { return --e; }
+// CHECK-LABEL: define{{.*}} zeroext i1 @n(
+// CHECK: %[[DEC:.*]] = add i1 %{{.*}}, true
+// CHECK: ret i1 %[[DEC]]
+
+void o(_Atomic(enum E) *p) { ++*p; }
+// CHECK-LABEL: define{{.*}} void @o(
+// CHECK-NOT: atomicrmw add
+// CHECK: store atomic i8 1, ptr %{{.*}} seq_cst
More information about the cfe-commits
mailing list