From patchwork Fri Oct 1 14:29:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530697 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E005C433F5 for ; Fri, 1 Oct 2021 14:29:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 86DB361A40 for ; Fri, 1 Oct 2021 14:29:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354519AbhJAObI (ORCPT ); Fri, 1 Oct 2021 10:31:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354517AbhJAObH (ORCPT ); Fri, 1 Oct 2021 10:31:07 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3948C061775 for ; Fri, 1 Oct 2021 07:29:22 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id a18so3727230wru.4 for ; Fri, 01 Oct 2021 07:29:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xqa/wFsDXWzZBwSmsUM9J1d/PhaCOTsAhzNHv7RYTzc=; b=JMpX9W4zQLEsth+ZQnV9Uokw4s7sBY8kv+cmibAEJwY/1ARe0Aa9eH47RaQxg5ejFg dfcIuR4DU4NxSxiadFpGbdKVPzGOcVO8I8z2XY8wggx0EKrlp5baPFTKmG/4QOAxlFXM RJtkOADsXoDyRfkbofy0xF0iU+K+Ok9yKwcEyz/2ecJY/6qQOBaun50foQSs8XWrf+cu iHF62zj8nsTE7beb0Hyanxq1YQMB+xWwa21h4POqxDDvGsRsi80NK9AH8OoQ9woSaNxn wFW9LU20+qcAL/WTqxisLrQnwtgbxCEei5op8PpYacbL1nZ5xlFlJKWfQPknCOHKuu7N jBcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xqa/wFsDXWzZBwSmsUM9J1d/PhaCOTsAhzNHv7RYTzc=; b=i3ct36Mb8AHiCnthW3KT7yGsCttL207Lj1DwtfoDHvZSntUc2VXlt8X1q4iPORXG/M XUdtZl9SfoCzVw3H1ublu0aBdb9o+ks3D0IiE6IRjm1LB56torxB3JlOOen7qdrYUltT eFew9wwWRCJuJOtMUuG63Mg4czHIZNCRcn2sv1rNIS7ZMTUlkNNp3ct3fY76B1YtMyG9 P/ny70qaSv2v1NIAaWjzBRqJda4IBTPUwP25p5XvUo/qqEWLh64MZ1n4sWcn2e81G1mz VsH9GoPsyfpri9qYpxpeGiBott+DW52cHD5Y/YLZ9K5zO7yidZujjsfaeyPHBVg9wVDx V5TA== X-Gm-Message-State: AOAM531Eu50oxCdpURtezgn2mIbJ0zwXHnY8+zVYnxIZPaA4l9YAsLSE tLtVJbQmxH4HgUKSMpw7wfYJy3R7PEQWeg== X-Google-Smtp-Source: ABdhPJyOYVJy30QGdxb5HalaP3UxYv7Fq20L3h8uMrJ+MBPLg82Xi+7iclykY7ZRn+Edbv/HE6YlwA== X-Received: by 2002:adf:fe89:: with SMTP id l9mr12796756wrr.0.1633098561349; Fri, 01 Oct 2021 07:29:21 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:20 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 01/11] parse-options.h: move PARSE_OPT_SHELL_EVAL between enums Date: Fri, 1 Oct 2021 16:29:07 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a bad landmine of a bug which has been with us ever since PARSE_OPT_SHELL_EVAL was added in 47e9cd28f8a (parseopt: wrap rev-parse --parseopt usage for eval consumption, 2010-06-12). It's an argument to parse_options() and should therefore be in "enum parse_opt_flags", but it was added to the per-option "enum parse_opt_option_flags" by mistake. Therefore as soon as we'd have an enum member in the former that reached its value of "1 << 8" we'd run into a seemingly bizarre bug where that new option would turn on the unrelated PARSE_OPT_SHELL_EVAL in "git rev-parse --parseopt" by proxy. I manually checked that no other enum members suffered from such overlap, by setting the values to non-overlapping values, and making the relevant codepaths BUG() out if the given value was above/below the expected (excluding flags=0 in the case of "enum parse_opt_flags"). Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse-options.h b/parse-options.h index 39d90882548..3a3176ae65c 100644 --- a/parse-options.h +++ b/parse-options.h @@ -33,6 +33,7 @@ enum parse_opt_flags { PARSE_OPT_KEEP_UNKNOWN = 1 << 3, PARSE_OPT_NO_INTERNAL_HELP = 1 << 4, PARSE_OPT_ONE_SHOT = 1 << 5, + PARSE_OPT_SHELL_EVAL = 1 << 6, }; enum parse_opt_option_flags { @@ -44,7 +45,6 @@ enum parse_opt_option_flags { PARSE_OPT_NODASH = 1 << 5, PARSE_OPT_LITERAL_ARGHELP = 1 << 6, PARSE_OPT_FROM_ALIAS = 1 << 7, - PARSE_OPT_SHELL_EVAL = 1 << 8, PARSE_OPT_NOCOMPLETE = 1 << 9, PARSE_OPT_COMP_ARG = 1 << 10, PARSE_OPT_CMDMODE = 1 << 11, From patchwork Fri Oct 1 14:29:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530699 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CAB46C433F5 for ; Fri, 1 Oct 2021 14:29:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B0A4361A50 for ; Fri, 1 Oct 2021 14:29:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238667AbhJAObK (ORCPT ); Fri, 1 Oct 2021 10:31:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354514AbhJAObI (ORCPT ); Fri, 1 Oct 2021 10:31:08 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11828C061775 for ; Fri, 1 Oct 2021 07:29:24 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id d6so15743559wrc.11 for ; Fri, 01 Oct 2021 07:29:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wnMQ/PeBa9XgyAi15/bLt1q2MQoOQih3HUOYN+cx550=; b=dyHJAbn3Zc/pciVICZjgaGMe/kh5Eq21iHk213uE/R7QKmEEhosxedD6jEir08L7RV A36Jj3HKZfIWkCX3uQr8hz2yQ+Yjieq/7HFGtW4ZEWzmmGFhQMYaXorqhbw/iYebulM2 dekMZiw9vN15s1qJyq6tN0VKX4xbRwft61bg/LtfpzgsQwiYirRz/rgBaZDOoW65IzK3 HUhh2qg2N6JZ2ikK/D7lQ4zpy0FAAaFmxkrsMA8hjOMz5Z4TCB+LmiHTgLN2ZrADyZo1 yM/CZgP+90oSzdZP3MC8EFUCQDYN7mUWQg6Acy/hxY4EfH8iTSplIn7oHMP59vNCbTXI Vm+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wnMQ/PeBa9XgyAi15/bLt1q2MQoOQih3HUOYN+cx550=; b=S8S9G6hoH7gzSB4O89uCcLacwf0MaSzXTET9kIllU8doAiljsoYxbLArTzVjnmThj+ 33f73lXGQknAHbMThfBaTZntNwDwrLJiJnhxFzSOA3ic1WNrEeSoK8Wm76CIxMX/2ncW av8MhYX8fl+Zet6jqQxlEqjueTxDtMnfVbYbA4IPwH4GNlUt+qD47vhF19xyknUOA/DO S1GSjSLj4shQMcsrqBn1wrbybiMGGLohe5w2j1HAKJP4+d5r2gnb7auiVZ7JNX6mkNuf YSbk9myA1e1ftP/yemsbXTdVy7LhJTCqjL8A/2U4TVt5xS2F37F99O90/Oc0MjuLGLDR FOQw== X-Gm-Message-State: AOAM533PvzJJ38xmDXo7K0z0cSO9JasM+2Lt8ooHJ1WmN/YkGqUAPh+2 BNfJiL/eT88nNfGvFgA2GpBLysD/fvG9ZQ== X-Google-Smtp-Source: ABdhPJyg92/ICiy89d/kBI5wmuJ34paA7tJa9px27zeKQK/RgbyPVCUbTit+lKf5dRh01xW3PEPQWQ== X-Received: by 2002:adf:f610:: with SMTP id t16mr13217309wrp.309.1633098562363; Fri, 01 Oct 2021 07:29:22 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:21 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 02/11] parse-options.[ch]: consistently use "enum parse_opt_flags" Date: Fri, 1 Oct 2021 16:29:08 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Use the "enum parse_opt_flags" instead of an "int flags" as arguments to the various functions in parse-options.c. In C enums aren't first-class types, and the "enum parse_opt_option_flag" uses a enum-as-bitfield pattern. So unlike exhaustively enumerated "case" arms we're not going to get validation that we used the "right" enum labels. I.e. this won't catch the sort of bug that was fixed with "PARSE_OPT_SHELL_EVAL" in the preceding commit. But there's still 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 312cff52074 (bloom: split 'get_bloom_filter()' in two, 2020-09-16) and the "permitted" enum added in ce910287e72 (add -p: fix checking of user input, 2020-08-17). 1. https://lore.kernel.org/git/87mtnvvj3c.fsf@evledraar.gmail.com/ 2. https://github.com/sinelaw/elfs-clang-plugins/blob/master/enums_conversion/README.md Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 11 +++++++---- parse-options.h | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/parse-options.c b/parse-options.c index 55c5821b08d..9c8ba963400 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; @@ -506,7 +507,8 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx, void parse_options_start(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) { memset(ctx, 0, sizeof(*ctx)); parse_options_start_1(ctx, argc, argv, prefix, options, flags); @@ -838,8 +840,9 @@ int parse_options_end(struct parse_opt_ctx_t *ctx) } int parse_options(int argc, const char **argv, const char *prefix, - const struct option *options, const char * const usagestr[], - int flags) + const struct option *options, + const char * const usagestr[], + enum parse_opt_flags flags) { struct parse_opt_ctx_t ctx; struct option *real_options; diff --git a/parse-options.h b/parse-options.h index 3a3176ae65c..fb5aafd4f7b 100644 --- a/parse-options.h +++ b/parse-options.h @@ -213,7 +213,8 @@ struct option { */ int parse_options(int argc, const char **argv, const char *prefix, const struct option *options, - const char * const usagestr[], int flags); + const char * const usagestr[], + enum parse_opt_flags flags); NORETURN void usage_with_options(const char * const *usagestr, const struct option *options); @@ -270,7 +271,8 @@ struct parse_opt_ctx_t { void parse_options_start(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); int parse_options_step(struct parse_opt_ctx_t *ctx, const struct option *options, From patchwork Fri Oct 1 14:29:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530701 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF26CC433EF for ; Fri, 1 Oct 2021 14:29:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A90F161A3A for ; Fri, 1 Oct 2021 14:29:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354524AbhJAObM (ORCPT ); Fri, 1 Oct 2021 10:31:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238636AbhJAObJ (ORCPT ); Fri, 1 Oct 2021 10:31:09 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E46C4C061775 for ; Fri, 1 Oct 2021 07:29:24 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id v25so5188085wra.2 for ; Fri, 01 Oct 2021 07:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=C+J4vYeG8G+YnXjiE8eWeDR32ZXMGjjPfpcrpKwVc5w=; b=iHMNOzCcZnmhvDcyqJdeqnEcaO0vNXxdSeDH62QJZZCsF6a0/kX3SC85nVbLxW8AwY F1mzkt8IFAzOFacCihxiZx2FNBIByurUmsClDHbOv2tHOTW54+Dw69wz0nCLa7VnFkPQ 59rJQqp058nAoCwBptjcqNTYALqtUdi1LHMwr+IuPecJIJS19aFm6gi3gHjr6TKkLzq+ jpOMm8kzVUEsAw5cTNv+++uN/IVDeiLNvH/s7oR9VdZ3jKe4/hWG/TE2639kQ9Wnt1Jh bWv/IbE2b2/oeX2KRiQt2oxMHXNzdlqnwCpKW/m/2u4iIIILWvCP74sT8KS/JWKmeWKz fYXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=C+J4vYeG8G+YnXjiE8eWeDR32ZXMGjjPfpcrpKwVc5w=; b=zYHx5xMmGN+fjQ7EKzztPOLoP7lZeVeyjj/7Skcgg82zh4L7MJCx/9e+snH8IMP2Ei FL6SW3jaNRgD0eZ+6VRH/d6sIW4hLl/C6N9Na6d5ksVdOfh1KmNklkt+fp4jrz3RR1ZQ 1o2ZKTzqwPTKU2vO1CQPBQVCbhQ0CkkG1LrQA+NjZGPIT/AV673XJAOgcal+HnpjB0EC IRFhRHTWmQ2yk/HdketD5li4Bv9hKdCx4nqH0sUaaZKJlw8d5xT4AFz7IT+mEfs8AQCF fajL1W39Uzy6R6svfMKXG29EPm5xRcE39iZn7G3/nqcTCQ0/4G9g8WgbeDg7Gkidj3KQ GUOg== X-Gm-Message-State: AOAM530jaBXWdztFjzxcKZSVprB1V0P0uCfeUrY+jqR80VMMf0De2uDx Q9jYS23Y5Uget47fWzujc2jgdGsUnxQ0IQ== X-Google-Smtp-Source: ABdhPJweohQ3uKh7mVQeilKVh36FiZ8MNOruoe66ZcvI+cl39EAY2boq8GBSLyOR0vfXVXtgnmZljg== X-Received: by 2002:adf:b35c:: with SMTP id k28mr12661255wrd.209.1633098563190; Fri, 01 Oct 2021 07:29:23 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:22 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 03/11] parse-options.[ch]: consistently use "enum parse_opt_result" Date: Fri, 1 Oct 2021 16:29:09 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Use the "enum parse_opt_result" instead of an "int flags" as the return value of the applicable functions in parse-options.c. This will help catch future bugs, such as the missing "case" arms in the two existing users of the API in "blame.c" and "shortlog.c". A third caller in 309be813c9b (update-index: migrate to parse-options API, 2010-12-01) was already checking for these. As can be seen when trying to sort through the deluge of warnings produced when compiling this with CC=g++ (mostly unrelated to this change) we're not consistently using "enum parse_opt_result" even now, i.e. we'll return error() and "return 0;". See f41179f16ba (parse-options: avoid magic return codes, 2019-01-27) for a commit which started changing some of that. I'm not doing any more of that exhaustive migration here, and it's probably not worthwhile past the point of being able to check "enum parse_opt_result" in switch(). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/blame.c | 3 +++ builtin/shortlog.c | 3 +++ parse-options.c | 31 +++++++++++++++++-------------- parse-options.h | 15 ++++++++------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 641523ff9af..9273fb222d5 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -917,6 +917,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix) PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); for (;;) { switch (parse_options_step(&ctx, options, blame_opt_usage)) { + case PARSE_OPT_NON_OPTION: + case PARSE_OPT_UNKNOWN: + break; case PARSE_OPT_HELP: case PARSE_OPT_ERROR: exit(129); diff --git a/builtin/shortlog.c b/builtin/shortlog.c index 3e7ab1ca821..e7f7af5de3f 100644 --- a/builtin/shortlog.c +++ b/builtin/shortlog.c @@ -374,6 +374,9 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix) for (;;) { switch (parse_options_step(&ctx, options, shortlog_usage)) { + case PARSE_OPT_NON_OPTION: + case PARSE_OPT_UNKNOWN: + break; case PARSE_OPT_HELP: case PARSE_OPT_ERROR: exit(129); diff --git a/parse-options.c b/parse-options.c index 9c8ba963400..f718242096c 100644 --- a/parse-options.c +++ b/parse-options.c @@ -699,13 +699,14 @@ static void free_preprocessed_options(struct option *options) free(options); } -static int usage_with_options_internal(struct parse_opt_ctx_t *, - const char * const *, - const struct option *, int, int); - -int parse_options_step(struct parse_opt_ctx_t *ctx, - const struct option *options, - const char * const usagestr[]) +static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *, + const char * const *, + const struct option *, + int, int); + +enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, + const struct option *options, + const char * const usagestr[]) { int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); @@ -839,10 +840,11 @@ int parse_options_end(struct parse_opt_ctx_t *ctx) return ctx->cpidx + ctx->argc; } -int parse_options(int argc, const char **argv, const char *prefix, - const struct option *options, - const char * const usagestr[], - enum parse_opt_flags flags) +enum parse_opt_result parse_options(int argc, const char **argv, + const char *prefix, + const struct option *options, + const char * const usagestr[], + enum parse_opt_flags flags) { struct parse_opt_ctx_t ctx; struct option *real_options; @@ -900,9 +902,10 @@ static int usage_argh(const struct option *opts, FILE *outfile) #define USAGE_OPTS_WIDTH 24 #define USAGE_GAP 2 -static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, - const char * const *usagestr, - const struct option *opts, int full, int err) +static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx, + const char * const *usagestr, + const struct option *opts, + int full, int err) { FILE *outfile = err ? stderr : stdout; int need_newline; diff --git a/parse-options.h b/parse-options.h index fb5aafd4f7b..d931300f4d6 100644 --- a/parse-options.h +++ b/parse-options.h @@ -211,10 +211,11 @@ struct option { * untouched and parse_options() returns the number of options * processed. */ -int parse_options(int argc, const char **argv, const char *prefix, - const struct option *options, - const char * const usagestr[], - enum parse_opt_flags flags); +enum parse_opt_result parse_options(int argc, const char **argv, + const char *prefix, + const struct option *options, + const char * const usagestr[], + enum parse_opt_flags flags); NORETURN void usage_with_options(const char * const *usagestr, const struct option *options); @@ -274,9 +275,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx, const struct option *options, enum parse_opt_flags flags); -int parse_options_step(struct parse_opt_ctx_t *ctx, - const struct option *options, - const char * const usagestr[]); +enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, + const struct option *options, + const char * const usagestr[]); int parse_options_end(struct parse_opt_ctx_t *ctx); From patchwork Fri Oct 1 14:29:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530703 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62C20C433FE for ; Fri, 1 Oct 2021 14:29:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4AC5061A50 for ; Fri, 1 Oct 2021 14:29:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354529AbhJAObN (ORCPT ); Fri, 1 Oct 2021 10:31:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238685AbhJAObK (ORCPT ); Fri, 1 Oct 2021 10:31:10 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D800AC06177C for ; Fri, 1 Oct 2021 07:29:25 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id t16-20020a1c7710000000b003049690d882so11377215wmi.5 for ; Fri, 01 Oct 2021 07:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XHUAGWbOwAxV/v3khiE3WXmu57lfY2Ws+KVBwTNVTPg=; b=ZRIrwApH6TGqXI9+tH8opSvpec9UfVV3iZhBGLqgxy1ckKiLdJuFawk4kffT6ahBQC iwEO8DyH7pSD5ValXbo+45CyECtTVx6uccyZwqytcgOL8Do0NyST6QJnbFGP+axuj0xc 3bifElQfb0ajdhl4EBFcZ/AvE44Mf79sueevSKWaMIuAvRPkimNpueWNI/DzZJ+OxyiX qqEJUOj0uGNgc2aiKB+2Uq+PHX0FmQtHhFgbxNAdAVdUzH7Dsa1fQirLqlN1d1bE01+c BXBJ8/04ux6EDSzvUkyTNVoK8i8qStPuSCKNYUMwgvtKy7KjNokzuItEOH+zR+cU2MCC aJzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XHUAGWbOwAxV/v3khiE3WXmu57lfY2Ws+KVBwTNVTPg=; b=NEeQFVCe/uBHWgcf20p8ZWYhtdEoUlW6CcmortInmTveHS/Xlr+U0c7y0Q757KHzXs ptWqYvd18xKSNgbLA+IzMaUHH19IOTFqtrTwItDqqdaOCh64y9L5mCG7dyyuDPnLqaLh rk8KhY3Ogm8Lr1kfDB3ndR9AI0phPGzxD7w0B4nAwxgEmD+yR9WelnYkPnRDls6zZdwO 3lLE9p1QJKV9uKrgOP/BdqraUz3Pyd0NJ21L4lqIJtCX03C51icMrfJ/TXpSVTmgZ8Oi zr3nmYViVPkl6lJS3p2epoo1pXBjROMU/p0u7FlJSp5CKIoYM9BPF1TNa7LC+8ox1IqX +wYQ== X-Gm-Message-State: AOAM531GuvBqzoKAIfVCFpYRd9ZsU5y12+bKGgPaYZJKfE+CrkbHBksh I7Xc5/XK7lv1Rqbq08PwXKWLpg9H9Ffikw== X-Google-Smtp-Source: ABdhPJy8JaPAHtJu+ORTsUlcdbSlJge3eLTNvinFdhQdNSO5kmJm0aGK09VWzrQnrHITXfPt/dO+jQ== X-Received: by 2002:a1c:28b:: with SMTP id 133mr4810476wmc.15.1633098564121; Fri, 01 Oct 2021 07:29:24 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:23 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 04/11] parse-options.c: use exhaustive "case" arms for "enum parse_opt_result" Date: Fri, 1 Oct 2021 16:29:10 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the "default" case in parse_options() that handles the return value of parse_options_step() to simply have a "case" arm for PARSE_OPT_UNKNOWN, instead of leaving it to a comment. This means the compiler can warn us about any missing case arms. This adjusts code added in ff43ec3e2d2 (parse-opt: create parse_options_step., 2008-06-23), given its age it may pre-date the existence (or widespread use) of this coding style, which we've since adopted more widely. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse-options.c b/parse-options.c index f718242096c..e33700d6e71 100644 --- a/parse-options.c +++ b/parse-options.c @@ -866,7 +866,7 @@ enum parse_opt_result parse_options(int argc, const char **argv, case PARSE_OPT_NON_OPTION: case PARSE_OPT_DONE: break; - default: /* PARSE_OPT_UNKNOWN */ + case PARSE_OPT_UNKNOWN: if (ctx.argv[0][1] == '-') { error(_("unknown option `%s'"), ctx.argv[0] + 2); } else if (isascii(*ctx.opt)) { From patchwork Fri Oct 1 14:29:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530705 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A92CC4332F for ; Fri, 1 Oct 2021 14:29:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 35B7461A3A for ; Fri, 1 Oct 2021 14:29:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354527AbhJAObO (ORCPT ); Fri, 1 Oct 2021 10:31:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354523AbhJAObL (ORCPT ); Fri, 1 Oct 2021 10:31:11 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4472C06177E for ; Fri, 1 Oct 2021 07:29:26 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id l18-20020a05600c4f1200b002f8cf606262so11404626wmq.1 for ; Fri, 01 Oct 2021 07:29:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iu6PdNVjwT2fgh+AgFM/fZ1uWdEaqpsZmyTf+3G3ZgM=; b=kVurZfwEs+d5mBYOyz0i9BtLKb5khLBF+O7W1iXwzCwzhO2qzjHY/5GXCUoDhwxNrK Z0iBV5ei/4JDkDUQY+T15PxasPeoykyj3JIFLBYTfZCpNnOrlyHP9YSsrKqKQ3VQTPPn NWTGoflgQPR7nAC7bBqKHzrQ5VTR8wUcQnaHv1MrGbk9//6VY+f2JOpLgys2Yu441J9g 5Lvz66RuCKgUDoYnWNMCIgSJ/jYizvcZKFWkc8mFD0UNwi0TccJQA7bNud+lysng+yNh ClJmkyALMEPifRX31MQ+9QYMfNVljQ7Vxuxp3DWSpAYNp9CaRD7EYt5QRXDDRHqRDZ5D EEgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iu6PdNVjwT2fgh+AgFM/fZ1uWdEaqpsZmyTf+3G3ZgM=; b=vBsVUbOvF8SkEEpe2+ZrrkReVxJox0iAnriBAWdF/J7kiXX45BlIq5JamhgkVmgGIC 3GnI2HaMEsVY2vnFTe395nubV47zVwed+VPcCdOuKPYtabzfMYG/w2a3VUix22kw8nMy NlyUt4ZdtxPfM0AMLB+rH9YBnH29X+z2O0+Dkpdj5xSsV2kqmKU7hgd3bagZl4IKOqjB iMNjFbHnRWYu7vaNtavkOxH8NAyaAIgSStedXwtjGT6q4eC5ooAxhOgGl1oDFb98q8EB LU1VsX/LzHQUbgVvXr2UxwWqoB5k2jkzA83xCY65L1UUWgnGsgwG+MnQGOaAFqQyt+7g L4ug== X-Gm-Message-State: AOAM532R/xMWAt2At337eHUjaTGUPtSPAYqnsy7ahHqcJv42POQw3S+B 43yUQBYb4fgIBm16Uq3VsxUGuaIqLhVQtw== X-Google-Smtp-Source: ABdhPJy4mXZkS8al9FTyzJrit1VOBVSEBEDFnVl8gbXoURmguUE/U3CP8NWLn6eI5+l8hIf1nhcf1g== X-Received: by 2002:a05:600c:3585:: with SMTP id p5mr5029342wmq.110.1633098565128; Fri, 01 Oct 2021 07:29:25 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:24 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 05/11] parse-options.c: use exhaustive "case" arms for "enum parse_opt_type" Date: Fri, 1 Oct 2021 16:29:11 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code in get_value(), parse_options_check() etc. to do away with the "default" case in favor of exhaustively checking the relevant fields. The added "return -1" is needed for the GCC version commented on inline, my local clang 11.0.1-2 does not require it. Let's add it for now to appease GCC. The added "special types" etc. comments correspond to the relevant comments and ordering on the "enum parse_opt_type". Let's try to keep the same order and commentary as there where possible for clarity. This doesn't reach that end-state, and due to the different handling of options it's probably not worth it to get there, but let's match its ordering where it's easy to do so. There was a discussion about whether this was worth the added verbosity, as argued in[1] I think it's worth it for getting compile-time checking when adding new option types. We *should* have tests for some of these, but e.g. in the show_gitcomp() case one might run through the whole test suite and only hit a missing case at the end on the completion tests. This technically changes the handling of OPTION_END, but it's obviously the right thing to do. We're calling this code from within a loop that uses OPTION_END as a break condition, so it was never caught by the "default" case. So let's make encountering OPTION_END a BUG(), just like it already is in the get_value() handling added in 4a59fd13122 (Add a simple option parser., 2007-10-15). 1. https://lore.kernel.org/git/87tui3vk8y.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- parse-options.h | 2 +- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/parse-options.c b/parse-options.c index e33700d6e71..dedd40efec5 100644 --- a/parse-options.c +++ b/parse-options.c @@ -219,9 +219,14 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, optname(opt, flags)); return 0; - default: + /* special types */ + case OPTION_END: + case OPTION_GROUP: + case OPTION_NUMBER: + case OPTION_ALIAS: BUG("opt->type %d should not happen", opt->type); } + return -1; /* gcc 10.2.1-6's -Werror=return-type */ } static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p, @@ -443,6 +448,9 @@ static void parse_options_check(const struct option *opts) err |= optbug(opts, "uses feature " "not supported for dashless options"); switch (opts->type) { + case OPTION_END: + BUG("unreachable"); + case OPTION_COUNTUP: case OPTION_BIT: case OPTION_NEGBIT: @@ -468,8 +476,14 @@ static void parse_options_check(const struct option *opts) BUG("OPT_ALIAS() should not remain at this point. " "Are you using parse_options_step() directly?\n" "That case is not supported yet."); - default: - ; /* ok. (usually accepts an argument) */ + + case OPTION_BITOP: + case OPTION_FILENAME: + case OPTION_GROUP: + case OPTION_INTEGER: + case OPTION_MAGNITUDE: + case OPTION_STRING: + break; } if (opts->argh && strcspn(opts->argh, " _") != strlen(opts->argh)) @@ -532,6 +546,9 @@ static void show_negated_gitcomp(const struct option *opts, int show_all, continue; switch (opts->type) { + case OPTION_END: + BUG("unreachable"); + case OPTION_STRING: case OPTION_FILENAME: case OPTION_INTEGER: @@ -543,7 +560,14 @@ static void show_negated_gitcomp(const struct option *opts, int show_all, case OPTION_SET_INT: has_unset_form = 1; break; - default: + /* special types */ + case OPTION_GROUP: + case OPTION_NUMBER: + case OPTION_ALIAS: + /* options with no arguments */ + case OPTION_BITOP: + /* options with arguments (usually) */ + case OPTION_LOWLEVEL_CALLBACK: break; } if (!has_unset_form) @@ -578,6 +602,8 @@ static int show_gitcomp(const struct option *opts, int show_all) continue; switch (opts->type) { + case OPTION_END: + BUG("unreachable"); case OPTION_GROUP: continue; case OPTION_STRING: @@ -593,7 +619,19 @@ static int show_gitcomp(const struct option *opts, int show_all) break; suffix = "="; break; - default: + /* special types */ + case OPTION_NUMBER: + case OPTION_ALIAS: + + /* options with no arguments */ + case OPTION_BIT: + case OPTION_NEGBIT: + case OPTION_BITOP: + case OPTION_COUNTUP: + case OPTION_SET_INT: + + /* options with arguments (usually) */ + case OPTION_LOWLEVEL_CALLBACK: break; } if (opts->flags & PARSE_OPT_COMP_ARG) diff --git a/parse-options.h b/parse-options.h index d931300f4d6..a1c7c86ad30 100644 --- a/parse-options.h +++ b/parse-options.h @@ -264,7 +264,7 @@ struct parse_opt_ctx_t { const char **out; int argc, cpidx, total; const char *opt; - int flags; + enum parse_opt_flags flags; const char *prefix; const char **alias_groups; /* must be in groups of 3 elements! */ struct option *updated_options; From patchwork Fri Oct 1 14:29:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51CE7C433EF for ; Fri, 1 Oct 2021 14:29:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3AB1661A40 for ; Fri, 1 Oct 2021 14:29:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354536AbhJAObP (ORCPT ); Fri, 1 Oct 2021 10:31:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238622AbhJAObM (ORCPT ); Fri, 1 Oct 2021 10:31:12 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D127AC0613E2 for ; Fri, 1 Oct 2021 07:29:27 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id s24so7374924wmh.4 for ; Fri, 01 Oct 2021 07:29:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q2y8zrfRtDHg8OX+AT91kIksW2WfT5rFXf6dNBiaze8=; b=DbU6LGwDouGAxH6R0+TyRwPHjvwEpls6kpdaEguQZM3DWWeKUTTtCKMAx7ANKTd1jH G9aUUO/W7vm9M1FM3VWyZdsIcxo3iB6gvcr3/ggKWmCKfv660BzjMHcIbo6hzdRv6spc fwlQw6ZWd+89VfOOOBTJxwY+dkyc7qZ1/SmuVfO32fWi6Z2sjlBO3vblI0ipZ1kPmX6s lVKMEMRoqxSy4TIh7ncnUF8lMROSJlulSMEHhI98sPZKHGPAMkfp8nCfmUm1ScGiDH7Y p55csq2wUJoZMQO/aI4lGhiImaogvtWEAcY28C4UbG95trPqQQcS45ezh7W9VOOaW4Nf KEKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q2y8zrfRtDHg8OX+AT91kIksW2WfT5rFXf6dNBiaze8=; b=DzGklaH5FTAnn2V7kVVbt4pffHTP20JIOCljPPXEoDdqfA6edbdjmDigWZlQmREz2l FGgcGWFEvJ9Jhu0LbRqUr1HGdSV/vzEPhVgs0fPRDYo4ij9yVutZH61dUgXhZiLjDwqs c1PR9rdXRNqW1hzXQSws6mfMA8ql6upGR75GBjVJEmOzp60CTDpwqHn0d1qkEltntCXp 8DKPHLilrC4+KLltd2q1qtpWjjZa5gRAHCk0HroA9tGP/MjrO9e2nEJRAFA/K8uHUjUZ yty+6/gEHlCTfuKoC1zouj036xav4CV06dyGFtIvY2wOR+9QY6rutlSjJlUrF2nDH1Mn SaHg== X-Gm-Message-State: AOAM530zXOPevu9iMOmBjUNZKYlE6IhyHq1Du+fnrHVL28dcWOh6fxXj rKO+ND86mWwtXZCEW7nBzsXqqZ6JzxAR1A== X-Google-Smtp-Source: ABdhPJzyGhrIgb0RZ+pa/DbhdalafPogVaWWyoAZkl+gDT5k4gMypr9rfbGz8QFZmg6IjNyG0zyaRA== X-Received: by 2002:a7b:c303:: with SMTP id k3mr4863029wmj.44.1633098566094; Fri, 01 Oct 2021 07:29:26 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:25 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 06/11] parse-options.h: make the "flags" in "struct option" an enum Date: Fri, 1 Oct 2021 16:29:12 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the "flags" members of "struct option" to refer to their corresponding "enum" defined earlier in the file. The benefit of changing this to an enum isn't as great as with some "enum parse_opt_type" as we'll always check this as a bitfield, so we can't rely on the compiler checking "case" arms for us. But let's do it for consistency with the rest of the file. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse-options.h b/parse-options.h index a1c7c86ad30..74b66ba6e93 100644 --- a/parse-options.h +++ b/parse-options.h @@ -134,7 +134,7 @@ struct option { const char *argh; const char *help; - int flags; + enum parse_opt_option_flags flags; parse_opt_cb *callback; intptr_t defval; parse_opt_ll_cb *ll_callback; From patchwork Fri Oct 1 14:29:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530709 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82714C433FE for ; Fri, 1 Oct 2021 14:29:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C0AC61A3A for ; Fri, 1 Oct 2021 14:29:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354541AbhJAObQ (ORCPT ); Fri, 1 Oct 2021 10:31:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238636AbhJAObN (ORCPT ); Fri, 1 Oct 2021 10:31:13 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B26ECC06177D for ; Fri, 1 Oct 2021 07:29:28 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id h15so14186066wrc.3 for ; Fri, 01 Oct 2021 07:29:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4kbRbk6/As74d17uhCKxB2R7ExVlYE90YOoON1Fp7VU=; b=abNgnSJzOg7UMkKqWgJECRecr+z5MFY8VA+oAM+jtnFZ/GhncJe5MaOEB9WIu4F5a+ nXli9ISb4CqCIxwG1seM8BWhvsItCFrcvTI9dkRb/oE8QuKJioa+Hy4AmswEZOQTV4g9 ah8lMwhCLRfGMBStvfrPwVJQmTD20xrqlZvsQPGr70a3Q8HfH3Bzlm+VGcEfdfEmRiKQ udsyAcSVrBSeBPQlnVf1Dk9a2XmC31nisCIv8ESxy57uHQVenCn/gIlekkYKG+Udy9yY Z+5WwZzqb7wgduQ4Xa6MuQIkag0Cddcy2HWcg0uci9LpMnhkYKndk5h5zZ3BbXAzhLlH 0lIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4kbRbk6/As74d17uhCKxB2R7ExVlYE90YOoON1Fp7VU=; b=5PbTB5a/JV6wi+OCfHSPoQy4lgz3X3n6+hFidVURWe4zWtz7KeWJ65RZosfLY22aLb FcpZmzHbszaJIg5OOliEwKJYoqeopIfOVMwdVRwy6BxRgceWYeVt4S3td9pW0MKYW6Sd fgsBp71I5aLNmSXf2lXbAmaBiSgWmeVT6oJv2OI1RD/OPIdd7CD8F3WyOmkeihB9OS6f 9ttpEKUUzvAwflHurxcIrg/2oFxYVsXfj4j3+sNg6NVwACHrlzV2D686gprCwS52xv4g Hks4pC+bFkKz+c4+STdBx+2VEq7g6mhalnBY6fsmaTzH/ypGzmwPyEN/Qsmf8vUKB3WZ +mzg== X-Gm-Message-State: AOAM530zOzXFVik+QvaPgEz3Ees1rhN4hO7lPX89TrB4DaCWux/ZRwJb gL4kVyj4CWIeSBOw8foQ0ItfPSZ8VQuIRQ== X-Google-Smtp-Source: ABdhPJxAXuFgrKg2qqlbfEZaYPEVMYWWMSbRpj7wGTmynxLMChRlwscuZZkpJ51o1q3pP3VFbYLYMg== X-Received: by 2002:a5d:6ace:: with SMTP id u14mr13245451wrw.206.1633098567032; Fri, 01 Oct 2021 07:29:27 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:26 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 07/11] parse-options.c: move optname() earlier in the file Date: Fri, 1 Oct 2021 16:29:13 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In preparation for making "optname" a static function move it above its first user in parse-options.c. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/parse-options.c b/parse-options.c index dedd40efec5..2cf6f4d01c1 100644 --- a/parse-options.c +++ b/parse-options.c @@ -22,6 +22,21 @@ int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } +const char *optname(const struct option *opt, int flags) +{ + static struct strbuf sb = STRBUF_INIT; + + strbuf_reset(&sb); + if (flags & OPT_SHORT) + strbuf_addf(&sb, "switch `%c'", opt->short_name); + else if (flags & OPT_UNSET) + strbuf_addf(&sb, "option `no-%s'", opt->long_name); + else + strbuf_addf(&sb, "option `%s'", opt->long_name); + + return sb.buf; +} + static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, const struct option *opt, int flags, const char **arg) @@ -1044,18 +1059,3 @@ void NORETURN usage_msg_opt(const char *msg, fprintf(stderr, "fatal: %s\n\n", msg); usage_with_options(usagestr, options); } - -const char *optname(const struct option *opt, int flags) -{ - static struct strbuf sb = STRBUF_INIT; - - strbuf_reset(&sb); - if (flags & OPT_SHORT) - strbuf_addf(&sb, "switch `%c'", opt->short_name); - else if (flags & OPT_UNSET) - strbuf_addf(&sb, "option `no-%s'", opt->long_name); - else - strbuf_addf(&sb, "option `%s'", opt->long_name); - - return sb.buf; -} From patchwork Fri Oct 1 14:29:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530711 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46A53C4332F for ; Fri, 1 Oct 2021 14:29:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 307AF61A3A for ; Fri, 1 Oct 2021 14:29:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354546AbhJAObS (ORCPT ); Fri, 1 Oct 2021 10:31:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354530AbhJAObO (ORCPT ); Fri, 1 Oct 2021 10:31:14 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E454C06177E for ; Fri, 1 Oct 2021 07:29:29 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id s24so7374990wmh.4 for ; Fri, 01 Oct 2021 07:29:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lHY5PtXvHtbXEolf7Y6c6ISS7WXIbrQnH4QNAbaWY7E=; b=ofokyW5AXwU65EtWclYUyb6CyfNPq7zgAdrj3OP9U/tY5UFbzf2pkCMNTQ+451w0Z/ mhV0vligFNppxTrd0TZsIdWOizzWFYjSNPFQtJm/up/ILgFu6YG2X0OLiaGtNn1z4Atk kurKDinoLQRBcI3dpnQT6FpVXzdSgGJB8baORVOLZ4T6JWQYmcM56al475yfAWhspYwt jrfuaXCI+JpMlG2KbAvWJS93ZKcIji4fUvVyZG7iBeWUXmm1CEH9DDi5DRriEH431/bZ DVafqdoOuFbQrjVYwveay4dMxR3jgyhRBjUD2IKNBOTjLVtPIhrqGECqUD8/BDe0JRP+ 2k8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lHY5PtXvHtbXEolf7Y6c6ISS7WXIbrQnH4QNAbaWY7E=; b=IxYLQJOxxxMMyTsqfZN1lq76+NuQId3In9NycEp/aCggs9vpPr5qK0LZdkiRNftnXk 8TBwfAfH4kbik90oS1NKoZximBHjgXNBnW7qt7+As7CZBu7Ma3eEeX2PCsh1HPCbTt6f 429PBt7KlXKYAO+JKqfxl462tdmaS5VcEVQ/mttT/I9kpp3dKKAU1t8H71v2zPtm2WBL 1qqes+//A8oph7YVX7E5iozIEDImne7gm1lUYT5hT90ZCYRy2iDKYxPHmrAEBqM6TOr3 RKm0gnuqGl0c/PXnqhaEiXzEDvksI9iJkbrCaMq95z5ACvRP64BV3sQjeSa42od+atyX vKIQ== X-Gm-Message-State: AOAM532fB/K71gnY58w0UOYScpjcAq1WYtxZFfqJtbkqThrPPd27prcn x4h8v/J0Y/Xo3n7nfxn6TIklfC2hKtDzjw== X-Google-Smtp-Source: ABdhPJyirRoN/ZfjzK28Ikp1+49sJ4sUKlRvdyqfj9FWw9z8mQWWrMYrMm5tGHrjHnr0POi3paL0YQ== X-Received: by 2002:a7b:c102:: with SMTP id w2mr4916717wmi.112.1633098567986; Fri, 01 Oct 2021 07:29:27 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:27 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 08/11] commit-graph: stop using optname() Date: Fri, 1 Oct 2021 16:29:14 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Stop using optname() in builtin/commit-graph.c to emit an error with the --max-new-filters option. This changes code added in 809e0327f57 (builtin/commit-graph.c: introduce '--max-new-filters=', 2020-09-18). See 9440b831ad5 (parse-options: replace opterror() with optname(), 2018-11-10) for why using optname() like this is considered bad, i.e. it's assembling human-readable output piecemeal, and the "option `X'" at the start can't be translated. It didn't matter in this case, but this code was also buggy in its use of "opt->flags" to optname(), that function expects flags, but not *those* flags. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/commit-graph.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c index 0386f5c7755..36552db89fe 100644 --- a/builtin/commit-graph.c +++ b/builtin/commit-graph.c @@ -172,8 +172,7 @@ static int write_option_max_new_filters(const struct option *opt, const char *s; *to = strtol(arg, (char **)&s, 10); if (*s) - return error(_("%s expects a numerical value"), - optname(opt, opt->flags)); + return error(_("option `max-new-filters' expects a numerical value")); } return 0; } From patchwork Fri Oct 1 14:29:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530713 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD996C433EF for ; Fri, 1 Oct 2021 14:29:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B4B6E6135E for ; Fri, 1 Oct 2021 14:29:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354550AbhJAObT (ORCPT ); Fri, 1 Oct 2021 10:31:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354532AbhJAObP (ORCPT ); Fri, 1 Oct 2021 10:31:15 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 870F7C06177D for ; Fri, 1 Oct 2021 07:29:30 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id t16-20020a1c7710000000b003049690d882so11377455wmi.5 for ; Fri, 01 Oct 2021 07:29:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eFSKEVD7wYb1UB0DMzAbk7s/DwGgVN0ahxtmkJFSLvE=; b=WF/69iwTNVPfjzvYT10HzKbtrIojnuHYXWozsD+0eMRD6UEj3yPjjpeohVpwvYCXUU pJkdiXow6FlGYTkqi3M5Rd7ED1yQ/fKWmIs3rIPQJdJJcOnsXm06t/3HkmpLgUVWkVXy oMH0sc7/SO2Mbm2Ojy6GGPoQu0wmQqRTL+RvRQx6kVRL47BnzE+WUqMOazyKDW1zHpuA mP3YVK1rNdo1DUzsZbkhW8iOPqAezvg0pfO8A0utaLMLUUCTkfaKWY7MKIglFBSfAxol SURGwfvmRWyqNjFUVMy3vn1Q65OQ3mnHS6hWjq37TrOOSnMvIP0rW7Iy5Q+WALG3k/8g mv4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eFSKEVD7wYb1UB0DMzAbk7s/DwGgVN0ahxtmkJFSLvE=; b=FYuGpeWBOrc41/wn14xwc+TjoCYlmhX+ouMW+HZoIys+JIrVU6sdtKEwGclRGy1a2J pgTsDAhoJ+nNjam6h52wrXYpd/V8DO6XCxuesC6HWOqNmk6tsa5jpRX5P9bEIC2o4JS9 YPGprPdXXdq4am2Re6deAVd6uXHM3WUd1LI6EKSUuEprRADa/leZv8Adj8cAibQ/ky4B iecLCyEDQAcv15noDrThyvOsLcrllEp0eH1iNgbNOWo1iD3vCBMdW3+fIXsjIpxX0Wk4 jUxQIfCH8VVhUH9InI/yCnSvfcdy34VI7caUKgBF/PIIhoQPSsViYWjL6JpyAO3sgLmi hoJw== X-Gm-Message-State: AOAM5316ST51z1VbhA0tQTFxkryJk8AdYPy6QUzIMLNeKmKb02W4MpoT vKgzJl56fPUuFEAZH9X4MyfIS7qoNZXBrg== X-Google-Smtp-Source: ABdhPJySpxbGlG8vbbtT0in42aUzysdVy32HeA/X+lzITyU1mDCipZ1V/mBk0NJ8MDSBqxTWNNQvxA== X-Received: by 2002:a05:600c:2c50:: with SMTP id r16mr4860896wmg.105.1633098568841; Fri, 01 Oct 2021 07:29:28 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:28 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 09/11] parse-options.[ch]: make opt{bug,name}() "static" Date: Fri, 1 Oct 2021 16:29:15 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change these two functions to "static", the last user of "optname()" outside of parse-options.c itself went away in the preceding commit, for the reasons noted in 9440b831ad5 (parse-options: replace opterror() with optname(), 2018-11-10) we shouldn't be adding any more users of it. The "optbug()" function was never used outside of parse-options.c, but was made non-static in 1f275b7c4ca (parse-options: export opterr, optbug, 2011-08-11). I think the only external user of optname() was the commit-graph.c caller added in 09e0327f57 (builtin/commit-graph.c: introduce '--max-new-filters=', 2020-09-18). Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 4 ++-- parse-options.h | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/parse-options.c b/parse-options.c index 2cf6f4d01c1..0239c6bd418 100644 --- a/parse-options.c +++ b/parse-options.c @@ -11,7 +11,7 @@ static int disallow_abbreviated_options; #define OPT_SHORT 1 #define OPT_UNSET 2 -int optbug(const struct option *opt, const char *reason) +static int optbug(const struct option *opt, const char *reason) { if (opt->long_name) { if (opt->short_name) @@ -22,7 +22,7 @@ int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } -const char *optname(const struct option *opt, int flags) +static const char *optname(const struct option *opt, int flags) { static struct strbuf sb = STRBUF_INIT; diff --git a/parse-options.h b/parse-options.h index 74b66ba6e93..dd79c9c566f 100644 --- a/parse-options.h +++ b/parse-options.h @@ -224,9 +224,6 @@ NORETURN void usage_msg_opt(const char *msg, const char * const *usagestr, const struct option *options); -int optbug(const struct option *opt, const char *reason); -const char *optname(const struct option *opt, int flags); - /* * Use these assertions for callbacks that expect to be called with NONEG and * NOARG respectively, and do not otherwise handle the "unset" and "arg" From patchwork Fri Oct 1 14:29:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530715 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A998DC433EF for ; Fri, 1 Oct 2021 14:29:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8CF996135E for ; Fri, 1 Oct 2021 14:29:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354587AbhJAObb (ORCPT ); Fri, 1 Oct 2021 10:31:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354523AbhJAObP (ORCPT ); Fri, 1 Oct 2021 10:31:15 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73281C06177C for ; Fri, 1 Oct 2021 07:29:31 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id s21so15777606wra.7 for ; Fri, 01 Oct 2021 07:29:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Io7SYrdxgMSJ9sByVo2eBI2ZJl6RhY/Sms8VaXoXdpg=; b=oA1SKzI/6vLDCPZh5oc6+6HyaKzs9CP0SygVLW5juwQzKaWWUtsEG1ZG1tYdwnJIpK Y9jA+Twz2iPUCViFnvEc0W7Rp7lP/jCC20TtC+RmOkmRMVkSfbtE0hXtLgHFAzx118Gs feISqlG7r+eVvkqRhiEXwuR93HcdoIaKnAUgdm67MDmZN4hcgCw2YPhg2ytg5tD64Gux vmHriYX0unkEwTyk4UDrboa9Mx2Thyr7zUBf/Kc6DxgyTvUIiC7h+/zfjOyT4JZybEvp qoTupIDW86vA2LLlCu8x61Fryl0zOvByAoQ3u9YpFalX75Eu8fwZLA8dxFFx4ISgqfof omiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Io7SYrdxgMSJ9sByVo2eBI2ZJl6RhY/Sms8VaXoXdpg=; b=diImoojlMFFlBApGGCfSlRPKc9OkeV33icLacJhTHOX/MR41auY56Mybzaeidl1mPs n2+v4rosSGZT/8N4WOgXAh71cAMCsB4BnPmGYjQsA28+dpfH6TNABgGaILsZ+CR6s6nq uLseCYYMVTCmy3XsbZXBSSG6i5mPvwys9eAp0W0mM/bwiN+bl7tYajnW0PeSGBzvWc7t tgVmM/gS2v24YY3lY3kvLESkOZtZ7cmCuytXte6Z8z0Z+hxYYJWc1xTpZg9OLgmtKIXn ENkxZsVj+FTtS2HyAeVmJ7QbqBiJqAbFoa6IZrq3+v5XiMRhnCTYcVLiEg5nsitI7BQJ Y/OA== X-Gm-Message-State: AOAM53355W5Z2Nzs4icc/IIIlcE8/ogxrfiJ3rxsuKJ+M2bPto0m1Jl4 lBsu+OZsYPF+IZhkjy+KADSHfoLCAw033w== X-Google-Smtp-Source: ABdhPJx3SXPVudFEfa/3qQxK6qc9oqX8tJfLdwJspUIFGsiLK0lTefjeuRWOl03urCPPXODB2nsTaQ== X-Received: by 2002:a05:6000:186a:: with SMTP id d10mr13036621wri.113.1633098569738; Fri, 01 Oct 2021 07:29:29 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:29 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 10/11] parse-options tests: test optname() output Date: Fri, 1 Oct 2021 16:29:16 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There were no tests for checking the specific output that we'll generate in optname(), let's add some. That output was added back in 4a59fd13122 (Add a simple option parser., 2007-10-15). Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t0040-parse-options.sh | 42 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index da310ed29b1..d6f391a497b 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -168,9 +168,45 @@ test_expect_success 'long options' ' ' test_expect_success 'missing required value' ' - test_expect_code 129 test-tool parse-options -s && - test_expect_code 129 test-tool parse-options --string && - test_expect_code 129 test-tool parse-options --file + cat >expect <<-\EOF && + error: switch `s'\'' requires a value + EOF + test_expect_code 129 test-tool parse-options -s 2>actual && + test_cmp expect actual && + + cat >expect <<-\EOF && + error: option `string'\'' requires a value + EOF + test_expect_code 129 test-tool parse-options --string 2>actual && + test_cmp expect actual && + + cat >expect <<-\EOF && + error: option `file'\'' requires a value + EOF + test_expect_code 129 test-tool parse-options --file 2>actual && + test_cmp expect actual +' + +test_expect_success 'superfluous value provided: boolean' ' + cat >expect <<-\EOF && + error: option `yes'\'' takes no value + EOF + test_expect_code 129 test-tool parse-options --yes=hi 2>actual && + test_cmp expect actual && + + cat >expect <<-\EOF && + error: option `no-yes'\'' takes no value + EOF + test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual && + test_cmp expect actual +' + +test_expect_success 'superfluous value provided: cmdmode' ' + cat >expect <<-\EOF && + error: option `mode1'\'' takes no value + EOF + test_expect_code 129 test-tool parse-options --mode1=hi 2>actual && + test_cmp expect actual ' cat >expect <<\EOF From patchwork Fri Oct 1 14:29:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12530717 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DDDF4C433F5 for ; Fri, 1 Oct 2021 14:29:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C74F961A3A for ; Fri, 1 Oct 2021 14:29:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354558AbhJAObg (ORCPT ); Fri, 1 Oct 2021 10:31:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354579AbhJAOba (ORCPT ); Fri, 1 Oct 2021 10:31:30 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96B0EC0613E4 for ; Fri, 1 Oct 2021 07:29:32 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id w29so15811354wra.8 for ; Fri, 01 Oct 2021 07:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fbZPlM83ZBxDsP+bY7JpDa/BpYAVuJjI5iCwjav2AVQ=; b=VfeiIhYkAnmChTlDtBvOaemmiwJm75suC6PIV3Kg0+ca6OEKksbteau1/0mBNmQozI 7H7gc+lRUlWVMuZ9qmsdp47cA/IVzblTGtLhBFC/ya9eJ58yfb8tEDJrfINFCCkYGpJK lTitx7f4Osgle3oxfHs7a8lioqf0Ie1kO+DaUR+xy4/jBJBqDPB3CCYClZwBkg4lgDST pBPF63zrsLf1ofafuqn7B5lyyoQ0GnPbE/pbIkfPYo59oi5rSnMn+YycpvK4feHxetA2 yIGFrMvh2Najm+UHQycaMDFgbItpWswdma+5D1OHuPTjZIqM7vOScufgs7G8jdgj0XK5 gmyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fbZPlM83ZBxDsP+bY7JpDa/BpYAVuJjI5iCwjav2AVQ=; b=Rsr1kmXXWcbuAr4LSrCr39EFnc42VV6o2Yo8cnYG3oPOxnQfQ0ZYqkei8oz5EsNYH6 aCtD3NwuIj07cDvUOXXrnZlTsl31cRlX/3WABXIeqRghoIBRIfj9pSkPHHBl+qWGQ9gQ tgaVbEYs9aLZLV06xVhnZhrBKLG5wegBXJpvbtWWBD5ohe+TcT5Nr8HY6CXo1MJRlr0C w+u68awfaH3VS500NgiNG5+u/Yof2cGLQNbXHo3D3gpe8TdemwvyYyXf672T1W6zrpTl mN+vPQiLVdS87eZygFy4Y7LjlPKHZ7WAgeHbyziNmpScaKah8tJ8qxcUu/nSHB+KWpJM dFuQ== X-Gm-Message-State: AOAM530/M9knZhizmkGkV4eEc3NHitxK892tt6LNPylPKr0prM7BAvnW K1eJngh7dreMwBk8IQwjQnBI7jh4p8ZmQQ== X-Google-Smtp-Source: ABdhPJwAltm1DKmRNUwPn8cfFSQezeYoyXj5WR2azpLoDFntAHfEMhiwok8FZd8WnevfZys9kaesfA== X-Received: by 2002:a5d:4481:: with SMTP id j1mr13159514wrq.6.1633098570644; Fri, 01 Oct 2021 07:29:30 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 189sm9042069wmz.27.2021.10.01.07.29.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 07:29:30 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Thomas Rast , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v2 11/11] parse-options: change OPT_{SHORT,UNSET} to an enum Date: Fri, 1 Oct 2021 16:29:17 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1374.gc8f4fa74caf In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the comparisons against OPT_SHORT and OPT_UNSET to an enum which keeps track of how a given option got parsed. The case of "0" was an implicit OPT_LONG, so let's add an explicit label for it. Due to the xor in 0f1930c5875 (parse-options: allow positivation of options starting, with no-, 2012-02-25) the code already relied on this being set back to 0. To avoid refactoring the logic involved in that let's just start the enum at "0" instead of the usual "1<<0" (1), but BUG() out if we don't have one of our expected flags. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/parse-options.c b/parse-options.c index 0239c6bd418..61c294b7895 100644 --- a/parse-options.c +++ b/parse-options.c @@ -8,8 +8,11 @@ static int disallow_abbreviated_options; -#define OPT_SHORT 1 -#define OPT_UNSET 2 +enum opt_parsed { + OPT_LONG = 0, + OPT_SHORT = 1<<0, + OPT_UNSET = 1<<1, +}; static int optbug(const struct option *opt, const char *reason) { @@ -22,7 +25,7 @@ static int optbug(const struct option *opt, const char *reason) return error("BUG: switch '%c' %s", opt->short_name, reason); } -static const char *optname(const struct option *opt, int flags) +static const char *optname(const struct option *opt, enum opt_parsed flags) { static struct strbuf sb = STRBUF_INIT; @@ -31,15 +34,17 @@ static const char *optname(const struct option *opt, int flags) strbuf_addf(&sb, "switch `%c'", opt->short_name); else if (flags & OPT_UNSET) strbuf_addf(&sb, "option `no-%s'", opt->long_name); - else + else if (flags == OPT_LONG) strbuf_addf(&sb, "option `%s'", opt->long_name); + else + BUG("optname() got unknown flags %d", flags); return sb.buf; } static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, const struct option *opt, - int flags, const char **arg) + enum opt_parsed flags, const char **arg) { if (p->opt) { *arg = p->opt; @@ -65,7 +70,7 @@ static void fix_filename(const char *prefix, const char **file) static enum parse_opt_result opt_command_mode_error( const struct option *opt, const struct option *all_opts, - int flags) + enum opt_parsed flags) { const struct option *that; struct strbuf that_name = STRBUF_INIT; @@ -97,7 +102,7 @@ static enum parse_opt_result opt_command_mode_error( static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, const struct option *opt, const struct option *all_opts, - int flags) + enum opt_parsed flags) { const char *s, *arg; const int unset = flags & OPT_UNSET; @@ -318,11 +323,11 @@ static enum parse_opt_result parse_long_opt( const struct option *all_opts = options; const char *arg_end = strchrnul(arg, '='); const struct option *abbrev_option = NULL, *ambiguous_option = NULL; - int abbrev_flags = 0, ambiguous_flags = 0; + enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG; for (; options->type != OPTION_END; options++) { const char *rest, *long_name = options->long_name; - int flags = 0, opt_flags = 0; + enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG; if (!long_name) continue;