Skip to content

Commit e65a763

Browse files
committed
[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 (cherry picked from commit 6796053)
1 parent 91abcd2 commit e65a763

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,11 @@ llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
23512351

23522352
llvm::ArrayType *Desired =
23532353
cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
2354+
2355+
// Fix the type of incomplete arrays if the initializer isn't empty.
2356+
if (DestType->isIncompleteArrayType() && !Elts.empty())
2357+
Desired = llvm::ArrayType::get(Desired->getElementType(), Elts.size());
2358+
23542359
return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
23552360
Filler);
23562361
}

clang/test/CodeGenCXX/flexible-array-init.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ void g() {
2323
struct B { int x; char y; char z[]; };
2424
B e = {f(), f(), f(), f()}; // expected-error {{cannot compile this flexible array initializer yet}}
2525
#endif
26+
27+
namespace zero_initializer {
28+
A a0{0, 0}, a1{0, {0, 0}};
29+
// CHECK: @_ZN16zero_initializer2a0E = global { i32, [1 x i32] } zeroinitializer,
30+
// CHECK: @_ZN16zero_initializer2a1E = global { i32, [2 x i32] } zeroinitializer,
31+
32+
struct S { int x[]; };
33+
34+
S s{0};
35+
// CHECK: @_ZN16zero_initializer1sE = global { [1 x i32] } zeroinitializer,
36+
}

0 commit comments

Comments
 (0)