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
This commit is contained in:
Junio C Hamano 2017-02-15 12:54:19 -08:00
commit cbf1860d73
14 changed files with 63 additions and 65 deletions

23
apply.c
View File

@ -2187,29 +2187,20 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
return offset + hdrsize + patchsize; return offset + hdrsize + patchsize;
} }
#define swap(a,b) myswap((a),(b),sizeof(a))
#define myswap(a, b, size) do { \
unsigned char mytmp[size]; \
memcpy(mytmp, &a, size); \
memcpy(&a, &b, size); \
memcpy(&b, mytmp, size); \
} while (0)
static void reverse_patches(struct patch *p) static void reverse_patches(struct patch *p)
{ {
for (; p; p = p->next) { for (; p; p = p->next) {
struct fragment *frag = p->fragments; struct fragment *frag = p->fragments;
swap(p->new_name, p->old_name); SWAP(p->new_name, p->old_name);
swap(p->new_mode, p->old_mode); SWAP(p->new_mode, p->old_mode);
swap(p->is_new, p->is_delete); SWAP(p->is_new, p->is_delete);
swap(p->lines_added, p->lines_deleted); SWAP(p->lines_added, p->lines_deleted);
swap(p->old_sha1_prefix, p->new_sha1_prefix); SWAP(p->old_sha1_prefix, p->new_sha1_prefix);
for (; frag; frag = frag->next) { for (; frag; frag = frag->next) {
swap(frag->newpos, frag->oldpos); SWAP(frag->newpos, frag->oldpos);
swap(frag->newlines, frag->oldlines); SWAP(frag->newlines, frag->oldlines);
} }
} }
} }

View File

@ -147,9 +147,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
tree1 = opt->pending.objects[0].item; tree1 = opt->pending.objects[0].item;
tree2 = opt->pending.objects[1].item; tree2 = opt->pending.objects[1].item;
if (tree2->flags & UNINTERESTING) { if (tree2->flags & UNINTERESTING) {
struct object *tmp = tree2; SWAP(tree2, tree1);
tree2 = tree1;
tree1 = tmp;
} }
diff_tree_sha1(tree1->oid.hash, diff_tree_sha1(tree1->oid.hash,
tree2->oid.hash, tree2->oid.hash,

View File

@ -45,12 +45,9 @@ static void stuff_change(struct diff_options *opt,
return; return;
if (DIFF_OPT_TST(opt, REVERSE_DIFF)) { if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
unsigned tmp; SWAP(old_mode, new_mode);
const unsigned char *tmp_u; SWAP(old_sha1, new_sha1);
const char *tmp_c; SWAP(old_name, new_name);
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
tmp_c = old_name; old_name = new_name; new_name = tmp_c;
} }
if (opt->prefix && if (opt->prefix &&

View File

@ -0,0 +1,28 @@
@ swap_with_declaration @
type T;
identifier tmp;
T a, b;
@@
- T tmp = a;
+ T tmp;
+ tmp = a;
a = b;
b = tmp;
@ swap @
type T;
T tmp, a, b;
@@
- tmp = a;
- a = b;
- b = tmp;
+ SWAP(a, b);
@ extends swap @
identifier unused;
@@
{
...
- T unused;
... when != unused
}

View File

@ -185,10 +185,8 @@ static int queue_diff(struct diff_options *o,
struct diff_filespec *d1, *d2; struct diff_filespec *d1, *d2;
if (DIFF_OPT_TST(o, REVERSE_DIFF)) { if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
unsigned tmp; SWAP(mode1, mode2);
const char *tmp_c; SWAP(name1, name2);
tmp = mode1; mode1 = mode2; mode2 = tmp;
tmp_c = name1; name1 = name2; name2 = tmp_c;
} }
d1 = noindex_filespec(name1, mode1); d1 = noindex_filespec(name1, mode1);

12
diff.c
View File

@ -5118,14 +5118,10 @@ void diff_change(struct diff_options *options,
return; return;
if (DIFF_OPT_TST(options, REVERSE_DIFF)) { if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
unsigned tmp; SWAP(old_mode, new_mode);
const unsigned char *tmp_c; SWAP(old_sha1, new_sha1);
tmp = old_mode; old_mode = new_mode; new_mode = tmp; SWAP(old_sha1_valid, new_sha1_valid);
tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; SWAP(old_dirty_submodule, new_dirty_submodule);
tmp = old_sha1_valid; old_sha1_valid = new_sha1_valid;
new_sha1_valid = tmp;
tmp = old_dirty_submodule; old_dirty_submodule = new_dirty_submodule;
new_dirty_submodule = tmp;
} }
if (options->prefix && if (options->prefix &&

View File

@ -527,6 +527,16 @@ static inline int ends_with(const char *str, const char *suffix)
return strip_suffix(str, suffix, &len); return strip_suffix(str, suffix, &len);
} }
#define SWAP(a, b) do { \
void *_swap_a_ptr = &(a); \
void *_swap_b_ptr = &(b); \
unsigned char _swap_buffer[sizeof(a)]; \
memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
} while (0)
#if defined(NO_MMAP) || defined(USE_WIN32_MMAP) #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
#ifndef PROT_READ #ifndef PROT_READ

11
graph.c
View File

@ -497,7 +497,6 @@ static void graph_update_width(struct git_graph *graph,
static void graph_update_columns(struct git_graph *graph) static void graph_update_columns(struct git_graph *graph)
{ {
struct commit_list *parent; struct commit_list *parent;
struct column *tmp_columns;
int max_new_columns; int max_new_columns;
int mapping_idx; int mapping_idx;
int i, seen_this, is_commit_in_columns; int i, seen_this, is_commit_in_columns;
@ -510,11 +509,8 @@ static void graph_update_columns(struct git_graph *graph)
* We'll re-use the old columns array as storage to compute the new * We'll re-use the old columns array as storage to compute the new
* columns list for the commit after this one. * columns list for the commit after this one.
*/ */
tmp_columns = graph->columns; SWAP(graph->columns, graph->new_columns);
graph->columns = graph->new_columns;
graph->num_columns = graph->num_new_columns; graph->num_columns = graph->num_new_columns;
graph->new_columns = tmp_columns;
graph->num_new_columns = 0; graph->num_new_columns = 0;
/* /*
@ -1031,7 +1027,6 @@ static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf
static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb) static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
{ {
int i; int i;
int *tmp_mapping;
short used_horizontal = 0; short used_horizontal = 0;
int horizontal_edge = -1; int horizontal_edge = -1;
int horizontal_edge_target = -1; int horizontal_edge_target = -1;
@ -1166,9 +1161,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
/* /*
* Swap mapping and new_mapping * Swap mapping and new_mapping
*/ */
tmp_mapping = graph->mapping; SWAP(graph->mapping, graph->new_mapping);
graph->mapping = graph->new_mapping;
graph->new_mapping = tmp_mapping;
/* /*
* If graph->mapping indicates that all of the branch lines * If graph->mapping indicates that all of the branch lines

View File

@ -269,8 +269,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
return -1; return -1;
if (*begin && *end && *end < *begin) { if (*begin && *end && *end < *begin) {
long tmp; SWAP(*end, *begin);
tmp = *end; *end = *begin; *begin = tmp;
} }
return 0; return 0;

View File

@ -1390,14 +1390,11 @@ static int process_renames(struct merge_options *o,
branch1 = o->branch1; branch1 = o->branch1;
branch2 = o->branch2; branch2 = o->branch2;
} else { } else {
struct rename *tmp;
renames1 = b_renames; renames1 = b_renames;
renames2Dst = &a_by_dst; renames2Dst = &a_by_dst;
branch1 = o->branch2; branch1 = o->branch2;
branch2 = o->branch1; branch2 = o->branch1;
tmp = ren2; SWAP(ren2, ren1);
ren2 = ren1;
ren1 = tmp;
} }
if (ren1->processed) if (ren1->processed)

View File

@ -104,9 +104,7 @@ struct object *lookup_object(const unsigned char *sha1)
* that we do not need to walk the hash table the next * that we do not need to walk the hash table the next
* time we look for it. * time we look for it.
*/ */
struct object *tmp = obj_hash[i]; SWAP(obj_hash[i], obj_hash[first]);
obj_hash[i] = obj_hash[first];
obj_hash[first] = tmp;
} }
return obj; return obj;
} }

View File

@ -59,7 +59,6 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
* be a no-op, as everybody lands in the same zero-th bucket. * be a no-op, as everybody lands in the same zero-th bucket.
*/ */
for (bits = 0; max >> bits; bits += DIGIT_SIZE) { for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
struct revindex_entry *swap;
unsigned i; unsigned i;
memset(pos, 0, BUCKETS * sizeof(*pos)); memset(pos, 0, BUCKETS * sizeof(*pos));
@ -97,9 +96,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
* Now "to" contains the most sorted list, so we swap "from" and * Now "to" contains the most sorted list, so we swap "from" and
* "to" for the next iteration. * "to" for the next iteration.
*/ */
swap = from; SWAP(from, to);
from = to;
to = swap;
} }
/* /*

View File

@ -12,9 +12,7 @@ static inline int compare(struct prio_queue *queue, int i, int j)
static inline void swap(struct prio_queue *queue, int i, int j) static inline void swap(struct prio_queue *queue, int i, int j)
{ {
struct prio_queue_entry tmp = queue->array[i]; SWAP(queue->array[i], queue->array[j]);
queue->array[i] = queue->array[j];
queue->array[j] = tmp;
} }
void prio_queue_reverse(struct prio_queue *queue) void prio_queue_reverse(struct prio_queue *queue)

View File

@ -109,9 +109,7 @@ extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
*/ */
static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
{ {
struct strbuf tmp = *a; SWAP(*a, *b);
*a = *b;
*b = tmp;
} }