Empirically, omitted parameter names are difficult to catch in code
review.  With this change, projects can build with
-Werror=missing-parameter-name, to avoid this unnecessary
incompatibility with older GCC versions.  The existing
-pedantic-errors option is too broad for that because it also flags
widely used and widely available GCC extensions.  Likewise for
-Werror=c11-c23-compat.

gcc/

        * c-family/c-opts.cc (c_common_post_options): Initialize
        warn_missing_parameter_name.
        * c-family/c.opt (Wmissing-parameter-name): New.
        * c/c-decl.cc (store_parm_decls_newstyle): Use
        OPT_Wmissing_parameter_name for missing parameter name
        warning.
        * c/c-errors.cc (pedwarn_c11): Enable fine-grained warning
        control via the option_id argument.
        * doc/invoke.texi: Document Wmissing-parameter-name.
        * testsuite/gcc.dg/Wmissing-parameter-name-1.c: New test.
        * testsuite/gcc.dg/Wmissing-parameter-name-2.c: New test.
        * testsuite/gcc.dg/Wmissing-parameter-name-3.c: New test.

---
 gcc/c-family/c-opts.cc                           |  7 +++++++
 gcc/c-family/c.opt                               |  4 ++++
 gcc/c/c-decl.cc                                  |  2 +-
 gcc/c/c-errors.cc                                | 17 ++++++++++++-----
 gcc/doc/invoke.texi                              | 24 ++++++++++++++++++------
 gcc/testsuite/gcc.dg/Wmissing-parameter-name-1.c |  7 +++++++
 gcc/testsuite/gcc.dg/Wmissing-parameter-name-2.c |  7 +++++++
 gcc/testsuite/gcc.dg/Wmissing-parameter-name-3.c |  7 +++++++
 8 files changed, 63 insertions(+), 12 deletions(-)

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index afb963de061..868b03cc410 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -985,6 +985,13 @@ c_common_post_options (const char **pfilename)
   if (warn_shift_overflow == -1)
     warn_shift_overflow = cxx_dialect >= cxx11 || flag_isoc99;
 
+  /* -Wmissing-parameter-name is enabled by -pedantic before C23,
+     and for -Wc11-c23-compat.  */
+  if (warn_missing_parameter_name == -1)
+    warn_missing_parameter_name
+      = ((pedantic && !flag_isoc23 && warn_c11_c23_compat != 0)
+        || warn_c11_c23_compat > 0);
+
   /* -Wshift-negative-value is enabled by -Wextra in C99 and C++11 to C++17
      modes.  */
   if (warn_shift_negative_value == -1)
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index d5608ec363c..eb4e5e8c500 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1007,6 +1007,10 @@ Wmissing-include-dirs
 C ObjC C++ ObjC++ CPP(warn_missing_include_dirs) 
CppReason(CPP_W_MISSING_INCLUDE_DIRS) Var(cpp_warn_missing_include_dirs) 
Init(0) Warning
 Warn about user-specified include directories that do not exist.
 
+Wmissing-parameter-name
+C ObjC Var(warn_missing_parameter_name) Init(-1) Warning
+Warn about function definitions omitting parameter names.
+
 Wmissing-parameter-type
 C ObjC Var(warn_missing_parameter_type) Warning EnabledBy(Wextra)
 Warn about function parameters declared without a type specifier in K&R-style 
functions.
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 3733ecfc13f..7ab5e6f920e 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -10946,7 +10946,7 @@ store_parm_decls_newstyle (tree fndecl, const struct 
c_arg_info *arg_info)
            warn_if_shadowing (decl);
        }
       else
-       pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
+       pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wmissing_parameter_name,
                     "ISO C does not support omitting parameter names in "
                     "function definitions before C23");
     }
diff --git a/gcc/c/c-errors.cc b/gcc/c/c-errors.cc
index c6b7c108354..77f008eba1c 100644
--- a/gcc/c/c-errors.cc
+++ b/gcc/c/c-errors.cc
@@ -70,7 +70,10 @@ pedwarn_c23 (location_t location,
    otherwise issue warning MSGID if -Wc11-c23-compat is specified.
    This function is supposed to be used for matters that are allowed in
    ISO C23 but not supported in ISO C11, thus we explicitly don't pedwarn
-   when C23 is specified.  */
+   when C23 is specified.
+
+   Additionally, warn if OPTION_ID is not OPT_Wpedantic nor
+   OPT_Wc11_c23_compat.  */
 
 bool
 pedwarn_c11 (location_t location,
@@ -83,14 +86,18 @@ pedwarn_c11 (location_t location,
   rich_location richloc (line_table, location);
 
   va_start (ap, gmsgid);
-  /* If desired, issue the C11/C23 compat warning, which is more specific
-     than -pedantic.  */
-  if (warn_c11_c23_compat > 0)
+  /* If desired, issue the C11/C23 compat warning, which is more specific than
+     -pedantic, or the warning specified by option_id.  */
+  if (warn_c11_c23_compat > 0 || (option_id.m_idx != OPT_Wpedantic
+                                 && option_id.m_idx != OPT_Wc11_c23_compat))
     {
       diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
                           (pedantic && !flag_isoc23)
                           ? DK_PEDWARN : DK_WARNING);
-      diagnostic.option_id = OPT_Wc11_c23_compat;
+      if (option_id == OPT_Wpedantic)
+       diagnostic.option_id = OPT_Wc11_c23_compat;
+      else
+       diagnostic.option_id = option_id;
       warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
     }
   /* -Wno-c11-c23-compat suppresses even the pedwarns.  */
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index aebcc9082ff..2e8913a9395 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -517,12 +517,12 @@ Objective-C and Objective-C++ Dialects}.
 }
 
 @item C and Objective-C-only Warning Options
-@gccoptlist{-Wbad-function-cast  -Wmissing-declarations
--Wmissing-parameter-type -Wdeclaration-missing-parameter-type
--Wmissing-prototypes -Wmissing-variable-declarations
--Wnested-externs -Wold-style-declaration  -Wold-style-definition
--Wstrict-prototypes  -Wtraditional  -Wtraditional-conversion
--Wdeclaration-after-statement  -Wpointer-sign}
+@gccoptlist{-Wbad-function-cast -Wmissing-declarations
+-Wmissing-parameter-name -Wmissing-parameter-type
+-Wdeclaration-missing-parameter-type -Wmissing-prototypes
+-Wmissing-variable-declarations -Wnested-externs -Wold-style-declaration
+-Wold-style-definition -Wstrict-prototypes -Wtraditional
+-Wtraditional-conversion -Wdeclaration-after-statement -Wpointer-sign}
 
 @item Debugging Options
 @xref{Debugging Options,,Options for Debugging Your Program}.
@@ -6273,6 +6273,7 @@ name is still supported, but the newer name is more 
descriptive.)
 -Wimplicit-fallthrough=3
 -Wmaybe-uninitialized
 -Wmissing-field-initializers
+-Wmissing-parameter-name @r{(C/ObjC only)}
 -Wmissing-parameter-type @r{(C/ObjC only)}
 -Wold-style-declaration @r{(C/ObjC only)}
 -Woverride-init @r{(C/ObjC only)}
@@ -9929,6 +9930,17 @@ is not considered an old-style definition in C23 mode, 
because it is
 equivalent to @samp{(void)} in that case, but is considered an
 old-style definition for older standards.
 
+@opindex Wmissing-parameter-name
+@opindex Wno-missing-parameter-name
+@item -Wmissing-parameter-name @r{(C and Objective-C only)}
+Warn if a function definition omits are parameter name, specifying only
+its type.  This can be used to document that a parameter is unused
+in the definition.  It is part of C23 and later dialects of C,
+and available as a GCC extension in all other dialects.
+
+This warning is also enabled by @option{-Wc11-c23-compat}.  It is turned
+into an error by @option{-pedantic-errors}.
+
 @opindex Wmissing-parameter-type
 @opindex Wno-missing-parameter-type
 @item -Wmissing-parameter-type @r{(C and Objective-C only)}
diff --git a/gcc/testsuite/gcc.dg/Wmissing-parameter-name-1.c 
b/gcc/testsuite/gcc.dg/Wmissing-parameter-name-1.c
new file mode 100644
index 00000000000..b804f7b3d4d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wmissing-parameter-name-1.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wmissing-parameter-name" } */
+
+int
+f (int) /* { dg-warning "omitting parameter names" } */
+{
+}
diff --git a/gcc/testsuite/gcc.dg/Wmissing-parameter-name-2.c 
b/gcc/testsuite/gcc.dg/Wmissing-parameter-name-2.c
new file mode 100644
index 00000000000..a02083ffe27
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wmissing-parameter-name-2.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic -Wno-missing-parameter-name" } */
+
+int
+f (int)
+{
+}
diff --git a/gcc/testsuite/gcc.dg/Wmissing-parameter-name-3.c 
b/gcc/testsuite/gcc.dg/Wmissing-parameter-name-3.c
new file mode 100644
index 00000000000..b89d886637e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wmissing-parameter-name-3.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc11-c23-compat -Wno-missing-parameter-name" } */
+
+int
+f (int)
+{
+}

base-commit: 1a4c5643a5911d130dfab9a064222baeeb7f9be7

Reply via email to