diff mbox series

[committed] c: Fix -std=gnu23 -Wtraditional for () in function definitions

Message ID 9d8e780a-56a6-5a15-b60b-283d748d89d8@redhat.com
State New
Headers show
Series [committed] c: Fix -std=gnu23 -Wtraditional for () in function definitions | expand

Commit Message

Joseph Myers Oct. 19, 2024, 12:22 a.m. UTC
We don't yet have clear agreement on removing -Wtraditional (although
it seems there is little to no use for most of the warnings therein),
so fix the bug in its interaction with -std=gnu23 to continue progress
on making -std=gnu23 the default while -Wtraditional remains under
discussion.

The warning for ISO C function definitions with -Wtraditional properly
covers (void), but also wrongly warned for () in C23 mode as that has
the same semantics as (void) in that case.  Keep track in c_arg_info
of when () was converted to (void) for C23 so that -Wtraditional can
avoid warning in that case (with an appropriate comment on the
definition of the new field to make clear it can be removed along with
-Wtraditional).

Bootstrapped with no regressions for x86_64-pc-linux-gnu.

gcc/c/
	* c-tree.h (c_arg_info): Add c23_empty_parens.
	* c-decl.cc (grokparms): Set c23_empty_parens.
	(build_arg_info): Clear c23_empty_parens.
	(store_parm_decls_newstyle): Do not give -Wtraditional warning for
	ISO C function definition if c23_empty_parens.

gcc/testsuite/
	* gcc.dg/wtr-gnu17-1.c, gcc.dg/wtr-gnu23-1.c: New tests.
diff mbox series

Patch

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 491c24b9fe7..3733ecfc13f 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -8519,7 +8519,10 @@  grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
 	  && !arg_types
 	  && !arg_info->parms
 	  && !arg_info->no_named_args_stdarg_p)
-	arg_types = arg_info->types = void_list_node;
+	{
+	  arg_types = arg_info->types = void_list_node;
+	  arg_info->c23_empty_parens = 1;
+	}
 
       /* If there is a parameter of incomplete type in a definition,
 	 this is an error.  In a declaration this is valid, and a
@@ -8589,6 +8592,7 @@  build_arg_info (void)
   ret->pending_sizes = NULL;
   ret->had_vla_unspec = 0;
   ret->no_named_args_stdarg_p = 0;
+  ret->c23_empty_parens = 0;
   return ret;
 }
 
@@ -10923,7 +10927,8 @@  store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
      its parameter list).  */
   else if (!in_system_header_at (input_location)
 	   && !current_function_scope
-	   && arg_info->types != error_mark_node)
+	   && arg_info->types != error_mark_node
+	   && !arg_info->c23_empty_parens)
     warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
 		"traditional C rejects ISO C style function definitions");
 
diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h
index bfdcb78bbcc..a1435e7cb0c 100644
--- a/gcc/c/c-tree.h
+++ b/gcc/c/c-tree.h
@@ -525,6 +525,10 @@  struct c_arg_info {
   BOOL_BITFIELD had_vla_unspec : 1;
   /* True when the arguments are a (...) prototype.  */
   BOOL_BITFIELD no_named_args_stdarg_p : 1;
+  /* True when empty parentheses have been interpreted as (void) in C23 or
+     later.  This is only for use by -Wtraditional and is no longer needed if
+     -Wtraditional is removed.  */
+  BOOL_BITFIELD c23_empty_parens : 1;
 };
 
 /* A declarator.  */
diff --git a/gcc/testsuite/gcc.dg/wtr-gnu17-1.c b/gcc/testsuite/gcc.dg/wtr-gnu17-1.c
new file mode 100644
index 00000000000..74c06e4aa4c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/wtr-gnu17-1.c
@@ -0,0 +1,9 @@ 
+/* Test -Wtraditional -std=gnu17 does not warn for empty parentheses in
+   function definition.  */
+/* { dg-do compile } */
+/* { dg-options "-Wtraditional -std=gnu17" } */
+
+void
+f ()
+{
+}
diff --git a/gcc/testsuite/gcc.dg/wtr-gnu23-1.c b/gcc/testsuite/gcc.dg/wtr-gnu23-1.c
new file mode 100644
index 00000000000..207e7c59d27
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/wtr-gnu23-1.c
@@ -0,0 +1,9 @@ 
+/* Test -Wtraditional -std=gnu23 does not warn for empty parentheses in
+   function definition.  */
+/* { dg-do compile } */
+/* { dg-options "-Wtraditional -std=gnu23" } */
+
+void
+f ()
+{
+}