[clang] [clang] Use getScalarTypeKind for bool-like enums in CodeGen and add constexpr tests (PR #248790)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 21:26:20 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Mohit Mishra (mohitmishra786)

<details>
<summary>Changes</summary>

Follow-up to #<!-- -->244494, from the review discussion in https://github.com/llvm/llvm-project/pull/244494#discussion_r4684559370.

`hasBoolConversionSemantics()` in CGExprScalar.cpp repeated the check that `Type::getScalarTypeKind()` already does for an enumeration with a fixed underlying type of `bool`. It now uses `getScalarTypeKind()`. The `isScalarType()` check is needed because `getScalarTypeKind()` asserts on non-scalar types, and `EmitScalarConversion` also sees vector and matrix types.

Tests for constant evaluation of conversions to such enumerations: C23 `constexpr` initializers in C/C23/n3030.c (including `(enum eb)12` from the review), C++ `static_cast` and C-style casts in SemaCXX/enum-scoped.cpp, and fixed-point sources in the new Sema/fixed-point-bool-enum.c.

Parser, Sema, SemaCXX, C, CodeGen, CodeGenCXX and OpenMP tests show no new failures.

Fixes #<!-- -->245877


---
Full diff: https://github.com/llvm/llvm-project/pull/248790.diff


4 Files Affected:

- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+1-3) 
- (modified) clang/test/C/C23/n3030.c (+6) 
- (added) clang/test/Sema/fixed-point-bool-enum.c (+10) 
- (modified) clang/test/SemaCXX/enum-scoped.cpp (+14) 


``````````diff
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index c9f46686e3edcf..b419663acad0d6 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -176,9 +176,7 @@ static bool MustVisitNullValue(const Expr *E) {
 /// \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();
+  return T->isScalarType() && T->getScalarTypeKind() == Type::STK_Bool;
 }
 
 /// If \p E is a widened promoted integer, get its base (unpromoted) type.
diff --git a/clang/test/C/C23/n3030.c b/clang/test/C/C23/n3030.c
index d38184b50aee6c..17cdf782c570f2 100644
--- a/clang/test/C/C23/n3030.c
+++ b/clang/test/C/C23/n3030.c
@@ -66,6 +66,12 @@ 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);
+constexpr enum eb eb_c1 = (enum eb)12;
+static_assert(eb_c1 == eb_true);
+constexpr enum eb eb_c2 = (enum eb)256;
+static_assert(eb_c2 == eb_true);
+constexpr enum eb eb_c3 = (enum eb)0;
+static_assert(eb_c3 == 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}}
diff --git a/clang/test/Sema/fixed-point-bool-enum.c b/clang/test/Sema/fixed-point-bool-enum.c
new file mode 100644
index 00000000000000..e6d72e38b6e719
--- /dev/null
+++ b/clang/test/Sema/fixed-point-bool-enum.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -ffixed-point -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+// Conversion to an enumeration with an underlying type of bool has the
+// semantics of conversion to bool (C23 6.7.3.3p16).
+enum E : bool { F, T };
+constexpr enum E a = (enum E)0.5k;
+static_assert(a == T);
+static_assert((enum E)-0.25r == T);
+static_assert((enum E)0.0k == F);
diff --git a/clang/test/SemaCXX/enum-scoped.cpp b/clang/test/SemaCXX/enum-scoped.cpp
index 09f3206c84ccb3..fa7b3b39c82485 100644
--- a/clang/test/SemaCXX/enum-scoped.cpp
+++ b/clang/test/SemaCXX/enum-scoped.cpp
@@ -318,6 +318,20 @@ namespace rdar15124329 {
   static_assert(T2 == B::T, "");
 }
 
+namespace bool_underlying_conversion {
+  // The value is converted to the underlying type bool first, so any nonzero
+  // value gives true.
+  enum E : bool { F, T };
+  enum class S : bool { SF, ST };
+  constexpr E e1 = static_cast<E>(12);
+  constexpr E e2 = (E)256;
+  constexpr S s1 = static_cast<S>(2);
+  static_assert(e1 == T, "");
+  static_assert(e2 == T, "");
+  static_assert(s1 == S::ST, "");
+  static_assert(static_cast<E>(0) == F, "");
+}
+
 namespace PR18044 {
   enum class E { a };
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/248790


More information about the cfe-commits mailing list