]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-indentation.c
Allow larger copies when not slow_unaligned_access and no padding.
[thirdparty/gcc.git] / gcc / c-family / c-indentation.c
CommitLineData
c3388e62 1/* Implementation of -Wmisleading-indentation
85ec4feb 2 Copyright (C) 2015-2018 Free Software Foundation, Inc.
c3388e62
DM
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "tm.h"
2adfab87 24#include "c-common.h"
992118a1 25#include "c-indentation.h"
c3388e62
DM
26
27extern cpp_options *cpp_opts;
28
64b23c13
DM
29/* Round up VIS_COLUMN to nearest tab stop. */
30
31static unsigned int
32next_tab_stop (unsigned int vis_column)
33{
34 const unsigned int tab_width = cpp_opts->tabstop;
35 vis_column = ((vis_column + tab_width) / tab_width) * tab_width;
36 return vis_column;
37}
38
c3388e62
DM
39/* Convert libcpp's notion of a column (a 1-based char count) to
40 the "visual column" (0-based column, respecting tabs), by reading the
41 relevant line.
1a1e101f 42
c3388e62 43 Returns true if a conversion was possible, writing the result to OUT,
1a1e101f
PP
44 otherwise returns false. If FIRST_NWS is not NULL, then write to it
45 the visual column corresponding to the first non-whitespace character
46 on the line. */
c3388e62
DM
47
48static bool
c7df95d8 49get_visual_column (expanded_location exploc, location_t loc,
1a1e101f 50 unsigned int *out,
c589e975 51 unsigned int *first_nws)
c3388e62 52{
c7df95d8
DM
53 /* PR c++/68819: if the column number is zero, we presumably
54 had a location_t > LINE_MAP_MAX_LOCATION_WITH_COLS, and so
55 we have no column information.
56 Act as if no conversion was possible, triggering the
57 error-handling path in the caller. */
58 if (!exploc.column)
59 {
60 static bool issued_note = false;
61 if (!issued_note)
62 {
63 /* Notify the user the first time this happens. */
64 issued_note = true;
65 inform (loc,
66 "-Wmisleading-indentation is disabled from this point"
67 " onwards, since column-tracking was disabled due to"
68 " the size of the code/headers");
69 }
70 return false;
71 }
72
7761dfbe 73 char_span line = location_get_source_line (exploc.file, exploc.line);
c3388e62
DM
74 if (!line)
75 return false;
76 unsigned int vis_column = 0;
77 for (int i = 1; i < exploc.column; i++)
78 {
79 unsigned char ch = line[i - 1];
1a1e101f
PP
80
81 if (first_nws != NULL && !ISSPACE (ch))
82 {
83 *first_nws = vis_column;
84 first_nws = NULL;
85 }
86
c3388e62 87 if (ch == '\t')
64b23c13 88 vis_column = next_tab_stop (vis_column);
c3388e62
DM
89 else
90 vis_column++;
91 }
92
1a1e101f
PP
93 if (first_nws != NULL)
94 *first_nws = vis_column;
c3388e62 95
1a1e101f 96 *out = vis_column;
c3388e62
DM
97 return true;
98}
99
64b23c13
DM
100/* Attempt to determine the first non-whitespace character in line LINE_NUM
101 of source line FILE.
102
103 If this is possible, return true and write its "visual column" to
104 *FIRST_NWS.
105 Otherwise, return false, leaving *FIRST_NWS untouched. */
c3388e62
DM
106
107static bool
64b23c13
DM
108get_first_nws_vis_column (const char *file, int line_num,
109 unsigned int *first_nws)
c3388e62 110{
64b23c13
DM
111 gcc_assert (first_nws);
112
7761dfbe 113 char_span line = location_get_source_line (file, line_num);
c3388e62
DM
114 if (!line)
115 return false;
64b23c13 116 unsigned int vis_column = 0;
7761dfbe 117 for (size_t i = 1; i < line.length (); i++)
64b23c13
DM
118 {
119 unsigned char ch = line[i - 1];
c3388e62 120
64b23c13
DM
121 if (!ISSPACE (ch))
122 {
123 *first_nws = vis_column;
124 return true;
125 }
c3388e62 126
64b23c13
DM
127 if (ch == '\t')
128 vis_column = next_tab_stop (vis_column);
129 else
130 vis_column++;
c3388e62
DM
131 }
132
64b23c13 133 /* No non-whitespace characters found. */
c3388e62
DM
134 return false;
135}
136
64b23c13 137/* Determine if there is an unindent/outdent between
c3388e62 138 BODY_EXPLOC and NEXT_STMT_EXPLOC, to ensure that we don't
64b23c13
DM
139 issue a warning for cases like the following:
140
141 (1) Preprocessor logic
c3388e62
DM
142
143 if (flagA)
144 foo ();
145 ^ BODY_EXPLOC
146 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
147 if (flagB)
148 #endif
149 bar ();
150 ^ NEXT_STMT_EXPLOC
151
64b23c13
DM
152 "bar ();" is visually aligned below "foo ();" and
153 is (as far as the parser sees) the next token, but
154 this isn't misleading to a human reader.
c3388e62 155
64b23c13 156 (2) Empty macro with bad indentation
c3388e62 157
64b23c13
DM
158 In the following, the
159 "if (i > 0)"
160 is poorly indented, and ought to be on the same column as
161 "engine_ref_debug(e, 0, -1)"
162 However, it is not misleadingly indented, due to the presence
163 of that macro.
c3388e62 164
64b23c13
DM
165 #define engine_ref_debug(X, Y, Z)
166
167 if (locked)
168 i = foo (0);
169 else
170 i = foo (1);
171 engine_ref_debug(e, 0, -1)
172 if (i > 0)
173 return 1;
c3388e62 174
64b23c13 175 Return true if such an unindent/outdent is detected. */
c3388e62 176
64b23c13
DM
177static bool
178detect_intervening_unindent (const char *file,
179 int body_line,
180 int next_stmt_line,
181 unsigned int vis_column)
182{
183 gcc_assert (file);
184 gcc_assert (next_stmt_line > body_line);
185
186 for (int line = body_line + 1; line < next_stmt_line; line++)
187 {
188 unsigned int line_vis_column;
189 if (get_first_nws_vis_column (file, line, &line_vis_column))
190 if (line_vis_column < vis_column)
191 return true;
192 }
c3388e62
DM
193
194 /* Not found. */
195 return false;
196}
197
198
199/* Helper function for warn_for_misleading_indentation; see
200 description of that function below. */
201
202static bool
992118a1
PP
203should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
204 const token_indent_info &body_tinfo,
205 const token_indent_info &next_tinfo)
c3388e62 206{
992118a1
PP
207 location_t guard_loc = guard_tinfo.location;
208 location_t body_loc = body_tinfo.location;
209 location_t next_stmt_loc = next_tinfo.location;
210
8ebca419 211 enum cpp_ttype body_type = body_tinfo.type;
992118a1
PP
212 enum cpp_ttype next_tok_type = next_tinfo.type;
213
c3388e62
DM
214 /* Don't attempt to compare the indentation of BODY_LOC and NEXT_STMT_LOC
215 if either are within macros. */
216 if (linemap_location_from_macro_expansion_p (line_table, body_loc)
217 || linemap_location_from_macro_expansion_p (line_table, next_stmt_loc))
218 return false;
219
220 /* Don't attempt to compare indentation if #line or # 44 "file"-style
221 directives are present, suggesting generated code.
222
223 All bets are off if these are present: the file that the #line
224 directive could have an entirely different coding layout to C/C++
225 (e.g. .md files).
226
227 To determine if a #line is present, in theory we could look for a
228 map with reason == LC_RENAME_VERBATIM. However, if there has
229 subsequently been a long line requiring a column number larger than
230 that representable by the original LC_RENAME_VERBATIM map, then
231 we'll have a map with reason LC_RENAME.
232 Rather than attempting to search all of the maps for a
233 LC_RENAME_VERBATIM, instead we have libcpp set a flag whenever one
234 is seen, and we check for the flag here.
235 */
236 if (line_table->seen_line_directive)
237 return false;
238
b3a77f21
MP
239 /* We can't usefully warn about do-while and switch statements since the
240 bodies of these statements are always explicitly delimited at both ends,
241 so control flow is quite obvious. */
242 if (guard_tinfo.keyword == RID_DO
243 || guard_tinfo.keyword == RID_SWITCH)
21efdd80
PP
244 return false;
245
992118a1
PP
246 /* If the token following the body is a close brace or an "else"
247 then while indentation may be sloppy, there is not much ambiguity
248 about control flow, e.g.
249
250 if (foo) <- GUARD
251 bar (); <- BODY
252 else baz (); <- NEXT
253
254 {
255 while (foo) <- GUARD
256 bar (); <- BODY
257 } <- NEXT
258 baz ();
259 */
260 if (next_tok_type == CPP_CLOSE_BRACE
261 || next_tinfo.keyword == RID_ELSE)
c3388e62
DM
262 return false;
263
8ebca419
PP
264 /* Likewise, if the body of the guard is a compound statement then control
265 flow is quite visually explicit regardless of the code's possibly poor
266 indentation, e.g.
267
268 while (foo) <- GUARD
269 { <- BODY
270 bar ();
271 }
272 baz (); <- NEXT
273
274 Things only get muddy when the body of the guard does not have
275 braces, e.g.
276
277 if (foo) <- GUARD
278 bar (); <- BODY
279 baz (); <- NEXT
280 */
281 if (body_type == CPP_OPEN_BRACE)
282 return false;
283
c3388e62
DM
284 /* Don't warn here about spurious semicolons. */
285 if (next_tok_type == CPP_SEMICOLON)
286 return false;
287
6ac48155
DM
288 expanded_location body_exploc = expand_location (body_loc);
289 expanded_location next_stmt_exploc = expand_location (next_stmt_loc);
8ebca419 290 expanded_location guard_exploc = expand_location (guard_loc);
c3388e62
DM
291
292 /* They must be in the same file. */
293 if (next_stmt_exploc.file != body_exploc.file)
294 return false;
295
296 /* If NEXT_STMT_LOC and BODY_LOC are on the same line, consider
297 the location of the guard.
298
299 Cases where we want to issue a warning:
300
301 if (flag)
302 foo (); bar ();
303 ^ WARN HERE
304
305 if (flag) foo (); bar ();
306 ^ WARN HERE
307
8ebca419
PP
308
309 if (flag) ; {
310 ^ WARN HERE
311
312 if (flag)
313 ; {
314 ^ WARN HERE
315
c3388e62
DM
316 Cases where we don't want to issue a warning:
317
318 various_code (); if (flag) foo (); bar (); more_code ();
319 ^ DON'T WARN HERE. */
320 if (next_stmt_exploc.line == body_exploc.line)
321 {
c3388e62
DM
322 if (guard_exploc.file != body_exploc.file)
323 return true;
324 if (guard_exploc.line < body_exploc.line)
325 /* The guard is on a line before a line that contains both
326 the body and the next stmt. */
327 return true;
328 else if (guard_exploc.line == body_exploc.line)
329 {
330 /* They're all on the same line. */
331 gcc_assert (guard_exploc.file == next_stmt_exploc.file);
332 gcc_assert (guard_exploc.line == next_stmt_exploc.line);
1a1e101f
PP
333 unsigned int guard_vis_column;
334 unsigned int guard_line_first_nws;
c7df95d8 335 if (!get_visual_column (guard_exploc, guard_loc,
1a1e101f
PP
336 &guard_vis_column,
337 &guard_line_first_nws))
338 return false;
c3388e62
DM
339 /* Heuristic: only warn if the guard is the first thing
340 on its line. */
1a1e101f 341 if (guard_vis_column == guard_line_first_nws)
c3388e62
DM
342 return true;
343 }
344 }
345
346 /* If NEXT_STMT_LOC is on a line after BODY_LOC, consider
347 their relative locations, and of the guard.
348
349 Cases where we want to issue a warning:
350 if (flag)
351 foo ();
352 bar ();
353 ^ WARN HERE
354
355 Cases where we don't want to issue a warning:
356 if (flag)
357 foo ();
358 bar ();
359 ^ DON'T WARN HERE (autogenerated code?)
360
361 if (flagA)
362 foo ();
363 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
364 if (flagB)
365 #endif
366 bar ();
367 ^ DON'T WARN HERE
6ac48155 368
8ebca419
PP
369 if (flag)
370 ;
371 foo ();
372 ^ DON'T WARN HERE
c589e975
DM
373
374 #define emit
375 if (flag)
376 foo ();
377 emit bar ();
378 ^ DON'T WARN HERE
379
c3388e62
DM
380 */
381 if (next_stmt_exploc.line > body_exploc.line)
382 {
383 /* Determine if GUARD_LOC and NEXT_STMT_LOC are aligned on the same
384 "visual column"... */
385 unsigned int next_stmt_vis_column;
c589e975 386 unsigned int next_stmt_line_first_nws;
c3388e62 387 unsigned int body_vis_column;
8ebca419 388 unsigned int body_line_first_nws;
d5398058
PP
389 unsigned int guard_vis_column;
390 unsigned int guard_line_first_nws;
c3388e62
DM
391 /* If we can't determine it, don't issue a warning. This is sometimes
392 the case for input files containing #line directives, and these
393 are often for autogenerated sources (e.g. from .md files), where
394 it's not clear that it's meaningful to look at indentation. */
c7df95d8
DM
395 if (!get_visual_column (next_stmt_exploc, next_stmt_loc,
396 &next_stmt_vis_column,
c589e975 397 &next_stmt_line_first_nws))
c3388e62 398 return false;
c7df95d8 399 if (!get_visual_column (body_exploc, body_loc,
8ebca419
PP
400 &body_vis_column,
401 &body_line_first_nws))
c3388e62 402 return false;
c7df95d8 403 if (!get_visual_column (guard_exploc, guard_loc,
d5398058
PP
404 &guard_vis_column,
405 &guard_line_first_nws))
406 return false;
407
c589e975
DM
408 /* If the line where the next stmt starts has non-whitespace
409 on it before the stmt, then don't warn:
410 #define emit
411 if (flag)
412 foo ();
413 emit bar ();
414 ^ DON'T WARN HERE
415 (PR c/69122). */
416 if (next_stmt_line_first_nws < next_stmt_vis_column)
417 return false;
418
8ebca419
PP
419 if ((body_type != CPP_SEMICOLON
420 && next_stmt_vis_column == body_vis_column)
421 /* As a special case handle the case where the body is a semicolon
422 that may be hidden by a preceding comment, e.g. */
423
424 // if (p)
425 // /* blah */;
426 // foo (1);
427
428 /* by looking instead at the column of the first non-whitespace
429 character on the body line. */
430 || (body_type == CPP_SEMICOLON
431 && body_exploc.line > guard_exploc.line
432 && body_line_first_nws != body_vis_column
d5398058 433 && next_stmt_vis_column > guard_line_first_nws))
c3388e62 434 {
8ebca419
PP
435 /* Don't warn if they are aligned on the same column
436 as the guard itself (suggesting autogenerated code that doesn't
729526f5
DM
437 bother indenting at all).
438 For "else" clauses, we consider the column of the first
8ebca419
PP
439 non-whitespace character on the guard line instead of the column
440 of the actual guard token itself because it is more sensible.
441 Consider:
442
443 if (p) {
444 foo (1);
445 } else // GUARD
446 foo (2); // BODY
447 foo (3); // NEXT
448
449 and:
450
451 if (p)
452 foo (1);
453 } else // GUARD
454 foo (2); // BODY
455 foo (3); // NEXT
456
729526f5 457 If we just used the column of the "else" token, we would warn on
8ebca419
PP
458 the first example and not warn on the second. But we want the
459 exact opposite to happen: to not warn on the first example (which
460 is probably autogenerated) and to warn on the second (whose
461 indentation is misleading). Using the column of the first
462 non-whitespace character on the guard line makes that
463 happen. */
729526f5
DM
464 unsigned int guard_column = (guard_tinfo.keyword == RID_ELSE
465 ? guard_line_first_nws
466 : guard_vis_column);
467 if (guard_column == body_vis_column)
c3388e62
DM
468 return false;
469
8ebca419
PP
470 /* We may have something like:
471
472 if (p)
473 {
474 foo (1);
475 } else // GUARD
476 foo (2); // BODY
477 foo (3); // NEXT
478
479 in which case the columns are not aligned but the code is not
729526f5
DM
480 misleadingly indented. If the column of the body isn't indented
481 more than the guard line then don't warn. */
482 if (body_vis_column <= guard_line_first_nws)
6ac48155
DM
483 return false;
484
64b23c13
DM
485 /* Don't warn if there is an unindent between the two statements. */
486 int vis_column = MIN (next_stmt_vis_column, body_vis_column);
487 if (detect_intervening_unindent (body_exploc.file, body_exploc.line,
488 next_stmt_exploc.line,
489 vis_column))
c3388e62
DM
490 return false;
491
492 /* Otherwise, they are visually aligned: issue a warning. */
493 return true;
494 }
8ebca419
PP
495
496 /* Also issue a warning for code having the form:
497
498 if (flag);
499 foo ();
500
501 while (flag);
502 {
503 ...
504 }
505
506 for (...);
507 {
508 ...
509 }
510
511 if (flag)
512 ;
513 else if (flag);
514 foo ();
515
516 where the semicolon at the end of each guard is most likely spurious.
517
518 But do not warn on:
519
520 for (..);
521 foo ();
522
523 where the next statement is aligned with the guard.
524 */
525 if (body_type == CPP_SEMICOLON)
526 {
527 if (body_exploc.line == guard_exploc.line)
528 {
8ebca419
PP
529 if (next_stmt_vis_column > guard_line_first_nws
530 || (next_tok_type == CPP_OPEN_BRACE
6b95d7cc 531 && next_stmt_vis_column == guard_line_first_nws))
8ebca419
PP
532 return true;
533 }
534 }
c3388e62
DM
535 }
536
537 return false;
538}
539
992118a1
PP
540/* Return the string identifier corresponding to the given guard token. */
541
3e2becc4
MP
542const char *
543guard_tinfo_to_string (enum rid keyword)
992118a1 544{
3e2becc4 545 switch (keyword)
992118a1
PP
546 {
547 case RID_FOR:
548 return "for";
549 case RID_ELSE:
550 return "else";
551 case RID_IF:
552 return "if";
553 case RID_WHILE:
554 return "while";
555 case RID_DO:
556 return "do";
3e2becc4
MP
557 case RID_SWITCH:
558 return "switch";
992118a1
PP
559 default:
560 gcc_unreachable ();
561 }
562}
563
c3388e62
DM
564/* Called by the C/C++ frontends when we have a guarding statement at
565 GUARD_LOC containing a statement at BODY_LOC, where the block wasn't
566 written using braces, like this:
567
568 if (flag)
569 foo ();
570
571 along with the location of the next token, at NEXT_STMT_LOC,
572 so that we can detect followup statements that are within
573 the same "visual block" as the guarded statement, but which
574 aren't logically grouped within the guarding statement, such
575 as:
576
577 GUARD_LOC
578 |
579 V
580 if (flag)
581 foo (); <- BODY_LOC
582 bar (); <- NEXT_STMT_LOC
583
584 In the above, "bar ();" isn't guarded by the "if", but
585 is indented to misleadingly suggest that it is in the same
586 block as "foo ();".
587
588 GUARD_KIND identifies the kind of clause e.g. "if", "else" etc. */
589
590void
992118a1
PP
591warn_for_misleading_indentation (const token_indent_info &guard_tinfo,
592 const token_indent_info &body_tinfo,
593 const token_indent_info &next_tinfo)
c3388e62 594{
773ce42e
DM
595 /* Early reject for the case where -Wmisleading-indentation is disabled,
596 to avoid doing work only to have the warning suppressed inside the
597 diagnostic machinery. */
598 if (!warn_misleading_indentation)
599 return;
600
992118a1
PP
601 if (should_warn_for_misleading_indentation (guard_tinfo,
602 body_tinfo,
603 next_tinfo))
604 {
5c240f4d
DM
605 if (warning_at (guard_tinfo.location, OPT_Wmisleading_indentation,
606 "this %qs clause does not guard...",
3e2becc4 607 guard_tinfo_to_string (guard_tinfo.keyword)))
5c240f4d 608 inform (next_tinfo.location,
a02fa805
DM
609 "...this statement, but the latter is misleadingly indented"
610 " as if it were guarded by the %qs",
3e2becc4 611 guard_tinfo_to_string (guard_tinfo.keyword));
992118a1 612 }
c3388e62 613}