diff options
author | Himangi Saraogi <himangi774@gmail.com> | 2014-06-14 11:45:41 -0400 |
---|---|---|
committer | Michal Marek <mmarek@suse.cz> | 2014-08-06 06:10:17 -0400 |
commit | 99fcec30e882ec214b689889b3599cf98f5c9843 (patch) | |
tree | c267bff61b2ea6dccde7685f52d8fed4f4cd1074 /scripts/coccinelle | |
parent | b5889ab7d4e6ff194ff1ef8ad2eb9266a23e376b (diff) |
Coccinelle: Script to detect cast after memory allocation
This script detects cases of use of cast for the value returned by
kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc,
kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes
the cast as it is not useful. This Coccinelle script replaces
drop_kmalloc_cast.cocci as it removes the casting in more limited
cases of kmalloc, kzalloc and kcalloc.
Signed-off-by: Himangi Saraogi <himangi774@gmail.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Michal Marek <mmarek@suse.cz>
Diffstat (limited to 'scripts/coccinelle')
-rw-r--r-- | scripts/coccinelle/api/alloc/alloc_cast.cocci | 72 | ||||
-rw-r--r-- | scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci | 67 |
2 files changed, 72 insertions, 67 deletions
diff --git a/scripts/coccinelle/api/alloc/alloc_cast.cocci b/scripts/coccinelle/api/alloc/alloc_cast.cocci new file mode 100644 index 000000000000..6c308ee19b32 --- /dev/null +++ b/scripts/coccinelle/api/alloc/alloc_cast.cocci | |||
@@ -0,0 +1,72 @@ | |||
1 | /// Remove casting the values returned by memory allocation functions | ||
2 | /// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc. | ||
3 | /// | ||
4 | //# This makes an effort to find cases of casting of values returned by | ||
5 | //# kmalloc, kzalloc, kcalloc, kmem_cache_alloc, kmem_cache_zalloc, | ||
6 | //# kmem_cache_alloc_node, kmalloc_node and kzalloc_node and removes | ||
7 | //# the casting as it is not required. The result in the patch case may | ||
8 | //#need some reformatting. | ||
9 | // | ||
10 | // Confidence: High | ||
11 | // Copyright: 2014, Himangi Saraogi GPLv2. | ||
12 | // Comments: | ||
13 | // Options: --no-includes --include-headers | ||
14 | // | ||
15 | |||
16 | virtual context | ||
17 | virtual patch | ||
18 | virtual org | ||
19 | virtual report | ||
20 | |||
21 | //---------------------------------------------------------- | ||
22 | // For context mode | ||
23 | //---------------------------------------------------------- | ||
24 | |||
25 | @depends on context@ | ||
26 | type T; | ||
27 | @@ | ||
28 | |||
29 | * (T *) | ||
30 | \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\| | ||
31 | kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...) | ||
32 | |||
33 | //---------------------------------------------------------- | ||
34 | // For patch mode | ||
35 | //---------------------------------------------------------- | ||
36 | |||
37 | @depends on patch@ | ||
38 | type T; | ||
39 | @@ | ||
40 | |||
41 | - (T *) | ||
42 | (\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\| | ||
43 | kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...)) | ||
44 | |||
45 | //---------------------------------------------------------- | ||
46 | // For org and report mode | ||
47 | //---------------------------------------------------------- | ||
48 | |||
49 | @r depends on org || report@ | ||
50 | type T; | ||
51 | position p; | ||
52 | @@ | ||
53 | |||
54 | (T@p *)\(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\| | ||
55 | kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\)(...) | ||
56 | |||
57 | @script:python depends on org@ | ||
58 | p << r.p; | ||
59 | t << r.T; | ||
60 | @@ | ||
61 | |||
62 | coccilib.org.print_safe_todo(p[0], t) | ||
63 | |||
64 | @script:python depends on report@ | ||
65 | p << r.p; | ||
66 | t << r.T; | ||
67 | @@ | ||
68 | |||
69 | msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t) | ||
70 | coccilib.report.print_report(p[0], msg) | ||
71 | |||
72 | |||
diff --git a/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci b/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci deleted file mode 100644 index bd5d08b882ee..000000000000 --- a/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci +++ /dev/null | |||
@@ -1,67 +0,0 @@ | |||
1 | /// | ||
2 | /// Casting (void *) value returned by kmalloc is useless | ||
3 | /// as mentioned in Documentation/CodingStyle, Chap 14. | ||
4 | /// | ||
5 | // Confidence: High | ||
6 | // Copyright: 2009,2010 Nicolas Palix, DIKU. GPLv2. | ||
7 | // URL: http://coccinelle.lip6.fr/ | ||
8 | // Options: --no-includes --include-headers | ||
9 | // | ||
10 | // Keywords: kmalloc, kzalloc, kcalloc | ||
11 | // Version min: < 2.6.12 kmalloc | ||
12 | // Version min: < 2.6.12 kcalloc | ||
13 | // Version min: 2.6.14 kzalloc | ||
14 | // | ||
15 | |||
16 | virtual context | ||
17 | virtual patch | ||
18 | virtual org | ||
19 | virtual report | ||
20 | |||
21 | //---------------------------------------------------------- | ||
22 | // For context mode | ||
23 | //---------------------------------------------------------- | ||
24 | |||
25 | @depends on context@ | ||
26 | type T; | ||
27 | @@ | ||
28 | |||
29 | * (T *) | ||
30 | \(kmalloc\|kzalloc\|kcalloc\)(...) | ||
31 | |||
32 | //---------------------------------------------------------- | ||
33 | // For patch mode | ||
34 | //---------------------------------------------------------- | ||
35 | |||
36 | @depends on patch@ | ||
37 | type T; | ||
38 | @@ | ||
39 | |||
40 | - (T *) | ||
41 | \(kmalloc\|kzalloc\|kcalloc\)(...) | ||
42 | |||
43 | //---------------------------------------------------------- | ||
44 | // For org and report mode | ||
45 | //---------------------------------------------------------- | ||
46 | |||
47 | @r depends on org || report@ | ||
48 | type T; | ||
49 | position p; | ||
50 | @@ | ||
51 | |||
52 | (T@p *)\(kmalloc\|kzalloc\|kcalloc\)(...) | ||
53 | |||
54 | @script:python depends on org@ | ||
55 | p << r.p; | ||
56 | t << r.T; | ||
57 | @@ | ||
58 | |||
59 | coccilib.org.print_safe_todo(p[0], t) | ||
60 | |||
61 | @script:python depends on report@ | ||
62 | p << r.p; | ||
63 | t << r.T; | ||
64 | @@ | ||
65 | |||
66 | msg="WARNING: casting value returned by k[cmz]alloc to (%s *) is useless." % (t) | ||
67 | coccilib.report.print_report(p[0], msg) | ||