https://github.com/mohitmishra786 updated 
https://github.com/llvm/llvm-project/pull/244494

>From 572bbf605e301f0c4880b12ea37d461a1d0f24d2 Mon Sep 17 00:00:00 2001
From: Mohit Mishra <[email protected]>
Date: Fri, 20 Nov 2026 08:40:00 +0530
Subject: [PATCH 1/2] [clang] Convert to a C23 enum with bool underlying type
 like bool

C23 6.7.3.3p16 says conversion to an enumerated type with a fixed
underlying type has the same semantics as conversion to the underlying
type. For `enum E : bool`, any nonzero value has to become true, but
PrepareScalarCast classified the enum as STK_Integral and used
CK_IntegralCast / CK_FloatingToIntegral, which truncate. So
`(enum E : bool)2` was 0, and a double 2.5 gave a poison fptoui.

Treat a destination enum with a fixed bool underlying type as STK_Bool
in PrepareScalarCast, which gives CK_IntegralToBoolean,
CK_FloatingToBoolean and the complex-to-boolean kinds. This matches what
C++ static_cast already does for such enums ([expr.static.cast]p8).

For the complex-to-boolean casts, CodeGen decided whether to compare
against zero by looking at the destination type, so it still took the
real part only for the enum. Convert to bool there instead, since the
cast kind already says the result is boolean.

Fixes #241289
---
 clang/docs/ReleaseNotes.md         |  1 +
 clang/lib/CodeGen/CGExprScalar.cpp |  6 ++--
 clang/lib/Sema/SemaExpr.cpp        | 21 ++++++++++----
 clang/test/C/C23/n3030.c           |  8 ++++++
 clang/test/CodeGen/enum-bool.c     | 44 ++++++++++++++++++++++++++++++
 5 files changed, 72 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/CodeGen/enum-bool.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 5c6ba2d846013d..0c313db94308f0 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -952,6 +952,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). (#GH241289)
 
 #### Bug Fixes to Compiler Builtins
 
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index 7b698208cacfdb..b2a37e133ba15d 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3148,9 +3148,11 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_IntegralComplexToBoolean: {
     CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E);
 
+    // The destination may be an enumeration whose underlying type is bool
+    // (C23 6.7.3.3p16), so convert to bool rather than to DestTy.
     // TODO: kill this function off, inline appropriate case here
-    return EmitComplexToScalarConversion(V, E->getType(), DestTy,
-                                         CE->getExprLoc());
+    return EmitComplexToScalarConversion(
+        V, E->getType(), CGF.getContext().BoolTy, CE->getExprLoc());
   }
 
   case CK_ZeroToOCLOpaqueType: {
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 571ef6ea6b19eb..2779bec26c2c16 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7983,6 +7983,15 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
   if (Context.hasSameUnqualifiedType(SrcTy, DestTy))
     return CK_NoOp;
 
+  // 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, so an
+  // enumeration with an underlying type of bool converts like bool.
+  Type::ScalarTypeKind DestKind = DestTy->getScalarTypeKind();
+  if (DestKind == Type::STK_Integral)
+    if (const EnumDecl *ED = DestTy->getAsEnumDecl();
+        ED && ED->isFixed() && ED->getIntegerType()->isBooleanType())
+      DestKind = Type::STK_Bool;
+
   switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) {
   case Type::STK_MemberPointer:
     llvm_unreachable("member pointer type in C");
@@ -7990,7 +7999,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
   case Type::STK_CPointer:
   case Type::STK_BlockPointer:
   case Type::STK_ObjCObjectPointer:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_CPointer: {
       LangAS SrcAS = SrcTy->getPointeeType().getAddressSpace();
       LangAS DestAS = DestTy->getPointeeType().getAddressSpace();
@@ -8024,7 +8033,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_FixedPoint:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_FixedPoint:
       return CK_FixedPointCast;
     case Type::STK_Bool:
@@ -8049,7 +8058,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
 
   case Type::STK_Bool: // casting from bool is like casting from an integer
   case Type::STK_Integral:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_CPointer:
     case Type::STK_ObjCObjectPointer:
     case Type::STK_BlockPointer:
@@ -8081,7 +8090,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_Floating:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_Floating:
       return CK_FloatingCast;
     case Type::STK_Bool:
@@ -8110,7 +8119,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_FloatingComplex:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_FloatingComplex:
       return CK_FloatingComplexCast;
     case Type::STK_IntegralComplex:
@@ -8144,7 +8153,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_IntegralComplex:
-    switch (DestTy->getScalarTypeKind()) {
+    switch (DestKind) {
     case Type::STK_FloatingComplex:
       return CK_IntegralComplexToFloatingComplex;
     case Type::STK_IntegralComplex:
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@-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..434174404a7953
--- /dev/null
+++ b/clang/test/CodeGen/enum-bool.c
@@ -0,0 +1,44 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c23 -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]]

>From af5100f19bb887a18153f0f4376a35c61d8f21a5 Mon Sep 17 00:00:00 2001
From: Mohit Mishra <[email protected]>
Date: Sat, 21 Nov 2026 08:50:32 +0530
Subject: [PATCH 2/2] [clang] Handle bool-like enums in getScalarTypeKind and
 in CodeGen conversions

Move the Sema change into Type::getScalarTypeKind(), which now returns
STK_Bool for an enumeration with a fixed underlying type of bool. The
other callers already treat STK_Bool like STK_Integral.

In CodeGen, check for such enums in EmitScalarConversion and
EmitComplexToScalarConversion instead of at the complex-to-bool cast.
Compound assignment converts its result through these functions, so
`e += 2` with e == 0 gave 0 and `e += 0.5` used fptoui to i1. The bool
special case for increment now covers these enums too; `e++` with
e == 1 gave 0, and the atomic form used atomicrmw add.
---
 clang/docs/ReleaseNotes.md         |  2 +-
 clang/lib/AST/Type.cpp             |  7 ++++-
 clang/lib/CodeGen/CGExprScalar.cpp | 27 ++++++++++------
 clang/lib/Sema/SemaExpr.cpp        | 21 ++++---------
 clang/test/CodeGen/enum-bool.c     | 50 +++++++++++++++++++++++++++++-
 5 files changed, 79 insertions(+), 28 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 0c313db94308f0..aee23148671e4b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -952,7 +952,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). (#GH241289)
+- 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 b2a37e133ba15d..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);
@@ -3148,11 +3157,9 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
   case CK_IntegralComplexToBoolean: {
     CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E);
 
-    // The destination may be an enumeration whose underlying type is bool
-    // (C23 6.7.3.3p16), so convert to bool rather than to DestTy.
     // TODO: kill this function off, inline appropriate case here
-    return EmitComplexToScalarConversion(
-        V, E->getType(), CGF.getContext().BoolTy, CE->getExprLoc());
+    return EmitComplexToScalarConversion(V, E->getType(), DestTy,
+                                         CE->getExprLoc());
   }
 
   case CK_ZeroToOCLOpaqueType: {
@@ -3356,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())
@@ -3372,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() !=
@@ -3428,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/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 2779bec26c2c16..571ef6ea6b19eb 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7983,15 +7983,6 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
   if (Context.hasSameUnqualifiedType(SrcTy, DestTy))
     return CK_NoOp;
 
-  // 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, so an
-  // enumeration with an underlying type of bool converts like bool.
-  Type::ScalarTypeKind DestKind = DestTy->getScalarTypeKind();
-  if (DestKind == Type::STK_Integral)
-    if (const EnumDecl *ED = DestTy->getAsEnumDecl();
-        ED && ED->isFixed() && ED->getIntegerType()->isBooleanType())
-      DestKind = Type::STK_Bool;
-
   switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) {
   case Type::STK_MemberPointer:
     llvm_unreachable("member pointer type in C");
@@ -7999,7 +7990,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
   case Type::STK_CPointer:
   case Type::STK_BlockPointer:
   case Type::STK_ObjCObjectPointer:
-    switch (DestKind) {
+    switch (DestTy->getScalarTypeKind()) {
     case Type::STK_CPointer: {
       LangAS SrcAS = SrcTy->getPointeeType().getAddressSpace();
       LangAS DestAS = DestTy->getPointeeType().getAddressSpace();
@@ -8033,7 +8024,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_FixedPoint:
-    switch (DestKind) {
+    switch (DestTy->getScalarTypeKind()) {
     case Type::STK_FixedPoint:
       return CK_FixedPointCast;
     case Type::STK_Bool:
@@ -8058,7 +8049,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
 
   case Type::STK_Bool: // casting from bool is like casting from an integer
   case Type::STK_Integral:
-    switch (DestKind) {
+    switch (DestTy->getScalarTypeKind()) {
     case Type::STK_CPointer:
     case Type::STK_ObjCObjectPointer:
     case Type::STK_BlockPointer:
@@ -8090,7 +8081,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_Floating:
-    switch (DestKind) {
+    switch (DestTy->getScalarTypeKind()) {
     case Type::STK_Floating:
       return CK_FloatingCast;
     case Type::STK_Bool:
@@ -8119,7 +8110,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_FloatingComplex:
-    switch (DestKind) {
+    switch (DestTy->getScalarTypeKind()) {
     case Type::STK_FloatingComplex:
       return CK_FloatingComplexCast;
     case Type::STK_IntegralComplex:
@@ -8153,7 +8144,7 @@ CastKind Sema::PrepareScalarCast(ExprResult &Src, 
QualType DestTy) {
     llvm_unreachable("Should have returned before this");
 
   case Type::STK_IntegralComplex:
-    switch (DestKind) {
+    switch (DestTy->getScalarTypeKind()) {
     case Type::STK_FloatingComplex:
       return CK_IntegralComplexToFloatingComplex;
     case Type::STK_IntegralComplex:
diff --git a/clang/test/CodeGen/enum-bool.c b/clang/test/CodeGen/enum-bool.c
index 434174404a7953..e98ecce22ee8df 100644
--- a/clang/test/CodeGen/enum-bool.c
+++ b/clang/test/CodeGen/enum-bool.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c23 -emit-llvm -o - %s 
| FileCheck %s
+// 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
@@ -42,3 +42,51 @@ enum E g(_Complex double x) { return (enum E)x; }
 // 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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to