diff mbox series

[RFC,v1,3/4] Add testing cases for flexible array members in unions and alone in structures.

Message ID 20240419184317.2138890-4-qing.zhao@oracle.com
State New
Headers show
Series Allow flexible array members in unions and alone in structures [PR53548] | expand

Commit Message

Qing Zhao April 19, 2024, 6:43 p.m. UTC
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

Comments

Joseph Myers April 23, 2024, 6:53 p.m. UTC | #1
On Fri, 19 Apr 2024, Qing Zhao wrote:

> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/flex-array-in-union-1.c: New test.
> 	* gcc.dg/flex-array-in-union-2.c: New test.

There should also be a -pedantic-errors test that these constructs get 
errors with -pedantic-errors.

The tests mix two cases: flexible arrays in unions, and flexible arrays on 
their own in structures.  That means the test names are misleading; either 
they should be renamed, or the struct tests should be split out.

Note that "no named members" also includes the case where there are 
unnamed bit-fields together with a flexible array member, so that should 
be tested as well.

Since this patch series involves changes for both C and C++, it would be 
best for the tests to be c-c++-common tests.  But if that's problematic 
for some reason - if there's still too much difference in behavior between 
C and C++ - then there should at least be tests for C++ that are as 
similar as possible to the tests for C.
Qing Zhao April 23, 2024, 7:30 p.m. UTC | #2
> On Apr 23, 2024, at 14:53, Joseph Myers <josmyers@redhat.com> wrote:
> 
> On Fri, 19 Apr 2024, Qing Zhao wrote:
> 
>> gcc/testsuite/ChangeLog:
>> 
>> 	* gcc.dg/flex-array-in-union-1.c: New test.
>> 	* gcc.dg/flex-array-in-union-2.c: New test.
> 
> There should also be a -pedantic-errors test that these constructs get 
> errors with -pedantic-errors.

Okay, will add. 
> 
> The tests mix two cases: flexible arrays in unions, and flexible arrays on 
> their own in structures.  That means the test names are misleading; either 
> they should be renamed, or the struct tests should be split out.
Okay, will update this.
> 
> Note that "no named members" also includes the case where there are 
> unnamed bit-fields together with a flexible array member, so that should 
> be tested as well.
Will add such testing cases.
> 
> Since this patch series involves changes for both C and C++, it would be 
> best for the tests to be c-c++-common tests.  But if that's problematic 
> for some reason - if there's still too much difference in behavior between 
> C and C++ - then there should at least be tests for C++ that are as 
> similar as possible to the tests for C.

I tried to put these two testing cases to c-c++-common, there were some inconsistent behavior 
I cannot resolve at that time, I will try to fix those issue or add C++ testing cases. 

Thanks for the review.

Qing
> 
> -- 
> Joseph S. Myers
> josmyers@redhat.com
>
diff mbox series

Patch

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;
+}
+