From: Qing Zhao Date: Mon, 6 May 2024 16:27:09 +0000 (+0000) Subject: Add testing cases for flexible array members in unions and alone in structures. X-Git-Url: https://gcc.gnu.org/git/?a=commitdiff_plain;h=93f6a47583f3fa8a1b66856ecb19ec28f26b2ba4;p=gcc.git Add testing cases for flexible array members in unions and alone in structures. PR c/53548 gcc/testsuite/ChangeLog: PR c/53548 * c-c++-common/fam-in-union-alone-in-struct-1.c: New testcase. * c-c++-common/fam-in-union-alone-in-struct-2.c: New testcase. * c-c++-common/fam-in-union-alone-in-struct-3.c: New testcase. --- diff --git a/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-1.c b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-1.c new file mode 100644 index 000000000000..7d4721aa95ac --- /dev/null +++ b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-1.c @@ -0,0 +1,52 @@ +/* testing the correct usage of flexible array members in unions + and alone in structures. */ +/* { dg-do run} */ +/* { dg-options "-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" } */ + /* { dg-warning "in an otherwise empty" "" { target c++ } .-1 } */ + int b[]; /* { dg-warning "flexible array member in union is a GCC extension" } */ +}; + +struct only_fam { + int b[]; + /* { dg-warning "in a struct with no named members" "" { target c } .-1 } */ + /* { dg-warning "in an otherwise empty" "" { target c++ } .-2 } */ + /* { dg-warning "forbids flexible array member" "" { target c++ } .-3 } */ +}; + +struct only_fam_2 { + unsigned int : 2; + unsigned int : 3; + int b[]; + /* { dg-warning "in a struct with no named members" "" { target c } .-1 } */ + /* { dg-warning "in an otherwise empty" "" { target c++ } .-2 } */ + /* { dg-warning "forbids flexible array member" "" { target c++ } .-3 } */ +}; + +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 (); + if (sizeof (struct only_fam_2) != sizeof (int)) + __builtin_abort (); + return 0; +} + diff --git a/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-2.c b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-2.c new file mode 100644 index 000000000000..3743f9e7dac5 --- /dev/null +++ b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-2.c @@ -0,0 +1,51 @@ +/* testing the correct usage of flexible array members in unions + and alone in structures: 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}}; + +struct only_fam_2 { + unsigned int : 2; + unsigned int : 3; + int b[]; +} only_fam_2_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 (); + if (only_fam_2_v.b[0] != 7 + || only_fam_2_v.b[1] != 11) + __builtin_abort (); + + return 0; +} + diff --git a/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-3.c b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-3.c new file mode 100644 index 000000000000..dd36fa01306d --- /dev/null +++ b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-3.c @@ -0,0 +1,36 @@ +/* testing the correct usage of flexible array members in unions + and alone in structures. */ +/* { dg-do compile } */ +/* { dg-options "-pedantic-errors" } */ + +union with_fam_1 { + int a; + int b[]; /* { dg-error "flexible array member in union is a GCC extension" } */ +}; + +union with_fam_2 { + char a; + int b[]; /* { dg-error "flexible array member in union is a GCC extension" } */ +}; + +union with_fam_3 { + char a[]; /* { dg-error "flexible array member in union is a GCC extension" } */ + /* { dg-error "in an otherwise empty" "" { target c++ } .-1 } */ + int b[]; /* { dg-error "flexible array member in union is a GCC extension" } */ +}; + +struct only_fam { + int b[]; + /* { dg-error "in a struct with no named members" "" { target c } .-1 } */ + /* { dg-error "in an otherwise empty" "" { target c++ } .-2 } */ + /* { dg-error "forbids flexible array member" "" { target c++ } .-3 } */ +}; + +struct only_fam_2 { + unsigned int : 2; + unsigned int : 3; + int b[]; + /* { dg-error "in a struct with no named members" "" { target c } .-1 } */ + /* { dg-error "in an otherwise empty" "" { target c++ } .-2 } */ + /* { dg-error "forbids flexible array member" "" { target c++ } .-3 } */ +};