[clang] [clang] Create PointerToBoolean casts for C casts (PR #155368)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 26 23:56:55 PDT 2025
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>,
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/155368 at github.com>
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/155368
>From cfe28648d256469e871e79fe44bad7eb1819aaa2 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 1/3] [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 de22419ee35de..9727a15aabc72 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -3170,7 +3170,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);
>From 8ce7ded176a4280fe89235ce7f170039ade524ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Aug 2025 12:04:46 +0200
Subject: [PATCH 2/3] Actually emit code
---
clang/test/CodeGen/issue155126.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/clang/test/CodeGen/issue155126.c b/clang/test/CodeGen/issue155126.c
index ec15355d8d7df..f071d36799dce 100644
--- a/clang/test/CodeGen/issue155126.c
+++ b/clang/test/CodeGen/issue155126.c
@@ -1,7 +1,5 @@
-// RUN: %clang_cc1 -std=c23 -verify %s
-// RUN: %clang_cc1 -std=c23 -verify -fexperimental-new-constant-interpreter %s
-
-// expected-no-diagnostics
+// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o -
+// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter
enum e : bool { b = true };
void foo ()
>From 9fe5195cd72529b705a735537713541e3ec175da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Aug 2025 17:45:24 +0200
Subject: [PATCH 3/3] Add FileCheck output
---
clang/test/CodeGen/issue155126.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/clang/test/CodeGen/issue155126.c b/clang/test/CodeGen/issue155126.c
index f071d36799dce..618772a42984a 100644
--- a/clang/test/CodeGen/issue155126.c
+++ b/clang/test/CodeGen/issue155126.c
@@ -1,7 +1,15 @@
-// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o -
-// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
+// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s
enum e : bool { b = true };
+// CHECK-LABEL: define dso_local void @foo(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[E1:%.*]] = alloca i8, align 1
+// CHECK-NEXT: store i8 0, ptr [[E1]], align 1
+// CHECK-NEXT: ret void
+//
void foo ()
{
enum e e1;
More information about the cfe-commits
mailing list