* doc/extend.texi: Use @pxref instead of @xref.
[official-gcc.git] / gcc / c-family / c-indentation.c
blob94565f6daa24ad019cb24782a3fcdbe13146c27f
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 "hash-set.h"
25 #include "machmode.h"
26 #include "vec.h"
27 #include "double-int.h"
28 #include "input.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "wide-int.h"
32 #include "inchash.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "stor-layout.h"
36 #include "input.h"
37 #include "c-common.h"
39 extern cpp_options *cpp_opts;
41 /* Convert libcpp's notion of a column (a 1-based char count) to
42 the "visual column" (0-based column, respecting tabs), by reading the
43 relevant line.
44 Returns true if a conversion was possible, writing the result to OUT,
45 otherwise returns false. */
47 static bool
48 get_visual_column (expanded_location exploc, unsigned int *out)
50 int line_len;
51 const char *line = location_get_source_line (exploc, &line_len);
52 if (!line)
53 return false;
54 unsigned int vis_column = 0;
55 for (int i = 1; i < exploc.column; i++)
57 unsigned char ch = line[i - 1];
58 if (ch == '\t')
60 /* Round up to nearest tab stop. */
61 const unsigned int tab_width = cpp_opts->tabstop;
62 vis_column = ((vis_column + tab_width) / tab_width) * tab_width;
64 else
65 vis_column++;
68 *out = vis_column;
69 return true;
72 /* Is the token at LOC the first non-whitespace on its line?
73 Helper function for should_warn_for_misleading_indentation. */
75 static bool
76 is_first_nonwhitespace_on_line (expanded_location exploc)
78 int line_len;
79 const char *line = location_get_source_line (exploc, &line_len);
81 /* If we can't determine it, return false so that we don't issue a
82 warning. This is sometimes the case for input files
83 containing #line directives, and these are often for autogenerated
84 sources (e.g. from .md files), where it's not clear that it's
85 meaningful to look at indentation. */
86 if (!line)
87 return false;
89 for (int i = 1; i < exploc.column; i++)
91 unsigned char ch = line[i - 1];
92 if (!ISSPACE (ch))
93 return false;
95 return true;
98 /* Does the given source line appear to contain a #if directive?
99 (or #ifdef/#ifndef). Ignore the possibility of it being inside a
100 comment, for simplicity.
101 Helper function for detect_preprocessor_logic. */
103 static bool
104 line_contains_hash_if (const char *file, int line_num)
106 expanded_location exploc;
107 exploc.file = file;
108 exploc.line = line_num;
109 exploc.column = 1;
111 int line_len;
112 const char *line = location_get_source_line (exploc, &line_len);
113 if (!line)
114 return false;
116 int idx;
118 /* Skip leading whitespace. */
119 for (idx = 0; idx < line_len; idx++)
120 if (!ISSPACE (line[idx]))
121 break;
122 if (idx == line_len)
123 return false;
125 /* Require a '#' character. */
126 if (line[idx] != '#')
127 return false;
128 idx++;
130 /* Skip whitespace. */
131 while (idx < line_len)
133 if (!ISSPACE (line[idx]))
134 break;
135 idx++;
138 /* Match #if/#ifdef/#ifndef. */
139 if (idx + 2 <= line_len)
140 if (line[idx] == 'i')
141 if (line[idx + 1] == 'f')
142 return true;
144 return false;
148 /* Determine if there is preprocessor logic between
149 BODY_EXPLOC and NEXT_STMT_EXPLOC, to ensure that we don't
150 issue a warning for cases like this:
152 if (flagA)
153 foo ();
154 ^ BODY_EXPLOC
155 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
156 if (flagB)
157 #endif
158 bar ();
159 ^ NEXT_STMT_EXPLOC
161 despite "bar ();" being visually aligned below "foo ();" and
162 being (as far as the parser sees) the next token.
164 Return true if such logic is detected. */
166 static bool
167 detect_preprocessor_logic (expanded_location body_exploc,
168 expanded_location next_stmt_exploc)
170 gcc_assert (next_stmt_exploc.file == body_exploc.file);
171 gcc_assert (next_stmt_exploc.line > body_exploc.line);
173 if (next_stmt_exploc.line - body_exploc.line < 4)
174 return false;
176 /* Is there a #if/#ifdef/#ifndef directive somewhere in the lines
177 between the given locations?
179 This is something of a layering violation, but by necessity,
180 given the nature of what we're testing for. For example,
181 in theory we could be fooled by a #if within a comment, but
182 it's unlikely to matter. */
183 for (int line = body_exploc.line + 1; line < next_stmt_exploc.line; line++)
184 if (line_contains_hash_if (body_exploc.file, line))
185 return true;
187 /* Not found. */
188 return false;
192 /* Helper function for warn_for_misleading_indentation; see
193 description of that function below. */
195 static bool
196 should_warn_for_misleading_indentation (location_t guard_loc,
197 location_t body_loc,
198 location_t next_stmt_loc,
199 enum cpp_ttype next_tok_type)
201 /* Don't attempt to compare the indentation of BODY_LOC and NEXT_STMT_LOC
202 if either are within macros. */
203 if (linemap_location_from_macro_expansion_p (line_table, body_loc)
204 || linemap_location_from_macro_expansion_p (line_table, next_stmt_loc))
205 return false;
207 /* Don't attempt to compare indentation if #line or # 44 "file"-style
208 directives are present, suggesting generated code.
210 All bets are off if these are present: the file that the #line
211 directive could have an entirely different coding layout to C/C++
212 (e.g. .md files).
214 To determine if a #line is present, in theory we could look for a
215 map with reason == LC_RENAME_VERBATIM. However, if there has
216 subsequently been a long line requiring a column number larger than
217 that representable by the original LC_RENAME_VERBATIM map, then
218 we'll have a map with reason LC_RENAME.
219 Rather than attempting to search all of the maps for a
220 LC_RENAME_VERBATIM, instead we have libcpp set a flag whenever one
221 is seen, and we check for the flag here.
223 if (line_table->seen_line_directive)
224 return false;
226 if (next_tok_type == CPP_CLOSE_BRACE)
227 return false;
229 /* Don't warn here about spurious semicolons. */
230 if (next_tok_type == CPP_SEMICOLON)
231 return false;
233 expanded_location body_exploc
234 = expand_location_to_spelling_point (body_loc);
235 expanded_location next_stmt_exploc
236 = expand_location_to_spelling_point (next_stmt_loc);
238 /* They must be in the same file. */
239 if (next_stmt_exploc.file != body_exploc.file)
240 return false;
242 /* If NEXT_STMT_LOC and BODY_LOC are on the same line, consider
243 the location of the guard.
245 Cases where we want to issue a warning:
247 if (flag)
248 foo (); bar ();
249 ^ WARN HERE
251 if (flag) foo (); bar ();
252 ^ WARN HERE
254 Cases where we don't want to issue a warning:
256 various_code (); if (flag) foo (); bar (); more_code ();
257 ^ DON'T WARN HERE. */
258 if (next_stmt_exploc.line == body_exploc.line)
260 expanded_location guard_exploc
261 = expand_location_to_spelling_point (guard_loc);
262 if (guard_exploc.file != body_exploc.file)
263 return true;
264 if (guard_exploc.line < body_exploc.line)
265 /* The guard is on a line before a line that contains both
266 the body and the next stmt. */
267 return true;
268 else if (guard_exploc.line == body_exploc.line)
270 /* They're all on the same line. */
271 gcc_assert (guard_exploc.file == next_stmt_exploc.file);
272 gcc_assert (guard_exploc.line == next_stmt_exploc.line);
273 /* Heuristic: only warn if the guard is the first thing
274 on its line. */
275 if (is_first_nonwhitespace_on_line (guard_exploc))
276 return true;
280 /* If NEXT_STMT_LOC is on a line after BODY_LOC, consider
281 their relative locations, and of the guard.
283 Cases where we want to issue a warning:
284 if (flag)
285 foo ();
286 bar ();
287 ^ WARN HERE
289 Cases where we don't want to issue a warning:
290 if (flag)
291 foo ();
292 bar ();
293 ^ DON'T WARN HERE (autogenerated code?)
295 if (flagA)
296 foo ();
297 #if SOME_CONDITION_THAT_DOES_NOT_HOLD
298 if (flagB)
299 #endif
300 bar ();
301 ^ DON'T WARN HERE
303 if (next_stmt_exploc.line > body_exploc.line)
305 /* Determine if GUARD_LOC and NEXT_STMT_LOC are aligned on the same
306 "visual column"... */
307 unsigned int next_stmt_vis_column;
308 unsigned int body_vis_column;
309 /* If we can't determine it, don't issue a warning. This is sometimes
310 the case for input files containing #line directives, and these
311 are often for autogenerated sources (e.g. from .md files), where
312 it's not clear that it's meaningful to look at indentation. */
313 if (!get_visual_column (next_stmt_exploc, &next_stmt_vis_column))
314 return false;
315 if (!get_visual_column (body_exploc, &body_vis_column))
316 return false;
317 if (next_stmt_vis_column == body_vis_column)
319 /* Don't warn if they aren't aligned on the same column
320 as the guard itself (suggesting autogenerated code that
321 doesn't bother indenting at all). */
322 expanded_location guard_exploc
323 = expand_location_to_spelling_point (guard_loc);
324 unsigned int guard_vis_column;
325 if (!get_visual_column (guard_exploc, &guard_vis_column))
326 return false;
327 if (guard_vis_column == body_vis_column)
328 return false;
330 /* Don't warn if there is multiline preprocessor logic between
331 the two statements. */
332 if (detect_preprocessor_logic (body_exploc, next_stmt_exploc))
333 return false;
335 /* Otherwise, they are visually aligned: issue a warning. */
336 return true;
340 return false;
343 /* Called by the C/C++ frontends when we have a guarding statement at
344 GUARD_LOC containing a statement at BODY_LOC, where the block wasn't
345 written using braces, like this:
347 if (flag)
348 foo ();
350 along with the location of the next token, at NEXT_STMT_LOC,
351 so that we can detect followup statements that are within
352 the same "visual block" as the guarded statement, but which
353 aren't logically grouped within the guarding statement, such
356 GUARD_LOC
359 if (flag)
360 foo (); <- BODY_LOC
361 bar (); <- NEXT_STMT_LOC
363 In the above, "bar ();" isn't guarded by the "if", but
364 is indented to misleadingly suggest that it is in the same
365 block as "foo ();".
367 GUARD_KIND identifies the kind of clause e.g. "if", "else" etc. */
369 void
370 warn_for_misleading_indentation (location_t guard_loc,
371 location_t body_loc,
372 location_t next_stmt_loc,
373 enum cpp_ttype next_tok_type,
374 const char *guard_kind)
376 if (should_warn_for_misleading_indentation (guard_loc,
377 body_loc,
378 next_stmt_loc,
379 next_tok_type))
380 if (warning_at (next_stmt_loc, OPT_Wmisleading_indentation,
381 "statement is indented as if it were guarded by..."))
382 inform (guard_loc,
383 "...this %qs clause, but it is not", guard_kind);