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>

René Scharfe committed Jan 28, 2017 at 22:40 UTC 35d803bc9a0d21c36b1381f6e42455beeb73b715
11 files changed +15 -39
builtin/diff-tree.c
+1 -3
@@ -147,9 +147,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
147 tree1 = opt->pending.objects[0].item;
148 tree2 = opt->pending.objects[1].item;
149 if (tree2->flags & UNINTERESTING) {
150 - struct object *tmp = tree2;
151 - tree2 = tree1;
152 - tree1 = tmp;
150 + SWAP(tree2, tree1);
151 }
152 diff_tree_sha1(tree1->oid.hash,
153 tree2->oid.hash,
builtin/diff.c
+3 -6
@@ -45,12 +45,9 @@ static void stuff_change(struct diff_options *opt,
45 return;
46
47 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);
51 }
52
53 if (opt->prefix &&
diff-no-index.c
+1 -2
@@ -186,9 +186,8 @@ static int queue_diff(struct diff_options *o,
186
187 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
188 unsigned tmp;
189 - const char *tmp_c;
189 tmp = mode1; mode1 = mode2; mode2 = tmp;
191 - tmp_c = name1; name1 = name2; name2 = tmp_c;
190 + SWAP(name1, name2);
191 }
192
193 d1 = noindex_filespec(name1, mode1);
diff.c
+3 -5
@@ -5118,13 +5118,11 @@ void diff_change(struct diff_options *options,
5118
5119 if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
5120 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);
5123 tmp = old_sha1_valid; old_sha1_valid = new_sha1_valid;
5124 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);
5126 }
5127
5128 if (options->prefix &&
graph.c
+1 -4
@@ -997,7 +997,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf
997 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
998 {
999 int i;
1000 - int *tmp_mapping;
1000 short used_horizontal = 0;
1001 int horizontal_edge = -1;
1002 int horizontal_edge_target = -1;
@@ -1132,9 +1131,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
1131 /*
1132 * Swap mapping and new_mapping
1133 */
1135 - tmp_mapping = graph->mapping;
1136 - graph->mapping = graph->new_mapping;
1137 - graph->new_mapping = tmp_mapping;
1134 + SWAP(graph->mapping, graph->new_mapping);
1135
1136 /*
1137 * If graph->mapping indicates that all of the branch lines
line-range.c
+1 -2
@@ -269,8 +269,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
269 return -1;
270
271 if (*begin && *end && *end < *begin) {
272 - long tmp;
273 - tmp = *end; *end = *begin; *begin = tmp;
272 + SWAP(*end, *begin);
273 }
274
275 return 0;
merge-recursive.c
+1 -4
@@ -1390,14 +1390,11 @@ static int process_renames(struct merge_options *o,
1390 branch1 = o->branch1;
1391 branch2 = o->branch2;
1392 } else {
1393 - struct rename *tmp;
1393 renames1 = b_renames;
1394 renames2Dst = &a_by_dst;
1395 branch1 = o->branch2;
1396 branch2 = o->branch1;
1398 - tmp = ren2;
1399 - ren2 = ren1;
1400 - ren1 = tmp;
1397 + SWAP(ren2, ren1);
1398 }
1399
1400 if (ren1->processed)
object.c
+1 -3
@@ -104,9 +104,7 @@ struct object *lookup_object(const unsigned char *sha1)
104 * that we do not need to walk the hash table the next
105 * time we look for it.
106 */
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]);
108 }
109 return obj;
110 }
pack-revindex.c
+1 -4
@@ -59,7 +59,6 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
59 * be a no-op, as everybody lands in the same zero-th bucket.
60 */
61 for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
62 - struct revindex_entry *swap;
62 unsigned i;
63
64 memset(pos, 0, BUCKETS * sizeof(*pos));
@@ -97,9 +96,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
96 * Now "to" contains the most sorted list, so we swap "from" and
97 * "to" for the next iteration.
98 */
100 - swap = from;
101 - from = to;
102 - to = swap;
99 + SWAP(from, to);
100 }
101
102 /*
prio-queue.c
+1 -3
@@ -12,9 +12,7 @@ static inline int compare(struct prio_queue *queue, int i, int j)
12
13 static inline void swap(struct prio_queue *queue, int i, int j)
14 {
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]);
16 }
17
18 void prio_queue_reverse(struct prio_queue *queue)
strbuf.h
+1 -3
@@ -109,9 +109,7 @@ extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
109 */
110 static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
111 {
112 - struct strbuf tmp = *a;
113 - *a = *b;
114 - *b = tmp;
112 + SWAP(*a, *b);
113 }
114
115