diff --git a/archive.c b/archive.c index 59790be98697c6..8cef206915e468 100644 --- a/archive.c +++ b/archive.c @@ -562,7 +562,7 @@ static void extra_file_info_clear(void *util, const char *str UNUSED) static int add_file_cb(const struct option *opt, const char *arg, int unset) { struct archiver_args *args = opt->value; - const char **basep = (const char **)opt->defval; + const char **basep = (const char **)DEFVAL_TO_PTR(opt->defval); const char *base = *basep; char *path; struct string_list_item *item; @@ -657,7 +657,7 @@ static int parse_archive_args(int argc, const char **argv, .argh = N_("file"), .help = N_("add untracked file to archive"), .callback = add_file_cb, - .defval = (intptr_t) &base, + .defval = DEFVAL_PTR(&base), }, { .type = OPTION_CALLBACK, @@ -666,7 +666,7 @@ static int parse_archive_args(int argc, const char **argv, .argh = N_("path:content"), .help = N_("add untracked file to archive"), .callback = add_file_cb, - .defval = (intptr_t) &base, + .defval = DEFVAL_PTR(&base), }, OPT_STRING('o', "output", &output, N_("file"), N_("write the archive to this file")), diff --git a/builtin/am.c b/builtin/am.c index e9623b8307793f..ab446761de958a 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -2294,7 +2294,7 @@ static int parse_opt_show_current_patch(const struct option *opt, const char *ar BUG_ON_OPT_NEG(unset); if (!arg) - *opt_value = opt->defval; + *opt_value = DEFVAL_TO_INT(opt->defval); else if (!strcmp(arg, "raw")) *opt_value = RESUME_SHOW_PATCH_RAW; else if (!strcmp(arg, "diff")) @@ -2413,7 +2413,7 @@ int cmd_am(int argc, .help = N_("show the patch being applied"), .flags = PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, .callback = parse_opt_show_current_patch, - .defval = RESUME_SHOW_PATCH_RAW, + .defval = DEFVAL_INT(RESUME_SHOW_PATCH_RAW), }, OPT_CMDMODE(0, "retry", &resume_mode, N_("try to apply current patch again"), @@ -2435,7 +2435,7 @@ int cmd_am(int argc, .argh = N_("key-id"), .help = N_("GPG-sign commits"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "", + .defval = DEFVAL_PTR(""), }, OPT_CALLBACK_F(0, "empty", &state.empty_type, "(stop|drop|keep)", N_("how to handle empty patches"), diff --git a/builtin/clone.c b/builtin/clone.c index d60d1b60bc238c..0f2f8c37dfe7b2 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -89,7 +89,7 @@ static int recurse_submodules_cb(const struct option *opt, string_list_append((struct string_list *)opt->value, arg); else string_list_append((struct string_list *)opt->value, - (const char *)opt->defval); + (const char *)DEFVAL_TO_PTR(opt->defval)); return 0; } @@ -946,7 +946,7 @@ int cmd_clone(int argc, .help = N_("initialize submodules in the clone"), .flags = PARSE_OPT_OPTARG, .callback = recurse_submodules_cb, - .defval = (intptr_t)".", + .defval = DEFVAL_PTR("."), }, OPT_ALIAS(0, "recursive", "recurse-submodules"), OPT_INTEGER('j', "jobs", &max_jobs, diff --git a/builtin/commit-tree.c b/builtin/commit-tree.c index 30535db131eaa6..9267ccf5ae4bea 100644 --- a/builtin/commit-tree.c +++ b/builtin/commit-tree.c @@ -120,7 +120,7 @@ int cmd_commit_tree(int argc, .argh = N_("key-id"), .help = N_("GPG sign commit"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "", + .defval = DEFVAL_PTR(""), }, OPT_END() }; diff --git a/builtin/commit.c b/builtin/commit.c index 28f61745034506..20d7cfb6585ba0 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1571,7 +1571,7 @@ struct repository *repo UNUSED) .argh = N_("mode"), .help = N_("show untracked files, optional modes: all, normal, no. (Default: all)"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t)"all", + .defval = DEFVAL_PTR("all"), }, { .type = OPTION_STRING, @@ -1580,7 +1580,7 @@ struct repository *repo UNUSED) .argh = N_("mode"), .help = N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t)"traditional", + .defval = DEFVAL_PTR("traditional"), }, { .type = OPTION_STRING, @@ -1589,7 +1589,7 @@ struct repository *repo UNUSED) .argh = N_("when"), .help = N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t)"all", + .defval = DEFVAL_PTR("all"), }, OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")), OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")), @@ -1735,7 +1735,7 @@ int cmd_commit(int argc, .argh = N_("key-id"), .help = N_("GPG sign commit"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "", + .defval = DEFVAL_PTR(""), }, /* end commit message options */ @@ -1771,7 +1771,7 @@ int cmd_commit(int argc, .argh = N_("mode"), .help = N_("show untracked files, optional modes: all, normal, no. (Default: all)"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t)"all", + .defval = DEFVAL_PTR("all"), }, OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), diff --git a/builtin/config.c b/builtin/config.c index 8d8ec0beead220..ce0ab28af8739f 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -145,7 +145,7 @@ struct config_display_options { .help = (h), \ .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, \ .callback = option_parse_type, \ - .defval = (i), \ + .defval = DEFVAL_INT(i), \ } static int option_parse_type(const struct option *opt, const char *arg, @@ -162,7 +162,7 @@ static int option_parse_type(const struct option *opt, const char *arg, * To support '--' style flags, begin with new_type equal to * opt->defval. */ - new_type = opt->defval; + new_type = DEFVAL_TO_INT(opt->defval); if (!new_type) { if (!strcmp(arg, "bool")) new_type = TYPE_BOOL; diff --git a/builtin/describe.c b/builtin/describe.c index a94dad998cddd7..e5f32b43e9a614 100644 --- a/builtin/describe.c +++ b/builtin/describe.c @@ -671,7 +671,7 @@ int cmd_describe(int argc, .argh = N_("mark"), .help = N_("append on dirty working tree (default: \"-dirty\")"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "-dirty", + .defval = DEFVAL_PTR("-dirty"), }, { .type = OPTION_STRING, @@ -680,7 +680,7 @@ int cmd_describe(int argc, .argh = N_("mark"), .help = N_("append on broken working tree (default: \"-broken\")"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "-broken", + .defval = DEFVAL_PTR("-broken"), }, OPT_END(), }; diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c index cf4273a52c2bae..81604b1602ecd5 100644 --- a/builtin/fmt-merge-msg.c +++ b/builtin/fmt-merge-msg.c @@ -28,7 +28,7 @@ int cmd_fmt_merge_msg(int argc, .argh = N_("n"), .help = N_("populate log with at most entries from shortlog"), .flags = PARSE_OPT_OPTARG, - .defval = DEFAULT_MERGE_LOG_LEN, + .defval = DEFVAL_INT(DEFAULT_MERGE_LOG_LEN), }, { .type = OPTION_INTEGER, @@ -38,7 +38,7 @@ int cmd_fmt_merge_msg(int argc, .argh = N_("n"), .help = N_("alias for --log (deprecated)"), .flags = PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, - .defval = DEFAULT_MERGE_LOG_LEN, + .defval = DEFVAL_INT(DEFAULT_MERGE_LOG_LEN), }, OPT_STRING('m', "message", &message, N_("text"), N_("use as start of message")), diff --git a/builtin/gc.c b/builtin/gc.c index c26c93ee0fe4a3..1fd4dda0c16989 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -869,7 +869,7 @@ int cmd_gc(int argc, .argh = N_("date"), .help = N_("prune unreferenced objects"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t)prune_expire_arg, + .defval = DEFVAL_PTR(prune_expire_arg), }, OPT_BOOL(0, "cruft", &cfg.cruft_packs, N_("pack unreferenced objects separately")), OPT_UNSIGNED(0, "max-cruft-size", &cfg.max_cruft_size, diff --git a/builtin/grep.c b/builtin/grep.c index 26b85479ca0d76..48b0015de52049 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -1167,7 +1167,7 @@ int cmd_grep(int argc, .argh = N_("pager"), .help = N_("show matching files in the pager"), .flags = PARSE_OPT_OPTARG | PARSE_OPT_NOCOMPLETE, - .defval = (intptr_t)default_pager, + .defval = DEFVAL_PTR(default_pager), }, OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored, N_("allow calling of grep(1) (ignored by this build)"), diff --git a/builtin/merge.c b/builtin/merge.c index 5b46a596f0bdf4..43a43bad22a53b 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -276,7 +276,7 @@ static struct option builtin_merge_options[] = { .argh = N_("n"), .help = N_("add (at most ) entries from shortlog to merge commit message"), .flags = PARSE_OPT_OPTARG, - .defval = DEFAULT_MERGE_LOG_LEN, + .defval = DEFVAL_INT(DEFAULT_MERGE_LOG_LEN), }, OPT_BOOL(0, "squash", &squash, N_("create a single commit instead of doing a merge")), @@ -329,7 +329,7 @@ static struct option builtin_merge_options[] = { .argh = N_("key-id"), .help = N_("GPG sign commit"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "", + .defval = DEFVAL_PTR(""), }, OPT_AUTOSTASH(&autostash), OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), diff --git a/builtin/rebase.c b/builtin/rebase.c index fa4f5d9306b856..68d2a6fff1ed69 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1139,7 +1139,7 @@ int cmd_rebase(int argc, .precision = sizeof(options.flags), .help = N_("do not show diffstat of what changed upstream"), .flags = PARSE_OPT_NOARG, - .defval = REBASE_DIFFSTAT, + .defval = DEFVAL_INT(REBASE_DIFFSTAT), }, OPT_STRVEC(0, "trailer", &options.trailer_args, N_("trailer"), N_("add custom trailer(s)")), @@ -1216,7 +1216,7 @@ int cmd_rebase(int argc, .argh = N_("key-id"), .help = N_("GPG-sign commits"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "", + .defval = DEFVAL_PTR(""), }, OPT_AUTOSTASH(&options.autostash), OPT_STRING_LIST('x', "exec", &options.exec, N_("exec"), diff --git a/builtin/revert.c b/builtin/revert.c index bedc40f368eccc..6dc24139a4fe37 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -141,7 +141,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix, .argh = N_("key-id"), .help = N_("GPG sign commit"), .flags = PARSE_OPT_OPTARG, - .defval = (intptr_t) "", + .defval = DEFVAL_PTR(""), }, OPT_END() }; diff --git a/builtin/show-branch.c b/builtin/show-branch.c index f02831b08500c4..77f0673223ebe1 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -680,7 +680,7 @@ int cmd_show_branch(int ac, .argh = N_("n"), .help = N_("show more commits after the common ancestor"), .flags = PARSE_OPT_OPTARG, - .defval = 1, + .defval = DEFVAL_INT(1), }, OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1), OPT_BOOL(0, "no-name", &no_name, N_("suppress naming strings")), diff --git a/builtin/tag.c b/builtin/tag.c index 06c125b53c88e8..cb5c85dfb2e2f9 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -488,7 +488,7 @@ int cmd_tag(int argc, .argh = N_("n"), .help = N_("print lines of each tag message"), .flags = PARSE_OPT_OPTARG, - .defval = 1, + .defval = DEFVAL_INT(1), }, OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'), OPT_CMDMODE('v', "verify", &cmdmode, N_("verify tags"), 'v'), @@ -528,7 +528,7 @@ int cmd_tag(int argc, .help = N_("print only tags of the object"), .flags = PARSE_OPT_LASTARG_DEFAULT, .callback = parse_opt_object_name, - .defval = (intptr_t) "HEAD", + .defval = DEFVAL_PTR("HEAD"), }, OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), diff --git a/builtin/update-index.c b/builtin/update-index.c index 3d6646c318b98e..f432ac03d64fc9 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -978,7 +978,7 @@ int cmd_update_index(int argc, .precision = sizeof(mark_valid_only), .help = N_("mark files as \"not changing\""), .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = MARK_FLAG, + .defval = DEFVAL_INT(MARK_FLAG), }, { .type = OPTION_SET_INT, @@ -987,7 +987,7 @@ int cmd_update_index(int argc, .precision = sizeof(mark_valid_only), .help = N_("clear assumed-unchanged bit"), .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = UNMARK_FLAG, + .defval = DEFVAL_INT(UNMARK_FLAG), }, { .type = OPTION_SET_INT, @@ -996,7 +996,7 @@ int cmd_update_index(int argc, .precision = sizeof(mark_skip_worktree_only), .help = N_("mark files as \"index-only\""), .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = MARK_FLAG, + .defval = DEFVAL_INT(MARK_FLAG), }, { .type = OPTION_SET_INT, @@ -1005,7 +1005,7 @@ int cmd_update_index(int argc, .precision = sizeof(mark_skip_worktree_only), .help = N_("clear skip-worktree bit"), .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = UNMARK_FLAG, + .defval = DEFVAL_INT(UNMARK_FLAG), }, OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries, N_("do not touch index-only entries")), @@ -1080,7 +1080,7 @@ int cmd_update_index(int argc, .precision = sizeof(mark_fsmonitor_only), .help = N_("mark files as fsmonitor valid"), .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = MARK_FLAG, + .defval = DEFVAL_INT(MARK_FLAG), }, { .type = OPTION_SET_INT, @@ -1089,7 +1089,7 @@ int cmd_update_index(int argc, .precision = sizeof(mark_fsmonitor_only), .help = N_("clear fsmonitor valid bit"), .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = UNMARK_FLAG, + .defval = DEFVAL_INT(UNMARK_FLAG), }, OPT_END() }; diff --git a/builtin/write-tree.c b/builtin/write-tree.c index e3bd1a40dbf389..c2a5520128bbc6 100644 --- a/builtin/write-tree.c +++ b/builtin/write-tree.c @@ -39,7 +39,7 @@ int cmd_write_tree(int argc, .precision = sizeof(flags), .help = N_("only useful for debugging"), .flags = PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, - .defval = WRITE_TREE_IGNORE_CACHE_TREE, + .defval = DEFVAL_INT(WRITE_TREE_IGNORE_CACHE_TREE), }, OPT_END() }; diff --git a/compat/obstack.c b/compat/obstack.c index 27cd5c1ea1f9b3..8cf113900c195b 100644 --- a/compat/obstack.c +++ b/compat/obstack.c @@ -142,6 +142,7 @@ _obstack_begin (struct obstack *h, if (alignment == 0) alignment = DEFAULT_ALIGNMENT; +#ifndef __FILC__ if (size == 0) /* Default size is what GNU malloc can fit in a 4096-byte block. */ { @@ -158,11 +159,18 @@ _obstack_begin (struct obstack *h, & ~(DEFAULT_ROUNDING - 1)); size = 4096 - extra; } +#endif h->chunkfun.plain = chunkfun; h->freefun.plain = freefun; +#ifdef __FILC__ + h->alignment_mask = alignment - 1; + size = sizeof (struct _obstack_chunk) + h->alignment_mask; + h->chunk_size = size; +#else h->chunk_size = size; h->alignment_mask = alignment - 1; +#endif h->use_extra_arg = 0; chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); @@ -189,6 +197,7 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment, if (alignment == 0) alignment = DEFAULT_ALIGNMENT; +#ifndef __FILC__ if (size == 0) /* Default size is what GNU malloc can fit in a 4096-byte block. */ { @@ -205,12 +214,19 @@ _obstack_begin_1 (struct obstack *h, int size, int alignment, & ~(DEFAULT_ROUNDING - 1)); size = 4096 - extra; } +#endif h->chunkfun.extra = (struct _obstack_chunk * (*)(void *,long)) chunkfun; h->freefun.extra = (void (*) (void *, struct _obstack_chunk *)) freefun; +#ifdef __FILC__ + h->alignment_mask = alignment - 1; + size = sizeof (struct _obstack_chunk) + h->alignment_mask; + h->chunk_size = size; +#else h->chunk_size = size; h->alignment_mask = alignment - 1; +#endif h->extra_arg = arg; h->use_extra_arg = 1; @@ -246,9 +262,17 @@ _obstack_newchunk (struct obstack *h, int length) char *object_base; /* Compute size for new chunk. */ +#ifdef __FILC__ + if (obj_size) + new_size = 2 * (obj_size + length); + else + new_size = length; + new_size += sizeof (struct _obstack_chunk) + h->alignment_mask; +#else new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100; if (new_size < h->chunk_size) new_size = h->chunk_size; +#endif /* Allocate and initialize the new chunk. */ new_chunk = CALL_CHUNKFUN (h, new_size); diff --git a/compat/obstack.h b/compat/obstack.h index f90a46d9b956ce..4b6f83d320fdee 100644 --- a/compat/obstack.h +++ b/compat/obstack.h @@ -352,7 +352,16 @@ __extension__ \ __o1->next_free += sizeof (int); \ (void) 0; }) -# define obstack_blank(OBSTACK,length) \ +# ifdef __FILC__ +# define obstack_blank(OBSTACK,length) \ +__extension__ \ +({ struct obstack *__o = (OBSTACK); \ + int __len = (length); \ + _obstack_newchunk (__o, __len); \ + obstack_blank_fast (__o, __len); \ + (void) 0; }) +# else +# define obstack_blank(OBSTACK,length) \ __extension__ \ ({ struct obstack *__o = (OBSTACK); \ int __len = (length); \ @@ -360,6 +369,7 @@ __extension__ \ _obstack_newchunk (__o, __len); \ obstack_blank_fast (__o, __len); \ (void) 0; }) +# endif # define obstack_alloc(OBSTACK,length) \ __extension__ \ @@ -465,11 +475,18 @@ __extension__ \ # define obstack_int_grow_fast(h,aint) \ (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint)) -# define obstack_blank(h,length) \ +# ifdef __FILC__ +# define obstack_blank(h,length) \ +( (h)->temp.tempint = (length), \ + _obstack_newchunk ((h), (h)->temp.tempint), \ + obstack_blank_fast (h, (h)->temp.tempint)) +# else +# define obstack_blank(h,length) \ ( (h)->temp.tempint = (length), \ (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \ ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \ obstack_blank_fast (h, (h)->temp.tempint)) +# endif # define obstack_alloc(h,length) \ (obstack_blank ((h), (length)), obstack_finish ((h))) diff --git a/parse-options-cb.c b/parse-options-cb.c index 976cc863851fac..3aac94cefc2f79 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -53,7 +53,7 @@ int parse_opt_color_flag_cb(const struct option *opt, const char *arg, enum git_colorbool value; if (!arg) - arg = unset ? "never" : (const char *)opt->defval; + arg = unset ? "never" : (const char *)DEFVAL_TO_PTR(opt->defval); value = git_config_colorbool(NULL, arg); if (value == GIT_COLOR_UNKNOWN) return error(_("option `%s' expects \"always\", \"auto\", or \"never\""), diff --git a/parse-options.c b/parse-options.c index f4647e0099ea99..7b42e934c36d35 100644 --- a/parse-options.c +++ b/parse-options.c @@ -52,7 +52,7 @@ static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, *arg = p->opt; p->opt = NULL; } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { - *arg = (const char *)opt->defval; + *arg = (const char *)DEFVAL_TO_PTR(opt->defval); } else if (p->argc > 1) { p->argc--; *arg = *++p->argv; @@ -150,9 +150,9 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, { intmax_t value = get_int_value(opt, flags); if (unset) - value &= ~opt->defval; + value &= ~DEFVAL_TO_INT(opt->defval); else - value |= opt->defval; + value |= DEFVAL_TO_INT(opt->defval); return set_int_value(opt, flags, value); } @@ -160,9 +160,9 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, { intmax_t value = get_int_value(opt, flags); if (unset) - value |= opt->defval; + value |= DEFVAL_TO_INT(opt->defval); else - value &= ~opt->defval; + value &= ~DEFVAL_TO_INT(opt->defval); return set_int_value(opt, flags, value); } @@ -172,7 +172,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, if (unset) BUG("BITOP can't have unset form"); value &= ~opt->extra; - value |= opt->defval; + value |= DEFVAL_TO_INT(opt->defval); return set_int_value(opt, flags, value); } @@ -195,13 +195,13 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, } case OPTION_SET_INT: - return set_int_value(opt, flags, unset ? 0 : opt->defval); + return set_int_value(opt, flags, unset ? 0 : DEFVAL_TO_INT(opt->defval)); case OPTION_STRING: if (unset) *(const char **)opt->value = NULL; else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) - *(const char **)opt->value = (const char *)opt->defval; + *(const char **)opt->value = (const char *)DEFVAL_TO_PTR(opt->defval); else return get_arg(p, opt, flags, (const char **)opt->value); return 0; @@ -214,7 +214,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, if (unset) value = NULL; else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) - value = (const char *)opt->defval; + value = (const char *)DEFVAL_TO_PTR(opt->defval); else { int err = get_arg(p, opt, flags, &value); if (err) @@ -266,7 +266,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, if (unset) { value = 0; } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { - value = opt->defval; + value = DEFVAL_TO_INT(opt->defval); } else if (get_arg(p, opt, flags, &arg)) { return -1; } else if (!*arg) { @@ -295,7 +295,7 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p, if (unset) { value = 0; } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { - value = opt->defval; + value = DEFVAL_TO_INT(opt->defval); } else if (get_arg(p, opt, flags, &arg)) { return -1; } else if (!*arg) { @@ -678,7 +678,7 @@ static void parse_options_check(const struct option *opts) case OPTION_NEGBIT: case OPTION_BITOP: case OPTION_COUNTUP: - if (!signed_int_fits(opts->defval, opts->precision)) + if (!signed_int_fits(DEFVAL_TO_INT(opts->defval), opts->precision)) optbug(opts, "has invalid defval"); /* fallthru */ case OPTION_NUMBER: diff --git a/parse-options.h b/parse-options.h index 0d1f738f8d8671..4dd9b16732737d 100644 --- a/parse-options.h +++ b/parse-options.h @@ -77,6 +77,28 @@ typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_ctx_t *ctx, typedef int parse_opt_subcommand_fn(int argc, const char **argv, const char *prefix, struct repository *repo); +/* + * `defval` stores either an integer or a pointer default value. Under + * Fil-C, a pointer must be stored as a real pointer (void *) to preserve + * the capability; casting through an integer would lose it. On non-Fil-C + * builds we use intmax_t so that even on 32-bit systems the full 64-bit + * integer range is available. The macros below abstract the casts so the + * same call sites work in both configurations. + */ +#ifdef __FILC__ +typedef void *defval_type; +#define DEFVAL_INT(x) ((defval_type)(intptr_t)(x)) +#define DEFVAL_PTR(x) ((defval_type)(x)) +#define DEFVAL_TO_INT(x) ((intmax_t)(intptr_t)(x)) +#define DEFVAL_TO_PTR(x) ((void *)(x)) +#else +typedef intmax_t defval_type; +#define DEFVAL_INT(x) ((defval_type)(x)) +#define DEFVAL_PTR(x) ((defval_type)(x)) +#define DEFVAL_TO_INT(x) ((intmax_t)(x)) +#define DEFVAL_TO_PTR(x) ((void *)(intptr_t)(x)) +#endif + /* * `type`:: * holds the type of the option, you must have an OPTION_END last in your @@ -162,7 +184,7 @@ struct option { enum parse_opt_option_flags flags; parse_opt_cb *callback; - intptr_t defval; + defval_type defval; parse_opt_ll_cb *ll_callback; intptr_t extra; parse_opt_subcommand_fn *subcommand_fn; @@ -177,7 +199,7 @@ struct option { .help = (h), \ .flags = PARSE_OPT_NOARG|(f), \ .callback = NULL, \ - .defval = (b), \ + .defval = DEFVAL_INT(b), \ } #define OPT_COUNTUP_F(s, l, v, h, f) { \ .type = OPTION_COUNTUP, \ @@ -196,7 +218,7 @@ struct option { .precision = sizeof(*v), \ .help = (h), \ .flags = PARSE_OPT_NOARG | (f), \ - .defval = (i), \ + .defval = DEFVAL_INT(i), \ } #define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f) #define OPT_CALLBACK_F(s, l, v, a, h, f, cb) { \ @@ -245,7 +267,7 @@ struct option { .precision = sizeof(*v), \ .help = (h), \ .flags = PARSE_OPT_NOARG|PARSE_OPT_NONEG, \ - .defval = (set), \ + .defval = DEFVAL_INT(set), \ .extra = (clear), \ } #define OPT_NEGBIT(s, l, v, h, b) { \ @@ -256,7 +278,7 @@ struct option { .precision = sizeof(*v), \ .help = (h), \ .flags = PARSE_OPT_NOARG, \ - .defval = (b), \ + .defval = DEFVAL_INT(b), \ } #define OPT_COUNTUP(s, l, v, h) OPT_COUNTUP_F(s, l, v, h, 0) #define OPT_SET_INT(s, l, v, h, i) OPT_SET_INT_F(s, l, v, h, i, 0) @@ -269,7 +291,7 @@ struct option { .precision = sizeof(*v), \ .help = (h), \ .flags = PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, \ - .defval = 1, \ + .defval = DEFVAL_INT(1), \ } #define OPT_CMDMODE_F(s, l, v, h, i, f) { \ .type = OPTION_SET_INT, \ @@ -279,7 +301,7 @@ struct option { .precision = sizeof(*v), \ .help = (h), \ .flags = PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG | (f), \ - .defval = (i), \ + .defval = DEFVAL_INT(i), \ } #define OPT_CMDMODE(s, l, v, h, i) OPT_CMDMODE_F(s, l, v, h, i, 0) @@ -356,7 +378,7 @@ struct option { .help = (h), \ .flags = PARSE_OPT_OPTARG, \ .callback = parse_opt_color_flag_cb, \ - .defval = (intptr_t)"always", \ + .defval = DEFVAL_PTR("always"), \ } #define OPT_NOOP_NOARG(s, l) { \ @@ -614,7 +636,7 @@ int parse_opt_tracking_mode(const struct option *, const char *, int); .help = (h), \ .flags = PARSE_OPT_LASTARG_DEFAULT | (f), \ .callback = parse_opt_commits, \ - .defval = (intptr_t) "HEAD", \ + .defval = DEFVAL_PTR("HEAD"), \ } #define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, PARSE_OPT_NONEG) #define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG) diff --git a/ref-filter.h b/ref-filter.h index 120221b47fa30d..e9b687c8fb783f 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -124,7 +124,7 @@ struct ref_format { .help = (h), \ .flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \ .callback = parse_opt_merge_filter, \ - .defval = (intptr_t) "HEAD", \ + .defval = DEFVAL_PTR("HEAD"), \ } #define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h) #define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h) diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c index 68579d83f3939e..e125803b61262d 100644 --- a/t/helper/test-parse-options.c +++ b/t/helper/test-parse-options.c @@ -134,7 +134,7 @@ int cmd__parse_options(int argc, const char **argv) .precision = sizeof(boolean), .help = "be brave", .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, - .defval = 1, + .defval = DEFVAL_INT(1), }, OPT_COUNTUP('b', "boolean", &boolean, "increment by one"), OPT_BIT('4', "or4", &boolean,