aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/coccinelle
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2019-06-15 20:32:58 +0200
committerJunio C Hamano <gitster@pobox.com>2019-06-17 18:14:59 -0700
commit177fbab747da4f58cb2a8ce010b3515c86dd67c9 (patch)
treef3936a6b4db9c110bef4bb4a28131ec6de06aa25 /contrib/coccinelle
parentb697d92f56511e804b8ba20ccbe7bdc85dc66810 (diff)
downloadgit-177fbab747da4f58cb2a8ce010b3515c86dd67c9.tar.gz
coccinelle: use COPY_ARRAY for copying arrays
The current semantic patch for COPY_ARRAY transforms memcpy(3) calls on pointers, but Coccinelle distinguishes them from arrays. It already contains three rules to handle the options for sizeof (i.e. source, destination and type), and handling arrays as source and destination would require four times as many rules if we enumerated all cases. We also don't handle array subscripts, and supporting that would increase the number of rules by another factor of four. (An isomorphism telling Coccinelle that "sizeof x[...]" is equivalent to "sizeof *x" would be nice..) Support arrays and array subscripts, but keep the number of rules down by adding normalization steps: First turn array subscripts into derefences, then determine the types of expressions used with sizeof and replace them with these types, and then convert the different possible combinations of arrays and pointers with memcpy(3) to COPY_ARRAY. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'contrib/coccinelle')
-rw-r--r--contrib/coccinelle/array.cocci61
1 files changed, 46 insertions, 15 deletions
diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci
index 01586821dc..46b8d2ee11 100644
--- a/contrib/coccinelle/array.cocci
+++ b/contrib/coccinelle/array.cocci
@@ -1,29 +1,60 @@
@@
-type T;
-T *dst;
-T *src;
-expression n;
+expression dst, src, n, E;
@@
-- memcpy(dst, src, (n) * sizeof(*dst));
-+ COPY_ARRAY(dst, src, n);
+ memcpy(dst, src, n * sizeof(
+- E[...]
++ *(E)
+ ))
@@
type T;
-T *dst;
-T *src;
-expression n;
+T *ptr;
+T[] arr;
+expression E, n;
@@
-- memcpy(dst, src, (n) * sizeof(*src));
-+ COPY_ARRAY(dst, src, n);
+(
+ memcpy(ptr, E,
+- n * sizeof(*(ptr))
++ n * sizeof(T)
+ )
+|
+ memcpy(arr, E,
+- n * sizeof(*(arr))
++ n * sizeof(T)
+ )
+|
+ memcpy(E, ptr,
+- n * sizeof(*(ptr))
++ n * sizeof(T)
+ )
+|
+ memcpy(E, arr,
+- n * sizeof(*(arr))
++ n * sizeof(T)
+ )
+)
@@
type T;
-T *dst;
-T *src;
+T *dst_ptr;
+T *src_ptr;
+T[] dst_arr;
+T[] src_arr;
expression n;
@@
-- memcpy(dst, src, (n) * sizeof(T));
-+ COPY_ARRAY(dst, src, n);
+(
+- memcpy(dst_ptr, src_ptr, (n) * sizeof(T))
++ COPY_ARRAY(dst_ptr, src_ptr, n)
+|
+- memcpy(dst_ptr, src_arr, (n) * sizeof(T))
++ COPY_ARRAY(dst_ptr, src_arr, n)
+|
+- memcpy(dst_arr, src_ptr, (n) * sizeof(T))
++ COPY_ARRAY(dst_arr, src_ptr, n)
+|
+- memcpy(dst_arr, src_arr, (n) * sizeof(T))
++ COPY_ARRAY(dst_arr, src_arr, n)
+)
@@
type T;