From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Elijah Newren" <newren@gmail.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH v2 4/6] cocci: add "string_list" rule to swap "DUP" <-> "NODUP"
Date: Thu, 21 Jul 2022 14:00:51 +0200 [thread overview]
Message-ID: <patch-v2-4.6-2d858c49243-20220721T111808Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-v2-0.6-00000000000-20220721T111808Z-avarab@gmail.com>
Add a coccinelle rule to swap "NODUP" and "DUP" initialization in
cases such as [1], which as this change shows produces an identical
change.
We happened to have only one change in-tree that matched this
criteria, but now we're more certain of that, and will convert these
sorts of cases in the future.
1. https://lore.kernel.org/git/xmqq7d471dns.fsf@gitster.g/
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
builtin/remote.c | 3 +--
contrib/coccinelle/string-list.cocci | 18 ++++++++++++++++++
contrib/coccinelle/tests/string-list.c | 13 +++++++++++++
contrib/coccinelle/tests/string-list.res | 11 +++++++++++
4 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index d9b8746cb3c..c713463d89d 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1229,10 +1229,9 @@ static int get_one_entry(struct remote *remote, void *priv)
static int show_all(void)
{
- struct string_list list = STRING_LIST_INIT_NODUP;
+ struct string_list list = STRING_LIST_INIT_DUP;
int result;
- list.strdup_strings = 1;
result = for_each_remote(get_one_entry, &list);
if (!result) {
diff --git a/contrib/coccinelle/string-list.cocci b/contrib/coccinelle/string-list.cocci
index 5d285d5732c..63bb2abc93a 100644
--- a/contrib/coccinelle/string-list.cocci
+++ b/contrib/coccinelle/string-list.cocci
@@ -6,3 +6,21 @@ struct string_list *P;
... when != P
- (P)->strdup_strings = 1;
+ string_list_init_dup(P);
+
+@@
+type T;
+identifier I;
+constant INIT_NODUP =~ "^STRING_LIST_INIT_NODUP$";
+constant INIT_DUP =~ "^STRING_LIST_INIT_DUP$";
+@@
+(
+- T I = INIT_NODUP;
++ T I = STRING_LIST_INIT_DUP;
+... when != &I
+- I.strdup_strings = 1;
+|
+- T I = INIT_DUP;
++ T I = STRING_LIST_INIT_NODUP;
+... when != &I
+- I.strdup_strings = 0;
+)
diff --git a/contrib/coccinelle/tests/string-list.c b/contrib/coccinelle/tests/string-list.c
index e77822b7682..1821ed4ebb4 100644
--- a/contrib/coccinelle/tests/string-list.c
+++ b/contrib/coccinelle/tests/string-list.c
@@ -1,7 +1,20 @@
int init(void)
{
struct string_list *list;
+ struct string_list list2 = STRING_LIST_INIT_NODUP;
+ struct string_list list3 = STRING_LIST_INIT_DUP;
+ struct string_list list4 = STRING_LIST_INIT_NODUP;
+ struct string_list list5 = STRING_LIST_INIT_DUP;
CALLOC_ARRAY(list, 1);
+
+ /* Exclude these */
+ string_list_append(&list4, "str");
+ string_list_append_nodup(&list5, xstrdup("str"));
+
list->strdup_strings = 1;
+ list2.strdup_strings = 1;
+ list3.strdup_strings = 0;
+ list4.strdup_strings = 1;
+ list5.strdup_strings = 0;
}
diff --git a/contrib/coccinelle/tests/string-list.res b/contrib/coccinelle/tests/string-list.res
index 7e666f5bf48..58b3733dec2 100644
--- a/contrib/coccinelle/tests/string-list.res
+++ b/contrib/coccinelle/tests/string-list.res
@@ -1,7 +1,18 @@
int init(void)
{
struct string_list *list;
+ struct string_list list2 = STRING_LIST_INIT_DUP;
+ struct string_list list3 = STRING_LIST_INIT_NODUP;
+ struct string_list list4 = STRING_LIST_INIT_NODUP;
+ struct string_list list5 = STRING_LIST_INIT_DUP;
ALLOC_ARRAY(list, 1);
+
+ /* Exclude these */
+ string_list_append(&list4, "str");
+ string_list_append_nodup(&list5, xstrdup("str"));
+
string_list_init_dup(list);
+ list4.strdup_strings = 1;
+ list5.strdup_strings = 0;
}
--
2.37.1.1095.g64a1e8362fd
next prev parent reply other threads:[~2022-07-21 12:01 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-21 6:39 [PATCH 0/2] string_list API users: use alloc + init, not calloc + strdup_strings Ævar Arnfjörð Bjarmason
2022-07-21 6:39 ` [PATCH 1/2] string_list API users + cocci: use string_list_init_dup() Ævar Arnfjörð Bjarmason
2022-07-21 6:39 ` [PATCH 2/2] string-list API users: manually use string_list_init_*() Ævar Arnfjörð Bjarmason
2022-07-21 7:48 ` Elijah Newren
2022-07-21 12:00 ` [PATCH v2 0/6] string-list API user: fix API use, some with coccinelle Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 1/6] string_list API users + cocci: use string_list_init_dup() Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 2/6] cocci: apply string_list.cocci with --disable-worth-trying-opt Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 3/6] reflog-walk.c: use string_list_init_dup() Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` Ævar Arnfjörð Bjarmason [this message]
2022-07-21 12:00 ` [PATCH v2 5/6] string-list API users: don't tweak "strdup_strings" to free dupes Ævar Arnfjörð Bjarmason
2022-07-21 12:00 ` [PATCH v2 6/6] notes.c: make "struct string_list display_notes_refs" non-static Ævar Arnfjörð Bjarmason
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=patch-v2-4.6-2d858c49243-20220721T111808Z-avarab@gmail.com \
--to=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=newren@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).