aboutsummaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2016-09-29 17:23:43 +0200
committerJunio C Hamano <gitster@pobox.com>2016-09-29 15:42:18 -0700
commitdbc540c7a5827529a3f58befc9e5b81a31ec8fab (patch)
treecd2c485c62e68411e418616c6ca26923e24b3e1b /contrib
parent21f862b498925194f8f1ebe8203b7a7df756555b (diff)
downloadgit-dbc540c7a5827529a3f58befc9e5b81a31ec8fab.tar.gz
git-dbc540c7a5827529a3f58befc9e5b81a31ec8fab.tar.xz
add QSORT
Add the macro QSORT, a convenient wrapper for qsort(3) that infers the size of the array elements and supports the convention of initializing empty arrays with a NULL pointer, which we use in some places. Calling qsort(3) directly with a NULL pointer is undefined -- even with an element count of zero -- and allows the compiler to optimize away any following NULL checks. Using the macro avoids such surprises. Add a semantic patch as well to demonstrate the macro's usage and to automate the transformation of trivial cases. 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/qsort.cocci19
1 files changed, 19 insertions, 0 deletions
diff --git a/contrib/coccinelle/qsort.cocci b/contrib/coccinelle/qsort.cocci
new file mode 100644
index 000000000..a094e7c5e
--- /dev/null
+++ b/contrib/coccinelle/qsort.cocci
@@ -0,0 +1,19 @@
+@@
+expression base, nmemb, compar;
+@@
+- qsort(base, nmemb, sizeof(*base), compar);
++ QSORT(base, nmemb, compar);
+
+@@
+expression base, nmemb, compar;
+@@
+- qsort(base, nmemb, sizeof(base[0]), compar);
++ QSORT(base, nmemb, compar);
+
+@@
+type T;
+T *base;
+expression nmemb, compar;
+@@
+- qsort(base, nmemb, sizeof(T), compar);
++ QSORT(base, nmemb, compar);