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

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 22 18:21:31 PDT 2024


Author: Congcong Cai
Date: 2024-10-23T09:21:27+08:00
New Revision: bd6c430dcb52f3e4f86abc5f572126355fa39936

URL: https://github.com/llvm/llvm-project/commit/bd6c430dcb52f3e4f86abc5f572126355fa39936
DIFF: https://github.com/llvm/llvm-project/commit/bd6c430dcb52f3e4f86abc5f572126355fa39936.diff

LOG: [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.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/CodeGen/CodeGenModule.cpp
    clang/test/CodeGenCXX/flexible-array-init.cpp

Removed: 
    


################################################################################
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