diff mbox series

c: Allow omitted parameter names for C2x

Message ID alpine.DEB.2.22.394.2010281858230.233090@digraph.polyomino.org.uk
State New
Headers show
Series c: Allow omitted parameter names for C2x | expand

Commit Message

Joseph Myers Oct. 28, 2020, 6:59 p.m. UTC
C2x allows parameter names to be omitted in function definitions, as
in C++; add support for this feature.  As with other features that
only result in previously rejected code being accepted, this feature
is now accepted as an extension for previous standard versions, with a
pedwarn-if-pedantic that is disabled by -Wno-c11-c2x-compat.  The
logic for avoiding unused-parameter warnings for unnamed parameters is
in code shared between C and C++, so no changes are needed there.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  Applied to 
mainline.

gcc/c/
2020-10-28  Joseph Myers  <joseph@codesourcery.com>

	* c-decl.c (store_parm_decls_newstyle): Use pedwarn_c11 not
	error_at for omitted parameter name.

gcc/testsuite/
2020-10-28  Joseph Myers  <joseph@codesourcery.com>

	* gcc.dg/c11-parm-omit-1.c, gcc.dg/c11-parm-omit-2.c,
	gcc.dg/c11-parm-omit-3.c, gcc.dg/c11-parm-omit-4.c,
	gcc.dg/c2x-parm-omit-1.c, gcc.dg/c2x-parm-omit-2.c,
	gcc.dg/c2x-parm-omit-3.c, gcc.dg/c2x-parm-omit-4.c: New tests.
	* gcc.dg/noncompile/pr79758.c: Do not expect error for omitted
	parameter name.
diff mbox series

Patch

diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 1673b958555..a5d0b158a26 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -9630,7 +9630,9 @@  store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
 	    warn_if_shadowing (decl);
 	}
       else
-	error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
+	pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
+		     "ISO C does not support omitting parameter names in "
+		     "function definitions before C2X");
     }
 
   /* Record the parameter list in the function declaration.  */
diff --git a/gcc/testsuite/gcc.dg/c11-parm-omit-1.c b/gcc/testsuite/gcc.dg/c11-parm-omit-1.c
new file mode 100644
index 00000000000..83d1b508286
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c11-parm-omit-1.c
@@ -0,0 +1,5 @@ 
+/* Test omitted parameter names not in C11: -pedantic-errors.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic-errors" } */
+
+void f (int) { } /* { dg-error "omitting parameter names" } */
diff --git a/gcc/testsuite/gcc.dg/c11-parm-omit-2.c b/gcc/testsuite/gcc.dg/c11-parm-omit-2.c
new file mode 100644
index 00000000000..2efd4505db3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c11-parm-omit-2.c
@@ -0,0 +1,5 @@ 
+/* Test omitted parameter names not in C11: -pedantic.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic" } */
+
+void f (int) { } /* { dg-warning "omitting parameter names" } */
diff --git a/gcc/testsuite/gcc.dg/c11-parm-omit-3.c b/gcc/testsuite/gcc.dg/c11-parm-omit-3.c
new file mode 100644
index 00000000000..5bf27a03aff
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c11-parm-omit-3.c
@@ -0,0 +1,5 @@ 
+/* Test omitted parameter names not in C11: -pedantic -Wno-c11-c2x-compat.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic -Wno-c11-c2x-compat" } */
+
+void f (int) { }
diff --git a/gcc/testsuite/gcc.dg/c11-parm-omit-4.c b/gcc/testsuite/gcc.dg/c11-parm-omit-4.c
new file mode 100644
index 00000000000..ea4cbfa9928
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c11-parm-omit-4.c
@@ -0,0 +1,6 @@ 
+/* Test omitted parameter names not in C11: accepted by default in the
+   absence of -pedantic.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11" } */
+
+void f (int) { }
diff --git a/gcc/testsuite/gcc.dg/c2x-parm-omit-1.c b/gcc/testsuite/gcc.dg/c2x-parm-omit-1.c
new file mode 100644
index 00000000000..0dc89bb0270
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-parm-omit-1.c
@@ -0,0 +1,5 @@ 
+/* Test omitted parameter names in C2x.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+void f (int) { }
diff --git a/gcc/testsuite/gcc.dg/c2x-parm-omit-2.c b/gcc/testsuite/gcc.dg/c2x-parm-omit-2.c
new file mode 100644
index 00000000000..7d689332813
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-parm-omit-2.c
@@ -0,0 +1,10 @@ 
+/* Test omitted parameter names in C2x.  Warning test: there should be
+   no warning for an unnamed parameter being unused.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors -Wall -Wextra" } */
+
+int
+f (int a, int, int c, int d) /* { dg-warning "unused parameter 'd'" } */
+{
+  return a + c;
+}
diff --git a/gcc/testsuite/gcc.dg/c2x-parm-omit-3.c b/gcc/testsuite/gcc.dg/c2x-parm-omit-3.c
new file mode 100644
index 00000000000..dac258b0fb8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-parm-omit-3.c
@@ -0,0 +1,23 @@ 
+/* Test omitted parameter names in C2x.  Execution test.  */
+/* { dg-do run } */
+/* { dg-options "-std=c2x -pedantic-errors" } */
+
+extern void abort (void);
+extern void exit (int);
+
+void
+f (int a, int [++a], int b)
+{
+  /* Verify array size expression of unnamed parameter is processed as
+     expected.  */
+  if (a != 2 || b != 3)
+    abort ();
+}
+
+int
+main (void)
+{
+  int t[2];
+  f (1, t, 3);
+  exit (0);
+}
diff --git a/gcc/testsuite/gcc.dg/c2x-parm-omit-4.c b/gcc/testsuite/gcc.dg/c2x-parm-omit-4.c
new file mode 100644
index 00000000000..a4b0deb7abc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c2x-parm-omit-4.c
@@ -0,0 +1,5 @@ 
+/* Test omitted parameter names in C2x: diagnosed with -Wc11-c2x-compat.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x -pedantic-errors -Wc11-c2x-compat" } */
+
+void f (int) { } /* { dg-warning "omitting parameter names" } */
diff --git a/gcc/testsuite/gcc.dg/noncompile/pr79758.c b/gcc/testsuite/gcc.dg/noncompile/pr79758.c
index aeaf7c71dec..a31216068f0 100644
--- a/gcc/testsuite/gcc.dg/noncompile/pr79758.c
+++ b/gcc/testsuite/gcc.dg/noncompile/pr79758.c
@@ -1,6 +1,6 @@ 
 /* PR c/79758 */
 /* { dg-do compile } */
 
-void fn1 (int[a]) { }; /* { dg-error "undeclared here|parameter name omitted" } */
+void fn1 (int[a]) { }; /* { dg-error "undeclared here" } */
 void fn1 (b) { }; /* { dg-error "redefinition" } */
 /* { dg-warning "defaults to 'int'" "" { target *-*-* } .-1 } */