diff options
author | Julia Lawall <Julia.Lawall@lip6.fr> | 2014-08-23 01:34:45 -0400 |
---|---|---|
committer | Michal Marek <mmarek@suse.cz> | 2014-09-26 07:42:31 -0400 |
commit | 932058a5d5f9fd919b90aaa2275d54b37340d585 (patch) | |
tree | c23f98f5f4d8fd69401db4092fc067336d944b82 /scripts/coccinelle | |
parent | 22739edfc563fa42f02a2f7323fd352061ef32b3 (diff) |
coccinelle: misc: semantic patch to delete overly complex return code processing
This semantic patch simplifies cases where the effect of the processing of
a function call's return code is just to return the result of the function
directly. It may also delete a local return flag variable, if this is no
longer used.
This was proposed by Uwe Kleine-König.
Signed-off-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/misc/simple_return.cocci | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/simple_return.cocci b/scripts/coccinelle/misc/simple_return.cocci new file mode 100644 index 000000000000..47f7084b6360 --- /dev/null +++ b/scripts/coccinelle/misc/simple_return.cocci | |||
@@ -0,0 +1,180 @@ | |||
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) | ||