summaryrefslogtreecommitdiff
path: root/builtin/update-index.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2018-11-05 01:45:42 -0500
committerJunio C Hamano <gitster@pobox.com>2018-11-06 12:56:29 +0900
commit517fe807d6903c629a739b23fe0e75b892096998 (patch)
tree532587a81f933fbe5d9b362f725a58b8bc4340a2 /builtin/update-index.c
parent0a8a16ade6b3a55114cd0f28e5e71c2a3483d825 (diff)
downloadgit-517fe807d6903c629a739b23fe0e75b892096998.tar.gz
assert NOARG/NONEG behavior of parse-options callbacks
When we define a parse-options callback, the flags we put in the option struct must match what the callback expects. For example, a callback which does not handle the "unset" parameter should only be used with PARSE_OPT_NONEG. But since the callback and the option struct are not defined next to each other, it's easy to get this wrong (as earlier patches in this series show). Fortunately, the compiler can help us here: compiling with -Wunused-parameters can show us which callbacks ignore their "unset" parameters (and likewise, ones that ignore "arg" expect to be triggered with PARSE_OPT_NOARG). But after we've inspected a callback and determined that all of its callers use the right flags, what do we do next? We'd like to silence the compiler warning, but do so in a way that will catch any wrong calls in the future. We can do that by actually checking those variables and asserting that they match our expectations. Because this is such a common pattern, we'll introduce some helper macros. The resulting messages aren't as descriptive as we could make them, but the file/line information from BUG() is enough to identify the problem (and anyway, the point is that these should never be seen). Each of the annotated callbacks in this patch triggers -Wunused-parameters, and was manually inspected to make sure all callers use the correct options (so none of these BUGs should be triggerable). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/update-index.c')
-rw-r--r--builtin/update-index.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 07c10bcb7d..faa16c61f1 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -790,12 +790,16 @@ static int refresh(struct refresh_params *o, unsigned int flag)
static int refresh_callback(const struct option *opt,
const char *arg, int unset)
{
+ BUG_ON_OPT_NEG(unset);
+ BUG_ON_OPT_ARG(arg);
return refresh(opt->value, 0);
}
static int really_refresh_callback(const struct option *opt,
const char *arg, int unset)
{
+ BUG_ON_OPT_NEG(unset);
+ BUG_ON_OPT_ARG(arg);
return refresh(opt->value, REFRESH_REALLY);
}
@@ -803,6 +807,7 @@ static int chmod_callback(const struct option *opt,
const char *arg, int unset)
{
char *flip = opt->value;
+ BUG_ON_OPT_NEG(unset);
if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
return error("option 'chmod' expects \"+x\" or \"-x\"");
*flip = arg[0];
@@ -812,6 +817,8 @@ static int chmod_callback(const struct option *opt,
static int resolve_undo_clear_callback(const struct option *opt,
const char *arg, int unset)
{
+ BUG_ON_OPT_NEG(unset);
+ BUG_ON_OPT_ARG(arg);
resolve_undo_clear();
return 0;
}
@@ -847,6 +854,8 @@ static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
unsigned int mode;
const char *path;
+ BUG_ON_OPT_NEG(unset);
+
if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) {
if (add_cacheinfo(mode, &oid, path, 0))
die("git update-index: --cacheinfo cannot add %s", path);
@@ -869,6 +878,8 @@ static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
{
int *nul_term_line = opt->value;
+ BUG_ON_OPT_NEG(unset);
+
if (ctx->argc != 1)
return error("option '%s' must be the last argument", opt->long_name);
allow_add = allow_replace = allow_remove = 1;
@@ -881,6 +892,8 @@ static int stdin_callback(struct parse_opt_ctx_t *ctx,
{
int *read_from_stdin = opt->value;
+ BUG_ON_OPT_NEG(unset);
+
if (ctx->argc != 1)
return error("option '%s' must be the last argument", opt->long_name);
*read_from_stdin = 1;
@@ -888,11 +901,13 @@ static int stdin_callback(struct parse_opt_ctx_t *ctx,
}
static int unresolve_callback(struct parse_opt_ctx_t *ctx,
- const struct option *opt, int flags)
+ const struct option *opt, int unset)
{
int *has_errors = opt->value;
const char *prefix = startup_info->prefix;
+ BUG_ON_OPT_NEG(unset);
+
/* consume remaining arguments. */
*has_errors = do_unresolve(ctx->argc, ctx->argv,
prefix, prefix ? strlen(prefix) : 0);
@@ -905,11 +920,13 @@ static int unresolve_callback(struct parse_opt_ctx_t *ctx,
}
static int reupdate_callback(struct parse_opt_ctx_t *ctx,
- const struct option *opt, int flags)
+ const struct option *opt, int unset)
{
int *has_errors = opt->value;
const char *prefix = startup_info->prefix;
+ BUG_ON_OPT_NEG(unset);
+
/* consume remaining arguments. */
setup_work_tree();
*has_errors = do_reupdate(ctx->argc, ctx->argv,