[PATCH] D151172: [CodeGen] Fix the type of the constant that is used to zero-initialize a flexible array member

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 22 17:16:51 PDT 2023


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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151172.524535.patch
Type: text/x-patch
Size: 1349 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230523/238ca5a9/attachment.bin>


More information about the cfe-commits mailing list