Skip to content

Commit 35d803b

Browse files
rscharfegitster
authored andcommitted
use SWAP macro
Apply the semantic patch swap.cocci to convert hand-rolled swaps to use the macro SWAP. The resulting code is shorter and easier to read, the object code is effectively unchanged. The patch for object.c had to be hand-edited in order to preserve the comment before the change; Coccinelle tried to eat it for some reason. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent db10199 commit 35d803b

11 files changed

Lines changed: 15 additions & 39 deletions

File tree

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 &&

diff-no-index.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ static int queue_diff(struct diff_options *o,
186186

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

194193
d1 = noindex_filespec(name1, mode1);

diff.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5118,13 +5118,11 @@ void diff_change(struct diff_options *options,
51185118

51195119
if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
51205120
unsigned tmp;
5121-
const unsigned char *tmp_c;
5122-
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
5123-
tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
5121+
SWAP(old_mode, new_mode);
5122+
SWAP(old_sha1, new_sha1);
51245123
tmp = old_sha1_valid; old_sha1_valid = new_sha1_valid;
51255124
new_sha1_valid = tmp;
5126-
tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule;
5127-
new_dirty_submodule = tmp;
5125+
SWAP(old_dirty_submodule, new_dirty_submodule);
51285126
}
51295127

51305128
if (options->prefix &&

graph.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf
997997
static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
998998
{
999999
int i;
1000-
int *tmp_mapping;
10011000
short used_horizontal = 0;
10021001
int horizontal_edge = -1;
10031002
int horizontal_edge_target = -1;
@@ -1132,9 +1131,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
11321131
/*
11331132
* Swap mapping and new_mapping
11341133
*/
1135-
tmp_mapping = graph->mapping;
1136-
graph->mapping = graph->new_mapping;
1137-
graph->new_mapping = tmp_mapping;
1134+
SWAP(graph->mapping, graph->new_mapping);
11381135

11391136
/*
11401137
* 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)

object.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ struct object *lookup_object(const unsigned char *sha1)
104104
* that we do not need to walk the hash table the next
105105
* time we look for it.
106106
*/
107-
struct object *tmp = obj_hash[i];
108-
obj_hash[i] = obj_hash[first];
109-
obj_hash[first] = tmp;
107+
SWAP(obj_hash[i], obj_hash[first]);
110108
}
111109
return obj;
112110
}

pack-revindex.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
5959
* be a no-op, as everybody lands in the same zero-th bucket.
6060
*/
6161
for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
62-
struct revindex_entry *swap;
6362
unsigned i;
6463

6564
memset(pos, 0, BUCKETS * sizeof(*pos));
@@ -97,9 +96,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
9796
* Now "to" contains the most sorted list, so we swap "from" and
9897
* "to" for the next iteration.
9998
*/
100-
swap = from;
101-
from = to;
102-
to = swap;
99+
SWAP(from, to);
103100
}
104101

105102
/*

prio-queue.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ static inline int compare(struct prio_queue *queue, int i, int j)
1212

1313
static inline void swap(struct prio_queue *queue, int i, int j)
1414
{
15-
struct prio_queue_entry tmp = queue->array[i];
16-
queue->array[i] = queue->array[j];
17-
queue->array[j] = tmp;
15+
SWAP(queue->array[i], queue->array[j]);
1816
}
1917

2018
void prio_queue_reverse(struct prio_queue *queue)

0 commit comments

Comments
 (0)