1 /* { dg-options "-Wmisleading-indentation -Wall" } */
2 /* { dg-do compile } */
3
4 extern int foo (int);
5 extern int bar (int, int);
6 extern int flagA;
7 extern int flagB;
8 extern int flagC;
9 extern int flagD;
10
11 int
fn_1(int flag)12 fn_1 (int flag)
13 {
14 int x = 4, y = 5;
15 if (flag) /* { dg-warning "3: this 'if' clause does not guard..." } */
16 x = 3;
17 y = 2; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
18 return x * y;
19 }
20
21 int
fn_2(int flag,int x,int y)22 fn_2 (int flag, int x, int y)
23 {
24 if (flag) /* { dg-warning "3: this 'if' clause does not guard..." } */
25 x++; y++; /* { dg-message "10: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
26
27 return x * y;
28 }
29
30 int
fn_3(int flag)31 fn_3 (int flag)
32 {
33 int x = 4, y = 5;
34 if (flag)
35 x = 3;
36 else /* { dg-warning "3: this 'else' clause does not guard..." } */
37 x = 2;
38 y = 2; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'" } */
39 return x * y;
40 }
41
42 void
fn_4(double * a,double * b,double * c)43 fn_4 (double *a, double *b, double *c)
44 {
45 int i = 0;
46 while (i < 10) /* { dg-warning "3: this 'while' clause does not guard..." } */
47 a[i] = b[i] * c[i];
48 i++; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
49 }
50
51 void
fn_5(double * a,double * b,double * sum,double * prod)52 fn_5 (double *a, double *b, double *sum, double *prod)
53 {
54 int i = 0;
55 for (i = 0; i < 10; i++) /* { dg-warning "3: this 'for' clause does not guard..." } */
56 sum[i] = a[i] * b[i];
57 prod[i] = a[i] * b[i]; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
58 }
59
60 /* Based on CVE-2014-1266 aka "goto fail" */
fn_6(int a,int b,int c)61 int fn_6 (int a, int b, int c)
62 {
63 int err;
64
65 /* ... */
66 if ((err = foo (a)) != 0)
67 goto fail;
68 if ((err = foo (b)) != 0) /* { dg-message "2: this 'if' clause does not guard..." } */
69 goto fail;
70 goto fail; /* { dg-message "3: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
71 if ((err = foo (c)) != 0)
72 goto fail;
73 /* ... */
74
75 fail:
76 return err;
77 }
78
fn_7(int p,int q,int r,int s,int t)79 int fn_7 (int p, int q, int r, int s, int t)
80 {
81 if (bar (p, q))
82 {
83 if (p) /* { dg-message "7: this 'if' clause does not guard..." } */
84 q++; r++; /* { dg-message "14: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
85 t++;
86 }
87 return p + q + r + s + t;
88 }
89
fn_8(int a,int b,int c)90 int fn_8 (int a, int b, int c)
91 {
92 /* This should *not* be flagged as misleading indentation. */
93 if (a) return b; else return c;
94 }
95
fn_9(int flag)96 void fn_9 (int flag)
97 {
98 if (flag) /* { dg-warning "3: this 'if' clause does not guard..." } */
99 foo (0);
100 foo (1); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
101 }
102
fn_10(int flag)103 void fn_10 (int flag)
104 {
105 if (flag) /* { dg-warning "3: this 'if' clause does not guard..." } */
106 if (flag / 2)
107 {
108 foo (0);
109 foo (1);
110 }
111 foo (2); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
112 foo (3);
113 }
114
fn_11(void)115 void fn_11 (void)
116 {
117 if (flagA)
118 if (flagB)
119 if (flagC) /* { dg-message "7: this 'if' clause does not guard..." } */
120 foo (0);
121 bar (1, 2); /* { dg-message "9: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
122 }
123
fn_12(void)124 void fn_12 (void)
125 {
126 if (flagA)
127 if (flagB) /* { dg-message "5: this 'if' clause does not guard..." } */
128 if (flagC)
129 foo (0);
130 bar (1, 2); /* { dg-message "7: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
131 }
132
fn_13(void)133 void fn_13 (void)
134 {
135 if (flagA) /* { dg-warning "3: this 'if' clause does not guard..." } */
136 if (flagB)
137 if (flagC)
138 foo (0);
139 bar (1, 2); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
140 }
141
142 #define FOR_EACH(VAR, START, STOP) \
143 for ((VAR) = (START); (VAR) < (STOP); (VAR++)) /* { dg-warning "3: this 'for' clause does not guard..." } */
144
fn_14(void)145 void fn_14 (void)
146 {
147 int i;
148 FOR_EACH (i, 0, 10) /* { dg-message "in expansion of macro .FOR_EACH." } */
149 foo (i);
150 bar (i, i); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
151 }
152 #undef FOR_EACH
153
154 #define FOR_EACH(VAR, START, STOP) for ((VAR) = (START); (VAR) < (STOP); (VAR++)) /* { dg-message "36: this 'for' clause does not guard..." } */
fn_15(void)155 void fn_15 (void)
156 {
157 int i;
158 FOR_EACH (i, 0, 10) /* { dg-message "in expansion of macro .FOR_EACH." } */
159 foo (i);
160 bar (i, i); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
161 }
162 #undef FOR_EACH
163
fn_16_spaces(void)164 void fn_16_spaces (void)
165 {
166 int i;
167 for (i = 0; i < 10; i++)
168 while (flagA)
169 if (flagB) /* { dg-message "7: this 'if' clause does not guard..." } */
170 foo (0);
171 foo (1); /* { dg-message "9: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
172 }
173
fn_16_tabs(void)174 void fn_16_tabs (void)
175 {
176 int i;
177 for (i = 0; i < 10; i++)
178 while (flagA)
179 if (flagB) /* { dg-message "7: this 'if' clause does not guard..." } */
180 foo (0);
181 foo (1);/* { dg-message "2: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
182 }
183
fn_17_spaces(void)184 void fn_17_spaces (void)
185 {
186 int i;
187 for (i = 0; i < 10; i++) /* { dg-warning "3: this 'for' clause does not guard..." } */
188 while (flagA)
189 if (flagB)
190 foo (0);
191 foo (1);/* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
192 }
193
fn_17_tabs(void)194 void fn_17_tabs (void)
195 {
196 int i;
197 for (i = 0; i < 10; i++) /* { dg-warning "3: this 'for' clause does not guard..." } */
198 while (flagA)
199 if (flagB)
200 foo (0);
201 foo (1);/* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
202 }
203
fn_18_spaces(void)204 void fn_18_spaces (void)
205 {
206 int i;
207 for (i = 0; i < 10; i++)
208 while (flagA) /* { dg-message "5: this 'while' clause does not guard..." } */
209 if (flagB)
210 foo (0);
211 foo (1);/* { dg-message "7: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
212 }
213
fn_18_tabs(void)214 void fn_18_tabs (void)
215 {
216 int i;
217 for (i = 0; i < 10; i++)
218 while (flagA) /* { dg-message "5: this 'while' clause does not guard..." } */
219 if (flagB)
220 foo (0);
221 foo (1);/* { dg-message "7: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
222 }
223
224 /* This shouldn't lead to a warning. */
fn_19(void)225 int fn_19 (void) { if (flagA) return 1; else return 0; }
226
227 /* A deeply-nested mixture of spaces and tabs, adapted from
228 c-c++-common/pr60101.c.
229 This should not lead to a warning. */
230 void
fn_20(unsigned int l)231 fn_20 (unsigned int l)
232 {
233 unsigned int i;
234
235 for (i = 0; i < 10; i++)
236 {
237 unsigned int n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11;
238
239 for (n0 = 0; n0 < l; n0++)
240 for (n1 = 0; n1 < l; n1++)
241 for (n2 = 0; n2 < l; n2++)
242 for (n3 = 0; n3 < l; n3++)
243 for (n4 = 0; n4 < l; n4++)
244 for (n5 = 0; n5 < l; n5++)
245 for (n6 = 0; n6 < l; n6++)
246 for (n7 = 0; n7 < l; n7++)
247 for (n8 = 0; n8 < l; n8++)
248 for (n9 = 0; n9 < l; n9++)
249 for (n10 = 0; n10 < l; n10++)
250 for (n11 = 0; n11 < l; n11++)
251 {
252 if (flagA)
253 foo (0);
254 foo (1);
255 }
256 foo (2);
257 }
258 }
259
260 /* Another nested mix of tabs and spaces that shouldn't lead to a warning,
261 with a preprocessor directive thrown in for good measure
262 (adapted from libgomp/loop.c: gomp_loop_init). */
fn_21(void)263 void fn_21 (void)
264 {
265 foo (0);
266 if (flagA)
267 {
268 foo (1);
269
270 #if 1
271 {
272 foo (2);
273 if (flagB)
274 {
275 if (flagC)
276 foo (3);
277 else
278 foo (4);
279 }
280 else if (flagD)
281 foo (5);
282 else
283 foo (6);
284 }
285 #endif
286 }
287 }
288
289 /* The conditionals within the following macros shouldn't be warned about.
290 Adapted from libgomp/driver.c: gomp_load_plugin_for_device. */
fn_22(void)291 int fn_22 (void)
292 {
293 int err = 0;
294
295 #define DLSYM() \
296 do \
297 { \
298 err = foo (0); \
299 if (err) \
300 goto out; \
301 } \
302 while (0)
303 #define DLSYM_OPT() \
304 do \
305 { \
306 err = foo (1); \
307 if (err) \
308 foo (2); \
309 else \
310 foo (3); \
311 foo (4); \
312 } \
313 while (0)
314 DLSYM ();
315 DLSYM_OPT ();
316 #undef DLSYM
317 #undef DLSYM_OPT
318
319 out:
320 return err;
321 }
322
323 /* This shouldn't be warned about. */
fn_23(void)324 void fn_23 (void) { foo (0); foo (1); if (flagA) foo (2); foo (3); foo (4); }
325
326 /* Code that simply doesn't bother indenting anywhere (e.g. autogenerated
327 code) shouldn't be warned about. */
fn_24(void)328 void fn_24 (void)
329 {
330 foo (0);
331 if (flagA)
332 foo (1);
333 foo (2);
334 }
335
336 /* Adapted from libiberty/regex.c; an example of a conditional in a
337 macro where the successor statement begins with a macro arg:
338
339 if (num < 0)
340 num = 0;
341 num = num * 10 + c - '0';
342 ^ this successor statement
343
344 and hence "num" has a spelling location at the argument of the
345 macro usage site ("lower_bound"), we want the definition of the
346 parameter ("num") for the indentation comparison to be meaninful.
347
348 This should not generate a misleading indentation warning. */
349
350 # define GET_UNSIGNED_NUMBER(num) \
351 { \
352 while (flagA) \
353 { \
354 if (flagB) \
355 { \
356 if (num < 0) \
357 num = 0; \
358 num = num * 10 + c - '0'; \
359 } \
360 } \
361 }
fn_25(int c,int lower_bound,int upper_bound)362 void fn_25 (int c, int lower_bound, int upper_bound)
363 {
364 GET_UNSIGNED_NUMBER (lower_bound);
365 }
366 #undef GET_UNSIGNED_NUMBER
367
368 /* Example adapted from libdecnumber/decNumber.c:decExpOp that shouldn't
369 trigger a warning. */
fn_26(void)370 void fn_26 (void)
371 {
372 if (flagA) {
373 if (flagB) foo (0); }
374 foo (1);
375 }
376
377 /* Ensure that we don't get confused by mixed tabs and spaces; the line
378 "foo (1);" has leading spaces before a tab, but this should not
379 lead to a warning from -Wmisleading-indentation. */
fn_27(void)380 void fn_27 (void)
381 {
382 if (flagA)
383 foo (0);
384 foo (1);
385 }
386
387 /* Example adapted from gcc/cgraph.h:symtab_node::get_availability of
388 a spurious trailing semicolon that shouldn't generate a warning. */
fn_28(void)389 void fn_28 (void)
390 {
391 if (flagA)
392 foo (0);
393 else
394 foo (1);;
395 }
396
397 /* However, other kinds of spurious semicolons can be a problem. Sadly
398 we don't yet report for the misleading-indented "foo (1);" in the
399 following, due to the spurious semicolon. */
fn_29(void)400 void fn_29 (void)
401 {
402 if (flagA)
403 if (flagB)
404 foo (0);;
405 foo (1);
406 }
407
408 /* Adapted from usage site of #ifdef HAVE_cc0. This should not lead
409 to a warning from -Wmisleading-indentation. */
fn_30(void)410 void fn_30 (void)
411 {
412 if (flagA)
413 foo (0);
414 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
415 if (flagB)
416 #endif
417 foo (1);
418 }
419
420 /* This shouldn't lead to a warning. */
fn_31(void)421 void fn_31 (void)
422 {
423 if (flagA)
424 foo (0);
425 else if (flagB)
426 foo (1);
427 else if (flagC)
428 foo (2);
429 else
430 foo (3);
431 }
432
433 /* Ensure that we can disable the warning. */
434 int
fn_32(int flag)435 fn_32 (int flag)
436 {
437 int x = 4, y = 5;
438 #pragma GCC diagnostic push
439 #pragma GCC diagnostic ignored "-Wmisleading-indentation"
440 if (flag)
441 x = 3;
442 y = 2;
443 #pragma GCC diagnostic pop
444
445 return x * y;
446 }
447
448 /* Verify that a variety of different indentation styles are supported
449 without leading to warnings. */
450 void
fn_33_k_and_r_style(void)451 fn_33_k_and_r_style (void)
452 {
453 int i;
454 for (i = 0; i < 10; i++) {
455 if (flagB) {
456 foo(0);
457 foo(1);
458 } else {
459 foo(2);
460 foo(3);
461 }
462 foo(4);
463 }
464 }
465
466 void
fn_33_stroustrup_style(void)467 fn_33_stroustrup_style (void)
468 {
469 int i;
470 for (i = 0; i < 10; i++) {
471 if (flagA) {
472 foo(0);
473 foo(1);
474 }
475 else {
476 foo(2);
477 foo(3);
478 }
479 foo(4);
480 }
481 }
482
483 void
fn_33_allman_style(void)484 fn_33_allman_style (void)
485 {
486 int i;
487 for (i = 0; i < 10; i++)
488 {
489 if (flagA)
490 {
491 foo(0);
492 foo(1);
493 }
494 else
495 {
496 foo(2);
497 foo(3);
498 }
499 foo(4);
500 }
501 }
502
503 void
fn_33_whitesmiths_style(void)504 fn_33_whitesmiths_style (void)
505 {
506 int i;
507 for (i = 0; i < 10; i++)
508 {
509 if (flagA)
510 {
511 foo(0);
512 foo(1);
513 }
514 else
515 {
516 foo(2);
517 foo(3);
518 }
519 foo(4);
520 }
521 }
522
523 void
fn_33_horstmann_style(void)524 fn_33_horstmann_style (void)
525 {
526 int i;
527 for (i = 0; i < 10; i++)
528 { if (flagA)
529 { foo(0);
530 foo(1);
531 }
532 else
533 { foo(2);
534 foo(3);
535 }
536 foo(4);
537 }
538 }
539
540 void
fn_33_ratliff_banner_style(void)541 fn_33_ratliff_banner_style (void)
542 {
543 int i;
544 for (i = 0; i < 10; i++) {
545 if (flagA) {
546 foo(0);
547 foo(1);
548 }
549 else {
550 foo(2);
551 foo(3);
552 }
553 foo(4);
554 }
555 }
556
557 void
fn_33_lisp_style(void)558 fn_33_lisp_style (void)
559 {
560 int i;
561 for (i = 0; i < 10; i++) {
562 if (flagA) {
563 foo(0);
564 foo(1); }
565 else {
566 foo(2);
567 foo(3); }
568 foo(4); }
569 }
570
571 /* A function run through GNU "indent" with various options.
572 None of these should lead to warnings. */
573
574 /* "indent -gnu". */
575 void
fn_34_indent_dash_gnu(void)576 fn_34_indent_dash_gnu (void)
577 {
578 int i;
579 while (flagA)
580 for (i = 0; i < 10; i++)
581 {
582 if (flagB)
583 {
584 foo (0);
585 foo (1);
586 }
587 else
588 {
589 foo (2);
590 foo (3);
591 }
592 foo (4);
593 }
594 foo (5);
595 }
596
597 /* "indent -kr". */
fn_34_indent_dash_kr(void)598 void fn_34_indent_dash_kr(void)
599 {
600 int i;
601 while (flagA)
602 for (i = 0; i < 10; i++) {
603 if (flagB) {
604 foo(0);
605 foo(1);
606 } else {
607 foo(2);
608 foo(3);
609 }
610 foo(4);
611 }
612 foo(5);
613 }
614
615 /* "indent -orig". */
616 void
fn_34_indent_dash_orig(void)617 fn_34_indent_dash_orig(void)
618 {
619 int i;
620 while (flagA)
621 for (i = 0; i < 10; i++) {
622 if (flagB) {
623 foo(0);
624 foo(1);
625 } else {
626 foo(2);
627 foo(3);
628 }
629 foo(4);
630 }
631 foo(5);
632 }
633
634 /* Linux style:
635 "indent \
636 -nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 \
637 -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai \
638 -saf -saw -ncs -nsc -sob -nfca -cp33 -ss -ts8 -il1". */
639
fn_34_indent_linux_style(void)640 void fn_34_indent_linux_style(void)
641 {
642 int i;
643 while (flagA)
644 for (i = 0; i < 10; i++) {
645 if (flagB) {
646 foo(0);
647 foo(1);
648 } else {
649 foo(2);
650 foo(3);
651 }
652 foo(4);
653 }
654 foo(5);
655 }
656
657 /* PR 66220. */
fn_35(int v)658 int fn_35 (int v)
659 {
660 int res = 28;
661
662 if (v == 2)
663 {
664 res = 27;
665 } else
666 {
667 res = 18;
668 }
669 return res;
670 }
671
672 /* This variant of K&R-style formatting (in the presence of conditional
673 compilation) shouldn't lead to a warning.
674
675 Based on false positive seen with r223098 when compiling
676 linux-4.0.3:arch/x86/crypto/aesni-intel_glue.c:aesni_init. */
677 void
fn_36(void)678 fn_36 (void)
679 {
680 #if 1 /* e.g. some configuration variable. */
681 if (flagA) {
682 foo(0);
683 foo(1);
684 foo(2);
685 } else
686 #endif
687 {
688 foo(3);
689 foo(4);
690 foo(5);
691 }
692 foo(6); /* We shouldn't warn here. */
693 }
694
695 /* The following function contain code whose indentation is misleading, thus
696 we warn about it. */
697
698 void
fn_37(void)699 fn_37 (void)
700 {
701 int i;
702
703 #define EMPTY
704 #define FOR_EACH(VAR, START, STOP) for (VAR = START; VAR < STOP; VAR++) /* { dg-warning "this 'for' clause" } */
705
706 while (flagA); /* { dg-warning "3: this 'while' clause" } */
707 foo (0); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
708
709 if (flagA)
710 ;
711 else if (flagB); /* { dg-warning "8: this 'if' clause" } */
712 foo (0); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
713 while (flagA) /* { dg-warning "3: this 'while' clause" } */
714 /* blah */;
715 foo (0); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
716
717 if (flagA)
718 ;
719 else if (flagB) /* { dg-warning "8: this 'if' clause" } */
720 foo (1);
721 foo (2); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
722
723 if (flagA)
724 foo (1);
725 else if (flagB) /* { dg-warning "8: this 'if' clause" } */
726 foo (2);
727 foo (3); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
728
729 if (flagB) /* { dg-warning "3: this 'if' clause" } */
730 /* blah */;
731 { /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
732 foo (0);
733 }
734
735 if (flagB) /* { dg-warning "3: this 'if' clause" } */
736 /* blah */;
737 { /* { dg-message "4: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
738 foo (0);
739 }
740
741
742 if (flagB)
743 ;
744 else; foo (0); /* { dg-warning "3: this 'else' clause" } */
745
746 if (flagC); foo (2); /* { dg-warning "3: this 'if' clause" } */
747
748 if (flagA) /* { dg-warning "3: this 'if' clause" } */
749 ; /* blah */ { /* { dg-message "18: ...this statement" } */
750 foo (1);
751 }
752
753 if (flagB) ; /* { dg-warning "3: this 'if' clause" } */
754 return; /* { dg-message "5: ...this statement" } */
755
756 if (flagB) EMPTY; /* { dg-warning "3: this 'if' clause" } */
757 foo (1); /* { dg-message "5: ...this statement" } */
758
759 for (i = 0; i < 10; i++); /* { dg-warning "3: this 'for' clause" } */
760 foo (2); /* { dg-message "5: ...this statement" } */
761
762 FOR_EACH (i, 0, 10); /* { dg-message "3: in expansion of macro .FOR_EACH." } */
763 foo (2); /* { dg-message "5: ...this statement" } */
764
765 FOR_EACH (i, 0, 10); /* { dg-message "3: in expansion of macro .FOR_EACH." } */
766 { /* { dg-message "5: ...this statement" } */
767 foo (3);
768 }
769
770 FOR_EACH (i, 0, 10); /* { dg-message "3: in expansion of macro .FOR_EACH." } */
771 { /* { dg-message "3: ...this statement" } */
772 foo (3);
773 }
774
775 while (i++); { /* { dg-warning "3: this 'while' clause" } */
776 foo (3);
777 }
778
779 if (i++); { /* { dg-warning "3: this 'if' clause" } */
780 foo (3);
781 }
782
783 if (flagA) {
784 foo (1);
785 } else /* { dg-warning "5: this 'else' clause" } */
786 if (flagB)
787 foo (2);
788 foo (3); /* { dg-message "5: ...this statement" } */
789
790 if (flagA)
791 foo (1);
792 else if (flagB); /* { dg-warning "8: this 'if' clause" } */
793 foo (2); /* { dg-message "5: ...this statement" } */
794
795 for (i = 0; /* { dg-warning "3: this 'for' clause" } */
796 i < 10;
797 i++);
798 foo (i); /* { dg-message "5: ...this statement" } */
799
800 if (flagA)
801 {
802 foo (1);
803 }
804 else if (flagB); /* { dg-warning "8: this 'if' clause" } */
805 { /* { dg-message "3: ...this statement" } */
806 foo (2);
807 }
808
809 #undef EMPTY
810 #undef FOR_EACH
811 }
812
813 /* The following function contains code whose indentation is not great but not
814 misleading, thus we don't warn. */
815
816 void
fn_38(void)817 fn_38 (void)
818 {
819 int i = 0;
820
821 while (flagA)
822 ;
823 foo (0);
824
825 if (flagB)
826 ;
827 {
828 foo (0);
829 }
830
831 while (flagC);
832 foo (2);
833
834 if (flagA)
835 while (flagC++);
836 else
837 foo (2);
838
839 if (i)
840 while (i++ < 10000);
841 foo (5);
842
843 if (i) while (i++ < 10000);
844 foo (5);
845
846 if (flagA) {
847 foo (1);
848 } else
849 if (flagB)
850 foo (2);
851 foo (3);
852
853 if (flagA)
854 {
855 foo (1);
856 } else
857 if (flagB)
858 foo (2);
859 foo (3);
860
861 for (i = 0;
862 i < 10;
863 i++
864 );
865 foo (i);
866 }
867
868 /* The following function contains good indentation which we definitely should
869 not warn about. */
870
871 void
fn_39(void)872 fn_39 (void)
873 {
874 int i;
875
876 if (flagA)
877 ;
878 if (flagB)
879 ;
880
881 if (flagA)
882 if (flagB)
883 foo (0);
884 else
885 foo (1);
886 else
887 foo (2);
888
889 for (i = 0;
890 i < 10;
891 i++);
892 foo (i);
893
894 do foo (0); while (flagA);
895 }
896
897 /* We shouldn't complain about the following function. */
898 #define emit
pr69122(void)899 void pr69122 (void)
900 {
901 if (flagA)
902 foo (0);
903 emit foo (1);
904 }
905 #undef emit
906
907 /* In the following, the 'if' within the 'for' statement is not indented,
908 but arguably should be.
909 The for loop:
910 "for (cnt = 0; cnt < thousands_len; ++cnt)"
911 does not guard this conditional:
912 "cnt < thousands_len;".
913 and the poor indentation is not misleading. Verify that we do
914 not erroneously emit a warning about this.
915 Based on an example seen in glibc (PR c/68187). */
916
917 void
fn_40_a(const char * end,const char * thousands,int thousands_len)918 fn_40_a (const char *end, const char *thousands, int thousands_len)
919 {
920 int cnt;
921
922 while (flagA)
923 if (flagA
924 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
925 if (thousands[cnt] != end[cnt])
926 break;
927 cnt < thousands_len; })
928 && flagB)
929 break;
930 }
931
932 /* As above, but with the indentation within the "for" loop fixed.
933 We should not emit a warning for this, either. */
934
935 void
fn_40_b(const char * end,const char * thousands,int thousands_len)936 fn_40_b (const char *end, const char *thousands, int thousands_len)
937 {
938 int cnt;
939
940 while (flagA)
941 if (flagA
942 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
943 if (thousands[cnt] != end[cnt])
944 break;
945 cnt < thousands_len; })
946 && flagB)
947 break;
948 }
949
950 /* We should not warn for the following
951 (based on libstdc++-v3/src/c++11/random.cc:random_device::_M_init). */
952
953 void
fn_41_a(void)954 fn_41_a (void)
955 {
956 if (flagA)
957 {
958 }
959 else if (flagB)
960 fail:
961 foo (0);
962
963 foo (1);
964 if (!flagC)
965 goto fail;
966 }
967
968 /* Tweaked version of the above (with the label indented), which we should
969 also not warn for. */
970
971 void
fn_41_b(void)972 fn_41_b (void)
973 {
974 if (flagA)
975 {
976 }
977 else if (flagB)
978 fail:
979 foo (0);
980
981 foo (1);
982 if (!flagC)
983 goto fail;
984 }
985
986 /* In the following, the
987 "if (i > 0)"
988 is poorly indented, and ought to be on the same column as
989 "engine_ref_debug(e, 0, -1)"
990 However, it is not misleadingly indented, due to the presence
991 of that macro. Verify that we do not emit a warning about it
992 not being guarded by the "else" clause above.
993
994 Based on an example seen in OpenSSL 1.0.1, which was filed as
995 PR c/68187 in comment #1, though it's arguably a separate bug to
996 the one in comment #0. */
997
998 int
fn_42_a(int locked)999 fn_42_a (int locked)
1000 {
1001 #define engine_ref_debug(X, Y, Z)
1002
1003 int i;
1004
1005 if (locked)
1006 i = foo (0);
1007 else
1008 i = foo (1);
1009 engine_ref_debug(e, 0, -1)
1010 if (i > 0)
1011 return 1;
1012 return 0;
1013 #undef engine_ref_debug
1014 }
1015
1016 /* As above, but the empty macro is at the same indentation level.
1017 This *is* misleading; verify that we do emit a warning about it. */
1018
1019 int
fn_42_b(int locked)1020 fn_42_b (int locked)
1021 {
1022 #define engine_ref_debug(X, Y, Z)
1023
1024 int i;
1025
1026 if (locked)
1027 i = foo (0);
1028 else /* { dg-warning "this .else. clause" } */
1029 i = foo (1);
1030 engine_ref_debug(e, 0, -1)
1031 if (i > 0) /* { dg-message "...this statement" } */
1032 return 1;
1033 return 0;
1034 #undef engine_ref_debug
1035 }
1036
1037 /* As above, but where the body is a semicolon "hidden" by a preceding
1038 comment, where the semicolon is not in the same column as the successor
1039 "if" statement, but the empty macro expansion is at the same indentation
1040 level as the guard.
1041 This is poor indentation, but not misleading; verify that we don't emit a
1042 warning about it. */
1043
1044 int
fn_42_c(int locked,int i)1045 fn_42_c (int locked, int i)
1046 {
1047 #define engine_ref_debug(X, Y, Z)
1048
1049 if (locked)
1050 /* blah */;
1051 engine_ref_debug(e, 0, -1)
1052 if (i > 0)
1053 return 1;
1054 return 0;
1055 #undef engine_ref_debug
1056 }
1057
1058 /* We shouldn't complain about the following function. */
1059 #define ENABLE_FEATURE
pr70085(int x,int y)1060 int pr70085 (int x, int y)
1061 {
1062 if (x > y)
1063 return x - y;
1064
1065 #ifdef ENABLE_FEATURE
1066 if (x == y)
1067 return 0;
1068 #endif
1069
1070 return -1;
1071 }
1072 #undef ENABLE_FEATURE
1073
1074 /* Additional test coverage for PR c/68187, with various locations for a
1075 pair of aligned statements ("foo (2);" and "foo (3);") that may or may
1076 not be misleadingly indented. */
1077
1078 /* Before the "}".
1079
1080 The two statements aren't visually "within" the above line, so we
1081 shouldn't warn. */
1082
1083 void
test43_a(void)1084 test43_a (void)
1085 {
1086 if (flagA) {
1087 foo (1);
1088 } else if (flagB)
1089 foo (2);
1090 foo (3);
1091 }
1092
1093 /* Aligned with the "}".
1094
1095 Again, the two statements aren't visually "within" the above line, so we
1096 shouldn't warn. */
1097
1098 void
test43_b(void)1099 test43_b (void)
1100 {
1101 if (flagA) {
1102 foo (1);
1103 } else if (flagB)
1104 foo (2);
1105 foo (3);
1106 }
1107
1108 /* Indented between the "}" and the "else".
1109
1110 The two statements are indented "within" the line above, so appear that
1111 they would be guarded together. We should warn about this. */
1112
1113 void
test43_c(void)1114 test43_c (void)
1115 {
1116 if (flagA) {
1117 foo (1);
1118 } else if (flagB) /* { dg-message "...this .if. clause" } */
1119 foo (2);
1120 foo (3); /* { dg-message "...this statement" } */
1121 }
1122
1123 /* Aligned with the "else". Likewise, we should warn. */
1124
1125 void
test43_d(void)1126 test43_d (void)
1127 {
1128 if (flagA) {
1129 foo (1);
1130 } else if (flagB) /* { dg-message "...this .if. clause" } */
1131 foo (2);
1132 foo (3); /* { dg-message "...this statement" } */
1133 }
1134
1135 /* Indented between the "else" and the "if". Likewise, we should warn. */
1136
1137 void
test43_e(void)1138 test43_e (void)
1139 {
1140 if (flagA) {
1141 foo (1);
1142 } else if (flagB) /* { dg-message "...this .if. clause" } */
1143 foo (2);
1144 foo (3); /* { dg-message "...this statement" } */
1145 }
1146
1147 /* Aligned with the "if". Likewise, we should warn. */
1148
1149 void
test43_f(void)1150 test43_f (void)
1151 {
1152 if (flagA) {
1153 foo (1);
1154 } else if (flagB) /* { dg-warning "this .else. clause" } */
1155 foo (2);
1156 foo (3); /* { dg-message "...this statement" } */
1157 }
1158
1159 /* Indented more than the "if". Likewise, we should warn. */
1160
1161 void
test43_g(void)1162 test43_g (void)
1163 {
1164 if (flagA) {
1165 foo (1);
1166 } else if (flagB) /* { dg-message "...this .if. clause" } */
1167 foo (2);
1168 foo (3); /* { dg-message "...this statement" } */
1169 }
1170
1171 /* Again, but without the 2nd "if". */
1172
1173 /* Before the "}".
1174
1175 As before, the two statements aren't visually "within" the above line,
1176 so we shouldn't warn. */
1177
1178 void
test44_a(void)1179 test44_a (void)
1180 {
1181 if (flagA) {
1182 foo (1);
1183 } else
1184 foo (2);
1185 foo (3);
1186 }
1187
1188 /* Aligned with the "}".
1189
1190 As before, the two statements aren't visually "within" the above line,
1191 so we shouldn't warn. */
1192
1193 void
test44_b(void)1194 test44_b (void)
1195 {
1196 if (flagA) {
1197 foo (1);
1198 } else
1199 foo (2);
1200 foo (3);
1201 }
1202
1203 /* Indented between the "}" and the "else".
1204
1205 The two statements are indented "within" the line above, so appear that
1206 they would be guarded together. We should warn about this. */
1207
1208 void
test44_c(void)1209 test44_c (void)
1210 {
1211 if (flagA) {
1212 foo (1);
1213 } else /* { dg-warning "this .else. clause" } */
1214 foo (2);
1215 foo (3); /* { dg-message "...this statement" } */
1216 }
1217
1218 /* Aligned with the "else". Likewise, we should warn. */
1219
1220 void
test44_d(void)1221 test44_d (void)
1222 {
1223 if (flagA) {
1224 foo (1);
1225 } else /* { dg-warning "this .else. clause" } */
1226 foo (2);
1227 foo (3); /* { dg-message "...this statement" } */
1228 }
1229
1230 /* Indented more than the "else". Likewise, we should warn. */
1231
1232 void
test44_e(void)1233 test44_e (void)
1234 {
1235 if (flagA) {
1236 foo (1);
1237 } else /* { dg-warning "this .else. clause" } */
1238 foo (2);
1239 foo (3); /* { dg-message "...this statement" } */
1240 }
1241