Skip to content

Commit cbf1860

Browse files
committed
Merge branch 'rs/swap'
Code clean-up. * rs/swap: graph: use SWAP macro diff: use SWAP macro use SWAP macro apply: use SWAP macro add SWAP macro
2 parents 2f4e87d + 9e2edd6 commit cbf1860

14 files changed

Lines changed: 63 additions & 65 deletions

File tree

apply.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,29 +2187,20 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
21872187
return offset + hdrsize + patchsize;
21882188
}
21892189

2190-
#define swap(a,b) myswap((a),(b),sizeof(a))
2191-
2192-
#define myswap(a, b, size) do { \
2193-
unsigned char mytmp[size]; \
2194-
memcpy(mytmp, &a, size); \
2195-
memcpy(&a, &b, size); \
2196-
memcpy(&b, mytmp, size); \
2197-
} while (0)
2198-
21992190
static void reverse_patches(struct patch *p)
22002191
{
22012192
for (; p; p = p->next) {
22022193
struct fragment *frag = p->fragments;
22032194

2204-
swap(p->new_name, p->old_name);
2205-
swap(p->new_mode, p->old_mode);
2206-
swap(p->is_new, p->is_delete);
2207-
swap(p->lines_added, p->lines_deleted);
2208-
swap(p->old_sha1_prefix, p->new_sha1_prefix);
2195+
SWAP(p->new_name, p->old_name);
2196+
SWAP(p->new_mode, p->old_mode);
2197+
SWAP(p->is_new, p->is_delete);
2198+
SWAP(p->lines_added, p->lines_deleted);
2199+
SWAP(p->old_sha1_prefix, p->new_sha1_prefix);
22092200

22102201
for (; frag; frag = frag->next) {
2211-
swap(frag->newpos, frag->oldpos);
2212-
swap(frag->newlines, frag->oldlines);
2202+
SWAP(frag->newpos, frag->oldpos);
2203+
SWAP(frag->newlines, frag->oldlines);
22132204
}
22142205
}
22152206
}

builtin/diff-tree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
147147
tree1 = opt->pending.objects[0].item;
148148
tree2 = opt->pending.objects[1].item;
149149
if (tree2->flags & UNINTERESTING) {
150-
struct object *tmp = tree2;
151-
tree2 = tree1;
152-
tree1 = tmp;
150+
SWAP(tree2, tree1);
153151
}
154152
diff_tree_sha1(tree1->oid.hash,
155153
tree2->oid.hash,

builtin/diff.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ static void stuff_change(struct diff_options *opt,
4545
return;
4646

4747
if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
48-
unsigned tmp;
49-
const unsigned char *tmp_u;
50-
const char *tmp_c;
51-
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
52-
tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
53-
tmp_c = old_name; old_name = new_name; new_name = tmp_c;
48+
SWAP(old_mode, new_mode);
49+
SWAP(old_sha1, new_sha1);
50+
SWAP(old_name, new_name);
5451
}
5552

5653
if (opt->prefix &&

contrib/coccinelle/swap.cocci

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@ swap_with_declaration @
2+
type T;
3+
identifier tmp;
4+
T a, b;
5+
@@
6+
- T tmp = a;
7+
+ T tmp;
8+
+ tmp = a;
9+
a = b;
10+
b = tmp;
11+
12+
@ swap @
13+
type T;
14+
T tmp, a, b;
15+
@@
16+
- tmp = a;
17+
- a = b;
18+
- b = tmp;
19+
+ SWAP(a, b);
20+
21+
@ extends swap @
22+
identifier unused;
23+
@@
24+
{
25+
...
26+
- T unused;
27+
... when != unused
28+
}

diff-no-index.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,8 @@ static int queue_diff(struct diff_options *o,
185185
struct diff_filespec *d1, *d2;
186186

187187
if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
188-
unsigned tmp;
189-
const char *tmp_c;
190-
tmp = mode1; mode1 = mode2; mode2 = tmp;
191-
tmp_c = name1; name1 = name2; name2 = tmp_c;
188+
SWAP(mode1, mode2);
189+
SWAP(name1, name2);
192190
}
193191

194192
d1 = noindex_filespec(name1, mode1);

diff.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5118,14 +5118,10 @@ void diff_change(struct diff_options *options,
51185118
return;
51195119

51205120
if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
5121-
unsigned tmp;
5122-
const unsigned char *tmp_c;
5123-
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
5124-
tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
5125-
tmp = old_sha1_valid; old_sha1_valid = new_sha1_valid;
5126-
new_sha1_valid = tmp;
5127-
tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule;
5128-
new_dirty_submodule = tmp;
5121+
SWAP(old_mode, new_mode);
5122+
SWAP(old_sha1, new_sha1);
5123+
SWAP(old_sha1_valid, new_sha1_valid);
5124+
SWAP(old_dirty_submodule, new_dirty_submodule);
51295125
}
51305126

51315127
if (options->prefix &&

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ static inline int ends_with(const char *str, const char *suffix)
527527
return strip_suffix(str, suffix, &len);
528528
}
529529

530+
#define SWAP(a, b) do { \
531+
void *_swap_a_ptr = &(a); \
532+
void *_swap_b_ptr = &(b); \
533+
unsigned char _swap_buffer[sizeof(a)]; \
534+
memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
535+
memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
536+
BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
537+
memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
538+
} while (0)
539+
530540
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
531541

532542
#ifndef PROT_READ

graph.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ static void graph_update_width(struct git_graph *graph,
497497
static void graph_update_columns(struct git_graph *graph)
498498
{
499499
struct commit_list *parent;
500-
struct column *tmp_columns;
501500
int max_new_columns;
502501
int mapping_idx;
503502
int i, seen_this, is_commit_in_columns;
@@ -510,11 +509,8 @@ static void graph_update_columns(struct git_graph *graph)
510509
* We'll re-use the old columns array as storage to compute the new
511510
* columns list for the commit after this one.
512511
*/
513-
tmp_columns = graph->columns;
514-
graph->columns = graph->new_columns;
512+
SWAP(graph->columns, graph->new_columns);
515513
graph->num_columns = graph->num_new_columns;
516-
517-
graph->new_columns = tmp_columns;
518514
graph->num_new_columns = 0;
519515

520516
/*
@@ -1031,7 +1027,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf
10311027
static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
10321028
{
10331029
int i;
1034-
int *tmp_mapping;
10351030
short used_horizontal = 0;
10361031
int horizontal_edge = -1;
10371032
int horizontal_edge_target = -1;
@@ -1166,9 +1161,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
11661161
/*
11671162
* Swap mapping and new_mapping
11681163
*/
1169-
tmp_mapping = graph->mapping;
1170-
graph->mapping = graph->new_mapping;
1171-
graph->new_mapping = tmp_mapping;
1164+
SWAP(graph->mapping, graph->new_mapping);
11721165

11731166
/*
11741167
* If graph->mapping indicates that all of the branch lines

line-range.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
269269
return -1;
270270

271271
if (*begin && *end && *end < *begin) {
272-
long tmp;
273-
tmp = *end; *end = *begin; *begin = tmp;
272+
SWAP(*end, *begin);
274273
}
275274

276275
return 0;

merge-recursive.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,14 +1390,11 @@ static int process_renames(struct merge_options *o,
13901390
branch1 = o->branch1;
13911391
branch2 = o->branch2;
13921392
} else {
1393-
struct rename *tmp;
13941393
renames1 = b_renames;
13951394
renames2Dst = &a_by_dst;
13961395
branch1 = o->branch2;
13971396
branch2 = o->branch1;
1398-
tmp = ren2;
1399-
ren2 = ren1;
1400-
ren1 = tmp;
1397+
SWAP(ren2, ren1);
14011398
}
14021399

14031400
if (ren1->processed)

0 commit comments

Comments
 (0)