gcc/testsuite/ChangeLog:

        * gcc.dg/flex-array-in-union-1.c: New test.
        * gcc.dg/flex-array-in-union-2.c: New test.
---
 gcc/testsuite/gcc.dg/flex-array-in-union-1.c | 37 +++++++++++++++++
 gcc/testsuite/gcc.dg/flex-array-in-union-2.c | 42 ++++++++++++++++++++
 2 files changed, 79 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/flex-array-in-union-1.c
 create mode 100644 gcc/testsuite/gcc.dg/flex-array-in-union-2.c

diff --git a/gcc/testsuite/gcc.dg/flex-array-in-union-1.c 
b/gcc/testsuite/gcc.dg/flex-array-in-union-1.c
new file mode 100644
index 000000000000..2a532d77c1dd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/flex-array-in-union-1.c
@@ -0,0 +1,37 @@
+/* testing the correct usage of flexible array members in unions 
+   and alone in structure.  */
+/* { dg-do run} */
+/* { dg-options "-O2 -Wpedantic" } */
+
+union with_fam_1 {
+  int a;
+  int b[];  /* { dg-warning "flexible array member in union is a GCC 
extension" } */
+};
+
+union with_fam_2 {
+  char a;
+  int b[];  /* { dg-warning "flexible array member in union is a GCC 
extension" } */
+};
+
+union with_fam_3 {
+  char a[];  /* { dg-warning " flexible array member in union is a GCC 
extension" } */
+  int b[];  /* { dg-warning "flexible array member in union is a GCC 
extension" } */
+};
+
+struct only_fam {
+  int b[];  /* { dg-warning "flexible array member in a struct with no named 
members is a GCC extension" } */
+};
+
+int main ()
+{
+  if (sizeof (union with_fam_1) != sizeof (int))
+    __builtin_abort ();
+  if (sizeof (union with_fam_2) != __alignof__ (int))
+    __builtin_abort ();
+  if (sizeof (union with_fam_3) != 0)
+    __builtin_abort ();
+  if (sizeof (struct only_fam) != 0)
+    __builtin_abort ();
+  return 0;
+}
+
diff --git a/gcc/testsuite/gcc.dg/flex-array-in-union-2.c 
b/gcc/testsuite/gcc.dg/flex-array-in-union-2.c
new file mode 100644
index 000000000000..130124bbe653
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/flex-array-in-union-2.c
@@ -0,0 +1,42 @@
+/* testing the correct usage of flexible array members in unions 
+   and alone in structure: initialization  */
+/* { dg-do run} */
+/* { dg-options "-O2" } */
+
+union with_fam_1 {
+  int a;
+  int b[]; 
+} with_fam_1_v = {.b = {1, 2, 3, 4}};
+
+union with_fam_2 {
+  int a;
+  char b[];  
+} with_fam_2_v = {.a = 0x1f2f3f4f};
+
+union with_fam_3 {
+  char a[];  
+  int b[];  
+} with_fam_3_v = {.b = {0x1f2f3f4f, 0x5f6f7f7f}};
+
+struct only_fam {
+  int b[]; 
+} only_fam_v = {{7, 11}};
+
+int main ()
+{
+  if (with_fam_1_v.b[3] != 4
+      || with_fam_1_v.b[0] != 1)
+    __builtin_abort ();
+  if (with_fam_2_v.b[3] != 0x1f
+      || with_fam_2_v.b[0] != 0x4f)
+    __builtin_abort ();
+  if (with_fam_3_v.a[0] != 0x4f
+      || with_fam_3_v.a[7] != 0x5f)
+    __builtin_abort ();
+  if (only_fam_v.b[0] != 7
+      || only_fam_v.b[1] != 11)
+    __builtin_abort ();
+
+  return 0;
+}
+
-- 
2.31.1

Reply via email to