Skip to content

Commit

Permalink
[CodeGen] Fix the type of the constant that is used to zero-initialize a
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ahatanaka committed May 23, 2023
1 parent 1f7c174 commit 6796053
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CGExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CodeGenCXX/flexible-array-init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 6796053

Please sign in to comment.