public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Further refinement to -Wswitch-unreachable
@ 2016-05-27  7:53 Marek Polacek
  2016-05-27 15:29 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2016-05-27  7:53 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill, Joseph Myers; +Cc: Martin Sebor

Martin complained that -Wswitch-unreachable wouldn't warn on try-blocks,
either compiler-generated or user-written.  This patch, which looks into
GIMPLE_TRY's body, seems to DTRT for both.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2016-05-26  Marek Polacek  <polacek@redhat.com>

	* gimplify.c (gimplify_switch_expr): Also handle GIMPLE_TRY.

	* c-c++-common/Wswitch-unreachable-3.c: New test.
	* g++.dg/warn/Wswitch-unreachable-1.C: New test.

diff --git gcc/gimplify.c gcc/gimplify.c
index 8316bb8..8b7dddc 100644
--- gcc/gimplify.c
+++ gcc/gimplify.c
@@ -1609,10 +1609,17 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
 	  while (gimple_code (seq) == GIMPLE_BIND)
 	    seq = gimple_bind_body (as_a <gbind *> (seq));
 	  gimple *stmt = gimple_seq_first_stmt (seq);
-	  enum gimple_code code = gimple_code (stmt);
-	  if (code != GIMPLE_LABEL && code != GIMPLE_TRY)
+	  if (gimple_code (stmt) == GIMPLE_TRY)
 	    {
-	      if (code == GIMPLE_GOTO
+	      /* 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
diff --git gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c
index e69de29..3748701 100644
--- gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c
+++ gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+
+extern void f (int *, int);
+
+void
+g (int i)
+{
+  switch (i)
+    {
+      int a[3];
+      __builtin_memset (a, 0, sizeof a); /* { dg-warning "statement will never be executed" } */
+
+    default:
+      f (a, 3);
+    }
+}
diff --git gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C
index e69de29..99d9a83 100644
--- gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C
+++ gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C
@@ -0,0 +1,34 @@
+// { dg-do compile }
+
+extern int j;
+
+void
+f (int i)
+{
+  switch (i) // { dg-warning "statement will never be executed" }
+    {
+      try
+      {
+      }
+      catch (...)
+      {
+      }
+    case 1:;
+    }
+}
+
+void
+g (int i)
+{
+  switch (i)
+    {
+      try
+      {
+	j = 42;  // { dg-warning "statement will never be executed" }
+      }
+      catch (...)
+      {
+      }
+    case 1:;
+    }
+}

	Marek

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Further refinement to -Wswitch-unreachable
  2016-05-27  7:53 Further refinement to -Wswitch-unreachable Marek Polacek
@ 2016-05-27 15:29 ` Jason Merrill
  2016-05-31 14:26   ` Marek Polacek
  0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2016-05-27 15:29 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Joseph Myers; +Cc: Martin Sebor

On 05/26/2016 02:44 PM, Marek Polacek wrote:
> +	  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);

Should this loop?  If there are two variables declared, do we get two 
try blocks?

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Further refinement to -Wswitch-unreachable
  2016-05-27 15:29 ` Jason Merrill
@ 2016-05-31 14:26   ` Marek Polacek
  2016-05-31 15:50     ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2016-05-31 14:26 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches, Joseph Myers, Martin Sebor

On Fri, May 27, 2016 at 10:41:51AM -0400, Jason Merrill wrote:
> On 05/26/2016 02:44 PM, Marek Polacek wrote:
> > +	  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);
> 
> Should this loop?  If there are two variables declared, do we get two try
> blocks?

It looks like we get only one try block, so this doesn't have to loop.  But I
at least added a new test to make sure we warn even with more decls.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2016-05-31  Marek Polacek  <polacek@redhat.com>

	* gimplify.c (gimplify_switch_expr): Also handle GIMPLE_TRY.

	* c-c++-common/Wswitch-unreachable-3.c: New test.
	* g++.dg/warn/Wswitch-unreachable-1.C: New test.

diff --git gcc/gimplify.c gcc/gimplify.c
index 8316bb8..8b7dddc 100644
--- gcc/gimplify.c
+++ gcc/gimplify.c
@@ -1609,10 +1609,17 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
 	  while (gimple_code (seq) == GIMPLE_BIND)
 	    seq = gimple_bind_body (as_a <gbind *> (seq));
 	  gimple *stmt = gimple_seq_first_stmt (seq);
-	  enum gimple_code code = gimple_code (stmt);
-	  if (code != GIMPLE_LABEL && code != GIMPLE_TRY)
+	  if (gimple_code (stmt) == GIMPLE_TRY)
 	    {
-	      if (code == GIMPLE_GOTO
+	      /* 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
diff --git gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c
index e69de29..c53cb10 100644
--- gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c
+++ gcc/testsuite/c-c++-common/Wswitch-unreachable-3.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+
+extern void f (int *);
+
+void
+g (int i)
+{
+  switch (i)
+    {
+      int a[3];
+      __builtin_memset (a, 0, sizeof a); /* { dg-warning "statement will never be executed" } */
+
+    default:
+      f (a);
+    }
+
+  switch (i)
+    {
+      int a[3];
+      int b[3];
+      int c[3];
+      b[1] = 60; /* { dg-warning "statement will never be executed" } */
+
+    default:
+      f (a);
+      f (b);
+      f (c);
+    }
+}
diff --git gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C
index e69de29..99d9a83 100644
--- gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C
+++ gcc/testsuite/g++.dg/warn/Wswitch-unreachable-1.C
@@ -0,0 +1,34 @@
+// { dg-do compile }
+
+extern int j;
+
+void
+f (int i)
+{
+  switch (i) // { dg-warning "statement will never be executed" }
+    {
+      try
+      {
+      }
+      catch (...)
+      {
+      }
+    case 1:;
+    }
+}
+
+void
+g (int i)
+{
+  switch (i)
+    {
+      try
+      {
+	j = 42;  // { dg-warning "statement will never be executed" }
+      }
+      catch (...)
+      {
+      }
+    case 1:;
+    }
+}

	Marek

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Further refinement to -Wswitch-unreachable
  2016-05-31 14:26   ` Marek Polacek
@ 2016-05-31 15:50     ` Jason Merrill
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2016-05-31 15:50 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches, Joseph Myers, Martin Sebor

On Tue, May 31, 2016 at 9:40 AM, Marek Polacek <polacek@redhat.com> wrote:
> On Fri, May 27, 2016 at 10:41:51AM -0400, Jason Merrill wrote:
>> On 05/26/2016 02:44 PM, Marek Polacek wrote:
>> > +     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);
>>
>> Should this loop?  If there are two variables declared, do we get two try
>> blocks?
>
> It looks like we get only one try block, so this doesn't have to loop.  But I
> at least added a new test to make sure we warn even with more decls.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?

OK.

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-05-31 14:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-27  7:53 Further refinement to -Wswitch-unreachable Marek Polacek
2016-05-27 15:29 ` Jason Merrill
2016-05-31 14:26   ` Marek Polacek
2016-05-31 15:50     ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).