The root cause of the bug is: the TYPE_INCLUDES_FLEXARRAY marking of the
structure type is not copied to its aliased type.
The fix is to copy this marking to all the variant types of the current
structure type.

        PR c/120353

gcc/c/ChangeLog:

        * c-decl.cc (finish_struct): Copy TYPE_INCLUDES_FLEXARRAY marking
        to all the variant types of the current structure type.

gcc/testsuite/ChangeLog:

        * gcc.dg/pr120353.c: New test.

(cherry picked from commit f37c5f1d88c9da17f16cdb33e7c9d43e4bb5f64d)

bootstrapped and regression tested on both x86 and aarch64.

Okay for backporting to GCC15?

Qing
---
 gcc/c/c-decl.cc                 |  1 +
 gcc/testsuite/gcc.dg/pr120353.c | 11 +++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr120353.c

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 8c420f22976..42e17dcdbf8 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -9909,6 +9909,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, 
tree attributes,
       C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
       C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
       C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
+      TYPE_INCLUDES_FLEXARRAY (x) = TYPE_INCLUDES_FLEXARRAY (t);
     }
 
   /* Update type location to the one of the definition, instead of e.g.
diff --git a/gcc/testsuite/gcc.dg/pr120353.c b/gcc/testsuite/gcc.dg/pr120353.c
new file mode 100644
index 00000000000..6f8e4acf7f2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr120353.c
@@ -0,0 +1,11 @@
+/* PR120353: Test for -Wflex-array-member-not-at-end on structure with 
+   typedef.  */ 
+/* { dg-do compile } */
+/* { dg-options "-Wflex-array-member-not-at-end" } */
+
+typedef struct flex flex_t;
+struct flex { int n; int data[]; };
+struct out_flex_mid {flex_t flex_data;  int m; }; /* { dg-warning "structure 
containing a flexible array member is not at the end of another structure" } */
+
+typedef struct flex flex_t1;
+struct out_flex_mid1 {flex_t1 flex_data1; int n; }; /* { dg-warning "structure 
containing a flexible array member is not at the end of another structure" } */ 
-- 
2.31.1

Reply via email to