c: Add more C2Y tests

Add further tests for things defined in C2Y that correspond to how GCC
already behaves.  Some of these are for previous undefined behavior
where what is violated has become a constraint but previously was a
"shall" outside Constraints.  Others (c2y-anon-init-1.c and
c2y-incomplete-2.c) reflect semantics that were intended all along but
not correctly or sufficiently clearly stated in the standard.

Tested for x86_64-pc-linux-gnu.

	* gcc.dg/c2y-anon-init-1.c, gcc.dg/c2y-incomplete-2.c:
	gcc.dg/c2y-linkage-1.c, gcc.dg/c2y-linkage-2.c,
	gcc.dg/c2y-linkage-3.c, gcc.dg/c2y-linkage-4.c,
	gcc.dg/c2y-static-assert-1.c: New tests.
This commit is contained in:
Joseph Myers
2025-09-18 22:08:04 +00:00
parent 44e3e66a14
commit 6e4698c136
7 changed files with 121 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
/* Test initialization of anonymous structures and unions (clarified in C2y by
N3451). */
/* { dg-do run } */
/* { dg-options "-std=c2y -pedantic-errors" } */
struct s { struct { int a, b; }; union { int c; }; } x = { 1, 2, 3 };
union u { struct { int x, y; }; };
struct s a = { 1, 2, 3 };
union u b = { 4, 5 };
extern void abort ();
extern void exit (int);
int
main ()
{
if (a.a != 1 || a.b != 2 || a.c != 3)
abort ();
if (b.x != 4 || b.y != 5)
abort ();
exit (0);
}

View File

@@ -0,0 +1,24 @@
/* Test member access to incomplete structures and unions (explicit constraint
violation in C2y). */
/* { dg-do compile } */
/* { dg-options "-std=c2y -pedantic-errors" } */
struct s1;
extern struct s1 vs1;
struct s1 { int a; int b : sizeof (vs1.a); }; /* { dg-error "invalid use of undefined type" } */
/* { dg-error "width not an integer constant" "constant" { target *-*-* } .-1 } */
union u1;
extern union u1 vu1;
union u1 { int a; int b : sizeof (vu1.a); }; /* { dg-error "invalid use of undefined type" } */
/* { dg-error "width not an integer constant" "constant" { target *-*-* } .-1 } */
struct s2;
extern struct s2 *ps2;
struct s2 { int a; int b : sizeof (ps2->a); }; /* { dg-error "invalid use of undefined type" } */
/* { dg-error "width not an integer constant" "constant" { target *-*-* } .-1 } */
union u2;
extern union u2 *pu2;
union u2 { int a; int b : sizeof (pu2->a); }; /* { dg-error "invalid use of undefined type" } */
/* { dg-error "width not an integer constant" "constant" { target *-*-* } .-1 } */

View File

@@ -0,0 +1,15 @@
/* Test mixing internal and external linker for the same identifier (a
constraint violation in C2y). */
/* { dg-do compile } */
/* { dg-options "-std=c2y -pedantic-errors" } */
static int x;
void
f ()
{
long x;
{
extern int x; /* { dg-error "variable previously declared 'static' redeclared 'extern'" } */
}
}

View File

@@ -0,0 +1,12 @@
/* Test mixing internal and external linker for the same identifier (a
constraint violation in C2y). */
/* { dg-do compile } */
/* { dg-options "-std=c2y -pedantic-errors" } */
void
f ()
{
extern int x; /* { dg-message "previous declaration" } */
}
static int x; /* { dg-error "static declaration of 'x' follows non-static declaration" } */

View File

@@ -0,0 +1,15 @@
/* Test mixing internal and external linker for the same identifier (a
constraint violation in C2y). */
/* { dg-do compile } */
/* { dg-options "-std=c2y -pedantic-errors" } */
static int x ();
void
f ()
{
long x;
{
extern int x (); /* { dg-error "function previously declared 'static' redeclared 'extern'" } */
}
}

View File

@@ -0,0 +1,12 @@
/* Test mixing internal and external linker for the same identifier (a
constraint violation in C2y). */
/* { dg-do compile } */
/* { dg-options "-std=c2y -pedantic-errors" } */
void
f ()
{
extern int x (); /* { dg-message "previous declaration" } */
}
static int x (); /* { dg-error "static declaration of 'x' follows non-static declaration" } */

View File

@@ -0,0 +1,20 @@
/* Test C2y static assertions with expressions that are not integer constant
expressions (taken from c11-static-assert-3.c; in C2y these are constraint
violations, whereas in C11 we diagnose them but they are undefined behavior
because the requirement to be an integer constant expression is in Semantics
not Constraints). */
/* { dg-do compile } */
/* { dg-options "-std=c2y -pedantic-errors" } */
_Static_assert (__INT_MAX__ * 2, "overflow"); /* { dg-warning "integer overflow in expression" } */
/* { dg-error "overflow in constant expression" "error" { target *-*-* } .-1 } */
_Static_assert ((void *)(__SIZE_TYPE__)16, "non-integer"); /* { dg-error "not an integer" } */
_Static_assert (1.0, "non-integer"); /* { dg-error "not an integer" } */
_Static_assert ((int)(1.0 + 1.0), "non-constant-expression"); /* { dg-error "not an integer constant expression" } */
int i;
_Static_assert (i, "non-constant"); /* { dg-error "not constant" } */