From patchwork Fri Oct 8 19:07:37 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: 12546203 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 DF38FC433EF for ; Fri, 8 Oct 2021 19:07:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C25AE60F4F for ; Fri, 8 Oct 2021 19:07:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240715AbhJHTJt (ORCPT ); Fri, 8 Oct 2021 15:09:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231424AbhJHTJq (ORCPT ); Fri, 8 Oct 2021 15:09:46 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C58BC061755 for ; Fri, 8 Oct 2021 12:07:50 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id r7so32601827wrc.10 for ; Fri, 08 Oct 2021 12:07:50 -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=j7bkEYgOMppw8HItc10x77itZQSux80FIcTo1H7rIWs=; b=HjZ1FcpFQsv9lmGuLNhO62Sh1pminzWL2OTXZ1ZwbPYOWv5jF7Y0dcMIKBlfEIb2+e JQZ695hhvToBRpCDiGHmbfAPeqUR4TvzlG9G9zwIecEWAEdKD6EaP1iEnzgrfIwxfDii GIhwAKCK7JMnzX7dhAogk/QJvI56oiyCKWVOoih8H/6mg20W+/fnpNMZ4+Nn5SML4C9w qM5Kn0VtxyWUcxVu62/UnVZfwflMyzc4EFP1+Rf+y9ktlKv50m6pbURtDl9K8Adzwb+W 5J5N4BZ2bDhhe8KaQFRIbGx3uord+97HSLX8oT041NpDy/O6XKE696uTP8j1ExdHEgUX YwUg== 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=j7bkEYgOMppw8HItc10x77itZQSux80FIcTo1H7rIWs=; b=J6vrMJWHp6g8U8Xztzm6CLPECahctCQaQGw6sABatyzll4kDmDoWFRZ5EH18xwGMPK oF8NNoARSvKZZYlOvvd9DOVhqV1Ict1RZ1OSW+UAaPKUGZ4HNHwYE5fG+u3fNlBpvTpB vFW+nCopWhNPplstcWKt7nE8ptfDAs0tGpIo+GeTW+n5OuJsFndwWG6pF4hDXxHWVKbA rjP38LSrFLrIaJuJns+a8NfthVWyHkrgBu6Jwajpigsr1lpG/MqIU2CNuldti1dD6/XV V9jO90ykK8RUckgCmntIeX8W8wevlA3jcEIZThodlAQpmNaaJizyPzwdKDZq1tPCYH/c 5BGA== X-Gm-Message-State: AOAM533zBNCvjRNLpGmhDvIDyYnsco/1QTfEosOiLLCeGLojS/Lh0ZWs 8XSe5SMW2TN9d09M1ymvjiowcE/aduo= X-Google-Smtp-Source: ABdhPJxN4HLROw16psTrTgxd9Xc8ajY7c6ZIBnYHrvjXkHiGSuKAEwcHDihAeJO/ZE3dQTS5oiepMg== X-Received: by 2002:adf:a443:: with SMTP id e3mr6328338wra.115.1633720068931; Fri, 08 Oct 2021 12:07:48 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:48 -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 v3 01/10] parse-options.h: move PARSE_OPT_SHELL_EVAL between enums Date: Fri, 8 Oct 2021 21:07:37 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 8 19:07:38 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: 12546207 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 B49FFC433EF for ; Fri, 8 Oct 2021 19:08:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9508360F6E for ; Fri, 8 Oct 2021 19:08:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240861AbhJHTKD (ORCPT ); Fri, 8 Oct 2021 15:10:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240659AbhJHTJt (ORCPT ); Fri, 8 Oct 2021 15:09:49 -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 1C533C061570 for ; Fri, 8 Oct 2021 12:07:53 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id r10so32670013wra.12 for ; Fri, 08 Oct 2021 12:07:53 -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=lHSTimRQK1P9+oMaU37/Qr3VBwTzs5I9LVjztFZr0yU=; b=kRAe754J+lsg6zOsABCJCyjKqrWbuSN4ghNVL/O6rm68+Y0DdblS1oH/ySaHUb8wYK bCr1ZrBjE3Gb3gdMy3Ls/Vc1TP3talabjEEUcZkIbzmpDL8rsrQs1mbMLWi7ZHuBJ8Jw IWq1Re2UHEKuCeQ8KW9GBcqwzNFdsQ4aRSQZUHjpZ/zwOT32F+AKULLCEnLRejQlpgR+ o5R4q5HC5Z4gYYoQl/OjfhoyN5arB0qvFrMSHHfg0WlapDkB6WbPjfQHtKYup6nmoS6x grasGlnG74NUK4fxPB+yFSGuBkkry611LcgocV8tRWjnWIJNWA0oH4u6wDMa6ljiLOWp pf6A== 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=lHSTimRQK1P9+oMaU37/Qr3VBwTzs5I9LVjztFZr0yU=; b=OMsFWaxHLEJPmoNrSrzD1+hC5iXcjrSOZdqt5V/zvmEYWU07FAH6Buu5HWAtZMtRN7 Q/MU2MIEvNARz7vZxW0EpSvzYue4bUivBZ7HXHgVtyJXQGEBGHJN3ziXF+zpwf0ysNms YRtDlLLzlPgWE/68DZwIrPXEZBsUC3i6zbqOtJ/YoSEpiY/gSk8Pigz9A7gnppPlK6X5 fZwXzkLud/RjG9KT+rQhswSXzqo4ZFcbWDJn9x4Rc6qRLVjVmkXWmb5vZ7INVCaj7pKA 1gO7/XWvCKjeYYMpMqOxXnB0r1r9VxBniFZQGWadic8jN4kV/xe2srhtHgj1opF6dnMh L5UQ== X-Gm-Message-State: AOAM531oDphFPC+PJfH3TE085b3lvAzqhPJb0pnnLsbzWXXXylx7pZ6p gZTvoVa4tRrhvnUflD3bGd2+eU9qeEFhtQ== X-Google-Smtp-Source: ABdhPJzlb3q3nVDXhcd5wSJ4/J93U+C5Of5453Ndk/VilyGFCF37Q3YavzXLunawylHuuGBYflcOvA== X-Received: by 2002:a1c:9ad4:: with SMTP id c203mr5267851wme.41.1633720070066; Fri, 08 Oct 2021 12:07:50 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:49 -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 v3 02/10] parse-options.[ch]: consistently use "enum parse_opt_flags" Date: Fri, 8 Oct 2021 21:07:38 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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. Even though this is an enum bitfield there's there's a benefit to doing this when it comes to the wider C ecosystem. E.g. the GNU debugger (gdb) will helpfully detect and print out meaningful enum labels in this case. Here's the output before and after when breaking in "parse_options()" after invoking "git stash show": Before: (gdb) p flags $1 = 9 After: (gdb) p flags $1 = (PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN) Of course as noted in[1] there's a limit to this smartness, i.e. manually setting it with unrelated enum labels won't be caught. There are some third-party extensions to do more exhaustive checking[2], perhaps we'll be able to make use of them sooner than later. We've also got prior art using this pattern in the codebase. See e.g. "enum bloom_filter_computed" added in 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 | 8 +++++--- 2 files changed, 12 insertions(+), 7 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..2e8798d8744 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); @@ -262,7 +263,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; @@ -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 8 19:07:39 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: 12546205 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 67527C433EF for ; Fri, 8 Oct 2021 19:08:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4D07C60F4F for ; Fri, 8 Oct 2021 19:08:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240731AbhJHTJz (ORCPT ); Fri, 8 Oct 2021 15:09:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240695AbhJHTJt (ORCPT ); Fri, 8 Oct 2021 15:09:49 -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 6E6AAC061764 for ; Fri, 8 Oct 2021 12:07:53 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id k7so32512965wrd.13 for ; Fri, 08 Oct 2021 12:07:53 -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=CH9uNT1ovcPcUx1HrgpRJHKZZ5gYHCr8ACF4ZtsTsA0=; b=Q9/p004gvEiNiQbj7vwsHDJG/vw89rGzIxt8It6aavpaRwlN2tr1MrZTkTi+fLAdIG 8E4tSYv79LCYplypPtvivE/AyifAj8c1PrsXWPBM2yKEjd6IxANl/dhe2H/fHamn0+K+ wfk6DZWU/UiAxOAUs8BueoBBIX7GqPzuru+nPlUsQ2n/o6ey88AL6TR9gu1aGfn1eFdP 2KZNhqrlNxFKaH/TEGaGjmPL5H0EtRGicArZzlt1oH4wDSE7tbGxzcHuKaFhAvbNGmY6 SCWJbE7tMceewfAzisg1x/Jiv629pTQJMC0AhGaKA7u4QtzgO2R2GGE1ZpkWBTSKH/tC FrkQ== 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=CH9uNT1ovcPcUx1HrgpRJHKZZ5gYHCr8ACF4ZtsTsA0=; b=JYdvbvs1RrvtjQWRhxYW2wMa66yLl8j5lGsEf4Yfni9J5Te31qplNgRhC5KT2GbZAq I5ji9MtArymprn3wSpZL7BDMPG98IKdrmgbYqvbJI9/M60cCVebXEXjRfQOwyZY3vEV9 GhslbNNdD0aoLVV186lI/eGCT/8aBiBTJFK9fpvLkWPSdPezGH3XI6sgzX8qH9zOF942 U45wDaYFX29eJ5vg39k+0iVMZJkrFrqyUTWe/olW2HkAAgkxFZU2bJ/BKgyKYBOqCTWE VO6V9J2Qnr7BRrTuJMZP71qPUZCi7N8ZzAYExn2+NaiSJ3X90+ugKtVC6KNdBqb8R9b6 VfSw== X-Gm-Message-State: AOAM5324VCJUdsOhOepY6duN5riWLAfI56iWLvc+h5P64H0xNUQ331kt Bxp4MdusTYNmjcs0g4bUvbZz8k8ekdN+Ow== X-Google-Smtp-Source: ABdhPJxwCp0Ql7J+z+SXuNnqW9ICNV2dDFI3LoLThwMtTWa3vobPlS+ulukmvVKq674ERkwRjaRNjg== X-Received: by 2002:adf:a3c9:: with SMTP id m9mr6403221wrb.237.1633720070683; Fri, 08 Oct 2021 12:07:50 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:50 -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 v3 03/10] parse-options.[ch]: consistently use "enum parse_opt_result" Date: Fri, 8 Oct 2021 21:07:39 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 2e8798d8744..a1c7c86ad30 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 8 19:07:40 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: 12546211 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 71BE5C433EF for ; Fri, 8 Oct 2021 19:08:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5B07060F6E for ; Fri, 8 Oct 2021 19:08:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240709AbhJHTKI (ORCPT ); Fri, 8 Oct 2021 15:10:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240698AbhJHTJt (ORCPT ); Fri, 8 Oct 2021 15:09:49 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41E88C061755 for ; Fri, 8 Oct 2021 12:07:53 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id v25so32599886wra.2 for ; Fri, 08 Oct 2021 12:07:53 -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=CmOxadZbZ0sC489o1MBTBZNEh32GbZbNl624BQgYl5o=; b=Lmt8/wb2br4KjdBLh6Nl62COhWShibGqNq/UOZ0tWMouuCESVOsQedFJZhSYuBn0vV T3GTVNefIHSZapbjsrfFWkQ1UFn7mEkg0cWU9Z9e0/95WmPa9yiNCrdGGYgGllLQhbaY nOPlO6hGqtkjtdQFV0idTfEQfgQ/7xjR7sncFQlAub+fcyrGq9TAgJ4dW+CYwvZey98E 7DUB2TF7mYqNa8lmF5xU6YZLOJFnrFH4ltCtgYWB1eIErY4llZC8dNCDMb9GRtdQd+Qi 6a5G5yIcE3cCkT+5LLz2YQGGtpHiygzrFecmMgRn07b56FI9ymZGL7H0emR7XwQWhS4H SR8A== 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=CmOxadZbZ0sC489o1MBTBZNEh32GbZbNl624BQgYl5o=; b=xwhyTvHbV428iSwBkRsfx/fGXsWvc7adAGVnL74iOSNVOtVjY4x3c+5PDvnF40lPr1 /dlbacPr6uYBDR8s3Q8SH3JDG/FjAmAQuFkCMFzq6ezORWcOgO8TQdg5guisiU/9BsxP wnw3Akfb2V5SbKBFtFEeAaTdnthu1ocE5nDKYbTQcN0LCDeA1T21dZ97IQmTFpAqXGcj 2oAI2pB8YkovAoHz/mMlVQPXkSRySSrK2Fx9lUn7cEEZJzuohEc8FtHK34YGLF3PZcBi ECwXsaIO7ZBHRAO4pLn2MWCq70Za8bWEFtIM6syHF3DG7xElcy8//mET7F5nOGy5eUjj sk3w== X-Gm-Message-State: AOAM532GN1KcJbgltHVWxRwdchdzvBPp+4mleOcpRx+Ytcjzvlxldq6j t428zzDRTr9w9bH0Ta7QiR6VwWKqa0nn5w== X-Google-Smtp-Source: ABdhPJyPvwzVeD9jTsFxlJSHtvMGFSOi74Ofb8OxoY491My7KvAy4rMPAYwk8263GZfLdcdbMf0Ahw== X-Received: by 2002:a5d:4882:: with SMTP id g2mr6235697wrq.399.1633720071474; Fri, 08 Oct 2021 12:07:51 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:51 -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 v3 04/10] parse-options.c: use exhaustive "case" arms for "enum parse_opt_result" Date: Fri, 8 Oct 2021 21:07:40 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 8 19:07:41 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: 12546209 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 5E16EC433EF for ; Fri, 8 Oct 2021 19:08:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 47D7E60FE8 for ; Fri, 8 Oct 2021 19:08:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240880AbhJHTKF (ORCPT ); Fri, 8 Oct 2021 15:10:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240728AbhJHTJu (ORCPT ); Fri, 8 Oct 2021 15:09:50 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49E57C061765 for ; Fri, 8 Oct 2021 12:07:54 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id m22so32769821wrb.0 for ; Fri, 08 Oct 2021 12:07:54 -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=D9+X8GbF0VrvBnFc9IdkgfIzIMh0guKLqQ3NrLiZJoc=; b=kUIUXn5diKODTRZJOEZvrn3zMYILjRZjqOEMLMa2dvgDfW7AoMVfUX6yZyGoZR21Od T9H1NrywfAd9MQrCzeQijNCRWTTcXhgrLmONsKl49QqmVnnJ2X8EQ7th7EEdUbMOt4dD uTuVq4ddcNnO9cjMoVpvwYRr/ZN5bfwuISTAI/opT9IGVL01HIxCL628v2H4JGB4VZ8r DC/4/9xF+aAdsUmNbbf10/TspPGlJw+qUParvGZwG+3Kj2jvykhF38janqQjlhhgPkXO 0mihhQimS4mMYVG97nf9Bc7SWDKccTxRzDLdtoFDEoqgZysPWIlMQs+ltboMBt91rYNZ cNlg== 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=D9+X8GbF0VrvBnFc9IdkgfIzIMh0guKLqQ3NrLiZJoc=; b=JZwqiwn2IV7LCyzo8FPE++nhH86z+q8i8v9oxvhNTlLILoR0OlV7j8kfUV/Z4/SjLM +yz235iDWIWNU49u36RH1V9MacJwkwS9v7U9ZV+fW4HrnG5hN18I10JvVH/5r1rSODFi ZWRTawKwCBwv0hwJtBmljNR4K8STnvUEPOGIizkonhkr/IZlnSmFwNgtN2dZ4LDdqYwu titijS9CCX1Qer4XHZIk5ttrsuBpDMOkEi6yH4M5J9cuwEWXxNRqBnC8VRNLVRpdutYb D5J35JHMpOcq+MQh71OTffQQQlmTbplkPbAA4SuO6JM/67gfYJQK7t96sjyXgz1z/ANR 8VHw== X-Gm-Message-State: AOAM533dTWPgWj/noPQx7eMOG3YWOXttaUdKRXWTxZjAW/2IDmU0xJgQ Aclij7oFE/Z73X4dQ+WIM2nq9JrT8iMYoQ== X-Google-Smtp-Source: ABdhPJwee8Q01e/8qqnIpc125gGJmIDDLzrEejXAtCrBTC9JVRfSupNuykTa1mJp/B2GxQy0B4cqWA== X-Received: by 2002:a5d:6ad2:: with SMTP id u18mr6404075wrw.47.1633720072572; Fri, 08 Oct 2021 12:07:52 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:51 -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 v3 05/10] parse-options.h: make the "flags" in "struct option" an enum Date: Fri, 8 Oct 2021 21:07:41 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 8 19:07:42 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: 12546213 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 2E55AC433F5 for ; Fri, 8 Oct 2021 19:08:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1518960F6E for ; Fri, 8 Oct 2021 19:08:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241088AbhJHTK0 (ORCPT ); Fri, 8 Oct 2021 15:10:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231342AbhJHTJv (ORCPT ); Fri, 8 Oct 2021 15:09:51 -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 318E1C061766 for ; Fri, 8 Oct 2021 12:07:55 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id o20so32571980wro.3 for ; Fri, 08 Oct 2021 12:07:55 -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=qGwGO7cbz8PSOPTEr6z7+Rtm6ZchfBTB4hsKgYPyY1k=; b=Hq6qCu8QpqPtO6WlibeoZfvE41OGfoKwajhxJVbfrbYynlTXTJOtg5EaW47g6ODhhk ByO7dSzt6uEhtRJ6RumJlMx05c8DftZUCnhg/sc2yrIGWWQ/OXlBkWjMgmeMMoI5Pqm3 XWn2O3Pd+4nLg5v8iE0gdSEqxr2DPt1KrWyTopKEklbFV8gS3FjQvtaMMi0oUuOgt7RD FpElvqzX+VQikwnAJZvhx+YAl76W6mg+S5u8i6cV6fFbWHU/zrZmykLsNLXOqnK2DXJ3 ETm4UPX/NXGCFbM2wKfcxPGJTujnH1jBRgmBYHAKRe+DfLuBOrp62RhjwgiFe6mD4Iea U6yg== 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=qGwGO7cbz8PSOPTEr6z7+Rtm6ZchfBTB4hsKgYPyY1k=; b=66WNC72/mcLxkXWXUYWM/TDvLJs5beQOprCM6CQ5UpFMjk6g0jqPmQsZjvqxYr0k8I PYW+57DXS3yCyFBGhhdVNq/bSbY3VUpMZVzWy78SP36IZZrcXc/Qfpy32mg8CXc4z1ST aYt3hi2G9NsCIewfDNTSf4bAgq/5oVoG9rsfne9nYEnOuEsx3TE2CKosyJx0ufe7z1Fi l7SszkoTMkbg83qON43Shj/RK+5vTIE0FXC5tcKLSKU4J4zV5H7IuKfUsjO09wvTDgga DMQw/PsgScA/AQRoKdb9qPWVLLIYCjX4ft8rtiDkSz8+pRP6bMODzDr6+7tPram594qE skaw== X-Gm-Message-State: AOAM532q8bg6Qmb63IVkiMxnltBP1NDnzAdFVwTdO9Hu2PIwFyo8pxwe l+kuAC1sYdMuVxEv+UDuIzwP9DXAe+9bJA== X-Google-Smtp-Source: ABdhPJwrQQwntAwg7IN36bw8pUx6VTxNXHtRFWbN5t2c+whWu4J3hgeUnOMMUUH4gW2rUmH7gCViXw== X-Received: by 2002:a5d:47c3:: with SMTP id o3mr6360768wrc.25.1633720073601; Fri, 08 Oct 2021 12:07:53 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:53 -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 v3 06/10] parse-options.c: move optname() earlier in the file Date: Fri, 8 Oct 2021 21:07:42 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 e33700d6e71..9e2da8383d7 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) @@ -1006,18 +1021,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 8 19:07:43 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: 12546215 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 636B8C433EF for ; Fri, 8 Oct 2021 19:08:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4435460F6E for ; Fri, 8 Oct 2021 19:08:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231245AbhJHTK3 (ORCPT ); Fri, 8 Oct 2021 15:10:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240735AbhJHTJv (ORCPT ); Fri, 8 Oct 2021 15:09:51 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 051FEC061762 for ; Fri, 8 Oct 2021 12:07:56 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id t8so32840785wri.1 for ; Fri, 08 Oct 2021 12:07:55 -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=U8dOdTE9nNOYUNqKEZTLE7TmDBsKiaDIsTgRa0y3DOY=; b=lcGRT59hAQIi8Glj2Uy+utJkoptV92p2tl8uLUd/uI7lM2CInO4JHCM9zdWpUywuWM KK5icKVsZaJJnhVgmwbRulTKOAMPZ6pe3q9UirLStJMMHvCYhw77RZKEqmRfw4jQZUi4 eguNw+JprPDhCWDBIO0h25Qd6ZLtFfju88Bti5t+PlXC38bDYnibulGizth9LkaF9XqB 3hbN4weeT0+TA54KKvvw5TftnnR0spNMIpgtbbFE3ZHOX9ad7Z3cusjdikBxh3ja3CDp +fpaTfDP5n4MuiMHRNLZ5EXFf+N8YcNjqRcCXrIjo90ObDZUa6xrgB3ggWmO1zoOZ1bq xa9g== 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=U8dOdTE9nNOYUNqKEZTLE7TmDBsKiaDIsTgRa0y3DOY=; b=3z861aws/w2EAl4z/OA9nTUOYeP7u4tuB4qZ1oCn1Kx1ATkzPZ3cpG2sqfU/QgLTCB QMuZgywWr3N7pAXBzxUmdEPJ//fwwAubBrh9KRsOqGMjrgz7lbS2ePuf4MswayZz5eAg D7vkvR7LGQ59RR71dcD3K2OwIbxMtkdm4QEFuTiLUYg7qBPo/tHwGjVeVTARX/TDtIiD GMITPWr7zgI0Ce0YV1ldCeVm1wKjC7oDc/AddH0CF7SJV2zF/HI2bpSsmCG0X/CubWoR y08TvbfvHuP4glH68LowUKwEBAgchv+eaJdVzojrhRcguSeu+pH/bRjbIZks8caCDyzo URCA== X-Gm-Message-State: AOAM53080/5VfYW8EF+dP4YEVtRvTX+XSNgfj+sl15TD68a/Rx6CmIZj eSUNazt5TZzdLDPCTABhWmoxGkORigTq1Q== X-Google-Smtp-Source: ABdhPJxRZgVPQkIS2NpVPAgxVfUy1eHjLINfvDFBuhMYSoR5Uce24fV20VRVaFiIyHtD4XRUSNpbLg== X-Received: by 2002:a1c:ac03:: with SMTP id v3mr5217558wme.13.1633720074322; Fri, 08 Oct 2021 12:07:54 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:53 -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 v3 07/10] commit-graph: stop using optname() Date: Fri, 8 Oct 2021 21:07:43 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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. Let's pass "max-new-filters" to the new error because the option name isn't translatable, and because we can re-use a translation added in f7e68a08780 (parse-options: check empty value in OPT_INTEGER and OPT_ABBREV, 2019-05-29). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/commit-graph.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c index 3c3de3a156f..ab8e5cd59af 100644 --- a/builtin/commit-graph.c +++ b/builtin/commit-graph.c @@ -172,8 +172,8 @@ 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 `%s' expects a numerical value"), + "max-new-filters"); } return 0; } From patchwork Fri Oct 8 19:07:44 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: 12546217 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 6F479C433EF for ; Fri, 8 Oct 2021 19:08:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EF6760F6E for ; Fri, 8 Oct 2021 19:08:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240735AbhJHTKd (ORCPT ); Fri, 8 Oct 2021 15:10:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240642AbhJHTJw (ORCPT ); Fri, 8 Oct 2021 15:09:52 -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 D5E6BC061570 for ; Fri, 8 Oct 2021 12:07:56 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id t2so32629734wrb.8 for ; Fri, 08 Oct 2021 12:07:56 -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=ntfxB4Y6zeB26EdcuWtHUHfTeFxP6IXFBOvZCPRLoNs=; b=mONjeavJJUxQ9UYI8nF4WYXFfbxGMjy2/sR2tssrST4pcXzp0MAxbQjUw9N8oQWQ4z hmibyl1bOAJaGR0EA7ixwJL6XeC7Trp4zmF7KvqkN8TlwGgPIE7Vee5psLofKwpQpkt3 Rd2fSIspIz8a+N6JMX3ifO4CqioFkuc3pC9FVsVDqSLrK+5mOZxAoL/yISy/rKmP1dEK Sq0qHAOIHE1xJZ4F+TwAp4mvOwjApraR9RZIQDyPffhf5pFi9Y8+hho+fYkJ6Pv4D132 bPhNogeo7bd/7ATaS/3/dWK7Qd8kLRByKxAdfNnGEqZvMu9pXR7DYl95M+qV8ny+Ir81 QLng== 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=ntfxB4Y6zeB26EdcuWtHUHfTeFxP6IXFBOvZCPRLoNs=; b=gNcOXLqcO6hxBud3RpLAF2O2szfQn22Qy1Xufu97Rj6nfOJI0r1mTXdfjYfVDASD6v WnDVTRfxJy3WlbGEnT8Dk0CMLMoAGpmrnr/m/MsaKVTB0tIMKQkWmwDks98CF34FlHZJ 1mMSt//plUbk48ThEUH2sDPTgZ3tAR78xR1RoUvGeaaTgSbRAjk+72zAfDM5XJG24VwA Q5oLfJNKg2LMhTHwkVUk2TNaeXDYGSIwtMYxfpnRUyCKhMb6FKyz1rapDNpdNNT3W57T 4hipox00R2enK+vq54Xv1yuPsu0blDCOQGIxVXwOa2Y8g3ztpv9PwbqX0xgr6gaCyD14 CfIQ== X-Gm-Message-State: AOAM531kU93qriFVoX3T8jY1GHSeQRDLfQD/rtfVD8WdXSKasrbzUUJI P2VlXnObYltSvYuUYz10lIgiL7oYm4R4dA== X-Google-Smtp-Source: ABdhPJwHrJGnT2NcYYyxKQXuTL/WZmDjFD+CBSM9uSlmB9+n77/z4so/SI7JVul2TQ5HJrEpOtK9xg== X-Received: by 2002:adf:a30b:: with SMTP id c11mr6268763wrb.289.1633720075236; Fri, 08 Oct 2021 12:07:55 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:54 -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 v3 08/10] parse-options.[ch]: make opt{bug,name}() "static" Date: Fri, 8 Oct 2021 21:07:44 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 9e2da8383d7..64bd4c32854 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 8 19:07:45 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: 12546219 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 B133FC433F5 for ; Fri, 8 Oct 2021 19:08:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 991DF60F4F for ; Fri, 8 Oct 2021 19:08:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241202AbhJHTKh (ORCPT ); Fri, 8 Oct 2021 15:10:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231424AbhJHTJx (ORCPT ); Fri, 8 Oct 2021 15:09:53 -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 A724CC061755 for ; Fri, 8 Oct 2021 12:07:57 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id i12so20028324wrb.7 for ; Fri, 08 Oct 2021 12:07:57 -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=zpUsMFj5n88bokfZ9JR7ANnZQk9c8UrFvAw0lEH6Te4=; b=bQwFhz5mEzNA0T2d3ZRmJHnXXPAW7v0POp+G/mdpJSg1SKwHxdQ9izBv/9IWo24cYi 85SafR8f47FAEL8pMekp2i/evlgOHvl0eke5MoqccD+1bZGBTSOe0hjx06QCu//JW9oa TK5YA6tlZKI/RWY0NYbEspyUPeywvX/uTIwiL5jbal9lhEbuCELtho8K6Apdqb0VF6H7 YcmWzzDMMDy8r6aG8b8+rIoh4cYxpCzl7XTdi7PH6tD3qjppqsCd5XQ5Q/zIBdwe2R7b gKccHo+sKu05pWK00nqNMR72YWi0Vhy4wReStsPmYgFD06yx5vGePW7W4cmQ8m0jZuKg tvBg== 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=zpUsMFj5n88bokfZ9JR7ANnZQk9c8UrFvAw0lEH6Te4=; b=HQX0v+9tlmRZAZohR9jaNAxdZOeOdPKmag0nnbwEwpJNVG5rysg9zklekYKrxuxktr T5qFTVVR0X0LUEnj0Iee9yCP8cI1tP+S61U1i3Kc0A1Ed44eBYdRtWq1/yXutaYxnGxO OtMf1SVraGxg9Vlt21GNoUO0vdYEFbzzaOQMmq6HVkPWh2jNXJyRXqu8j4St+ivu3xxv hryx9qQJglMsDnHhvkUNkXbkODUT3cVOm67jIkLV/feryV44QKyyQRp4TEtBkLx+2dRI 5Z1Cy5Zy+qWqoUTn6XVeiFRscrcjSf8ipenRxaUVwX4QL+xG2dw3atynfwMXk8abD2ED TjCQ== X-Gm-Message-State: AOAM532Sjsz100q+956/8evrztZz4zP9KrYoot/JhfypY1qYcD6/fJhB QvduxKCKqs+bfV3MgYhNpYo68ODcMsQaoQ== X-Google-Smtp-Source: ABdhPJzsJQfRfEqfxUDkg6lpjTxoeWxPibKVZ+NXXRpebnkKSgvBQ//68rJkR/mcINv/VpIRF9AEIw== X-Received: by 2002:adf:a31d:: with SMTP id c29mr6168428wrb.381.1633720075987; Fri, 08 Oct 2021 12:07:55 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:55 -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 v3 09/10] parse-options tests: test optname() output Date: Fri, 8 Oct 2021 21:07:45 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 8 19:07:46 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: 12546221 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 84B9EC433EF for ; Fri, 8 Oct 2021 19:08:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6912060F4F for ; Fri, 8 Oct 2021 19:08:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241274AbhJHTKk (ORCPT ); Fri, 8 Oct 2021 15:10:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240187AbhJHTJz (ORCPT ); Fri, 8 Oct 2021 15:09:55 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EC31C061762 for ; Fri, 8 Oct 2021 12:07:58 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id t8so32841037wri.1 for ; Fri, 08 Oct 2021 12:07:58 -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=0QXO4fjeBefOTyARWqTPwOds6RKJ3TwZZPrwpYeA7xA=; b=qxhsKmG1Q1f76he36fMCcurl2An5AwHk3YTeFccv2SgugVXivlqQDXImeFgAGjlR0I fGcJ40b1jOhCcpO2gTiTNwmnRPpqxyOh3vBShCclngXiCPL0w2RBp8Uk5F6NFFko/ptT m+6/cWk3+C4Lyi2GeF0xwaHGg9aK4jr/0g1OkycOmftZ48De2JWYZSwZP/fptynB25/G ahT0kmJxhwUddM7cSj/SHr62NSuPI1Up6DDO2GiJfZFnEYVRjncPJlXMyDio+EnbvWvY thZ3leOCs7PCDV7sbyozXLUMjjsBDjAonkiHS91Mrw89ci+qnxUQO5BJ50PUVUsu77FL SK2Q== 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=0QXO4fjeBefOTyARWqTPwOds6RKJ3TwZZPrwpYeA7xA=; b=5MLB39lgHk1ayCzG+i+fHmqzrDCVBShTzXLipeyo/Zf3tMY0X77jgJ9Mb+XeaQiw/w unuyymgBrUcLKDlj+ZR7BUEoJ+q3bsC+4OGpshS6Lo1JGAb6Vg/QxdhJ7ZqZm6ckcqB3 wqNreyVM1KpPmAkzVb9TTtCsJ1jn6dsxrFvKC4z7eu6c5IG2Kbx7m7sa5pETDgaF9rvL VowJabMUTDRvax7y2ebTCKUD5uUL4aDnUPhicxydYbSGxrvQHxOqLfEVNKR6HJCuGOYQ ovIWLZIxGEL2r3eeB2aXXarMW9NBrtwA9u3uC1XUdS3J/DaJKLJ1qAWep9Qakgyhb5+v BHgA== X-Gm-Message-State: AOAM530AS69ApT8lgugmeFfOT2nVix1P6d6Ju1D60CsjlioNodPt3lUo tIRtYkLh2hWcYO9vEMxEn2yh3qxe3jlGJQ== X-Google-Smtp-Source: ABdhPJwo2z+0zK/hDZkXoD3g/xMKnYYU6+4TBp9wLlUJYpkb5piv4ahjv2cp6ul4ErlYZ1evxUh9VQ== X-Received: by 2002:a5d:414e:: with SMTP id c14mr1456015wrq.415.1633720076879; Fri, 08 Oct 2021 12:07:56 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id x17sm169882wrc.51.2021.10.08.12.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 12:07:56 -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 v3 10/10] parse-options: change OPT_{SHORT,UNSET} to an enum Date: Fri, 8 Oct 2021 21:07:46 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1446.g6af949f83bd 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 64bd4c32854..2a2c0ee24f2 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; @@ -313,11 +318,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;