diff options
Diffstat (limited to 'scripts/coccinelle/misc/boolinit.cocci')
-rw-r--r-- | scripts/coccinelle/misc/boolinit.cocci | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/boolinit.cocci b/scripts/coccinelle/misc/boolinit.cocci new file mode 100644 index 000000000000..97ce41ce8135 --- /dev/null +++ b/scripts/coccinelle/misc/boolinit.cocci | |||
@@ -0,0 +1,178 @@ | |||
1 | /// Bool initializations should use true and false. Bool tests don't need | ||
2 | /// comparisons. Based on contributions from Joe Perches, Rusty Russell | ||
3 | /// and Bruce W Allan. | ||
4 | /// | ||
5 | // Confidence: High | ||
6 | // Copyright: (C) 2012 Julia Lawall, INRIA/LIP6. GPLv2. | ||
7 | // Copyright: (C) 2012 Gilles Muller, INRIA/LiP6. GPLv2. | ||
8 | // URL: http://coccinelle.lip6.fr/ | ||
9 | // Options: -include_headers | ||
10 | |||
11 | virtual patch | ||
12 | virtual context | ||
13 | virtual org | ||
14 | virtual report | ||
15 | |||
16 | @depends on patch@ | ||
17 | bool t; | ||
18 | symbol true; | ||
19 | symbol false; | ||
20 | @@ | ||
21 | |||
22 | ( | ||
23 | - t == true | ||
24 | + t | ||
25 | | | ||
26 | - true == t | ||
27 | + t | ||
28 | | | ||
29 | - t != true | ||
30 | + !t | ||
31 | | | ||
32 | - true != t | ||
33 | + !t | ||
34 | | | ||
35 | - t == false | ||
36 | + !t | ||
37 | | | ||
38 | - false == t | ||
39 | + !t | ||
40 | | | ||
41 | - t != false | ||
42 | + t | ||
43 | | | ||
44 | - false != t | ||
45 | + t | ||
46 | ) | ||
47 | |||
48 | @depends on patch disable is_zero, isnt_zero@ | ||
49 | bool t; | ||
50 | @@ | ||
51 | |||
52 | ( | ||
53 | - t == 1 | ||
54 | + t | ||
55 | | | ||
56 | - t != 1 | ||
57 | + !t | ||
58 | | | ||
59 | - t == 0 | ||
60 | + !t | ||
61 | | | ||
62 | - t != 0 | ||
63 | + t | ||
64 | ) | ||
65 | |||
66 | @depends on patch@ | ||
67 | bool b; | ||
68 | @@ | ||
69 | ( | ||
70 | b = | ||
71 | - 0 | ||
72 | + false | ||
73 | | | ||
74 | b = | ||
75 | - 1 | ||
76 | + true | ||
77 | ) | ||
78 | |||
79 | // --------------------------------------------------------------------- | ||
80 | |||
81 | @r1 depends on !patch@ | ||
82 | bool t; | ||
83 | position p; | ||
84 | @@ | ||
85 | |||
86 | ( | ||
87 | * t@p == true | ||
88 | | | ||
89 | * true == t@p | ||
90 | | | ||
91 | * t@p != true | ||
92 | | | ||
93 | * true != t@p | ||
94 | | | ||
95 | * t@p == false | ||
96 | | | ||
97 | * false == t@p | ||
98 | | | ||
99 | * t@p != false | ||
100 | | | ||
101 | * false != t@p | ||
102 | ) | ||
103 | |||
104 | @r2 depends on !patch disable is_zero, isnt_zero@ | ||
105 | bool t; | ||
106 | position p; | ||
107 | @@ | ||
108 | |||
109 | ( | ||
110 | * t@p == 1 | ||
111 | | | ||
112 | * t@p != 1 | ||
113 | | | ||
114 | * t@p == 0 | ||
115 | | | ||
116 | * t@p != 0 | ||
117 | ) | ||
118 | |||
119 | @r3 depends on !patch@ | ||
120 | bool b; | ||
121 | position p1,p2; | ||
122 | constant c; | ||
123 | @@ | ||
124 | ( | ||
125 | *b@p1 = 0 | ||
126 | | | ||
127 | *b@p1 = 1 | ||
128 | | | ||
129 | *b@p2 = c | ||
130 | ) | ||
131 | |||
132 | @script:python depends on org@ | ||
133 | p << r1.p; | ||
134 | @@ | ||
135 | |||
136 | cocci.print_main("WARNING: Comparison to bool",p) | ||
137 | |||
138 | @script:python depends on org@ | ||
139 | p << r2.p; | ||
140 | @@ | ||
141 | |||
142 | cocci.print_main("WARNING: Comparison of bool to 0/1",p) | ||
143 | |||
144 | @script:python depends on org@ | ||
145 | p1 << r3.p1; | ||
146 | @@ | ||
147 | |||
148 | cocci.print_main("WARNING: Assignment of bool to 0/1",p1) | ||
149 | |||
150 | @script:python depends on org@ | ||
151 | p2 << r3.p2; | ||
152 | @@ | ||
153 | |||
154 | cocci.print_main("ERROR: Assignment of bool to non-0/1 constant",p2) | ||
155 | |||
156 | @script:python depends on report@ | ||
157 | p << r1.p; | ||
158 | @@ | ||
159 | |||
160 | coccilib.report.print_report(p[0],"WARNING: Comparison to bool") | ||
161 | |||
162 | @script:python depends on report@ | ||
163 | p << r2.p; | ||
164 | @@ | ||
165 | |||
166 | coccilib.report.print_report(p[0],"WARNING: Comparison of bool to 0/1") | ||
167 | |||
168 | @script:python depends on report@ | ||
169 | p1 << r3.p1; | ||
170 | @@ | ||
171 | |||
172 | coccilib.report.print_report(p1[0],"WARNING: Assignment of bool to 0/1") | ||
173 | |||
174 | @script:python depends on report@ | ||
175 | p2 << r3.p2; | ||
176 | @@ | ||
177 | |||
178 | coccilib.report.print_report(p2[0],"ERROR: Assignment of bool to non-0/1 constant") | ||