On 05/20/2015 11:05 AM, David Malcolm wrote:
This patch fixes the false positive seen from -Wmisleading-indentation
on this code:
if (v == 2)
{
res = 27;
} else
{
res = 18;
}
return res;
^ FALSE POSITIVE HERE
along with similar code seen when I tested it with linux-4.0.3.
The patch adds a reject for the case where the guard ("else" in
the above example) is more indented than the things it guards.
Doing so uncovered an issue with this testcase:
#define FOR_EACH(VAR, START, STOP) for ((VAR) = (START); (VAR) < (STOP); (VAR++)) /* {
dg-message "36: ...this 'for' clause, but it is not" } */
void fn_15 (void)
{
int i;
FOR_EACH (i, 0, 10) /* { dg-message "3: in expansion of macro" } */
foo (i);
bar (i, i); /* { dg-warning "statement is indented as if it were guarded
by..." } */
}
#undef FOR_EACH
which would then fail to report the warning, due to it using the
location of the "for" in the definition of macro FOR_EACH, rather than
the location of the FOR_EACH (i, 0, 10). The fix for this is to use
expand_location to get file/line/col of each thing, rather than
expand_location_to_spelling_point.
With that, all testcases in Wmisleading-indentation.c pass (including
the new ones posted in
https://gcc.gnu.org/ml/gcc-patches/2015-05/msg01846.html ).
OK for trunk if it passes bootstrap®rest? (only tested with
make check-gcc RUNTESTFLAGS="dg.exp=Wmisleading-indentation.c"
make check-g++ RUNTESTFLAGS="dg.exp=Wmisleading-indentation.c"
so far)
gcc/c-family/ChangeLog:
PR c/66220:
* c-indentation.c (should_warn_for_misleading_indentation): Use
expand_location rather than expand_location_to_spelling_point.
Don't warn if the guarding statement is more indented than the
next/body stmts.
gcc/testsuite/ChangeLog:
PR c/66220:
* c-c++-common/Wmisleading-indentation.c (fn_35): New.
(fn_36): New.
OK.
jeff