Debian Codegraph @

gcc/testsuite/gcc.misc-tests/gcov-23.c

raw
1 /* { dg-options "-fcondition-coverage -ftest-coverage -O2" } */
2 /* { dg-do compile } */
3
4 #include <stdint.h>
5 #include <limits.h>
6 #include <setjmp.h>
7 jmp_buf buf;
8
9 int id (int);
10 int idp (int *);
11 int err;
12 char c;
13
14 /* This becomes problematic only under optimization for the case when the
15 compiler cannot inline the function but have to generate a call. It is not
16 really interesting to run, only build. Notably, both the function calls and
17 the return values are important to construct a problematic graph.
18
19 This test is also a good example of where optimization makes condition
20 coverage unpredictable, but not unusable. If this is built without
21 optimization the conditions work as you would expect from reading the
22 source. */
23 /* Adapted from cpio-2.14 gnu/utmens.c lutimens (). */
24 int
25 mcdc001 (int *v)
26 {
27 int adjusted;
28 int adjustment_needed = 0;
29
30 int *ts = v ? &adjusted : 0; /* conditions(0/4) true(0 1) false(0 1) */
31 /* conditions(end) */
32 if (ts)
33 adjustment_needed = idp (ts);
34 if (adjustment_needed < 0)
35 return -1;
36
37 if (adjustment_needed) /* conditions(0/2) true(0) false(0) */
38 /* conditions(end) */
39 {
40 if (adjustment_needed != 3) /* conditions(0/2) true(0) false(0) */
41 /* conditions(end) */
42 return -1;
43 if (ts) /* conditions(0/2) true(0) false(0) */
44 /* conditions(end) */
45 return 0;
46 }
47
48 if (adjustment_needed && idp (&adjusted)) /* conditions(0/4) true(0 1) false(0 1) */
49 /* conditions(end) */
50 return -1;
51 if (adjusted) /* conditions(0/2) true(0) false(0) */
52 /* conditions(end) */
53 return idp (ts);
54
55 return -1;
56 }
57
58 /* This failed when the candidate set internal/contracted-past nodes were not
59 properly marked as reachable in the candidate reduction phase. */
60 /* Adapted from cpio-2.14 gnu/mktime.c mktime_internal (). */
61 int
62 mcdc002 ()
63 {
64 int a;
65 if (idp (&a)) /* conditions(0/2) true(0) false(0) */
66 /* conditions(end) */
67 {
68 if (id (a)) /* conditions(0/2) true(0/2) true(0) false(0) */
69 /* conditions(end) */
70 goto exit;
71
72 if (err) /* conditions(0/2) true(0/2) true(0) false(0) */
73 /* conditions(end) */
74 return -1;
75 }
76
77 exit:
78 return a;
79 }
80
81 /* Adapted from icu4c-73.1 common/ucase.cpp ucase_getCaseLocale (). */
82 int
83 mcdc003 (const char *locale)
84 {
85 /* extern, so its effect won't be optimized out. */
86 c = *locale++;
87 if (c == 'z') /* conditions(0/2) true(0) false(0) */
88 /* conditions(end) */
89 {
90 return 1;
91 }
92 else if (c >= 'a') /* conditions(0/2) true(0) false(0) */
93 /* conditions(end) */
94 {
95 if (id (c)) /* conditions(0/2) true(0) false(0) */
96 /* conditions(end) */
97 c = *locale++;
98 }
99 else
100 {
101 if (c == 'T')
102 {
103 if (id (c)) /* conditions(0/2) true(0) false(0) */
104 /* conditions(end) */
105 c = *locale++;
106 if (id (c)) /* conditions(0/2) true(0) false(0) */
107 /* conditions(end) */
108 c = *locale++;
109 }
110 /* This may or may not become a jump table. */
111 else if (c == 'L') /* conditions(suppress) */
112 /* conditions(end) */
113 c = *locale++;
114 else if (c == 'E') /* conditions(suppress) */
115 /* conditions(end) */
116 c = *locale++;
117 else if (c == 'N') /* conditions(suppress) */
118 /* conditions(end) */
119 c = *locale++;
120 else if (c == 'H') /* conditions(suppress) */
121 /* conditions(end) */
122 {
123 c = *locale++;
124 if (id (c)) /* conditions(0/2) true(0) false(0) */
125 /* conditions(end) */
126 c = *locale++;
127 }
128 }
129
130 return 1;
131 }
132
133 /* The || will be changed to |, so it is impractical to predict the number of
134 conditions. If the walk is not properly implemented this will not finish
135 compiling, so the actual coverage is not interesting. */
136 /* Adapted from icu4c-73.1 common/uresdata.cpp res_findResource (). */
137 int
138 mcdc004 (int r, char* path, char* key)
139 {
140 char *idcc (char *, char);
141 #define is_kind1(type) ((type) == 23 || (type) == 14 || (type == 115))
142 #define is_kind2(type) ((type) == 16 || (type) == 77 || (type == 118))
143 #define is_kind12(type) (is_kind1 ((type)) || is_kind2 ((type)))
144
145 char *nextSepP = path;
146 int t1 = r;
147 int type = id (t1);
148
149 if (!is_kind12 (type)) /* conditions(suppress) */
150 /* conditions(end) */
151 return -1;
152
153 while (*path && t1 != -1 && is_kind12 (type)) /* conditions(suppress) */
154 /* conditions(end) */
155 {
156 nextSepP = idcc(path, '/');
157 if(nextSepP == path) /* conditions(0/2) true(0) false(0) */
158 /* conditions(end) */
159 return -1;
160
161 if (*nextSepP == 'a') /* conditions(0/2) true(0) false(0) */
162 /* conditions(end) */
163 *key = *path;
164 else if (*nextSepP == 'b') /* conditions(0/2) true(0) false(0) */
165 /* conditions(end) */
166 *key = 0;
167 type = t1;
168 }
169
170 return t1;
171 }
172
173 /* Adapted from jxl 0.8.2 lib/extras/dec/apng.cc processing_start ().
174 This created a graph where depth-first traversal of the CFG would not
175 process nodes in the wrong order (the extra control inserted from setjmp
176 created a path of complexes from root to !b without going through !a).
177
178 This only happened under optimization. */
179 int
180 mcdc005 (int a, int b)
181 {
182 a = id (a);
183 b = id (b);
184
185 /* The a || b gets transformed to a | b, then fused with setjmp because
186 they both have the same return value. */
187 if (a || b) /* conditions(0/4) true(0 1) false(0 1) */
188 /* conditions(end) */
189 return 1;
190 else
191 a += 1;
192
193 if (setjmp (buf))
194 return 1;
195
196 return a;
197 }
198
199 /* Adapted from cpio-2.14 gnu/quotearg.c quotearg_buffer_restyled. The ifs in
200 the cases (with fallthrough) re-use the same value which under optimization
201 causes path reuse which must be sorted out. */
202 int
203 mcdc006 (int quoting_style, int elide, int *buffer)
204 {
205 int backslash = 0;
206 switch (quoting_style)
207 {
208 case 1:
209 if (!elide)
210 backslash = 1;
211 case 2:
212 if (!elide)
213 if (buffer)
214 *buffer = '"';
215 }
216
217 if (quoting_style == 2 && backslash)
218 quoting_style = 1;
219 return 1;
220 }
221
222 /* Adapted from pcre2-10.42 pcre2_compile.c pcre2_compile. If SSA nodes are
223 created at the wrong place in the block it will fail flow analysis (because
224 the label is in the middle of block), caused by the final break in this
225 case. */
226 void
227 mcdc007 (int options, int *pso, int len)
228 {
229 if (options == 5)
230 return;
231
232 while (options--)
233 {
234 int i;
235 for (i = 0; i < len; i++)
236 {
237 int *p = pso + i;
238 int skipatstart = *p + 2;
239 if (skipatstart) {
240 switch(*p)
241 {
242 case 1:
243 *p |= *p + 1;
244 break;
245 case 2:
246 skipatstart += *p - skipatstart;
247 break;
248 }
249 break;
250 }
251 }
252 if (i >= len) break;
253 }
254 }
255
256 /* Adapted from alsa-lib 1.2.8 pcm/pcm.c snd_pcm_chmap_print. */
257 int
258 mcdc008 (int *map, unsigned maxlen, int *buf)
259 {
260 unsigned int len = 0;
261 for (unsigned i = 0; i < *map; i++) {
262 unsigned int p = map[i] & 0xF;
263 if (i > 0) {
264 if (len >= maxlen)
265 return -1;
266 }
267 if (map[i] & 0xFF)
268 len += idp (buf + len);
269 else {
270 len += idp (buf);
271 }
272 if (map[i] & 0xFF00) {
273 len += idp (buf + len);
274 if (len >= maxlen)
275 return -1;
276 }
277 }
278 return len;
279 }
280
281 /* Adapted from cpio-2.14 gnu/mktime.c mktime_internal (). The combination of
282 goto, automatic variables, and the ternary causes the post dominator of the
283 highest topological ordered node not to be the common post dominator of the
284 expression as a whole. */
285 int
286 mcdc009 (int *tp, int t, int isdst)
287 {
288 int t0 = tp[0];
289 int t1 = tp[1];
290 int t2 = tp[2];
291
292 if (t0 < 0 || (isdst < 0 ? t1 : (isdst != 0)))
293 goto offset_found;
294
295 if (t == 0)
296 return -1;
297
298 t1 = t2;
299
300 offset_found:
301 return t;
302 }
303
304 /* Adapted from Berkeley db 4.8.30 rep/rep_elect.c __rep_cmp_vote. This
305 particular combination of fallthrough and folding creates a path into the
306 the inner if () that does not go through the first basic condition. */
307 void
308 mcdc010 (int cmp, int *rep, int sites, int priority, int flags)
309 {
310 if (sites > 1 && (priority != 0 || (flags & 0xFF)))
311 {
312 if ( (priority != 0 && *rep == 0)
313 || (((priority == 0 && *rep == 0)
314 || (priority != 0 && *rep != 0)) && cmp > 0))
315 {
316 *rep = cmp;
317 }
318 }
319 }
320
321 /* For not sufficiently protected back edges this would create an infinite
322 loop. */
323 void
324 mcdc011 (int a, int b)
325 {
326 if (a && id (b))
327 for (;;) {}
328 id (a+1);
329 }
330
331 /* Adapted from alsa-1.2.8 tlv.c get_tlv_info (). Under optimization, the
332 conditions may be replaced with min (). */
333 int
334 mcdc012 (int x, int y)
335 {
336 int err;
337 err = id (x);
338 if (err < 0)
339 return err;
340 err = id (y);
341 if (err < 0)
342 return err;
343 return 0;
344 }
345
346 /* Adapted from alsa-1.2.8 control.c snd_ctl_elem_id_compare_numid (). This
347 test is probably not so accurate on targets where int == int64. Under
348 optimization, the conditions may be replaced with min/max. */
349 int
350 mcdc013 (const int64_t *id1, const int64_t *id2)
351 {
352 int64_t d;
353 d = *id1 - *id2;
354 if (d & 0xFF)
355 {
356 if (d > INT_MAX)
357 d = INT_MAX;
358 else if (d < INT_MIN)
359 d = INT_MIN;
360 }
361 return d;
362 }