Skip to content

Commit 2162a18

Browse files
authored
[clang][CodeGen] Check initializer of zero-size fields for nullptr (llvm#109271)
In llvm#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 llvm#96422 we can get here when compiling `C`, but `Init` need not exist. This patch adds a null-check to account for this situtation.
1 parent 605420e commit 2162a18

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-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
}

clang/test/CodeGen/union-init2.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -emit-llvm %s -o - -triple i686-pc-linux-gnu | FileCheck %s
2+
// RUN: %clang_cc1 -x c++ %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK-CXX
23

34
// Make sure we generate something sane instead of a ptrtoint
45
// CHECK: @r, [4 x i8] undef
@@ -11,3 +12,10 @@ union z {
1112
long long b;
1213
};
1314
union z y = {};
15+
16+
// CHECK: @foo = {{.*}}global %union.Foo undef, align 1
17+
// CHECK-CXX: @foo = {{.*}}global %union.Foo undef, align 1
18+
union Foo {
19+
struct Empty {} val;
20+
};
21+
union Foo foo = {};

0 commit comments

Comments
 (0)