[clang] [clang] Create PointerToBoolean casts for C casts (PR #155368)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 26 00:48:38 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/155368
Don't create CK_BitCast casts from `nullptr_t` to `bool`.
Fixes #155126
>From 65031365f138e1ab8d3668ce902b441922ef5ff9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Aug 2025 09:38:44 +0200
Subject: [PATCH] [clang] Create PointerToBoolean casts for C casts
Don't create CK_BitCast casts from `nullptr_t` to `bool`.
Fixes #155126
---
clang/lib/Sema/SemaCast.cpp | 7 ++++++-
clang/test/CodeGen/issue155126.c | 11 +++++++++++
clang/test/Sema/constexpr.c | 2 ++
3 files changed, 19 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/issue155126.c
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index 933a6c558e7ac..72c5e37c8e2ea 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -3171,7 +3171,12 @@ void CastOperation::CheckCStyleCast() {
SrcExpr = ExprError();
return;
}
- if (!DestType->isNullPtrType()) {
+ if (DestType->isBooleanType()) {
+ SrcExpr = ImplicitCastExpr::Create(
+ Self.Context, DestType, CK_PointerToBoolean, SrcExpr.get(), nullptr,
+ VK_PRValue, Self.CurFPFeatureOverrides());
+
+ } else if (!DestType->isNullPtrType()) {
// Implicitly cast from the null pointer type to the type of the
// destination.
CastKind CK = DestType->isPointerType() ? CK_NullToPointer : CK_BitCast;
diff --git a/clang/test/CodeGen/issue155126.c b/clang/test/CodeGen/issue155126.c
new file mode 100644
index 0000000000000..ec15355d8d7df
--- /dev/null
+++ b/clang/test/CodeGen/issue155126.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c23 -verify %s
+// RUN: %clang_cc1 -std=c23 -verify -fexperimental-new-constant-interpreter %s
+
+// expected-no-diagnostics
+
+enum e : bool { b = true };
+void foo ()
+{
+ enum e e1;
+ e1 = (bool) nullptr;
+}
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index 3dcb0b3a7d95f..e8f0700c5894f 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -391,3 +391,5 @@ void ghissue109095() {
_Static_assert(i == c[0]); // expected-error {{static assertion expression is not an integral constant expression}}\
// expected-note {{initializer of 'i' is not a constant expression}}
}
+constexpr bool b2 = (bool)nullptr;
+_Static_assert(!b2);
More information about the cfe-commits
mailing list