Skip to content

Commit 5a9f103

Browse files
authored
[clang] Create PointerToBoolean casts for C casts (#155368)
Don't create CK_BitCast casts from `nullptr_t` to `bool`. Fixes #155126
1 parent a0cc776 commit 5a9f103

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

clang/lib/Sema/SemaCast.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,12 @@ void CastOperation::CheckCStyleCast() {
31713171
SrcExpr = ExprError();
31723172
return;
31733173
}
3174-
if (!DestType->isNullPtrType()) {
3174+
if (DestType->isBooleanType()) {
3175+
SrcExpr = ImplicitCastExpr::Create(
3176+
Self.Context, DestType, CK_PointerToBoolean, SrcExpr.get(), nullptr,
3177+
VK_PRValue, Self.CurFPFeatureOverrides());
3178+
3179+
} else if (!DestType->isNullPtrType()) {
31753180
// Implicitly cast from the null pointer type to the type of the
31763181
// destination.
31773182
CastKind CK = DestType->isPointerType() ? CK_NullToPointer : CK_BitCast;

clang/test/CodeGen/issue155126.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o - | FileCheck %s
3+
// RUN: %clang_cc1 -std=c23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s
4+
5+
enum e : bool { b = true };
6+
// CHECK-LABEL: define dso_local void @foo(
7+
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
8+
// CHECK-NEXT: [[ENTRY:.*:]]
9+
// CHECK-NEXT: [[E1:%.*]] = alloca i8, align 1
10+
// CHECK-NEXT: store i8 0, ptr [[E1]], align 1
11+
// CHECK-NEXT: ret void
12+
//
13+
void foo ()
14+
{
15+
enum e e1;
16+
e1 = (bool) nullptr;
17+
}

clang/test/Sema/constexpr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,6 @@ typedef short v2int16_t __attribute__((ext_vector_type(2)));
398398
bool issue155507(v2int16_t a, v2int16_t b) {
399399
return __builtin_bit_cast(unsigned char, __builtin_convertvector(a == b, __vbool2)) == 0b11;
400400
}
401+
402+
constexpr bool b2 = (bool)nullptr;
403+
_Static_assert(!b2);

0 commit comments

Comments
 (0)