Skip to content

Commit 3f9ab7c

Browse files
avargitster
authored andcommitted
parse-options.[ch]: consistently use "enum parse_opt_flags"
Use the "enum parse_opt_flags" instead of an "int flags" as arguments to the various functions in parse-options.c. Even though this is an enum bitfield there's there's a benefit to doing this when it comes to the wider C ecosystem. E.g. the GNU debugger (gdb) will helpfully detect and print out meaningful enum labels in this case. Here's the output before and after when breaking in "parse_options()" after invoking "git stash show": Before: (gdb) p flags $1 = 9 After: (gdb) p flags $1 = (PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN) Of course as noted in[1] there's a limit to this smartness, i.e. manually setting it with unrelated enum labels won't be caught. There are some third-party extensions to do more exhaustive checking[2], perhaps we'll be able to make use of them sooner than later. We've also got prior art using this pattern in the codebase. See e.g. "enum bloom_filter_computed" added in 312cff5 (bloom: split 'get_bloom_filter()' in two, 2020-09-16) and the "permitted" enum added in ce91028 (add -p: fix checking of user input, 2020-08-17). 1. https://lore.kernel.org/git/[email protected]/ 2. https://github.com/sinelaw/elfs-clang-plugins/blob/master/enums_conversion/README.md Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b723f7 commit 3f9ab7c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

parse-options.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ static void parse_options_check(const struct option *opts)
481481

482482
static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
483483
int argc, const char **argv, const char *prefix,
484-
const struct option *options, int flags)
484+
const struct option *options,
485+
enum parse_opt_flags flags)
485486
{
486487
ctx->argc = argc;
487488
ctx->argv = argv;
@@ -506,7 +507,8 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
506507

507508
void parse_options_start(struct parse_opt_ctx_t *ctx,
508509
int argc, const char **argv, const char *prefix,
509-
const struct option *options, int flags)
510+
const struct option *options,
511+
enum parse_opt_flags flags)
510512
{
511513
memset(ctx, 0, sizeof(*ctx));
512514
parse_options_start_1(ctx, argc, argv, prefix, options, flags);
@@ -838,8 +840,9 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
838840
}
839841

840842
int parse_options(int argc, const char **argv, const char *prefix,
841-
const struct option *options, const char * const usagestr[],
842-
int flags)
843+
const struct option *options,
844+
const char * const usagestr[],
845+
enum parse_opt_flags flags)
843846
{
844847
struct parse_opt_ctx_t ctx;
845848
struct option *real_options;

parse-options.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ struct option {
213213
*/
214214
int parse_options(int argc, const char **argv, const char *prefix,
215215
const struct option *options,
216-
const char * const usagestr[], int flags);
216+
const char * const usagestr[],
217+
enum parse_opt_flags flags);
217218

218219
NORETURN void usage_with_options(const char * const *usagestr,
219220
const struct option *options);
@@ -262,15 +263,16 @@ struct parse_opt_ctx_t {
262263
const char **out;
263264
int argc, cpidx, total;
264265
const char *opt;
265-
int flags;
266+
enum parse_opt_flags flags;
266267
const char *prefix;
267268
const char **alias_groups; /* must be in groups of 3 elements! */
268269
struct option *updated_options;
269270
};
270271

271272
void parse_options_start(struct parse_opt_ctx_t *ctx,
272273
int argc, const char **argv, const char *prefix,
273-
const struct option *options, int flags);
274+
const struct option *options,
275+
enum parse_opt_flags flags);
274276

275277
int parse_options_step(struct parse_opt_ctx_t *ctx,
276278
const struct option *options,

0 commit comments

Comments
 (0)