Skip to content

Commit 6e57841

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d2ec87a commit 6e57841

File tree

8 files changed

+15
-16
lines changed

8 files changed

+15
-16
lines changed

attr.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ struct attr_check *attr_check_dup(const struct attr_check *check)
599599

600600
ret->nr = check->nr;
601601
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);
604603

605604
return ret;
606605
}

builtin/am.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1489,8 +1489,7 @@ static int run_apply(const struct am_state *state, const char *index_file)
14891489
* apply_opts.v keeps referencing the allocated strings for
14901490
* strvec_clear() to release.
14911491
*/
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);
14941493

14951494
opts_left = apply_parse_options(apply_opts.nr, apply_argv,
14961495
&apply_state, &force_apply, &options,

commit-graph.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx)
15941594
_("Computing commit changed paths Bloom filters"),
15951595
ctx->commits.nr);
15961596

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);
15991598

16001599
if (ctx->order_by_pack)
16011600
QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);

commit-reach.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ static int remove_redundant_with_gen(struct repository *r,
245245
* min_gen_pos points to the current position within 'array'
246246
* that is not yet known to be STALE.
247247
*/
248-
ALLOC_ARRAY(sorted, cnt);
249-
COPY_ARRAY(sorted, array, cnt);
248+
DUP_ARRAY(sorted, array, cnt);
250249
QSORT(sorted, cnt, compare_commits_by_gen);
251250
min_generation = commit_graph_generation(sorted[0]);
252251

compat/mingw.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1396,8 +1396,7 @@ static wchar_t *make_environment_block(char **deltaenv)
13961396
p += s;
13971397
}
13981398

1399-
ALLOC_ARRAY(result, size);
1400-
COPY_ARRAY(result, wenv, size);
1399+
DUP_ARRAY(result, wenv, size);
14011400
FreeEnvironmentStringsW(wenv);
14021401
return result;
14031402
}

contrib/coccinelle/array.cocci

+7
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@ expression n != 1;
9494
@@
9595
- ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) )
9696
+ 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
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,7 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
702702
if (!nr_aliases)
703703
return NULL;
704704

705-
ALLOC_ARRAY(newopt, nr + 1);
706-
COPY_ARRAY(newopt, options, nr + 1);
705+
DUP_ARRAY(newopt, options, nr + 1);
707706

708707
/* each alias has two string pointers and NULL */
709708
CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));

pathspec.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
681681
int i, j;
682682

683683
*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);
686685

687686
for (i = 0; i < dst->nr; i++) {
688687
struct pathspec_item *d = &dst->items[i];
@@ -691,8 +690,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
691690
d->match = xstrdup(s->match);
692691
d->original = xstrdup(s->original);
693692

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);
696694
for (j = 0; j < d->attr_match_nr; j++) {
697695
const char *value = s->attr_match[j].value;
698696
d->attr_match[j].value = xstrdup_or_null(value);

0 commit comments

Comments
 (0)