diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -958,7 +958,10 @@ --NonzeroLength; } - if (NonzeroLength == 0) + // Return an all-zero aggregate unless the array is a flexible array member + // and Elements isn't empty. + if (NonzeroLength == 0 && + (DesiredType->getNumElements() != 0 || Elements.empty())) return llvm::ConstantAggregateZero::get(DesiredType); // Add a zeroinitializer array filler if we have lots of trailing zeroes. diff --git a/clang/test/CodeGenCXX/flexible-array-init.cpp b/clang/test/CodeGenCXX/flexible-array-init.cpp --- a/clang/test/CodeGenCXX/flexible-array-init.cpp +++ b/clang/test/CodeGenCXX/flexible-array-init.cpp @@ -23,3 +23,14 @@ struct B { int x; char y; char z[]; }; B e = {f(), f(), f(), f()}; // expected-error {{cannot compile this flexible array initializer yet}} #endif + +namespace zero_initializer { +A a0{0, 0}, a1{0, {0, 0}}; +// CHECK: @_ZN16zero_initializer2a0E = global { i32, [1 x i32] } zeroinitializer, +// CHECK: @_ZN16zero_initializer2a1E = global { i32, [2 x i32] } zeroinitializer, + +struct S { int x[]; }; + +S s{0}; +// CHECK: @_ZN16zero_initializer1sE = global { [1 x i32] } zeroinitializer, +}