[clang] 5a9f103 - [clang] Create PointerToBoolean casts for C casts (#155368)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 27 21:20:03 PDT 2025


Author: Timm Baeder
Date: 2025-08-28T06:19:59+02:00
New Revision: 5a9f1039c7732920dd1f4374074fdf09b58184ea

URL: https://github.com/llvm/llvm-project/commit/5a9f1039c7732920dd1f4374074fdf09b58184ea
DIFF: https://github.com/llvm/llvm-project/commit/5a9f1039c7732920dd1f4374074fdf09b58184ea.diff

LOG: [clang] Create PointerToBoolean casts for C casts (#155368)

Don't create CK_BitCast casts from `nullptr_t` to `bool`.

Fixes #155126

Added: 
    clang/test/CodeGen/issue155126.c

Modified: 
    clang/lib/Sema/SemaCast.cpp
    clang/test/Sema/constexpr.c

Removed: 
    


################################################################################
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..618772a42984a
--- /dev/null
+++ b/clang/test/CodeGen/issue155126.c
@@ -0,0 +1,17 @@
+// 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;
+  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);


        


More information about the cfe-commits mailing list