Skip to content

Commit 6d54109

Browse files
committed
[clang][CodeGen] Check initializer of zero-size fields for nullptr
llvm#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 llvm#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.
1 parent 9548dbe commit 6d54109

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
738738
// Zero-sized fields are not emitted, but their initializers may still
739739
// prevent emission of this struct as a constant.
740740
if (isEmptyFieldForLayout(CGM.getContext(), Field)) {
741-
if (Init->HasSideEffects(CGM.getContext()))
741+
if (Init && Init->HasSideEffects(CGM.getContext()))
742742
return false;
743743
continue;
744744
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK
2+
// RUN: %clang_cc1 -x c++ %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK-CXX
3+
4+
union Foo {
5+
struct Empty {} val;
6+
};
7+
8+
union Foo foo = {};
9+
10+
// CHECK: @foo = {{.*}}global %union.Foo undef, align 1
11+
// CHECK-CXX: @foo = {{.*}}global %union.Foo undef, align 1

0 commit comments

Comments
 (0)