llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Michael Buch (Michael137) <details> <summary>Changes</summary> 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. --- Full diff: https://github.com/llvm/llvm-project/pull/109271.diff 2 Files Affected: - (modified) clang/lib/CodeGen/CGExprConstant.cpp (+1-1) - (added) clang/test/CodeGenCXX/union-empty-field-init.c (+11) ``````````diff 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 `````````` </details> https://github.com/llvm/llvm-project/pull/109271 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits