igt.cocci 3.5 KB
Newer Older
Rhys Kidd's avatar
Rhys Kidd committed
1 2
// Semantic patch for common patterns and their replacement by igt
// infrastructure and macros. Please run with
Daniel Vetter's avatar
Daniel Vetter committed
3 4 5 6 7 8
//
// spatch --sp-file lib/igt.cocci --in-place tests/*.c
//
// on your new testcase.


9
// Replace open-coded augmented igt_assert/skip/require with macro versions
Daniel Vetter's avatar
Daniel Vetter committed
10 11 12 13 14
@@
expression Ec;
expression list[n] Ep;
@@
- if (Ec) {
15 16 17 18 19 20 21
(
- igt_warn( Ep );
|
- igt_info( Ep );
|
- igt_debug( Ep );
)
Daniel Vetter's avatar
Daniel Vetter committed
22 23
- igt_fail(...);
- }
24 25 26 27 28 29 30 31
+ igt_fail_on_f(Ec, Ep);
@@
expression Ec;
@@
- if (Ec) {
- igt_fail(...);
- }
+ igt_fail_on(Ec);
Daniel Vetter's avatar
Daniel Vetter committed
32 33 34 35 36 37 38 39
@@
expression Ec;
expression list[n] Ep;
@@
- if (Ec) {
- igt_skip(Ep);
- }
+ igt_skip_on_f(Ec, Ep);
40 41 42 43 44 45 46 47
@@
expression Ec;
expression list[n] Ep;
@@
- if (Ec) {
- igt_warn(Ep);
- }
+ igt_warn_on_f(Ec, Ep);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

// 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);
70 71 72 73 74 75

// No abort for tests, really. Should only be used for internal library checks
// in lib/*
@@
@@
-abort();
76
+igt_fail(IGT_EXIT_FAILURE);
77 78 79 80 81 82 83 84 85 86

@@
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) {
...
}
87 88 89 90 91 92 93

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

95
// Replace open-coded igt_swap()
96 97 98 99 100 101 102
@@
type T;
T a, b, tmp;
@@
- tmp = a;
- a = b;
- b = tmp;
103
+ igt_swap(a, b);
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

// 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)
)
130 131 132 133 134 135 136 137 138 139 140 141 142 143

// drm_open_any always returns a valid file descriptor
@@
expression a;
@@
a = drm_open_any();
(
- igt_assert(a >= 0);
|
- if (a < 0) {
- ...
- return ...;
- }
)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

// 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);
|
164 165 166 167 168 169
- igt_assert(E1 >= E2);
+ igt_assert_lte_u32(E2, E1);
|
- igt_assert(E1 > E2);
+ igt_assert_lt_u32(E2, E1);
|
170 171 172 173 174 175 176 177 178 179 180
- 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);
181 182 183 184 185 186
|
- igt_assert(E3 >= E4);
+ igt_assert_lte(E4, E3);
|
- igt_assert(E3 > E4);
+ igt_assert_lt(E4, E3);
187
)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

// 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);
208
+igt_assert(fgets(E) != NULL);
209 210 211 212 213 214 215

@@
identifier func =~ "^v?asprintf$";
expression list E;
@@
-func(E);
+igt_assert_neq(func(E), -1);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

// 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);
)