Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / scripts / coccinelle / misc / simple_return.cocci
1 /// Simplify a trivial if-return sequence.  Possibly combine with a
2 /// preceding function call.
3 //
4 // Confidence: High
5 // Copyright: (C) 2014 Julia Lawall, INRIA/LIP6.  GPLv2.
6 // Copyright: (C) 2014 Gilles Muller, INRIA/LiP6.  GPLv2.
7 // URL: http://coccinelle.lip6.fr/
8 // Comments:
9 // Options: --no-includes --include-headers
10
11 virtual patch
12 virtual context
13 virtual org
14 virtual report
15
16 @r depends on patch@
17 local idexpression e;
18 identifier i,f,fn;
19 @@
20
21 fn(...) { <...
22 - e@i =
23 + return
24     f(...);
25 -if (i != 0) return i;
26 -return 0;
27 ...> }
28
29 @depends on patch@
30 identifier r.i;
31 type t;
32 @@
33
34 -t i;
35  ... when != i
36
37 @depends on patch@
38 expression e;
39 @@
40
41 -if (e != 0)
42    return e;
43 -return 0;
44
45 // -----------------------------------------------------------------------
46
47 @s1 depends on context || org || report@
48 local idexpression e;
49 identifier i,f,fn;
50 position p,p1,p2;
51 @@
52
53 fn(...) { <...
54 * e@i@p = f(...);
55   if (\(i@p1 != 0\|i@p2 < 0\))
56      return i;
57   return 0;
58 ...> }
59
60 @s2 depends on context || org || report forall@
61 identifier s1.i;
62 type t;
63 position q,s1.p;
64 expression e,f;
65 @@
66
67 * t i@q;
68   ... when != i
69   e@p = f(...);
70
71 @s3 depends on context || org || report@
72 expression e;
73 position p1!=s1.p1;
74 position p2!=s1.p2;
75 @@
76
77 *if (\(e@p1 != 0\|e@p2 < 0\))
78    return e;
79  return 0;
80
81 // -----------------------------------------------------------------------
82
83 @script:python depends on org@
84 p << s1.p;
85 p1 << s1.p1;
86 q << s2.q;
87 @@
88
89 cocci.print_main("decl",q)
90 cocci.print_secs("use",p)
91 cocci.include_match(False)
92
93 @script:python depends on org@
94 p << s1.p;
95 p2 << s1.p2;
96 q << s2.q;
97 @@
98
99 cocci.print_main("decl",q)
100 cocci.print_secs("use with questionable test",p)
101 cocci.include_match(False)
102
103 @script:python depends on org@
104 p << s1.p;
105 p1 << s1.p1;
106 @@
107
108 cocci.print_main("use",p)
109
110 @script:python depends on org@
111 p << s1.p;
112 p2 << s1.p2;
113 @@
114
115 cocci.print_main("use with questionable test",p)
116
117 @script:python depends on org@
118 p << s3.p1;
119 @@
120
121 cocci.print_main("test",p)
122
123 @script:python depends on org@
124 p << s3.p2;
125 @@
126
127 cocci.print_main("questionable test",p)
128
129 // -----------------------------------------------------------------------
130
131 @script:python depends on report@
132 p << s1.p;
133 p1 << s1.p1;
134 q << s2.q;
135 @@
136
137 msg = "WARNING: end returns can be simpified and declaration on line %s can be dropped" % (q[0].line)
138 coccilib.report.print_report(p[0],msg)
139 cocci.include_match(False)
140
141 @script:python depends on report@
142 p << s1.p;
143 p1 << s1.p1;
144 q << s2.q
145 ;
146 @@
147
148 msg = "WARNING: end returns may be simpified if negative or 0 value and declaration on line %s can be dropped" % (q[0].line)
149 coccilib.report.print_report(p[0],msg)
150 cocci.include_match(False)
151
152 @script:python depends on report@
153 p << s1.p;
154 p1 << s1.p1;
155 @@
156
157 msg = "WARNING: end returns can be simpified"
158 coccilib.report.print_report(p[0],msg)
159
160 @script:python depends on report@
161 p << s1.p;
162 p2 << s1.p2;
163 @@
164
165 msg = "WARNING: end returns can be simpified if negative or 0 value"
166 coccilib.report.print_report(p[0],msg)
167
168 @script:python depends on report@
169 p << s3.p1;
170 @@
171
172 msg = "WARNING: end returns can be simpified"
173 coccilib.report.print_report(p[0],msg)
174
175 @script:python depends on report@
176 p << s3.p2;
177 @@
178
179 msg = "WARNING: end returns can be simpified if tested value is negative or 0"
180 coccilib.report.print_report(p[0],msg)