PR middle-end/71476 75/189275/2
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 13 Jun 2016 08:57:02 +0000 (08:57 +0000)
committerMikhail Kashkarov <m.kashkarov@partner.samsung.com>
Thu, 18 Oct 2018 13:20:06 +0000 (16:20 +0300)
* gimplify.c (maybe_warn_switch_unreachable): Factored out of
gimplify_switch_expr.
(warn_switch_unreachable_r): New function.

* c-c++-common/Wswitch-unreachable-4.c: New test.
* gcc.dg/Wswitch-unreachable-2.c: New test.
* g++.dg/tm/jump1.C: Move dg-warning.

upstream hash: 1a54d3bee5c40687c53b2680d0b110f23a193713
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@237367 138bc75d-0d04-0410-961f-82ee72b054a4

Change-Id: Ie8bc6fb965fdb08c7e76ee02fe01eec4707336b0

gcc/gimplify.c
gcc/testsuite/c-c++-common/Wswitch-unreachable-4.c [new file with mode: 0644]
gcc/testsuite/g++.dg/tm/jump1.C
gcc/testsuite/gcc.dg/Wswitch-unreachable-2.c [new file with mode: 0644]

index 056d5ef..a66b846 100644 (file)
@@ -1552,6 +1552,73 @@ gimplify_statement_list (tree *expr_p, gimple_seq *pre_p)
   return GS_ALL_DONE;
 }
 
+/* Callback for walk_gimple_seq.  */
+
+static tree
+warn_switch_unreachable_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
+                          struct walk_stmt_info *wi)
+{
+  gimple *stmt = gsi_stmt (*gsi_p);
+
+  *handled_ops_p = true;
+  switch (gimple_code (stmt))
+    {
+    case GIMPLE_TRY:
+      /* A compiler-generated cleanup or a user-written try block.
+        If it's empty, don't dive into it--that would result in
+        worse location info.  */
+      if (gimple_try_eval (stmt) == NULL)
+       {
+         wi->info = stmt;
+         return integer_zero_node;
+       }
+      /* Fall through.  */
+    case GIMPLE_BIND:
+    case GIMPLE_CATCH:
+    case GIMPLE_EH_FILTER:
+    case GIMPLE_TRANSACTION:
+      /* Walk the sub-statements.  */
+      *handled_ops_p = false;
+      break;
+    default:
+      /* Save the first "real" statement (not a decl/lexical scope/...).  */
+      wi->info = stmt;
+      return integer_zero_node;
+    }
+  return NULL_TREE;
+}
+
+/* Possibly warn about unreachable statements between switch's controlling
+   expression and the first case.  SEQ is the body of a switch expression.  */
+
+static void
+maybe_warn_switch_unreachable (gimple_seq seq)
+{
+  if (!warn_switch_unreachable
+      /* This warning doesn't play well with Fortran when optimizations
+        are on.  */
+      || lang_GNU_Fortran ()
+      || seq == NULL)
+    return;
+
+  struct walk_stmt_info wi;
+  memset (&wi, 0, sizeof (wi));
+  walk_gimple_seq (seq, warn_switch_unreachable_r, NULL, &wi);
+  gimple *stmt = (gimple *) wi.info;
+
+  if (stmt && gimple_code (stmt) != GIMPLE_LABEL)
+    {
+      if (gimple_code (stmt) == GIMPLE_GOTO
+         && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
+         && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
+       /* Don't warn for compiler-generated gotos.  These occur
+          in Duff's devices, for example.  */;
+      else
+       warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
+                   "statement will never be executed");
+    }
+}
+
 \f
 /* Gimplify a SWITCH_EXPR, and collect the vector of labels it can
    branch to.  */
@@ -1589,39 +1656,8 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
 
       gimplify_stmt (&SWITCH_BODY (switch_expr), &switch_body_seq);
 
-      /* Possibly warn about unreachable statements between switch's
-        controlling expression and the first case.  */
-      if (warn_switch_unreachable
-         /* This warning doesn't play well with Fortran when optimizations
-            are on.  */
-         && !lang_GNU_Fortran ()
-         && switch_body_seq != NULL)
-       {
-         gimple_seq seq = switch_body_seq;
-         /* Look into the innermost lexical scope.  */
-         while (gimple_code (seq) == GIMPLE_BIND)
-           seq = gimple_bind_body (as_a <gbind *> (seq));
-         gimple *stmt = gimple_seq_first_stmt (seq);
-         if (gimple_code (stmt) == GIMPLE_TRY)
-           {
-             /* A compiler-generated cleanup or a user-written try block.
-                Try to get the first statement in its try-block, for better
-                location.  */
-             if ((seq = gimple_try_eval (stmt)))
-               stmt = gimple_seq_first_stmt (seq);
-           }
-         if (gimple_code (stmt) != GIMPLE_LABEL)
-           {
-             if (gimple_code (stmt) == GIMPLE_GOTO
-                 && TREE_CODE (gimple_goto_dest (stmt)) == LABEL_DECL
-                 && DECL_ARTIFICIAL (gimple_goto_dest (stmt)))
-               /* Don't warn for compiler-generated gotos.  These occur
-                  in Duff's devices, for example.  */;
-             else
-               warning_at (gimple_location (stmt), OPT_Wswitch_unreachable,
-                           "statement will never be executed");
-           }
-       }
+      maybe_warn_switch_unreachable (switch_body_seq);
+
       labels = gimplify_ctxp->case_labels;
       gimplify_ctxp->case_labels = saved_labels;
 
diff --git a/gcc/testsuite/c-c++-common/Wswitch-unreachable-4.c b/gcc/testsuite/c-c++-common/Wswitch-unreachable-4.c
new file mode 100644 (file)
index 0000000..e7378a7
--- /dev/null
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+
+void
+foo (int a, int b)
+{
+  switch (a)
+    {
+      { int c; }
+      { int d; }
+      { int e; }
+      b++; /* { dg-warning "statement will never be executed" } */
+    case 1:
+      break;
+    }
+
+  switch (a)
+    {
+      { int c; }
+      { int d = 1; } /* { dg-warning "statement will never be executed" } */
+      { int e; }
+      b++;
+    case 1:
+      break;
+    }
+}
index e28282d..a27c201 100644 (file)
@@ -14,8 +14,8 @@ void f()
 
   switch (i)
     {
-      synchronized {           // { dg-warning "statement will never be executed" }
-       ++i;
+      synchronized {
+       ++i;                    // { dg-warning "statement will never be executed" }
       case 42:                 // { dg-error "" }
        ++i;
       }
diff --git a/gcc/testsuite/gcc.dg/Wswitch-unreachable-2.c b/gcc/testsuite/gcc.dg/Wswitch-unreachable-2.c
new file mode 100644 (file)
index 0000000..343baea
--- /dev/null
@@ -0,0 +1,12 @@
+/* PR middle-end/71476 */
+/* { dg-do compile } */
+/* { dg-options "-Wswitch-unreachable" } */
+
+void
+foo (int a)
+{
+  switch (a)
+    {
+      void f (void) { }
+    }
+}