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.
This commit is contained in:
Qing Zhao 2024-05-06 16:27:09 +00:00
parent f27fc59d9f
commit 93f6a47583
3 changed files with 139 additions and 0 deletions

View file

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

View file

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

View file

@ -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 } */
};