]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Add more C2Y tests
authorJoseph Myers <josmyers@redhat.com>
Thu, 18 Sep 2025 22:08:04 +0000 (22:08 +0000)
committerJoseph Myers <josmyers@redhat.com>
Thu, 18 Sep 2025 22:08:04 +0000 (22:08 +0000)
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.

gcc/testsuite/gcc.dg/c2y-anon-init-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-incomplete-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-linkage-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-linkage-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-linkage-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-linkage-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c2y-static-assert-1.c [new file with mode: 0644]

diff --git a/gcc/testsuite/gcc.dg/c2y-anon-init-1.c b/gcc/testsuite/gcc.dg/c2y-anon-init-1.c
new file mode 100644 (file)
index 0000000..7f42277
--- /dev/null
@@ -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);
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-incomplete-2.c b/gcc/testsuite/gcc.dg/c2y-incomplete-2.c
new file mode 100644 (file)
index 0000000..81bf922
--- /dev/null
@@ -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 } */
diff --git a/gcc/testsuite/gcc.dg/c2y-linkage-1.c b/gcc/testsuite/gcc.dg/c2y-linkage-1.c
new file mode 100644 (file)
index 0000000..bda0098
--- /dev/null
@@ -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'" } */
+  }
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-linkage-2.c b/gcc/testsuite/gcc.dg/c2y-linkage-2.c
new file mode 100644 (file)
index 0000000..e8be5c7
--- /dev/null
@@ -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" } */
diff --git a/gcc/testsuite/gcc.dg/c2y-linkage-3.c b/gcc/testsuite/gcc.dg/c2y-linkage-3.c
new file mode 100644 (file)
index 0000000..e46548a
--- /dev/null
@@ -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'" } */
+  }
+}
diff --git a/gcc/testsuite/gcc.dg/c2y-linkage-4.c b/gcc/testsuite/gcc.dg/c2y-linkage-4.c
new file mode 100644 (file)
index 0000000..25e6369
--- /dev/null
@@ -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" } */
diff --git a/gcc/testsuite/gcc.dg/c2y-static-assert-1.c b/gcc/testsuite/gcc.dg/c2y-static-assert-1.c
new file mode 100644 (file)
index 0000000..5865960
--- /dev/null
@@ -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" } */