PR c/66220: Fix false positive from -Wmisleading-indentation
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 2 Jun 2015 18:45:50 +0000 (18:45 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Tue, 2 Jun 2015 18:45:50 +0000 (18:45 +0000)
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.

From-SVN: r224041

gcc/c-family/ChangeLog
gcc/c-family/c-indentation.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/Wmisleading-indentation.c

index 8fc0281d41c3018196bb351b24e54fa90877547f..f1501bcb335fc83b128b5cc7870fc3c08bf6cba3 100644 (file)
@@ -1,3 +1,11 @@
+2015-06-02  David Malcolm  <dmalcolm@redhat.com>
+
+       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.
+
 2015-06-02  David Malcolm  <dmalcolm@redhat.com>
 
        * c-indentation.c (warn_for_misleading_indentation): Bail out
index 9aeebae3a81f7ce5f0efce19e06e997d5ff4deed..1e3a6d82f279c55ce45fb4dbb80fba810f68659a 100644 (file)
@@ -230,10 +230,8 @@ should_warn_for_misleading_indentation (location_t guard_loc,
   if (next_tok_type == CPP_SEMICOLON)
     return false;
 
-  expanded_location body_exploc
-    = expand_location_to_spelling_point (body_loc);
-  expanded_location next_stmt_exploc
-    = expand_location_to_spelling_point (next_stmt_loc);
+  expanded_location body_exploc = expand_location (body_loc);
+  expanded_location next_stmt_exploc = expand_location (next_stmt_loc);
 
   /* They must be in the same file.  */
   if (next_stmt_exploc.file != body_exploc.file)
@@ -257,8 +255,7 @@ should_warn_for_misleading_indentation (location_t guard_loc,
                                           ^ DON'T WARN HERE.  */
   if (next_stmt_exploc.line == body_exploc.line)
     {
-      expanded_location guard_exploc
-       = expand_location_to_spelling_point (guard_loc);
+      expanded_location guard_exploc = expand_location (guard_loc);
       if (guard_exploc.file != body_exploc.file)
        return true;
       if (guard_exploc.line < body_exploc.line)
@@ -299,6 +296,15 @@ should_warn_for_misleading_indentation (location_t guard_loc,
       #endif
          bar ();
          ^ DON'T WARN HERE
+
+        if (flag) {
+          foo ();
+        } else
+        {
+          bar ();
+        }
+        baz ();
+        ^ DON'T WARN HERE
   */
   if (next_stmt_exploc.line > body_exploc.line)
     {
@@ -319,14 +325,18 @@ should_warn_for_misleading_indentation (location_t guard_loc,
          /* Don't warn if they aren't aligned on the same column
             as the guard itself (suggesting autogenerated code that
             doesn't bother indenting at all).  */
-         expanded_location guard_exploc
-           = expand_location_to_spelling_point (guard_loc);
+         expanded_location guard_exploc = expand_location (guard_loc);
          unsigned int guard_vis_column;
          if (!get_visual_column (guard_exploc, &guard_vis_column))
            return false;
          if (guard_vis_column == body_vis_column)
            return false;
 
+         /* PR 66220: Don't warn if the guarding statement is more
+            indented than the next/body stmts.  */
+         if (guard_vis_column > body_vis_column)
+           return false;
+
          /* Don't warn if there is multiline preprocessor logic between
             the two statements. */
          if (detect_preprocessor_logic (body_exploc, next_stmt_exploc))
index 5060c5339449c4e1bdc47df403190f1759019d54..70a05dae193a0f322847ff04d87f4548ef1718ab 100644 (file)
@@ -1,3 +1,9 @@
+2015-06-02  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c/66220:
+       * c-c++-common/Wmisleading-indentation.c (fn_35): New.
+       (fn_36): New.
+
 2015-06-02  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
 
        PR c/49551
index 6363d71fe7f71465ad351404b781aae7cf6697ae..443e3ddc30aa047edbd8359e8abc6d698ae43b0f 100644 (file)
@@ -653,3 +653,41 @@ void fn_34_indent_linux_style(void)
                }
        foo(5);
 }
+
+/* PR 66220.  */
+int fn_35 (int v)
+{
+    int res = 28;
+
+    if (v == 2)
+    {
+        res = 27;
+    } else
+    {
+        res = 18;
+    }
+    return res;
+}
+
+/* This variant of K&R-style formatting (in the presence of conditional
+   compilation) shouldn't lead to a warning.
+
+   Based on false positive seen with r223098 when compiling
+   linux-4.0.3:arch/x86/crypto/aesni-intel_glue.c:aesni_init.  */
+void
+fn_36 (void)
+{
+#if 1 /* e.g. some configuration variable.  */
+       if (flagA) {
+               foo(0);
+               foo(1);
+               foo(2);
+       } else
+#endif
+       {
+               foo(3);
+               foo(4);
+               foo(5);
+       }
+       foo(6); /* We shouldn't warn here.  */
+}