]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
getopt: port to NetBSD git head gcc -Wuseless cast
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 17 Jul 2026 20:47:19 +0000 (13:47 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 17 Jul 2026 20:48:08 +0000 (13:48 -0700)
Problem reported by Thomas Klausner in:
https://lists.gnu.org/r/emacs-devel/2026-07/msg00169.html
* lib/getopt-pfx-ext.h (__getopt_argv_const_is_empty): New macro,
defined to 1 when we define __getopt_argv_const to empty.
* lib/getopt1.c (ARGV_CAST): New macro.
(getopt_long, getopt_long_only): Use it.

ChangeLog
lib/getopt-pfx-ext.h
lib/getopt1.c

index d1ea69185f91b5074eabb17af75ed0931382e27f..224bcf30b3e55f7f053a8bf1a17a18a9ddfecfa9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2026-07-17  Paul Eggert  <eggert@cs.ucla.edu>
 
+       getopt: port to NetBSD git head gcc -Wuseless cast
+       Problem reported by Thomas Klausner in:
+       https://lists.gnu.org/r/emacs-devel/2026-07/msg00169.html
+       * lib/getopt-pfx-ext.h (__getopt_argv_const_is_empty): New macro,
+       defined to 1 when we define __getopt_argv_const to empty.
+       * lib/getopt1.c (ARGV_CAST): New macro.
+       (getopt_long, getopt_long_only): Use it.
+
        gendocs: output human-readable file sizes
        * build-aux/gendocs.sh (calcsize): Output human-readable file
        size, rather than always size in KiB mislabled as "K bytes".
index db2e27f36c3f4aeb47e5a15423c409c8c2abf626..189598a125b5d9b2e873de13aff22c6dad71e37d 100644 (file)
@@ -58,6 +58,7 @@
 #ifndef __getopt_argv_const
 # if defined __GETOPT_PREFIX
 #  define __getopt_argv_const /* empty */
+#  define __getopt_argv_const_is_empty 1
 # else
 #  define __getopt_argv_const const
 # endif
index a5f9988828b5a4296434ef3a4405697333007d4d..7a5ae1cad0771e21dcd362bfa5162a3e13e4d8fd 100644 (file)
 #include <getopt.h>
 #include "getopt_int.h"
 
+/* Convert char *__getopt_argv_const * to char ** without provoking
+   gcc -Wuseless-cast when __getopt_argv_gconst is empty.  */
+#ifdef __getopt_argv_const_is_empty
+# define ARGV_CAST(argv) (argv)
+#else
+# define ARGV_CAST(argv) ((char **) (argv))
+#endif
+
 int
 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
             const struct option *long_options, int *opt_index)
 {
-  return _getopt_internal (argc, (char **) argv, options, long_options,
+  return _getopt_internal (argc, ARGV_CAST (argv), options, long_options,
                           opt_index, 0, 0);
 }
 
@@ -51,7 +59,7 @@ getopt_long_only (int argc, char *__getopt_argv_const *argv,
                  const char *options,
                  const struct option *long_options, int *opt_index)
 {
-  return _getopt_internal (argc, (char **) argv, options, long_options,
+  return _getopt_internal (argc, ARGV_CAST (argv), options, long_options,
                           opt_index, 1, 0);
 }