Skip to content

Commit bf3ff33

Browse files
pcloudsgitster
authored andcommitted
parse-options: stop abusing 'callback' for lowlevel callbacks
Lowlevel callbacks have different function signatures. Add a new field in 'struct option' with the right type for lowlevel callbacks. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f62470c commit bf3ff33

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
814814
* and are only included here to get included in the "-h"
815815
* output:
816816
*/
817-
{ OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
817+
{ OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental heuristic to improve diffs"), PARSE_OPT_NOARG, NULL, 0, parse_opt_unknown_cb },
818818

819819
OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
820820
OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static struct option builtin_merge_options[] = {
261261
option_parse_message),
262262
{ OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
263263
N_("read message from file"), PARSE_OPT_NONEG,
264-
(parse_opt_cb *) option_read_message },
264+
NULL, 0, option_read_message },
265265
OPT__VERBOSITY(&verbosity),
266266
OPT_BOOL(0, "abort", &abort_current_merge,
267267
N_("abort the current in-progress merge")),

builtin/update-index.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
985985
N_("add the specified entry to the index"),
986986
PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
987987
PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
988-
(parse_opt_cb *) cacheinfo_callback},
988+
NULL, 0,
989+
cacheinfo_callback},
989990
{OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+|-)x",
990991
N_("override the executable bit of the listed files"),
991992
PARSE_OPT_NONEG,
@@ -1011,19 +1012,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10111012
{OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
10121013
N_("read list of paths to be updated from standard input"),
10131014
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1014-
(parse_opt_cb *) stdin_callback},
1015+
NULL, 0, stdin_callback},
10151016
{OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &nul_term_line, NULL,
10161017
N_("add entries from standard input to the index"),
10171018
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1018-
(parse_opt_cb *) stdin_cacheinfo_callback},
1019+
NULL, 0, stdin_cacheinfo_callback},
10191020
{OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
10201021
N_("repopulate stages #2 and #3 for the listed paths"),
10211022
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1022-
(parse_opt_cb *) unresolve_callback},
1023+
NULL, 0, unresolve_callback},
10231024
{OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
10241025
N_("only update entries that differ from HEAD"),
10251026
PARSE_OPT_NONEG | PARSE_OPT_NOARG,
1026-
(parse_opt_cb *) reupdate_callback},
1027+
NULL, 0, reupdate_callback},
10271028
OPT_BIT(0, "ignore-missing", &refresh_args.flags,
10281029
N_("ignore files missing from worktree"),
10291030
REFRESH_IGNORE_MISSING),

parse-options-cb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
170170
* "-h" output even if it's not being handled directly by
171171
* parse_options().
172172
*/
173-
int parse_opt_unknown_cb(const struct option *opt, const char *arg, int unset)
173+
int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
174+
const struct option *opt, int unset)
174175
{
175176
return -2;
176177
}

parse-options.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int get_value(struct parse_opt_ctx_t *p,
9393

9494
switch (opt->type) {
9595
case OPTION_LOWLEVEL_CALLBACK:
96-
return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
96+
return opt->ll_callback(p, opt, unset);
9797

9898
case OPTION_BIT:
9999
if (unset)
@@ -408,6 +408,19 @@ static void parse_options_check(const struct option *opts)
408408
if ((opts->flags & PARSE_OPT_OPTARG) ||
409409
!(opts->flags & PARSE_OPT_NOARG))
410410
err |= optbug(opts, "should not accept an argument");
411+
break;
412+
case OPTION_CALLBACK:
413+
if (!opts->callback)
414+
BUG("OPTION_CALLBACK needs a callback");
415+
if (opts->ll_callback)
416+
BUG("OPTION_CALLBACK needs no ll_callback");
417+
break;
418+
case OPTION_LOWLEVEL_CALLBACK:
419+
if (!opts->ll_callback)
420+
BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
421+
if (opts->callback)
422+
BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
423+
break;
411424
default:
412425
; /* ok. (usually accepts an argument) */
413426
}

parse-options.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
100100
* the option takes optional argument.
101101
*
102102
* `callback`::
103-
* pointer to the callback to use for OPTION_CALLBACK or
104-
* OPTION_LOWLEVEL_CALLBACK.
103+
* pointer to the callback to use for OPTION_CALLBACK
105104
*
106105
* `defval`::
107106
* default value to fill (*->value) with for PARSE_OPT_OPTARG.
108107
* OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
109108
* CALLBACKS can use it like they want.
109+
*
110+
* `ll_callback`::
111+
* pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
112+
*
110113
*/
111114
struct option {
112115
enum parse_opt_type type;
@@ -119,6 +122,7 @@ struct option {
119122
int flags;
120123
parse_opt_cb *callback;
121124
intptr_t defval;
125+
parse_opt_ll_cb *ll_callback;
122126
intptr_t extra;
123127
};
124128

@@ -137,7 +141,7 @@ struct option {
137141
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
138142
#define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
139143
PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, \
140-
(set), (clear) }
144+
(set), NULL, (clear) }
141145
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
142146
(h), PARSE_OPT_NOARG, NULL, (b) }
143147
#define OPT_COUNTUP(s, l, v, h) OPT_COUNTUP_F(s, l, v, h, 0)
@@ -263,7 +267,7 @@ int parse_opt_commits(const struct option *, const char *, int);
263267
int parse_opt_tertiary(const struct option *, const char *, int);
264268
int parse_opt_string_list(const struct option *, const char *, int);
265269
int parse_opt_noop_cb(const struct option *, const char *, int);
266-
int parse_opt_unknown_cb(const struct option *, const char *, int);
270+
int parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx, const struct option *, int);
267271
int parse_opt_passthru(const struct option *, const char *, int);
268272
int parse_opt_passthru_argv(const struct option *, const char *, int);
269273

0 commit comments

Comments
 (0)