1// Semantic patch for common patterns and their replacement by igt
2// infrastructure and macros. Please run with
3//
4// spatch --sp-file lib/igt.cocci --in-place tests/*.c
5//
6// on your new testcase.
7
8
9// Replace open-coded augmented igt_assert/skip/require with macro versions
10@@
11expression Ec;
12expression list[n] Ep;
13@@
14- if (Ec) {
15(
16- igt_warn( Ep );
17|
18- igt_info( Ep );
19|
20- igt_debug( Ep );
21)
22- igt_fail(...);
23- }
24+ igt_fail_on_f(Ec, Ep);
25@@
26expression Ec;
27@@
28- if (Ec) {
29- igt_fail(...);
30- }
31+ igt_fail_on(Ec);
32@@
33expression Ec;
34expression list[n] Ep;
35@@
36- if (Ec) {
37- igt_skip(Ep);
38- }
39+ igt_skip_on_f(Ec, Ep);
40@@
41expression Ec;
42expression list[n] Ep;
43@@
44- if (Ec) {
45- igt_warn(Ep);
46- }
47+ igt_warn_on_f(Ec, Ep);
48
49// Enforce use of logging functions
50@@
51expression list[n] Ep;
52@@
53-fprintf(stderr, Ep);
54+igt_warn(Ep);
55@@
56expression E;
57@@
58-perror(E);
59+igt_warn(E);
60@@
61expression list[n] Ep;
62@@
63-fprintf(stdout, Ep);
64+igt_info(Ep);
65@@
66expression list[n] Ep;
67@@
68-printf(Ep);
69+igt_info(Ep);
70
71// No abort for tests, really. Should only be used for internal library checks
72// in lib/*
73@@
74@@
75-abort();
76+igt_fail(IGT_EXIT_FAILURE);
77
78@@
79iterator name for_each_pipe;
80igt_display_t *display;
81expression pipe;
82@@
83- for (pipe = 0; pipe < igt_display_get_n_pipes(display); pipe++) {
84+ for_each_pipe (display, pipe) {
85...
86}
87
88// Tests really shouldn't use plain assert!
89@@
90expression E;
91@@
92- assert(E);
93+ igt_assert(E);
94
95// Replace open-coded igt_swap()
96@@
97type T;
98T a, b, tmp;
99@@
100- tmp = a;
101- a = b;
102- b = tmp;
103+ igt_swap(a, b);
104
105// Replace open-coded min()
106@@
107expression a;
108expression b;
109@@
110(
111- ((a) < (b) ? (a) : (b))
112+ min(a, b)
113|
114- ((a) <= (b) ? (a) : (b))
115+ min(a, b)
116)
117
118// Replace open-coded max()
119@@
120expression a;
121expression b;
122@@
123(
124- ((a) > (b) ? (a) : (b))
125+ max(a, b)
126|
127- ((a) >= (b) ? (a) : (b))
128+ max(a, b)
129)
130
131// drm_open_any always returns a valid file descriptor
132@@
133expression a;
134@@
135a = drm_open_any();
136(
137- igt_assert(a >= 0);
138|
139- if (a < 0) {
140- ...
141- return ...;
142- }
143)
144
145// Use comparison macros instead of raw igt_assert when possible
146@@
147typedef uint32_t;
148uint32_t E1, E2;
149int E3, E4;
150@@
151(
152- igt_assert(E1 == E2);
153+ igt_assert_eq_u32(E1, E2);
154|
155- igt_assert(E1 != E2);
156+ igt_assert_neq_u32(E1, E2);
157|
158- igt_assert(E1 <= E2);
159+ igt_assert_lte_u32(E1, E2);
160|
161- igt_assert(E1 < E2);
162+ igt_assert_lt_u32(E1, E2);
163|
164- igt_assert(E1 >= E2);
165+ igt_assert_lte_u32(E2, E1);
166|
167- igt_assert(E1 > E2);
168+ igt_assert_lt_u32(E2, E1);
169|
170- igt_assert(E3 == E4);
171+ igt_assert_eq(E3, E4);
172|
173- igt_assert(E3 != E4);
174+ igt_assert_neq(E3, E4);
175|
176- igt_assert(E3 <= E4);
177+ igt_assert_lte(E3, E4);
178|
179- igt_assert(E3 < E4);
180+ igt_assert_lt(E3, E4);
181|
182- igt_assert(E3 >= E4);
183+ igt_assert_lte(E4, E3);
184|
185- igt_assert(E3 > E4);
186+ igt_assert_lt(E4, E3);
187)
188
189// avoid unused-result warnings when compiling with _FORTIFY_SOURCE defined
190@@
191identifier func =~ "^(read|write)$";
192expression list[2] E;
193expression size;
194@@
195-func(E, size);
196+igt_assert_eq(func(E, size), size);
197
198@@
199expression ptr, size, nmemb, stream;
200@@
201-fread(ptr, size, nmemb, stream);
202+igt_assert_eq(fread(ptr, size, nmemb, stream), nmemb);
203
204@@
205expression list E;
206@@
207-fgets(E);
208+igt_assert(fgets(E) != NULL);
209
210@@
211identifier func =~ "^v?asprintf$";
212expression list E;
213@@
214-func(E);
215+igt_assert_neq(func(E), -1);
216
217// replace open-coded do_ioctl
218@@
219expression a, b, c, e;
220@@
221(
222-do_or_die(drmIoctl(a, b, c));
223+do_ioctl(a, b, c);
224|
225-igt_assert(drmIoctl(a, b, c) == 0);
226+do_ioctl(a, b, c);
227|
228-igt_assert(drmIoctl(a, b, c) == -1 && errno == e);
229+do_ioctl_err(a, b, c, e);
230|
231-igt_assert(drmIoctl(a, b, c) < 0 && errno == e);
232+do_ioctl_err(a, b, c, e);
233)
234