This new way of writing an options array static struct program_option const options[] = { { "width", 'w', required_argument }, { NULL, 'x', no_argument }, { "help", 'h', no_argument, &show_help, 1 }, { "version", 'V', no_argument, &show_version, 1 }, };
produces -Wmissing-field-initializers warnings. -Wmissing-field-initializers is a useful warning in general. Unfortunately, it cannot be silenced on a per-struct basis. I see basically three ways out: * Add a pragma for silencing the warning: BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS static struct program_option const options[] = { { "width", 'w', required_argument }, { NULL, 'x', no_argument }, { "help", 'h', no_argument, &show_help, 1 }, { "version", 'V', no_argument, &show_version, 1 }, }; END_ALLOW_OMITTING_FIELD_INITIALIZERS * Add initializers in a hidden form: static struct program_option const options[] = { { "width", 'w', required_argument _NO_ACTION_ }, { NULL, 'x', no_argument _NO_ACTION_ }, { "help", 'h', no_argument, &show_help, 1 }, { "version", 'V', no_argument, &show_version, 1 }, }; * Use a variadic macro that provides the missing initializers: static struct program_option const options[] = { OPTION( "width", 'w', required_argument ), OPTION( NULL, 'x', no_argument ), OPTION( "help", 'h', no_argument, &show_help, 1 ), OPTION( "version", 'V', no_argument, &show_version, 1 ), }; It's a matter of taste. I prefer the first one. 2025-06-28 Bruno Haible <br...@clisp.org> options: Avoid -Wmissing-field-initializers warnings. * lib/options.h (BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS, END_ALLOW_OMITTING_FIELD_INITIALIZERS): New macros. diff --git a/lib/options.h b/lib/options.h index 6ac33a5ac7..044cc73265 100644 --- a/lib/options.h +++ b/lib/options.h @@ -58,6 +58,7 @@ you write + BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS static struct program_option const options[] = { { "width", 'w', required_argument }, @@ -65,6 +66,7 @@ { "help", 'h', no_argument, &show_help, 1 }, { "version", 'V', no_argument, &show_version, 1 }, }; + END_ALLOW_OMITTING_FIELD_INITIALIZERS start_options (argc, argv, options, MOVE_OPTIONS_FIRST, 0); while ((optchar = get_next_option ()) != -1) @@ -180,6 +182,21 @@ struct program_option int value; }; +/* These macros silence '-Wmissing-field-initializers' warnings from GCC and + clang in the definition of a 'struct program_option' array. + To be placed before and after the declaration of a 'struct program_option' + array. */ +#if _GL_GNUC_PREREQ (4, 6) || defined __clang__ +# define BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") +# define END_ALLOW_OMITTING_FIELD_INITIALIZERS \ + _Pragma("GCC diagnostic pop") +#else +# define BEGIN_ALLOW_OMITTING_FIELD_INITIALIZERS +# define END_ALLOW_OMITTING_FIELD_INITIALIZERS +#endif + /* Handling of non-option arguments. */ enum non_option_handling { /* Move options before non-option arguments.