Skip to content

[clang codegen] avoid to crash when emit init func for global variable with flexible array init #113336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ Bug Fixes in This Version
- Fixed a crash using ``__array_rank`` on 64-bit targets. (#GH113044).
- The warning emitted for an unsupported register variable type now points to
the unsupported type instead of the ``register`` keyword (#GH109776).
- Fixed a crash when emit ctor for global variant with flexible array init (#GH113187).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5529,12 +5529,14 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
T = D->getType();

if (getLangOpts().CPlusPlus) {
if (InitDecl->hasFlexibleArrayInit(getContext()))
ErrorUnsupported(D, "flexible array initializer");
Init = EmitNullConstant(T);

if (!IsDefinitionAvailableExternally)
NeedsGlobalCtor = true;
if (InitDecl->hasFlexibleArrayInit(getContext())) {
ErrorUnsupported(D, "flexible array initializer");
// We cannot create ctor for flexible array initializer
NeedsGlobalCtor = false;
}
} else {
ErrorUnsupported(D, "static initializer");
Init = llvm::UndefValue::get(getTypes().ConvertType(T));
Expand Down
6 changes: 6 additions & 0 deletions clang/test/CodeGenCXX/flexible-array-init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL1 %s
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL2 %s
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL3 %s
// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm-only -verify -DFAIL4 %s

struct A { int x; int y[]; };
A a = { 1, 7, 11 };
Expand All @@ -23,6 +24,11 @@ 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
#ifdef FAIL4
union { char a[]; } z = {};
union { char a[]; } z0 = {z.a[0]}; // expected-error {{cannot compile this flexible array initializer yet}}
char keep() { return z0.a[0]; }
#endif

namespace zero_initializer {
A a0{0, 0}, a1{0, {0, 0}};
Expand Down
Loading