Skip to content

Commit bd6c430

Browse files
authored
[clang codegen] avoid to crash when emit init func for global variable with flexible array init (#113336)
Fixes: #113187 Avoid to create init function since clang does not support global variable with flexible array init. It will cause assertion failure later.
1 parent 0fbf91a commit bd6c430

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ Bug Fixes in This Version
442442
- Fixed a crash using ``__array_rank`` on 64-bit targets. (#GH113044).
443443
- The warning emitted for an unsupported register variable type now points to
444444
the unsupported type instead of the ``register`` keyword (#GH109776).
445+
- Fixed a crash when emit ctor for global variant with flexible array init (#GH113187).
445446

446447
Bug Fixes to Compiler Builtins
447448
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5529,12 +5529,14 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
55295529
T = D->getType();
55305530

55315531
if (getLangOpts().CPlusPlus) {
5532-
if (InitDecl->hasFlexibleArrayInit(getContext()))
5533-
ErrorUnsupported(D, "flexible array initializer");
55345532
Init = EmitNullConstant(T);
5535-
55365533
if (!IsDefinitionAvailableExternally)
55375534
NeedsGlobalCtor = true;
5535+
if (InitDecl->hasFlexibleArrayInit(getContext())) {
5536+
ErrorUnsupported(D, "flexible array initializer");
5537+
// We cannot create ctor for flexible array initializer
5538+
NeedsGlobalCtor = false;
5539+
}
55385540
} else {
55395541
ErrorUnsupported(D, "static initializer");
55405542
Init = llvm::UndefValue::get(getTypes().ConvertType(T));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL1 %s
33
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL2 %s
44
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL3 %s
5+
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL4 %s
56

67
struct A { int x; int y[]; };
78
A a = { 1, 7, 11 };
@@ -23,6 +24,11 @@ void g() {
2324
struct B { int x; char y; char z[]; };
2425
B e = {f(), f(), f(), f()}; // expected-error {{cannot compile this flexible array initializer yet}}
2526
#endif
27+
#ifdef FAIL4
28+
union { char a[]; } z = {};
29+
union { char a[]; } z0 = {z.a[0]}; // expected-error {{cannot compile this flexible array initializer yet}}
30+
char keep() { return z0.a[0]; }
31+
#endif
2632

2733
namespace zero_initializer {
2834
A a0{0, 0}, a1{0, {0, 0}};

0 commit comments

Comments
 (0)