summaryrefslogtreecommitdiff
path: root/lib/igt.cocci
blob: 6b691862088b07a896424ae289f5a350630d2ddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Semantic patch for common patterns and their replacement by igt
// infrastructure and macros. Please run with
//
// spatch --sp-file lib/igt.cocci --in-place tests/*.c
//
// on your new testcase.


// Replace open-coded augmented igt_assert/skip/require with macro versions
@@
expression Ec;
expression list[n] Ep;
@@
- if (Ec) {
(
- igt_warn( Ep );
|
- igt_info( Ep );
|
- igt_debug( Ep );
)
- igt_fail(...);
- }
+ igt_fail_on_f(Ec, Ep);
@@
expression Ec;
@@
- if (Ec) {
- igt_fail(...);
- }
+ igt_fail_on(Ec);
@@
expression Ec;
expression list[n] Ep;
@@
- if (Ec) {
- igt_skip(Ep);
- }
+ igt_skip_on_f(Ec, Ep);
@@
expression Ec;
expression list[n] Ep;
@@
- if (Ec) {
- igt_warn(Ep);
- }
+ igt_warn_on_f(Ec, Ep);

// Enforce use of logging functions
@@
expression list[n] Ep;
@@
-fprintf(stderr, Ep);
+igt_warn(Ep);
@@
expression E;
@@
-perror(E);
+igt_warn(E);
@@
expression list[n] Ep;
@@
-fprintf(stdout, Ep);
+igt_info(Ep);
@@
expression list[n] Ep;
@@
-printf(Ep);
+igt_info(Ep);

// No abort for tests, really. Should only be used for internal library checks
// in lib/*
@@
@@
-abort();
+igt_fail(IGT_EXIT_FAILURE);

@@
iterator name for_each_pipe;
igt_display_t *display;
expression pipe;
@@
- for (pipe = 0; pipe < igt_display_get_n_pipes(display); pipe++) {
+ for_each_pipe (display, pipe) {
...
}

// Tests really shouldn't use plain assert!
@@
expression E;
@@
- assert(E);
+ igt_assert(E);

// Replace open-coded igt_swap()
@@
type T;
T a, b, tmp;
@@
- tmp = a;
- a = b;
- b = tmp;
+ igt_swap(a, b);

// Replace open-coded min()
@@
expression a;
expression b;
@@
(
- ((a) < (b) ? (a) : (b))
+ min(a, b)
|
- ((a) <= (b) ? (a) : (b))
+ min(a, b)
)

// Replace open-coded max()
@@
expression a;
expression b;
@@
(
- ((a) > (b) ? (a) : (b))
+ max(a, b)
|
- ((a) >= (b) ? (a) : (b))
+ max(a, b)
)

// drm_open_any always returns a valid file descriptor
@@
expression a;
@@
a = drm_open_any();
(
- igt_assert(a >= 0);
|
- if (a < 0) {
- ...
- return ...;
- }
)

// Use comparison macros instead of raw igt_assert when possible
@@
typedef uint32_t;
uint32_t E1, E2;
int E3, E4;
@@
(
- igt_assert(E1 == E2);
+ igt_assert_eq_u32(E1, E2);
|
- igt_assert(E1 != E2);
+ igt_assert_neq_u32(E1, E2);
|
- igt_assert(E1 <= E2);
+ igt_assert_lte_u32(E1, E2);
|
- igt_assert(E1 < E2);
+ igt_assert_lt_u32(E1, E2);
|
- igt_assert(E1 >= E2);
+ igt_assert_lte_u32(E2, E1);
|
- igt_assert(E1 > E2);
+ igt_assert_lt_u32(E2, E1);
|
- igt_assert(E3 == E4);
+ igt_assert_eq(E3, E4);
|
- igt_assert(E3 != E4);
+ igt_assert_neq(E3, E4);
|
- igt_assert(E3 <= E4);
+ igt_assert_lte(E3, E4);
|
- igt_assert(E3 < E4);
+ igt_assert_lt(E3, E4);
|
- igt_assert(E3 >= E4);
+ igt_assert_lte(E4, E3);
|
- igt_assert(E3 > E4);
+ igt_assert_lt(E4, E3);
)

// avoid unused-result warnings when compiling with _FORTIFY_SOURCE defined
@@
identifier func =~ "^(read|write)$";
expression list[2] E;
expression size;
@@
-func(E, size);
+igt_assert_eq(func(E, size), size);

@@
expression ptr, size, nmemb, stream;
@@
-fread(ptr, size, nmemb, stream);
+igt_assert_eq(fread(ptr, size, nmemb, stream), nmemb);

@@
expression list E;
@@
-fgets(E);
+igt_assert(fgets(E) != NULL);

@@
identifier func =~ "^v?asprintf$";
expression list E;
@@
-func(E);
+igt_assert_neq(func(E), -1);

// replace open-coded do_ioctl
@@
expression a, b, c, e;
@@
(
-do_or_die(drmIoctl(a, b, c));
+do_ioctl(a, b, c);
|
-igt_assert(drmIoctl(a, b, c) == 0);
+do_ioctl(a, b, c);
|
-igt_assert(drmIoctl(a, b, c) == -1 && errno == e);
+do_ioctl_err(a, b, c, e);
|
-igt_assert(drmIoctl(a, b, c) < 0 && errno == e);
+do_ioctl_err(a, b, c, e);
)