[clang] 6796053 - [CodeGen] Fix the type of the constant that is used to zero-initialize a

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Tue May 23 16:34:23 PDT 2023


Author: Akira Hatanaka
Date: 2023-05-23T16:32:27-07:00
New Revision: 6796053723a68b99149737439856e6da25953975

URL: https://github.com/llvm/llvm-project/commit/6796053723a68b99149737439856e6da25953975
DIFF: https://github.com/llvm/llvm-project/commit/6796053723a68b99149737439856e6da25953975.diff

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

A zero-element array type was incorrectly being used when an incomplete
array was being initialized with a non-empty initializer.

This fixes an assertion failure in AddInitializerToStaticVarDecl. See
the discussion here: https://reviews.llvm.org/D123649#4362210

Differential Revision: https://reviews.llvm.org/D151172

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprConstant.cpp
    clang/test/CodeGenCXX/flexible-array-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 5d77e513da5eb..2fb9722ecc7e3 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -2189,6 +2189,11 @@ llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
 
     llvm::ArrayType *Desired =
         cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
+
+    // Fix the type of incomplete arrays if the initializer isn't empty.
+    if (DestType->isIncompleteArrayType() && !Elts.empty())
+      Desired = llvm::ArrayType::get(Desired->getElementType(), Elts.size());
+
     return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
                              Filler);
   }

diff  --git a/clang/test/CodeGenCXX/flexible-array-init.cpp b/clang/test/CodeGenCXX/flexible-array-init.cpp
index 5840cbd8ab781..26854b1723c4c 100644
--- a/clang/test/CodeGenCXX/flexible-array-init.cpp
+++ b/clang/test/CodeGenCXX/flexible-array-init.cpp
@@ -23,3 +23,14 @@ void g() {
 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,
+}


        


More information about the cfe-commits mailing list