use DUP_ARRAY

Add a semantic patch for replace ALLOC_ARRAY+COPY_ARRAY with DUP_ARRAY to reduce code duplication and apply its results. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 1, 2023 at 22:16 UTC 6e578410960d9ceb35ec98ad4b6fc711f1a9c85c
8 files changed +15 -16
attr.c
+1 -2
@@ -599,8 +599,7 @@ struct attr_check *attr_check_dup(const struct attr_check *check)
599
600 ret->nr = check->nr;
601 ret->alloc = check->alloc;
602 - ALLOC_ARRAY(ret->items, ret->nr);
603 - COPY_ARRAY(ret->items, check->items, ret->nr);
602 + DUP_ARRAY(ret->items, check->items, ret->nr);
603
604 return ret;
605 }
builtin/am.c
+1 -2
@@ -1489,8 +1489,7 @@ static int run_apply(const struct am_state *state, const char *index_file)
1489 * apply_opts.v keeps referencing the allocated strings for
1490 * strvec_clear() to release.
1491 */
1492 - ALLOC_ARRAY(apply_argv, apply_opts.nr);
1493 - COPY_ARRAY(apply_argv, apply_opts.v, apply_opts.nr);
1492 + DUP_ARRAY(apply_argv, apply_opts.v, apply_opts.nr);
1493
1494 opts_left = apply_parse_options(apply_opts.nr, apply_argv,
1495 &apply_state, &force_apply, &options,
commit-graph.c
+1 -2
@@ -1594,8 +1594,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1594 _("Computing commit changed paths Bloom filters"),
1595 ctx->commits.nr);
1596
1597 - ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1598 - COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1597 + DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1598
1599 if (ctx->order_by_pack)
1600 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
commit-reach.c
+1 -2
@@ -245,8 +245,7 @@ static int remove_redundant_with_gen(struct repository *r,
245 * min_gen_pos points to the current position within 'array'
246 * that is not yet known to be STALE.
247 */
248 - ALLOC_ARRAY(sorted, cnt);
249 - COPY_ARRAY(sorted, array, cnt);
248 + DUP_ARRAY(sorted, array, cnt);
249 QSORT(sorted, cnt, compare_commits_by_gen);
250 min_generation = commit_graph_generation(sorted[0]);
251
compat/mingw.c
+1 -2
@@ -1396,8 +1396,7 @@ static wchar_t *make_environment_block(char **deltaenv)
1396 p += s;
1397 }
1398
1399 - ALLOC_ARRAY(result, size);
1400 - COPY_ARRAY(result, wenv, size);
1399 + DUP_ARRAY(result, wenv, size);
1400 FreeEnvironmentStringsW(wenv);
1401 return result;
1402 }
contrib/coccinelle/array.cocci
+7
@@ -94,3 +94,10 @@ expression n != 1;
94 @@
95 - ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) )
96 + CALLOC_ARRAY(ptr, n)
97 +
98 +@@
99 +expression dst, src, n;
100 +@@
101 +-ALLOC_ARRAY(dst, n);
102 +-COPY_ARRAY(dst, src, n);
103 ++DUP_ARRAY(dst, src, n);
parse-options.c
+1 -2
@@ -702,8 +702,7 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
702 if (!nr_aliases)
703 return NULL;
704
705 - ALLOC_ARRAY(newopt, nr + 1);
706 - COPY_ARRAY(newopt, options, nr + 1);
705 + DUP_ARRAY(newopt, options, nr + 1);
706
707 /* each alias has two string pointers and NULL */
708 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
pathspec.c
+2 -4
@@ -681,8 +681,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
681 int i, j;
682
683 *dst = *src;
684 - ALLOC_ARRAY(dst->items, dst->nr);
685 - COPY_ARRAY(dst->items, src->items, dst->nr);
684 + DUP_ARRAY(dst->items, src->items, dst->nr);
685
686 for (i = 0; i < dst->nr; i++) {
687 struct pathspec_item *d = &dst->items[i];
@@ -691,8 +690,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
690 d->match = xstrdup(s->match);
691 d->original = xstrdup(s->original);
692
694 - ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
695 - COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
693 + DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
694 for (j = 0; j < d->attr_match_nr; j++) {
695 const char *value = s->attr_match[j].value;
696 d->attr_match[j].value = xstrdup_or_null(value);