[AArch64] PR target/68129: Define TARGET_SUPPORTS_WIDE_INT
[official-gcc.git] / gcc / c-family / c-indentation.c
bloba525e9a345afb5c5fa646b9eb02cac314c180e8d
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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "c-common.h"
26 #include "stringpool.h"
27 #include "alias.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
35 relevant line.
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
40 on the line. */
42 static bool
43 get_visual_column (expanded_location exploc,
44 unsigned int *out,
45 unsigned int *first_nws = NULL)
47 int line_len;
48 const char *line = location_get_source_line (exploc.file, exploc.line,
49 &line_len);
50 if (!line)
51 return false;
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;
60 first_nws = NULL;
63 if (ch == '\t')
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;
69 else
70 vis_column++;
73 if (first_nws != NULL)
74 *first_nws = vis_column;
76 *out = vis_column;
77 return true;
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. */
85 static bool
86 line_contains_hash_if (const char *file, int line_num)
88 int line_len;
89 const char *line = location_get_source_line (file, line_num, &line_len);
90 if (!line)
91 return false;
93 int idx;
95 /* Skip leading whitespace. */
96 for (idx = 0; idx < line_len; idx++)
97 if (!ISSPACE (line[idx]))
98 break;
99 if (idx == line_len)
100 return false;
102 /* Require a '#' character. */
103 if (line[idx] != '#')
104 return false;
105 idx++;
107 /* Skip whitespace. */
108 while (idx < line_len)
110 if (!ISSPACE (line[idx]))
111 break;
112 idx++;
115 /* Match #if/#ifdef/#ifndef. */
116 if (idx + 2 <= line_len)
117 if (line[idx] == 'i')
118 if (line[idx + 1] == 'f')
119 return true;
121 return false;
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:
129 if (flagA)
130 foo ();
131 ^ BODY_EXPLOC
132 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
133 if (flagB)
134 #endif
135 bar ();
136 ^ NEXT_STMT_EXPLOC
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. */
143 static bool
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)
151 return false;
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))
162 return true;
164 /* Not found. */
165 return false;
169 /* Helper function for warn_for_misleading_indentation; see
170 description of that function below. */
172 static bool
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))
188 return false;
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++
195 (e.g. .md files).
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)
207 return false;
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.
213 if (foo) <- GUARD
214 bar (); <- BODY
215 else baz (); <- NEXT
218 while (foo) <- GUARD
219 bar (); <- BODY
220 } <- NEXT
221 baz ();
223 if (next_tok_type == CPP_CLOSE_BRACE
224 || next_tinfo.keyword == RID_ELSE)
225 return false;
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
229 indentation, e.g.
231 while (foo) <- GUARD
232 { <- BODY
233 bar ();
235 baz (); <- NEXT
237 Things only get muddy when the body of the guard does not have
238 braces, e.g.
240 if (foo) <- GUARD
241 bar (); <- BODY
242 baz (); <- NEXT
244 if (body_type == CPP_OPEN_BRACE)
245 return false;
247 /* Don't warn here about spurious semicolons. */
248 if (next_tok_type == CPP_SEMICOLON)
249 return false;
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)
257 return false;
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:
264 if (flag)
265 foo (); bar ();
266 ^ WARN HERE
268 if (flag) foo (); bar ();
269 ^ WARN HERE
272 if (flag) ; {
273 ^ WARN HERE
275 if (flag)
277 ^ WARN HERE
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)
286 return true;
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. */
290 return true;
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,
299 &guard_vis_column,
300 &guard_line_first_nws))
301 return false;
302 /* Heuristic: only warn if the guard is the first thing
303 on its line. */
304 if (guard_vis_column == guard_line_first_nws)
305 return true;
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:
313 if (flag)
314 foo ();
315 bar ();
316 ^ WARN HERE
318 Cases where we don't want to issue a warning:
319 if (flag)
320 foo ();
321 bar ();
322 ^ DON'T WARN HERE (autogenerated code?)
324 if (flagA)
325 foo ();
326 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
327 if (flagB)
328 #endif
329 bar ();
330 ^ DON'T WARN HERE
332 if (flag)
334 foo ();
335 ^ DON'T WARN HERE
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))
351 return false;
352 if (!get_visual_column (body_exploc,
353 &body_vis_column,
354 &body_line_first_nws))
355 return false;
356 if (!get_visual_column (guard_exploc,
357 &guard_vis_column,
358 &guard_line_first_nws))
359 return false;
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. */
366 // if (p)
367 // /* blah */;
368 // foo (1);
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.
382 Consider:
384 if (p) {
385 foo (1);
386 } else // GUARD
387 foo (2); // BODY
388 foo (3); // NEXT
390 and:
392 if (p)
393 foo (1);
394 } else // GUARD
395 foo (2); // BODY
396 foo (3); // NEXT
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
404 happen. */
405 if (guard_line_first_nws == body_vis_column)
406 return false;
408 /* We may have something like:
410 if (p)
412 foo (1);
413 } else // GUARD
414 foo (2); // BODY
415 foo (3); // NEXT
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)
421 return false;
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))
426 return false;
428 /* Otherwise, they are visually aligned: issue a warning. */
429 return true;
432 /* Also issue a warning for code having the form:
434 if (flag);
435 foo ();
437 while (flag);
442 for (...);
447 if (flag)
449 else if (flag);
450 foo ();
452 where the semicolon at the end of each guard is most likely spurious.
454 But do not warn on:
456 for (..);
457 foo ();
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))
468 return true;
473 return false;
476 /* Return the string identifier corresponding to the given guard token. */
478 static const char *
479 guard_tinfo_to_string (const token_indent_info &guard_tinfo)
481 switch (guard_tinfo.keyword)
483 case RID_FOR:
484 return "for";
485 case RID_ELSE:
486 return "else";
487 case RID_IF:
488 return "if";
489 case RID_WHILE:
490 return "while";
491 case RID_DO:
492 return "do";
493 default:
494 gcc_unreachable ();
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:
502 if (flag)
503 foo ();
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
511 GUARD_LOC
514 if (flag)
515 foo (); <- BODY_LOC
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
520 block as "foo ();".
522 GUARD_KIND identifies the kind of clause e.g. "if", "else" etc. */
524 void
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)
533 return;
535 if (should_warn_for_misleading_indentation (guard_tinfo,
536 body_tinfo,
537 next_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));