[clang] [clang] Use getScalarTypeKind for bool-like enums in CodeGen and add constexpr tests (PR #248790)
Mohit Mishra via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 3 21:25:44 PST 2026
https://github.com/mohitmishra786 created https://github.com/llvm/llvm-project/pull/248790
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
>From 27c87f7241c7eea9f58fffcb22c98e659c93ddde Mon Sep 17 00:00:00 2001
From: Mohit Mishra <immadmohit at gmail.com>
Date: Fri, 4 Dec 2026 09:25:38 +0530
Subject: [PATCH] [clang] Use getScalarTypeKind for bool-like enums in CodeGen
and add constexpr tests
hasBoolConversionSemantics() repeated the check that
Type::getScalarTypeKind() already does for an enumeration with a fixed
underlying type of bool. Use getScalarTypeKind() instead. The
isScalarType() check is needed because getScalarTypeKind() asserts on
non-scalar types, and EmitScalarConversion also sees vector and matrix
types.
Add constant evaluation tests for conversion to such enumerations in C23
constexpr initializers, in C++ with static_cast, and from fixed-point
values.
Fixes #245877
---
clang/lib/CodeGen/CGExprScalar.cpp | 4 +---
clang/test/C/C23/n3030.c | 6 ++++++
clang/test/Sema/fixed-point-bool-enum.c | 10 ++++++++++
clang/test/SemaCXX/enum-scoped.cpp | 14 ++++++++++++++
4 files changed, 31 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Sema/fixed-point-bool-enum.c
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 };
More information about the cfe-commits
mailing list