https://gcc.gnu.org/g:8833389e90d676baabb35c3e7a021a4f5444a5ba

commit r15-5314-g8833389e90d676baabb35c3e7a021a4f5444a5ba
Author: Florian Weimer <fwei...@redhat.com>
Date:   Thu Nov 14 12:42:25 2024 +0100

    c: Introduce -Wmissing-parameter-name
    
    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.opt (Wmissing-parameter-name): New.
    
    gcc/c/
            * c-decl.cc (store_parm_decls_newstyle): Use
            OPT_Wmissing_parameter_name for missing parameter name
            warning.
            * c-errors.cc (pedwarn_c11): Enable fine-grained warning
            control via the option_id argument.
    
    gcc/
    
            * doc/invoke.texi: Document Wmissing-parameter-name.
    
    gcc/testsuite/
    
            * gcc.dg/Wmissing-parameter-name-1.c: New test.
            * gcc.dg/Wmissing-parameter-name-2.c: New test.
            * gcc.dg/Wmissing-parameter-name-3.c: New test.

Diff:
---
 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                              | 25 ++++++++++++++++++------
 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, 64 insertions(+), 12 deletions(-)

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index b920b7d347d1..127f9553e0c0 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -986,6 +986,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 3f80ec41da92..61cfe33c2512 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1026,6 +1026,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 3a45c02a1ebf..07c18a34072c 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -10900,7 +10900,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 38b11a7c70a8..653ad8c5235f 100644
--- a/gcc/c/c-errors.cc
+++ b/gcc/c/c-errors.cc
@@ -71,7 +71,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,
@@ -84,14 +87,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 93e096bc9d53..36d79d1c76bf 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -520,12 +520,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}.
@@ -6370,6 +6370,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)}
@@ -10048,6 +10049,18 @@ 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 a 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 if building for a C version before C23 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 000000000000..b804f7b3d4dc
--- /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 000000000000..a02083ffe278
--- /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 000000000000..b89d886637ed
--- /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)
+{
+}

Reply via email to