b5d4bab60263dc5ab38c6d0d74a52ab92fb99002
[cascardo/linux.git] / scripts / coccinelle / misc / compare_const_fl.cocci
1 /// Move constants to the right of binary operators.
2 //# Depends on personal taste in some cases.
3 ///
4 // Confidence: Moderate
5 // Copyright: (C) 2015 Copyright: (C) 2015 Julia Lawall, Inria. GPLv2.
6 // URL: http://coccinelle.lip6.fr/
7 // Options: --no-includes --include-headers
8
9 virtual patch
10 virtual context
11 virtual org
12 virtual report
13
14 @r1 depends on patch && !context && !org && !report
15  disable bitor_comm, neg_if_exp@
16 constant c,c1;
17 local idexpression i;
18 expression e,e1,e2;
19 binary operator b = {==,!=,&,|};
20 type t;
21 @@
22
23 (
24 c b (c1)
25 |
26 sizeof(t) b e1
27 |
28 sizeof e b e1
29 |
30 i b e1
31 |
32 c | e1 | e2 | ...
33 |
34 c | (e ? e1 : e2)
35 |
36 - c
37 + e
38 b
39 - e
40 + c
41 )
42
43 @r2 depends on patch && !context && !org && !report
44  disable gtr_lss, gtr_lss_eq, not_int2@
45 constant c,c1;
46 expression e,e1,e2;
47 binary operator b;
48 binary operator b1 = {<,<=},b2 = {<,<=};
49 binary operator b3 = {>,>=},b4 = {>,>=};
50 local idexpression i;
51 type t;
52 @@
53
54 (
55 c b c1
56 |
57 sizeof(t) b e1
58 |
59 sizeof e b e1
60 |
61  (e1 b1 e) && (e b2 e2)
62 |
63  (e1 b3 e) && (e b4 e2)
64 |
65 i b e
66 |
67 - c < e
68 + e > c
69 |
70 - c <= e
71 + e >= c
72 |
73 - c > e
74 + e < c
75 |
76 - c >= e
77 + e <= c
78 )
79
80 // ----------------------------------------------------------------------------
81
82 @r1_context depends on !patch && (context || org || report)
83  disable bitor_comm, neg_if_exp exists@
84 type t;
85 binary operator b = {==,!=,&,|};
86 constant c, c1;
87 expression e, e1, e2;
88 local idexpression i;
89 position j0;
90 @@
91
92 (
93 c b (c1)
94 |
95 sizeof(t) b e1
96 |
97 sizeof e b e1
98 |
99 i b e1
100 |
101 c | e1 | e2 | ...
102 |
103 c | (e ? e1 : e2)
104 |
105 * c@j0 b e
106 )
107
108 @r2_context depends on !patch && (context || org || report)
109  disable gtr_lss, gtr_lss_eq, not_int2 exists@
110 type t;
111 binary operator b, b1 = {<,<=}, b2 = {<,<=}, b3 = {>,>=}, b4 = {>,>=};
112 constant c, c1;
113 expression e, e1, e2;
114 local idexpression i;
115 position j0;
116 @@
117
118 (
119 c b c1
120 |
121 sizeof(t) b e1
122 |
123 sizeof e b e1
124 |
125  (e1 b1 e) && (e b2 e2)
126 |
127  (e1 b3 e) && (e b4 e2)
128 |
129 i b e
130 |
131 * c@j0 < e
132 |
133 * c@j0 <= e
134 |
135 * c@j0 > e
136 |
137 * c@j0 >= e
138 )
139
140 // ----------------------------------------------------------------------------
141
142 @script:python r1_org depends on org@
143 j0 << r1_context.j0;
144 @@
145
146 msg = "Move constant to right."
147 coccilib.org.print_todo(j0[0], msg)
148
149 @script:python r2_org depends on org@
150 j0 << r2_context.j0;
151 @@
152
153 msg = "Move constant to right."
154 coccilib.org.print_todo(j0[0], msg)
155
156 // ----------------------------------------------------------------------------
157
158 @script:python r1_report depends on report@
159 j0 << r1_context.j0;
160 @@
161
162 msg = "Move constant to right."
163 coccilib.report.print_report(j0[0], msg)
164
165 @script:python r2_report depends on report@
166 j0 << r2_context.j0;
167 @@
168
169 msg = "Move constant to right."
170 coccilib.report.print_report(j0[0], msg)
171