[PATCH] cli: use designated initializers for opt desc

Jani Nikula jani at nikula.org
Sun Oct 1 11:35:48 PDT 2017


On Sun, 01 Oct 2017, Tomi Ollila <tomi.ollila at iki.fi> wrote:
> On Sun, Oct 01 2017, Jani Nikula wrote:
>
>> Several changes at once, just to not have to change the same lines
>> several times over:
>>
>> - Use designated initializers to initialize opt desc arrays.
>>
>> - Only initialize the needed fields.
>>
>> - Remove arg_id (short options) as unused.
>>
>> - Replace opt_type and output_var with several type safe output
>>   variables, where the output variable being non-NULL determines the
>>   type. Introduce checks to ensure only one is set. The downside is
>>   some waste of const space per argument; this could be saved by
>>   retaining opt_type and using a union, but that's still pretty
>>   verbose.
>>
>> - Fix some variables due to the type safety. Mostly a good thing, but
>>   leads to some enums being changed to ints. This is pedantically
>>   correct, but somewhat annoying. We could also cast, but that defeats
>>   the purpose a bit.
>>
>> - Terminate the opt desc arrays using {}.
>>
>> The output variable type safety and the ability to add new fields for
>> just some output types or arguments are the big wins. For example, if
>> we wanted to add a variable to set when the argument is present, we
>> could do so for just the arguments that need it.
>>
>> Beauty is in the eye of the beholder, but I think this looks nice when
>> defining the arguments, and reduces some of the verbosity we have
>> there.
>
> Good stuff. It just doesn't longer compile on older compilers (so some
> note on commit message should be added):

Does this on top fix it? I used the unnamed struct just for clarity, and
it doesn't serve a functional purpose.

BR,
Jani.

diff --git a/command-line-arguments.h b/command-line-arguments.h
index 97134ad535ee..799b7fef3f65 100644
--- a/command-line-arguments.h
+++ b/command-line-arguments.h
@@ -16,15 +16,13 @@ typedef struct notmuch_keyword {
 /* Describe one option. */
 typedef struct notmuch_opt_desc {
     /* One and only one of these must be set. */
-    struct {
-	const struct notmuch_opt_desc *opt_inherit;
-	notmuch_bool_t *opt_bool;
-	int *opt_int;
-	int *opt_keyword;
-	int *opt_flags;
-	const char **opt_string;
-	const char **opt_position;
-    };
+    const struct notmuch_opt_desc *opt_inherit;
+    notmuch_bool_t *opt_bool;
+    int *opt_int;
+    int *opt_keyword;
+    int *opt_flags;
+    const char **opt_string;
+    const char **opt_position;
 
     /* Must be set except for opt_inherit and opt_position. */
     const char *name;
--

>
> CC  -g -O2 notmuch.o
> notmuch.c:53: error: unknown field ‘opt_bool’ specified in initializer
> notmuch.c:53: warning: missing braces around initializer
> notmuch.c:53: warning: (near initialization for
> ‘notmuch_shared_options[0].<anonymous>’)
> notmuch.c:53: warning: initialization from incompatible pointer type
> notmuch.c:53: warning: missing initializer
> notmuch.c:53: warning: (near initialization for
> ‘notmuch_shared_options[0].<anonymous>.opt_bool’)
> notmuch.c:54: error: unknown field ‘opt_bool’ specified in initializer
> notmuch.c:54: warning: initialization from incompatible pointer type
> notmuch.c:54: warning: missing initializer
> notmuch.c:54: warning: (near initialization for
> ‘notmuch_shared_options[1].<anonymous>.opt_bool’)
> notmuch.c:55: error: unknown field ‘opt_string’ specified in initializer
> notmuch.c:55: warning: initialization from incompatible pointer type
> notmuch.c:55: warning: missing initializer
> notmuch.c:55: warning: (near initialization for
> ‘notmuch_shared_options[2].<anonymous>.opt_bool’)
> notmuch.c:56: warning: missing initializer
> notmuch.c:56: warning: (near initialization for
> ‘notmuch_shared_options[3].<anonymous>’)
> notmuch.c: In function ‘notmuch_minimal_options’:
> notmuch.c:85: error: unknown field ‘opt_inherit’ specified in initializer
> notmuch.c:85: warning: missing braces around initializer
> notmuch.c:85: warning: (near initialization for ‘options[0].<anonymous>’)
> notmuch.c:85: warning: missing initializer
> notmuch.c:85: warning: (near initialization for
> ‘options[0].<anonymous>.opt_bool’)
> notmuch.c:86: warning: missing initializer
> notmuch.c:86: warning: (near initialization for ‘options[1].<anonymous>’)
> notmuch.c: In function ‘main’:
> notmuch.c:414: error: unknown field ‘opt_string’ specified in initializer
> notmuch.c:414: warning: missing braces around initializer
> notmuch.c:414: warning: (near initialization for ‘options[0].<anonymous>’)
> notmuch.c:414: warning: initialization from incompatible pointer type
> notmuch.c:414: warning: missing initializer
> notmuch.c:414: warning: (near initialization for
> ‘options[0].<anonymous>.opt_bool’)
> notmuch.c:415: error: unknown field ‘opt_inherit’ specified in initializer
> notmuch.c:415: warning: missing initializer
> notmuch.c:415: warning: (near initialization for
> ‘options[1].<anonymous>.opt_bool’)
> notmuch.c:416: warning: missing initializer
> notmuch.c:416: warning: (near initialization for ‘options[2].<anonymous>’)
> make: *** [notmuch.o] Error 1
>
> gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 
>
> This was on Scientific Linux 6.2 -- will test on CentOS 7 (which IIRC has
> gcc 4.8) container soon...
>
>
> Tomi
> _______________________________________________
> notmuch mailing list
> notmuch at notmuchmail.org
> https://notmuchmail.org/mailman/listinfo/notmuch


More information about the notmuch mailing list