ahatanak created this revision. ahatanak added reviewers: efriedma, aaron.ballman, rjmccall. ahatanak added a project: clang. Herald added a project: All. ahatanak requested review of this revision.
`EmitArrayConstant` was incorrectly returning a zeroinitializer of zero-element array type when the initializer expression wasn't empty. This fixes an assertion added in https://reviews.llvm.org/D123649. See the discussion here: https://reviews.llvm.org/D123649#4362210 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D151172 Files: clang/lib/CodeGen/CGExprConstant.cpp clang/test/CodeGenCXX/flexible-array-init.cpp Index: clang/test/CodeGenCXX/flexible-array-init.cpp =================================================================== --- clang/test/CodeGenCXX/flexible-array-init.cpp +++ 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, +} Index: clang/lib/CodeGen/CGExprConstant.cpp =================================================================== --- clang/lib/CodeGen/CGExprConstant.cpp +++ 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.
Index: clang/test/CodeGenCXX/flexible-array-init.cpp =================================================================== --- clang/test/CodeGenCXX/flexible-array-init.cpp +++ 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, +} Index: clang/lib/CodeGen/CGExprConstant.cpp =================================================================== --- clang/lib/CodeGen/CGExprConstant.cpp +++ 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.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits