[clang] [clang][CodeGen] Check initializer of zero-size fields for nullptr (PR #109271)
Michael Buch via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 19 04:25:49 PDT 2024
https://github.com/Michael137 created https://github.com/llvm/llvm-project/pull/109271
In https://github.com/llvm/llvm-project/pull/96422 we started treating 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 `Init` need not exist. This patch adds a null-check to account for this situtation.
>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] [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
More information about the cfe-commits
mailing list