Timm =?utf-8?q?Bäder?= <tbae...@redhat.com>,
Timm =?utf-8?q?Bäder?= <tbae...@redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/155...@github.com>


https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/155368

>From c6f68c64eae649055070200431485f4fc421b0e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@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      |  3 +++
 3 files changed, 20 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 8ee3e0c7105b9..d986e3b2b7ac6 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 0987d175c91a8..e9b738ab4d190 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -398,3 +398,6 @@ typedef short v2int16_t __attribute__((ext_vector_type(2)));
 bool issue155507(v2int16_t a, v2int16_t b) {
     return __builtin_bit_cast(unsigned char, __builtin_convertvector(a == b, 
__vbool2)) == 0b11;
 }
+
+constexpr bool b2 = (bool)nullptr;
+_Static_assert(!b2);

>From 15f1d05c91237bdda3838e4a9c280e76bfed79f8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@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 1cbd272f88057160cc3b1cb8e3c56181d636a9ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@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;

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to