We currently correctly emit a -Wmisleading-indentation warning for the
code

  ...
  if (flagB);
  {
    foo ();
  }

but we fail to emit a warning for the syntactically similar code

  ...
  else if (flagB);
  {
    foo ();
  }

because in the heuristic in question we are inspecting the column of the
guard token when we should rather be inspecting the column of the first
non-whitespace character on the guard line.  This patch adjusts the
heuristic accordingly, making it consistently use guard_line_first_nws.

Tested by building the linux, git, vim, sqlite and gdb-binutils sources
with -Wmisleading-indentation.  Is this OK after a bootstrap + regtest?

gcc/c-family/ChangeLog:

        * c-indentation.c (should_warn_for_misleading_indentation):
        Compare next_stmt_vis_column with guard_line_first_nws instead
        of with guard_line_vis_column.

gcc/testsuite/ChangeLog:

        * c-c++-common/Wmisleading-indentation.c: Augment test.
---
 gcc/c-family/c-indentation.c                         | 2 +-
 gcc/testsuite/c-c++-common/Wmisleading-indentation.c | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/gcc/c-family/c-indentation.c b/gcc/c-family/c-indentation.c
index 5316316..5b119f7 100644
--- a/gcc/c-family/c-indentation.c
+++ b/gcc/c-family/c-indentation.c
@@ -464,7 +464,7 @@ should_warn_for_misleading_indentation (const 
token_indent_info &guard_tinfo,
              {
                if (next_stmt_vis_column > guard_line_first_nws
                    || (next_tok_type == CPP_OPEN_BRACE
-                       && next_stmt_vis_column == guard_vis_column))
+                       && next_stmt_vis_column == guard_line_first_nws))
                  return true;
              }
          }
diff --git a/gcc/testsuite/c-c++-common/Wmisleading-indentation.c 
b/gcc/testsuite/c-c++-common/Wmisleading-indentation.c
index f61c182..00c0a50 100644
--- a/gcc/testsuite/c-c++-common/Wmisleading-indentation.c
+++ b/gcc/testsuite/c-c++-common/Wmisleading-indentation.c
@@ -797,6 +797,15 @@ fn_37 (void)
        i++);
     foo (i); /* { dg-warning "statement is indented as if" } */
 
+  if (flagA)
+  {
+    foo (1);
+  }
+  else if (flagB); /* { dg-message "8: ...this 'if' clause" } */
+  { /* { dg-warning "statement is indented as if" } */
+    foo (2);
+  }
+
 #undef EMPTY
 #undef FOR_EACH
 }
-- 
2.6.0.rc2.26.g0af4266.dirty

Reply via email to