Skip to content

Commit 6796053

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
1 parent 1f7c174 commit 6796053

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
@@ -2189,6 +2189,11 @@ llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
21892189

21902190
llvm::ArrayType *Desired =
21912191
cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
2192+
2193+
// Fix the type of incomplete arrays if the initializer isn't empty.
2194+
if (DestType->isIncompleteArrayType() && !Elts.empty())
2195+
Desired = llvm::ArrayType::get(Desired->getElementType(), Elts.size());
2196+
21922197
return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
21932198
Filler);
21942199
}

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)