1 /* Implementation of -Wmisleading-indentation
2 Copyright (C) 2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "c-indentation.h"
31 extern cpp_options
*cpp_opts
;
33 /* Convert libcpp's notion of a column (a 1-based char count) to
34 the "visual column" (0-based column, respecting tabs), by reading the
37 Returns true if a conversion was possible, writing the result to OUT,
38 otherwise returns false. If FIRST_NWS is not NULL, then write to it
39 the visual column corresponding to the first non-whitespace character
43 get_visual_column (expanded_location exploc
,
45 unsigned int *first_nws
= NULL
)
48 const char *line
= location_get_source_line (exploc
.file
, exploc
.line
,
52 unsigned int vis_column
= 0;
53 for (int i
= 1; i
< exploc
.column
; i
++)
55 unsigned char ch
= line
[i
- 1];
57 if (first_nws
!= NULL
&& !ISSPACE (ch
))
59 *first_nws
= vis_column
;
65 /* Round up to nearest tab stop. */
66 const unsigned int tab_width
= cpp_opts
->tabstop
;
67 vis_column
= ((vis_column
+ tab_width
) / tab_width
) * tab_width
;
73 if (first_nws
!= NULL
)
74 *first_nws
= vis_column
;
80 /* Does the given source line appear to contain a #if directive?
81 (or #ifdef/#ifndef). Ignore the possibility of it being inside a
82 comment, for simplicity.
83 Helper function for detect_preprocessor_logic. */
86 line_contains_hash_if (const char *file
, int line_num
)
89 const char *line
= location_get_source_line (file
, line_num
, &line_len
);
95 /* Skip leading whitespace. */
96 for (idx
= 0; idx
< line_len
; idx
++)
97 if (!ISSPACE (line
[idx
]))
102 /* Require a '#' character. */
103 if (line
[idx
] != '#')
107 /* Skip whitespace. */
108 while (idx
< line_len
)
110 if (!ISSPACE (line
[idx
]))
115 /* Match #if/#ifdef/#ifndef. */
116 if (idx
+ 2 <= line_len
)
117 if (line
[idx
] == 'i')
118 if (line
[idx
+ 1] == 'f')
125 /* Determine if there is preprocessor logic between
126 BODY_EXPLOC and NEXT_STMT_EXPLOC, to ensure that we don't
127 issue a warning for cases like this:
132 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
138 despite "bar ();" being visually aligned below "foo ();" and
139 being (as far as the parser sees) the next token.
141 Return true if such logic is detected. */
144 detect_preprocessor_logic (expanded_location body_exploc
,
145 expanded_location next_stmt_exploc
)
147 gcc_assert (next_stmt_exploc
.file
== body_exploc
.file
);
148 gcc_assert (next_stmt_exploc
.line
> body_exploc
.line
);
150 if (next_stmt_exploc
.line
- body_exploc
.line
< 4)
153 /* Is there a #if/#ifdef/#ifndef directive somewhere in the lines
154 between the given locations?
156 This is something of a layering violation, but by necessity,
157 given the nature of what we're testing for. For example,
158 in theory we could be fooled by a #if within a comment, but
159 it's unlikely to matter. */
160 for (int line
= body_exploc
.line
+ 1; line
< next_stmt_exploc
.line
; line
++)
161 if (line_contains_hash_if (body_exploc
.file
, line
))
169 /* Helper function for warn_for_misleading_indentation; see
170 description of that function below. */
173 should_warn_for_misleading_indentation (const token_indent_info
&guard_tinfo
,
174 const token_indent_info
&body_tinfo
,
175 const token_indent_info
&next_tinfo
)
177 location_t guard_loc
= guard_tinfo
.location
;
178 location_t body_loc
= body_tinfo
.location
;
179 location_t next_stmt_loc
= next_tinfo
.location
;
181 enum cpp_ttype body_type
= body_tinfo
.type
;
182 enum cpp_ttype next_tok_type
= next_tinfo
.type
;
184 /* Don't attempt to compare the indentation of BODY_LOC and NEXT_STMT_LOC
185 if either are within macros. */
186 if (linemap_location_from_macro_expansion_p (line_table
, body_loc
)
187 || linemap_location_from_macro_expansion_p (line_table
, next_stmt_loc
))
190 /* Don't attempt to compare indentation if #line or # 44 "file"-style
191 directives are present, suggesting generated code.
193 All bets are off if these are present: the file that the #line
194 directive could have an entirely different coding layout to C/C++
197 To determine if a #line is present, in theory we could look for a
198 map with reason == LC_RENAME_VERBATIM. However, if there has
199 subsequently been a long line requiring a column number larger than
200 that representable by the original LC_RENAME_VERBATIM map, then
201 we'll have a map with reason LC_RENAME.
202 Rather than attempting to search all of the maps for a
203 LC_RENAME_VERBATIM, instead we have libcpp set a flag whenever one
204 is seen, and we check for the flag here.
206 if (line_table
->seen_line_directive
)
209 /* If the token following the body is a close brace or an "else"
210 then while indentation may be sloppy, there is not much ambiguity
211 about control flow, e.g.
223 if (next_tok_type
== CPP_CLOSE_BRACE
224 || next_tinfo
.keyword
== RID_ELSE
)
227 /* Likewise, if the body of the guard is a compound statement then control
228 flow is quite visually explicit regardless of the code's possibly poor
237 Things only get muddy when the body of the guard does not have
244 if (body_type
== CPP_OPEN_BRACE
)
247 /* Don't warn here about spurious semicolons. */
248 if (next_tok_type
== CPP_SEMICOLON
)
251 expanded_location body_exploc
= expand_location (body_loc
);
252 expanded_location next_stmt_exploc
= expand_location (next_stmt_loc
);
253 expanded_location guard_exploc
= expand_location (guard_loc
);
255 /* They must be in the same file. */
256 if (next_stmt_exploc
.file
!= body_exploc
.file
)
259 /* If NEXT_STMT_LOC and BODY_LOC are on the same line, consider
260 the location of the guard.
262 Cases where we want to issue a warning:
268 if (flag) foo (); bar ();
279 Cases where we don't want to issue a warning:
281 various_code (); if (flag) foo (); bar (); more_code ();
282 ^ DON'T WARN HERE. */
283 if (next_stmt_exploc
.line
== body_exploc
.line
)
285 if (guard_exploc
.file
!= body_exploc
.file
)
287 if (guard_exploc
.line
< body_exploc
.line
)
288 /* The guard is on a line before a line that contains both
289 the body and the next stmt. */
291 else if (guard_exploc
.line
== body_exploc
.line
)
293 /* They're all on the same line. */
294 gcc_assert (guard_exploc
.file
== next_stmt_exploc
.file
);
295 gcc_assert (guard_exploc
.line
== next_stmt_exploc
.line
);
296 unsigned int guard_vis_column
;
297 unsigned int guard_line_first_nws
;
298 if (!get_visual_column (guard_exploc
,
300 &guard_line_first_nws
))
302 /* Heuristic: only warn if the guard is the first thing
304 if (guard_vis_column
== guard_line_first_nws
)
309 /* If NEXT_STMT_LOC is on a line after BODY_LOC, consider
310 their relative locations, and of the guard.
312 Cases where we want to issue a warning:
318 Cases where we don't want to issue a warning:
322 ^ DON'T WARN HERE (autogenerated code?)
326 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
337 if (next_stmt_exploc
.line
> body_exploc
.line
)
339 /* Determine if GUARD_LOC and NEXT_STMT_LOC are aligned on the same
340 "visual column"... */
341 unsigned int next_stmt_vis_column
;
342 unsigned int body_vis_column
;
343 unsigned int body_line_first_nws
;
344 unsigned int guard_vis_column
;
345 unsigned int guard_line_first_nws
;
346 /* If we can't determine it, don't issue a warning. This is sometimes
347 the case for input files containing #line directives, and these
348 are often for autogenerated sources (e.g. from .md files), where
349 it's not clear that it's meaningful to look at indentation. */
350 if (!get_visual_column (next_stmt_exploc
, &next_stmt_vis_column
))
352 if (!get_visual_column (body_exploc
,
354 &body_line_first_nws
))
356 if (!get_visual_column (guard_exploc
,
358 &guard_line_first_nws
))
361 if ((body_type
!= CPP_SEMICOLON
362 && next_stmt_vis_column
== body_vis_column
)
363 /* As a special case handle the case where the body is a semicolon
364 that may be hidden by a preceding comment, e.g. */
370 /* by looking instead at the column of the first non-whitespace
371 character on the body line. */
372 || (body_type
== CPP_SEMICOLON
373 && body_exploc
.line
> guard_exploc
.line
374 && body_line_first_nws
!= body_vis_column
375 && next_stmt_vis_column
> guard_line_first_nws
))
377 /* Don't warn if they are aligned on the same column
378 as the guard itself (suggesting autogenerated code that doesn't
379 bother indenting at all). We consider the column of the first
380 non-whitespace character on the guard line instead of the column
381 of the actual guard token itself because it is more sensible.
398 If we just used the column of the guard token, we would warn on
399 the first example and not warn on the second. But we want the
400 exact opposite to happen: to not warn on the first example (which
401 is probably autogenerated) and to warn on the second (whose
402 indentation is misleading). Using the column of the first
403 non-whitespace character on the guard line makes that
405 if (guard_line_first_nws
== body_vis_column
)
408 /* We may have something like:
417 in which case the columns are not aligned but the code is not
418 misleadingly indented. If the column of the body is less than
419 that of the guard line then don't warn. */
420 if (body_vis_column
< guard_line_first_nws
)
423 /* Don't warn if there is multiline preprocessor logic between
424 the two statements. */
425 if (detect_preprocessor_logic (body_exploc
, next_stmt_exploc
))
428 /* Otherwise, they are visually aligned: issue a warning. */
432 /* Also issue a warning for code having the form:
452 where the semicolon at the end of each guard is most likely spurious.
459 where the next statement is aligned with the guard.
461 if (body_type
== CPP_SEMICOLON
)
463 if (body_exploc
.line
== guard_exploc
.line
)
465 if (next_stmt_vis_column
> guard_line_first_nws
466 || (next_tok_type
== CPP_OPEN_BRACE
467 && next_stmt_vis_column
== guard_line_first_nws
))
476 /* Return the string identifier corresponding to the given guard token. */
479 guard_tinfo_to_string (const token_indent_info
&guard_tinfo
)
481 switch (guard_tinfo
.keyword
)
498 /* Called by the C/C++ frontends when we have a guarding statement at
499 GUARD_LOC containing a statement at BODY_LOC, where the block wasn't
500 written using braces, like this:
505 along with the location of the next token, at NEXT_STMT_LOC,
506 so that we can detect followup statements that are within
507 the same "visual block" as the guarded statement, but which
508 aren't logically grouped within the guarding statement, such
516 bar (); <- NEXT_STMT_LOC
518 In the above, "bar ();" isn't guarded by the "if", but
519 is indented to misleadingly suggest that it is in the same
522 GUARD_KIND identifies the kind of clause e.g. "if", "else" etc. */
525 warn_for_misleading_indentation (const token_indent_info
&guard_tinfo
,
526 const token_indent_info
&body_tinfo
,
527 const token_indent_info
&next_tinfo
)
529 /* Early reject for the case where -Wmisleading-indentation is disabled,
530 to avoid doing work only to have the warning suppressed inside the
531 diagnostic machinery. */
532 if (!warn_misleading_indentation
)
535 if (should_warn_for_misleading_indentation (guard_tinfo
,
539 if (warning_at (next_tinfo
.location
, OPT_Wmisleading_indentation
,
540 "statement is indented as if it were guarded by..."))
541 inform (guard_tinfo
.location
,
542 "...this %qs clause, but it is not",
543 guard_tinfo_to_string (guard_tinfo
));