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