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

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 22 09:09:20 PDT 2024


https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/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.


>From ac8916ceba73bd223527fcb89c45ee4a8fdb9b5b Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 23 Oct 2024 00:05:46 +0800
Subject: [PATCH] [clang codegen] avoid to crash when emit init func for global
 variable with flexible array init

Fixes: #113187
Avoid to create init function since clang does not support global variable with flexible array init.
It will cause assertion failure later.
---
 clang/docs/ReleaseNotes.rst                   | 1 +
 clang/lib/CodeGen/CodeGenModule.cpp           | 8 +++++---
 clang/test/CodeGenCXX/flexible-array-init.cpp | 6 ++++++
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6f44ff5c1efa96..28bb83a1c9d60f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 24655b809b2eff..2bcca5e85bdfeb 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -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));
diff --git a/clang/test/CodeGenCXX/flexible-array-init.cpp b/clang/test/CodeGenCXX/flexible-array-init.cpp
index 26854b1723c4cc..a711e7102989e2 100644
--- a/clang/test/CodeGenCXX/flexible-array-init.cpp
+++ b/clang/test/CodeGenCXX/flexible-array-init.cpp
@@ -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 };
@@ -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}};



More information about the cfe-commits mailing list