Skip to content

Commit

Permalink
[clang][CodeGen] Check initializer of zero-size fields for nullptr (#…
Browse files Browse the repository at this point in the history
…109271)

In #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
#96422 we can get here when
compiling `C`, but `Init` need not exist. This patch adds a null-check
to account for this situtation.
  • Loading branch information
Michael137 committed Sep 20, 2024
1 parent 605420e commit 2162a18
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CodeGen/union-init2.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {};

0 comments on commit 2162a18

Please sign in to comment.