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>
This commit is contained in:
René Scharfe 2019-06-15 20:32:58 +02:00 коммит произвёл Junio C Hamano
Родитель b697d92f56
Коммит 177fbab747
1 изменённых файлов: 46 добавлений и 15 удалений

Просмотреть файл

@ -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;