xref: /linux/scripts/coccinelle/api/alloc/alloc_cast.cocci (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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
16virtual context
17virtual patch
18virtual org
19virtual report
20
21//----------------------------------------------------------
22//  For context mode
23//----------------------------------------------------------
24
25@depends on context@
26type 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@
38type 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@
50type T;
51position 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@
58p << r.p;
59t << r.T;
60@@
61
62coccilib.org.print_safe_todo(p[0], t)
63
64@script:python depends on report@
65p << r.p;
66t << r.T;
67@@
68
69msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
70coccilib.report.print_report(p[0], msg)
71
72
73