[clang] [clang][CodeGen] Check initializer of zero-size fields for nullptr (PR #109271)
Michael Buch via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 20 02:44:58 PDT 2024
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/109271
>From 6d541092e00f0c59861ad17c8bf6988d849c42f0 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Thu, 19 Sep 2024 11:41:51 +0100
Subject: [PATCH 1/2] [clang][CodeGen] Check initializer of zero-size fields
for nullptr
https://github.com/llvm/llvm-project/pull/96422 changed treats empty
records as zero-sized for the purpose of layout. In `C`, empty fields
were never considered `isZeroSize`, so we would never have tried to
call `Init->hasSideEffects` on them. But since https://github.com/llvm/llvm-project/pull/96422
we can get here when compiling `C`, but the `Init` need to exist. This
patch adds a null-check to account for this situtation.
---
clang/lib/CodeGen/CGExprConstant.cpp | 2 +-
clang/test/CodeGenCXX/union-empty-field-init.c | 11 +++++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGenCXX/union-empty-field-init.c
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index f22321f0e738a1..dd65080a840446 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -738,7 +738,7 @@ bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
// Zero-sized fields are not emitted, but their initializers may still
// prevent emission of this struct as a constant.
if (isEmptyFieldForLayout(CGM.getContext(), Field)) {
- if (Init->HasSideEffects(CGM.getContext()))
+ if (Init && Init->HasSideEffects(CGM.getContext()))
return false;
continue;
}
diff --git a/clang/test/CodeGenCXX/union-empty-field-init.c b/clang/test/CodeGenCXX/union-empty-field-init.c
new file mode 100644
index 00000000000000..1ca8d84473e781
--- /dev/null
+++ b/clang/test/CodeGenCXX/union-empty-field-init.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK
+// RUN: %clang_cc1 -x c++ %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK-CXX
+
+union Foo {
+ struct Empty {} val;
+};
+
+union Foo foo = {};
+
+// CHECK: @foo = {{.*}}global %union.Foo undef, align 1
+// CHECK-CXX: @foo = {{.*}}global %union.Foo undef, align 1
>From 38072fe90fbfc52c07e5dbcb838524626055e9ec Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 20 Sep 2024 10:43:30 +0100
Subject: [PATCH 2/2] fixup! move test
---
clang/test/CodeGen/union-init2.c | 8 ++++++++
clang/test/CodeGenCXX/union-empty-field-init.c | 11 -----------
2 files changed, 8 insertions(+), 11 deletions(-)
delete mode 100644 clang/test/CodeGenCXX/union-empty-field-init.c
diff --git a/clang/test/CodeGen/union-init2.c b/clang/test/CodeGen/union-init2.c
index 2c167683c4e55d..048ff00517b4e8 100644
--- a/clang/test/CodeGen/union-init2.c
+++ b/clang/test/CodeGen/union-init2.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -emit-llvm %s -o - -triple i686-pc-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 -x c++ %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK-CXX
// Make sure we generate something sane instead of a ptrtoint
// CHECK: @r, [4 x i8] undef
@@ -11,3 +12,10 @@ union z {
long long b;
};
union z y = {};
+
+// CHECK: @foo = {{.*}}global %union.Foo undef, align 1
+// CHECK-CXX: @foo = {{.*}}global %union.Foo undef, align 1
+union Foo {
+ struct Empty {} val;
+};
+union Foo foo = {};
diff --git a/clang/test/CodeGenCXX/union-empty-field-init.c b/clang/test/CodeGenCXX/union-empty-field-init.c
deleted file mode 100644
index 1ca8d84473e781..00000000000000
--- a/clang/test/CodeGenCXX/union-empty-field-init.c
+++ /dev/null
@@ -1,11 +0,0 @@
-// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK
-// RUN: %clang_cc1 -x c++ %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK-CXX
-
-union Foo {
- struct Empty {} val;
-};
-
-union Foo foo = {};
-
-// CHECK: @foo = {{.*}}global %union.Foo undef, align 1
-// CHECK-CXX: @foo = {{.*}}global %union.Foo undef, align 1
More information about the cfe-commits
mailing list