aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/coccinelle
diff options
context:
space:
mode:
authorPeter Senna Tschudin <peter.senna@gmail.com>2013-01-23 17:06:30 -0500
committerMichal Marek <mmarek@suse.cz>2013-02-22 05:22:18 -0500
commitff3771cb717fd532d97f354cd169fd10da0d0339 (patch)
treec058d3e3266ac1a3db6bc782cb07012d969c253a /scripts/coccinelle
parente0367a61567de103b1946bf87115e15253194195 (diff)
scripts/coccinelle/misc/memcpy-assign.cocci: Replace memcpy with struct assignment
There are error-prone memcpy() that can be replaced by struct assignment that are type-safe and much easier to read. This semantic patch looks for memcpy() that can be replaced by struct assignment. Inspired by patches sent by Ezequiel Garcia <elezegarcia@gmail.com> Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com> Signed-off-by: Michal Marek <mmarek@suse.cz>
Diffstat (limited to 'scripts/coccinelle')
-rw-r--r--scripts/coccinelle/misc/memcpy-assign.cocci103
1 files changed, 103 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/memcpy-assign.cocci b/scripts/coccinelle/misc/memcpy-assign.cocci
new file mode 100644
index 000000000000..afd058be497f
--- /dev/null
+++ b/scripts/coccinelle/misc/memcpy-assign.cocci
@@ -0,0 +1,103 @@
1//
2// Replace memcpy with struct assignment.
3//
4// Confidence: High
5// Copyright: (C) 2012 Peter Senna Tschudin, INRIA/LIP6. GPLv2.
6// URL: http://coccinelle.lip6.fr/
7// Comments:
8// Options: --no-includes --include-headers
9
10virtual patch
11virtual report
12virtual context
13virtual org
14
15@r1 depends on !patch@
16identifier struct_name;
17struct struct_name to;
18struct struct_name from;
19struct struct_name *top;
20struct struct_name *fromp;
21position p;
22@@
23memcpy@p(\(&(to)\|top\), \(&(from)\|fromp\), \(sizeof(to)\|sizeof(from)\|sizeof(struct struct_name)\|sizeof(*top)\|sizeof(*fromp)\))
24
25@script:python depends on report@
26p << r1.p;
27@@
28coccilib.report.print_report(p[0],"Replace memcpy with struct assignment")
29
30@depends on context@
31position r1.p;
32@@
33*memcpy@p(...);
34
35@script:python depends on org@
36p << r1.p;
37@@
38cocci.print_main("Replace memcpy with struct assignment",p)
39
40@depends on patch@
41identifier struct_name;
42struct struct_name to;
43struct struct_name from;
44@@
45(
46-memcpy(&(to), &(from), sizeof(to));
47+to = from;
48|
49-memcpy(&(to), &(from), sizeof(from));
50+to = from;
51|
52-memcpy(&(to), &(from), sizeof(struct struct_name));
53+to = from;
54)
55
56@depends on patch@
57identifier struct_name;
58struct struct_name to;
59struct struct_name *from;
60@@
61(
62-memcpy(&(to), from, sizeof(to));
63+to = *from;
64|
65-memcpy(&(to), from, sizeof(*from));
66+to = *from;
67|
68-memcpy(&(to), from, sizeof(struct struct_name));
69+to = *from;
70)
71
72@depends on patch@
73identifier struct_name;
74struct struct_name *to;
75struct struct_name from;
76@@
77(
78-memcpy(to, &(from), sizeof(*to));
79+ *to = from;
80|
81-memcpy(to, &(from), sizeof(from));
82+ *to = from;
83|
84-memcpy(to, &(from), sizeof(struct struct_name));
85+ *to = from;
86)
87
88@depends on patch@
89identifier struct_name;
90struct struct_name *to;
91struct struct_name *from;
92@@
93(
94-memcpy(to, from, sizeof(*to));
95+ *to = *from;
96|
97-memcpy(to, from, sizeof(*from));
98+ *to = *from;
99|
100-memcpy(to, from, sizeof(struct struct_name));
101+ *to = *from;
102)
103