git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 02/10] parse-options.[ch]: consistently use "enum parse_opt_flags"

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Sep 29, 2021, 08:53 UTC
Message-ID
<87mtnvvj3c.fsf@evledraar.gmail.com>
In-Reply-To
<xmqqilykgs2k.fsf@gitster.g>
On Tue, Sep 28 2021, Junio C Hamano wrote:
Show 56 quoted lines
> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
>
>> Use the "enum parse_opt_flags" instead of an "int flags" as arguments
>> to the various functions in parse-options.c. This will help to catch
>> cases where we're not handling cases in switch statements, and
>
> I am not sure about most part this change, and the claim the second
> sentence makes is certainly dubious.  Let's look at the first hunk:
>
>> diff --git a/parse-options.c b/parse-options.c
>> index 55c5821b08d..9dce8b7f8a8 100644
>> --- a/parse-options.c
>> +++ b/parse-options.c
>> @@ -481,7 +481,8 @@ static void parse_options_check(const struct option *opts)
>
>  static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
>  				  int argc, const char **argv, const char *prefix,
> -				  const struct option *options, int flags)
> +				  const struct option *options,
> +				  enum parse_opt_flags flags)
>  {
>  	ctx->argc = argc;
>  	ctx->argv = argv;
>  	if (!(flags & PARSE_OPT_ONE_SHOT)) {
>  		ctx->argc--;
>  		ctx->argv++;
>  	}
>  	ctx->total = ctx->argc;
>  	ctx->out   = argv;
>  	ctx->prefix = prefix;
>  	ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
>  	ctx->flags = flags;
>  	if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
>  	    (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
>  	    !(flags & PARSE_OPT_ONE_SHOT))
>  		BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
>  	if ((flags & PARSE_OPT_ONE_SHOT) &&
>  	    (flags & PARSE_OPT_KEEP_ARGV0))
>  		BUG("Can't keep argv0 if you don't have it");
>  	parse_options_check(options);
>  }
>  
> The "flags" parameter does not take a value that is an "enum" in the
> usual "enumeration" sense at all.  Even though parse_opt_flags
> defines 7 distinct "enum" values, each enumerated value is a small
> unsigned integer with only single bit set, the caller can throw a
> value that is not among these 7 by OR'ing them together.  We would
> not sensibly do
>
> 	switch (flags) {
> 	case PARSE_OPT_KEEP_UNKNOWN:
> 		...
>
> In general, I am not all that enthusiastic for such a(n) (ab)use of
> "enum" for bit patterns, much less than "enumerate all possible
> values to make sure compilers would help us catch missing logic".

I agree that it's not as nice as enums where the fields are mutually exclusive, since those can be checked via "case" arms, and this is "unchecked" bitfields.

So e.g. the bug I fixed in 01/10 would not be found by a compiler I have access to (and I don't think one currently exists).

But I think this is perfectly good use of enums, we use this enums-as-bitfields pattern in various other places, e.g. builtin/rebase.c's "flags", the "commit_graph_write_flags", "expire_reflog_flags" & "todo_item_flags", just to name a few from some quick grepping.

One advantage is that integrates nicely with some wider C tooling. E.g. before this series, starting "git stash show" under gdb and inspecting flags:

    (gdb) p flags
    $1 = 9
And after:
    (gdb) p flags
    $1 = (PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN)
So the type information and bitfield-ness are retained.
Although you might argue that it leads you into a trap, as adding:
    flags |= PARSE_OPT_LASTARG_DEFAULT;
Will result in:
    (gdb) p flags
    $2 = (PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_NO_INTERNAL_HELP)

I.e. it decodes the enum based on the int value & its known labels, and it just so happens that PARSE_OPT_LASTARG_DEFAULT has the same value as PARSE_OPT_NO_INTERNAL_HELP in an unrelated enum.

Show 31 quoted lines
> The "parse_opt_result" enum is the "right" kind of enumeration that
> I can stand behind fully.  The hunk related to that enum in this
> patch is quite reasonable and takes advantage of the fact that the
> enum is meant to be the enumeration of all possible values.
>
> Compared to it, parse_opt_flags and parse_opt_option_flags, not
> really.
>
> If we wanted to really clean up the latter two, perhaps we should
> define the bit (which *can* be made to a proper "here are the all
> possible values" enumeration), like this:
>
>     enum parse_opt_flags_bit {
> 	PARSE_OPT_KEEP_DASHDASH_BIT = 0,
>         PARSE_OPT_STOP_AT_NON_OPTION_BIT,
> 	PARSE_OPT_KEEP_ARGV0_BIT,
> 	...
> 	PARSE_OPT_SHELL_EVAL_BIT,
>     };
>
> and then update the users to use (1U << PARSE_OPT_$FLAG$_BIT), or
> drop the pretense that it is a good idea to use enum for bit pattern
> and use the CPP macro, i.e.
>
>     #define PARSE_OPT_KEEP_DASHDASH (1U<<0)
>     #define PARSE_OPT_STOP_AT_NON_OPTION (1U<<1)
>     ...
>     #define PARSE_OPT_SHELL_EVAL (1U<<6)
>
> to make it clear that we do not mean these are "enumeration of
> possible values".

I'm not sure what the former suggestion here buys us, but the latter will drop the type information as noted above, i.e. you'll get a:

    (gdb) p flags
    $1 = 9
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 61 in “fix bug, use existing enums”
  1. 00/10 fix bug, use existing enumsÆvar Arnfjörð Bjarmason, Sep 28, 2021
  2. 01/10 parse-options.h: move PARSE_OPT_SHELL_EVAL between enumsÆvar Arnfjörð Bjarmason, Sep 28, 2021
  3. 02/10 parse-options.[ch]: consistently use "enum parse_opt_flags"Ævar Arnfjörð Bjarmason, Sep 28, 2021
  4. Junio C HamanoSep 29, 2021
  5. Ævar Arnfjörð BjarmasonSep 29, 2021
  6. Junio C HamanoSep 29, 2021
  7. Junio C HamanoSep 29, 2021
  8. 03/10 parse-options.[ch]: consistently use "enum parse_opt_result"Ævar Arnfjörð Bjarmason, Sep 28, 2021
  9. Junio C HamanoSep 29, 2021
  10. 04/10 parse-options.c: use exhaustive "case" arms for "enum parse_opt_type"Ævar Arnfjörð Bjarmason, Sep 28, 2021
  11. Junio C HamanoSep 29, 2021
  12. Ævar Arnfjörð BjarmasonSep 29, 2021
  13. Junio C HamanoSep 29, 2021
  14. 05/10 parse-options.h: make the "flags" in "struct option" an enumÆvar Arnfjörð Bjarmason, Sep 28, 2021
  15. Junio C HamanoSep 29, 2021
  16. 07/10 commit-graph: stop using optname()Ævar Arnfjörð Bjarmason, Sep 28, 2021
  17. Taylor BlauSep 29, 2021
  18. Ævar Arnfjörð BjarmasonOct 1, 2021
  19. 06/10 parse-options.c: move optname() earlier in the fileÆvar Arnfjörð Bjarmason, Sep 28, 2021
  20. 08/10 parse-options.[ch]: make opt{bug,name}() "static"Ævar Arnfjörð Bjarmason, Sep 28, 2021
  21. 09/10 parse-options tests: test optname() outputÆvar Arnfjörð Bjarmason, Sep 28, 2021
  22. 10/10 parse-options: change OPT_{SHORT,UNSET} to an enumÆvar Arnfjörð Bjarmason, Sep 28, 2021
  23. Junio C HamanoSep 29, 2021
  24. 00/11 fix bug, use existing enumsÆvar Arnfjörð Bjarmason, Oct 1, 2021
  25. 01/11 parse-options.h: move PARSE_OPT_SHELL_EVAL between enumsÆvar Arnfjörð Bjarmason, Oct 1, 2021
  26. 02/11 parse-options.[ch]: consistently use "enum parse_opt_flags"Ævar Arnfjörð Bjarmason, Oct 1, 2021
  27. Junio C HamanoOct 1, 2021
  28. 03/11 parse-options.[ch]: consistently use "enum parse_opt_result"Ævar Arnfjörð Bjarmason, Oct 1, 2021
  29. 04/11 parse-options.c: use exhaustive "case" arms for "enum parse_opt_result"Ævar Arnfjörð Bjarmason, Oct 1, 2021
  30. 05/11 parse-options.c: use exhaustive "case" arms for "enum parse_opt_type"Ævar Arnfjörð Bjarmason, Oct 1, 2021
  31. 06/11 parse-options.h: make the "flags" in "struct option" an enumÆvar Arnfjörð Bjarmason, Oct 1, 2021
  32. 07/11 parse-options.c: move optname() earlier in the fileÆvar Arnfjörð Bjarmason, Oct 1, 2021
  33. 08/11 commit-graph: stop using optname()Ævar Arnfjörð Bjarmason, Oct 1, 2021
  34. René ScharfeOct 1, 2021
  35. 09/11 parse-options.[ch]: make opt{bug,name}() "static"Ævar Arnfjörð Bjarmason, Oct 1, 2021
  36. 10/11 parse-options tests: test optname() outputÆvar Arnfjörð Bjarmason, Oct 1, 2021
  37. 11/11 parse-options: change OPT_{SHORT,UNSET} to an enumÆvar Arnfjörð Bjarmason, Oct 1, 2021
  38. Junio C HamanoOct 1, 2021
  39. Junio C HamanoOct 1, 2021
  40. 00/10 fix bug, use more enumsÆvar Arnfjörð Bjarmason, Oct 8, 2021
  41. 01/10 parse-options.h: move PARSE_OPT_SHELL_EVAL between enumsÆvar Arnfjörð Bjarmason, Oct 8, 2021
  42. 03/10 parse-options.[ch]: consistently use "enum parse_opt_result"Ævar Arnfjörð Bjarmason, Oct 8, 2021
  43. SZEDER GáborNov 6, 2021
  44. Ævar Arnfjörð BjarmasonNov 6, 2021
  45. 0/2 parse-options.[ch]: enum fixup & enum nitÆvar Arnfjörð Bjarmason, Nov 9, 2021
  46. 1/2 parse-options.[ch]: revert use of "enum" for parse_options()Ævar Arnfjörð Bjarmason, Nov 9, 2021
  47. Junio C HamanoNov 9, 2021
  48. 2/2 parse-options.c: use "enum parse_opt_result" for parse_nodash_opt()Ævar Arnfjörð Bjarmason, Nov 9, 2021
  49. Junio C HamanoNov 9, 2021
  50. Ævar Arnfjörð BjarmasonNov 9, 2021
  51. Junio C HamanoNov 9, 2021
  52. parse-options.c: use "enum parse_opt_result" for parse_nodash_opt()Ævar Arnfjörð Bjarmason, Nov 10, 2021
  53. Junio C HamanoNov 11, 2021
  54. 02/10 parse-options.[ch]: consistently use "enum parse_opt_flags"Ævar Arnfjörð Bjarmason, Oct 8, 2021
  55. 05/10 parse-options.h: make the "flags" in "struct option" an enumÆvar Arnfjörð Bjarmason, Oct 8, 2021
  56. 04/10 parse-options.c: use exhaustive "case" arms for "enum parse_opt_result"Ævar Arnfjörð Bjarmason, Oct 8, 2021
  57. 06/10 parse-options.c: move optname() earlier in the fileÆvar Arnfjörð Bjarmason, Oct 8, 2021
  58. 07/10 commit-graph: stop using optname()Ævar Arnfjörð Bjarmason, Oct 8, 2021
  59. 08/10 parse-options.[ch]: make opt{bug,name}() "static"Ævar Arnfjörð Bjarmason, Oct 8, 2021
  60. 09/10 parse-options tests: test optname() outputÆvar Arnfjörð Bjarmason, Oct 8, 2021
  61. 10/10 parse-options: change OPT_{SHORT,UNSET} to an enumÆvar Arnfjörð Bjarmason, Oct 8, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.