Skip to content

Commit 177fbab

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b697d92 commit 177fbab

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

contrib/coccinelle/array.cocci

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
@@
2-
type T;
3-
T *dst;
4-
T *src;
5-
expression n;
2+
expression dst, src, n, E;
63
@@
7-
- memcpy(dst, src, (n) * sizeof(*dst));
8-
+ COPY_ARRAY(dst, src, n);
4+
memcpy(dst, src, n * sizeof(
5+
- E[...]
6+
+ *(E)
7+
))
98

109
@@
1110
type T;
12-
T *dst;
13-
T *src;
14-
expression n;
11+
T *ptr;
12+
T[] arr;
13+
expression E, n;
1514
@@
16-
- memcpy(dst, src, (n) * sizeof(*src));
17-
+ COPY_ARRAY(dst, src, n);
15+
(
16+
memcpy(ptr, E,
17+
- n * sizeof(*(ptr))
18+
+ n * sizeof(T)
19+
)
20+
|
21+
memcpy(arr, E,
22+
- n * sizeof(*(arr))
23+
+ n * sizeof(T)
24+
)
25+
|
26+
memcpy(E, ptr,
27+
- n * sizeof(*(ptr))
28+
+ n * sizeof(T)
29+
)
30+
|
31+
memcpy(E, arr,
32+
- n * sizeof(*(arr))
33+
+ n * sizeof(T)
34+
)
35+
)
1836

1937
@@
2038
type T;
21-
T *dst;
22-
T *src;
39+
T *dst_ptr;
40+
T *src_ptr;
41+
T[] dst_arr;
42+
T[] src_arr;
2343
expression n;
2444
@@
25-
- memcpy(dst, src, (n) * sizeof(T));
26-
+ COPY_ARRAY(dst, src, n);
45+
(
46+
- memcpy(dst_ptr, src_ptr, (n) * sizeof(T))
47+
+ COPY_ARRAY(dst_ptr, src_ptr, n)
48+
|
49+
- memcpy(dst_ptr, src_arr, (n) * sizeof(T))
50+
+ COPY_ARRAY(dst_ptr, src_arr, n)
51+
|
52+
- memcpy(dst_arr, src_ptr, (n) * sizeof(T))
53+
+ COPY_ARRAY(dst_arr, src_ptr, n)
54+
|
55+
- memcpy(dst_arr, src_arr, (n) * sizeof(T))
56+
+ COPY_ARRAY(dst_arr, src_arr, n)
57+
)
2758

2859
@@
2960
type T;

0 commit comments

Comments
 (0)