aboutsummaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2017-01-28 22:38:21 +0100
committerJunio C Hamano <gitster@pobox.com>2017-01-30 14:07:45 -0800
commit568edcb95a8b86ffd0d267b124896df8ea81307c (patch)
treee0701b31ed1e8e7d0a628757d84cd7f236060654 /contrib
parent4e59582ff70d299f5a88449891e78d15b4b3fabe (diff)
downloadgit-568edcb95a8b86ffd0d267b124896df8ea81307c.tar.gz
git-568edcb95a8b86ffd0d267b124896df8ea81307c.tar.xz
add SWAP macro
Add a macro for exchanging the values of variables. It allows users to avoid repetition and takes care of the temporary variable for them. It also makes sure that the storage sizes of its two parameters are the same. Its memcpy(1) calls are optimized away by current compilers. Also add a conservative semantic patch for transforming only swaps of variables of the same type. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'contrib')
-rw-r--r--contrib/coccinelle/swap.cocci28
1 files changed, 28 insertions, 0 deletions
diff --git a/contrib/coccinelle/swap.cocci b/contrib/coccinelle/swap.cocci
new file mode 100644
index 000000000..a0934d1fd
--- /dev/null
+++ b/contrib/coccinelle/swap.cocci
@@ -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
+ }