]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Introduce -Wmissing-parameter-name
authorFlorian Weimer <fweimer@redhat.com>
Thu, 14 Nov 2024 11:42:25 +0000 (12:42 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Fri, 15 Nov 2024 11:18:42 +0000 (12:18 +0100)
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.

gcc/c-family/c-opts.cc
gcc/c-family/c.opt
gcc/c/c-decl.cc
gcc/c/c-errors.cc
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/Wmissing-parameter-name-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wmissing-parameter-name-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wmissing-parameter-name-3.c [new file with mode: 0644]

index b920b7d347d1e89ab7fec23a525e2c3e6fdfb78b..127f9553e0c087c57aa12048f1a40702b0318e7a 100644 (file)
@@ -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)
index 3f80ec41da92173c5e4e1755a96070c36fae76c0..61cfe33c251279577f181264d0031ab65500886f 100644 (file)
@@ -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.
index 3a45c02a1ebf8817de24cc05ebc996d8bf10b12d..07c18a34072c81810e26dd46339d520c3c345f37 100644 (file)
@@ -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");
     }
index 38b11a7c70a8b02221e0091f9e7ae99091e2d074..653ad8c5235f6c59cad0744b4983c45cd2cf3e66 100644 (file)
@@ -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.  */
index 93e096bc9d53561455ebf5351b53920e69065d6b..36d79d1c76bfbf9ce3b5073260eed559a96b423e 100644 (file)
@@ -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 (file)
index 0000000..b804f7b
--- /dev/null
@@ -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 (file)
index 0000000..a02083f
--- /dev/null
@@ -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 (file)
index 0000000..b89d886
--- /dev/null
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc11-c23-compat -Wno-missing-parameter-name" } */
+
+int
+f (int)
+{
+}