1 /* { dg-options "-Wmisleading-indentation -Wall" } */
2 /* { dg-do compile } */
5 extern int bar (int, int);
15 if (flag
) /* { dg-warning "3: this 'if' clause does not guard..." } */
17 y
= 2; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
22 fn_2 (int flag
, int x
, int y
)
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'" } */
36 else /* { dg-warning "3: this 'else' clause does not guard..." } */
38 y
= 2; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'" } */
43 fn_4 (double *a
, double *b
, double *c
)
46 while (i
< 10) /* { dg-warning "3: this 'while' clause does not guard..." } */
48 i
++; /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
52 fn_5 (double *a
, double *b
, double *sum
, double *prod
)
55 for (i
= 0; i
< 10; i
++) /* { dg-warning "3: this 'for' clause does not guard..." } */
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'" } */
60 /* Based on CVE-2014-1266 aka "goto fail" */
61 int fn_6 (int a
, int b
, int c
)
66 if ((err
= foo (a
)) != 0)
68 if ((err
= foo (b
)) != 0) /* { dg-message "2: this 'if' clause does not guard..." } */
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)
79 int fn_7 (int p
, int q
, int r
, int s
, int t
)
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'" } */
87 return p
+ q
+ r
+ s
+ t
;
90 int fn_8 (int a
, int b
, int c
)
92 /* This should *not* be flagged as misleading indentation. */
93 if (a
) return b
; else return c
;
98 if (flag
) /* { dg-warning "3: this 'if' clause does not guard..." } */
100 foo (1); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
103 void fn_10 (int flag
)
105 if (flag
) /* { dg-warning "3: this 'if' clause does not guard..." } */
111 foo (2); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
119 if (flagC
) /* { dg-message "7: this 'if' clause does not guard..." } */
121 bar (1, 2); /* { dg-message "9: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
127 if (flagB
) /* { dg-message "5: this 'if' clause does not guard..." } */
130 bar (1, 2); /* { dg-message "7: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
135 if (flagA
) /* { dg-warning "3: this 'if' clause does not guard..." } */
139 bar (1, 2); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
142 #define FOR_EACH(VAR, START, STOP) \
143 for ((VAR) = (START); (VAR) < (STOP); (VAR++)) /* { dg-warning "3: this 'for' clause does not guard..." } */
148 FOR_EACH (i
, 0, 10) /* { dg-message "in expansion of macro .FOR_EACH." } */
150 bar (i
, i
); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
154 #define FOR_EACH(VAR, START, STOP) for ((VAR) = (START); (VAR) < (STOP); (VAR++)) /* { dg-message "36: this 'for' clause does not guard..." } */
158 FOR_EACH (i
, 0, 10) /* { dg-message "in expansion of macro .FOR_EACH." } */
160 bar (i
, i
); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
164 void fn_16_spaces (void)
167 for (i
= 0; i
< 10; i
++)
169 if (flagB
) /* { dg-message "7: this 'if' clause does not guard..." } */
171 foo (1); /* { dg-message "9: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
174 void fn_16_tabs (void)
177 for (i
= 0; i
< 10; i
++)
179 if (flagB
) /* { dg-message "7: this 'if' clause does not guard..." } */
181 foo (1);/* { dg-message "2: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
184 void fn_17_spaces (void)
187 for (i
= 0; i
< 10; i
++) /* { dg-warning "3: this 'for' clause does not guard..." } */
191 foo (1);/* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
194 void fn_17_tabs (void)
197 for (i
= 0; i
< 10; i
++) /* { dg-warning "3: this 'for' clause does not guard..." } */
201 foo (1);/* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'" } */
204 void fn_18_spaces (void)
207 for (i
= 0; i
< 10; i
++)
208 while (flagA
) /* { dg-message "5: this 'while' clause does not guard..." } */
211 foo (1);/* { dg-message "7: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
214 void fn_18_tabs (void)
217 for (i
= 0; i
< 10; i
++)
218 while (flagA
) /* { dg-message "5: this 'while' clause does not guard..." } */
221 foo (1);/* { dg-message "7: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
224 /* This shouldn't lead to a warning. */
225 int fn_19 (void) { if (flagA
) return 1; else return 0; }
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. */
231 fn_20 (unsigned int l
)
235 for (i
= 0; i
< 10; i
++)
237 unsigned int n0
, n1
, n2
, n3
, n4
, n5
, n6
, n7
, n8
, n9
, n10
, n11
;
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
++)
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). */
289 /* The conditionals within the following macros shouldn't be warned about.
290 Adapted from libgomp/driver.c: gomp_load_plugin_for_device. */
303 #define DLSYM_OPT() \
323 /* This shouldn't be warned about. */
324 void fn_23 (void) { foo (0); foo (1); if (flagA
) foo (2); foo (3); foo (4); }
326 /* Code that simply doesn't bother indenting anywhere (e.g. autogenerated
327 code) shouldn't be warned about. */
336 /* Adapted from libiberty/regex.c; an example of a conditional in a
337 macro where the successor statement begins with a macro arg:
341 num = num * 10 + c - '0';
342 ^ this successor statement
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.
348 This should not generate a misleading indentation warning. */
350 # define GET_UNSIGNED_NUMBER(num) \
358 num = num * 10 + c - '0'; \
362 void fn_25 (int c
, int lower_bound
, int upper_bound
)
364 GET_UNSIGNED_NUMBER (lower_bound
);
366 #undef GET_UNSIGNED_NUMBER
368 /* Example adapted from libdecnumber/decNumber.c:decExpOp that shouldn't
369 trigger a warning. */
373 if (flagB
) foo (0); }
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. */
387 /* Example adapted from gcc/cgraph.h:symtab_node::get_availability of
388 a spurious trailing semicolon that shouldn't generate a warning. */
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. */
408 /* Adapted from usage site of #ifdef HAVE_cc0. This should not lead
409 to a warning from -Wmisleading-indentation. */
414 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
420 /* This shouldn't lead to a warning. */
433 /* Ensure that we can disable the warning. */
438 #pragma GCC diagnostic push
439 #pragma GCC diagnostic ignored "-Wmisleading-indentation"
443 #pragma GCC diagnostic pop
448 /* Verify that a variety of different indentation styles are supported
449 without leading to warnings. */
451 fn_33_k_and_r_style (void)
454 for (i
= 0; i
< 10; i
++) {
467 fn_33_stroustrup_style (void)
470 for (i
= 0; i
< 10; i
++) {
484 fn_33_allman_style (void)
487 for (i
= 0; i
< 10; i
++)
504 fn_33_whitesmiths_style (void)
507 for (i
= 0; i
< 10; i
++)
524 fn_33_horstmann_style (void)
527 for (i
= 0; i
< 10; i
++)
541 fn_33_ratliff_banner_style (void)
544 for (i
= 0; i
< 10; i
++) {
558 fn_33_lisp_style (void)
561 for (i
= 0; i
< 10; i
++) {
571 /* A function run through GNU "indent" with various options.
572 None of these should lead to warnings. */
576 fn_34_indent_dash_gnu (void)
580 for (i
= 0; i
< 10; i
++)
598 void fn_34_indent_dash_kr(void)
602 for (i
= 0; i
< 10; i
++) {
615 /* "indent -orig". */
617 fn_34_indent_dash_orig(void)
621 for (i
= 0; i
< 10; i
++) {
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". */
640 void fn_34_indent_linux_style(void)
644 for (i
= 0; i
< 10; i
++) {
672 /* This variant of K&R-style formatting (in the presence of conditional
673 compilation) shouldn't lead to a warning.
675 Based on false positive seen with r223098 when compiling
676 linux-4.0.3:arch/x86/crypto/aesni-intel_glue.c:aesni_init. */
680 #if 1 /* e.g. some configuration variable. */
692 foo(6); /* We shouldn't warn here. */
695 /* The following function contain code whose indentation is misleading, thus
704 #define FOR_EACH(VAR, START, STOP) for (VAR = START; VAR < STOP; VAR++) /* { dg-warning "this 'for' clause" } */
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'" } */
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" } */
715 foo (0); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'" } */
719 else if (flagB
) /* { dg-warning "8: this 'if' clause" } */
721 foo (2); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
725 else if (flagB
) /* { dg-warning "8: this 'if' clause" } */
727 foo (3); /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
729 if (flagB
) /* { dg-warning "3: this 'if' clause" } */
731 { /* { dg-message "5: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
735 if (flagB
) /* { dg-warning "3: this 'if' clause" } */
737 { /* { dg-message "4: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'" } */
744 else; foo (0); /* { dg-warning "3: this 'else' clause" } */
746 if (flagC
); foo (2); /* { dg-warning "3: this 'if' clause" } */
748 if (flagA
) /* { dg-warning "3: this 'if' clause" } */
749 ; /* blah */ { /* { dg-message "18: ...this statement" } */
753 if (flagB
) ; /* { dg-warning "3: this 'if' clause" } */
754 return; /* { dg-message "5: ...this statement" } */
756 if (flagB
) EMPTY
; /* { dg-warning "3: this 'if' clause" } */
757 foo (1); /* { dg-message "5: ...this statement" } */
759 for (i
= 0; i
< 10; i
++); /* { dg-warning "3: this 'for' clause" } */
760 foo (2); /* { dg-message "5: ...this statement" } */
762 FOR_EACH (i
, 0, 10); /* { dg-message "3: in expansion of macro .FOR_EACH." } */
763 foo (2); /* { dg-message "5: ...this statement" } */
765 FOR_EACH (i
, 0, 10); /* { dg-message "3: in expansion of macro .FOR_EACH." } */
766 { /* { dg-message "5: ...this statement" } */
770 FOR_EACH (i
, 0, 10); /* { dg-message "3: in expansion of macro .FOR_EACH." } */
771 { /* { dg-message "3: ...this statement" } */
775 while (i
++); { /* { dg-warning "3: this 'while' clause" } */
779 if (i
++); { /* { dg-warning "3: this 'if' clause" } */
785 } else /* { dg-warning "5: this 'else' clause" } */
788 foo (3); /* { dg-message "5: ...this statement" } */
792 else if (flagB
); /* { dg-warning "8: this 'if' clause" } */
793 foo (2); /* { dg-message "5: ...this statement" } */
795 for (i
= 0; /* { dg-warning "3: this 'for' clause" } */
798 foo (i
); /* { dg-message "5: ...this statement" } */
804 else if (flagB
); /* { dg-warning "8: this 'if' clause" } */
805 { /* { dg-message "3: ...this statement" } */
813 /* The following function contains code whose indentation is not great but not
814 misleading, thus we don't warn. */
843 if (i
) while (i
++ < 10000);
868 /* The following function contains good indentation which we definitely should
894 do foo (0); while (flagA
);
897 /* We shouldn't complain about the following function. */
907 /* In the following, the 'if' within the 'for' statement is not indented,
908 but arguably should be.
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). */
918 fn_40_a (const char *end
, const char *thousands
, int thousands_len
)
924 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
925 if (thousands
[cnt
] != end
[cnt
])
927 cnt
< thousands_len
; })
932 /* As above, but with the indentation within the "for" loop fixed.
933 We should not emit a warning for this, either. */
936 fn_40_b (const char *end
, const char *thousands
, int thousands_len
)
942 && ({ for (cnt
= 0; cnt
< thousands_len
; ++cnt
)
943 if (thousands
[cnt
] != end
[cnt
])
945 cnt
< thousands_len
; })
950 /* We should not warn for the following
951 (based on libstdc++-v3/src/c++11/random.cc:random_device::_M_init). */
968 /* Tweaked version of the above (with the label indented), which we should
969 also not warn for. */
986 /* In the following, the
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.
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. */
1001 #define engine_ref_debug(X, Y, Z)
1009 engine_ref_debug(e
, 0, -1)
1013 #undef engine_ref_debug
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. */
1020 fn_42_b (int locked
)
1022 #define engine_ref_debug(X, Y, Z)
1028 else /* { dg-warning "this .else. clause" } */
1030 engine_ref_debug(e
, 0, -1)
1031 if (i
> 0) /* { dg-message "...this statement" } */
1034 #undef engine_ref_debug
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
1041 This is poor indentation, but not misleading; verify that we don't emit a
1042 warning about it. */
1045 fn_42_c (int locked
, int i
)
1047 #define engine_ref_debug(X, Y, Z)
1051 engine_ref_debug(e
, 0, -1)
1055 #undef engine_ref_debug
1058 /* We shouldn't complain about the following function. */
1059 #define ENABLE_FEATURE
1060 int pr70085 (int x
, int y
)
1065 #ifdef ENABLE_FEATURE
1072 #undef ENABLE_FEATURE
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. */
1080 The two statements aren't visually "within" the above line, so we
1093 /* Aligned with the "}".
1095 Again, the two statements aren't visually "within" the above line, so we
1108 /* Indented between the "}" and the "else".
1110 The two statements are indented "within" the line above, so appear that
1111 they would be guarded together. We should warn about this. */
1118 } else if (flagB
) /* { dg-message "...this .if. clause" } */
1120 foo (3); /* { dg-message "...this statement" } */
1123 /* Aligned with the "else". Likewise, we should warn. */
1130 } else if (flagB
) /* { dg-message "...this .if. clause" } */
1132 foo (3); /* { dg-message "...this statement" } */
1135 /* Indented between the "else" and the "if". Likewise, we should warn. */
1142 } else if (flagB
) /* { dg-message "...this .if. clause" } */
1144 foo (3); /* { dg-message "...this statement" } */
1147 /* Aligned with the "if". Likewise, we should warn. */
1154 } else if (flagB
) /* { dg-warning "this .else. clause" } */
1156 foo (3); /* { dg-message "...this statement" } */
1159 /* Indented more than the "if". Likewise, we should warn. */
1166 } else if (flagB
) /* { dg-message "...this .if. clause" } */
1168 foo (3); /* { dg-message "...this statement" } */
1171 /* Again, but without the 2nd "if". */
1175 As before, the two statements aren't visually "within" the above line,
1176 so we shouldn't warn. */
1188 /* Aligned with the "}".
1190 As before, the two statements aren't visually "within" the above line,
1191 so we shouldn't warn. */
1203 /* Indented between the "}" and the "else".
1205 The two statements are indented "within" the line above, so appear that
1206 they would be guarded together. We should warn about this. */
1213 } else /* { dg-warning "this .else. clause" } */
1215 foo (3); /* { dg-message "...this statement" } */
1218 /* Aligned with the "else". Likewise, we should warn. */
1225 } else /* { dg-warning "this .else. clause" } */
1227 foo (3); /* { dg-message "...this statement" } */
1230 /* Indented more than the "else". Likewise, we should warn. */
1237 } else /* { dg-warning "this .else. clause" } */
1239 foo (3); /* { dg-message "...this statement" } */