public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix PR c++/66590: incorrect warning "reaches end of non-void function" for switch
@ 2021-08-13 18:40 apinski
  2021-08-25 21:15 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: apinski @ 2021-08-13 18:40 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

So the problem here is there is code in the C++ front-end not to add a
break statement (to the IR) if the previous block does not fall through.
The problem is the code which does the check to see if the block
may fallthrough does not check a CLEANUP_STMT; it assumes it is always
fall through.  Anyways this adds the code for the case of a CLEANUP_STMT
that is only for !CLEANUP_EH_ONLY (the try/finally case).

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/cp/ChangeLog:

	* cp-objcp-common.c (cxx_block_may_fallthru): Handle
	CLEANUP_STMT for the case which will be try/finally.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wreturn-5.C: New test.
---
 gcc/cp/cp-objcp-common.c              |  9 +++++++++
 gcc/testsuite/g++.dg/warn/Wreturn-5.C | 15 +++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wreturn-5.C

diff --git a/gcc/cp/cp-objcp-common.c b/gcc/cp/cp-objcp-common.c
index beef0123b04..43888507b85 100644
--- a/gcc/cp/cp-objcp-common.c
+++ b/gcc/cp/cp-objcp-common.c
@@ -317,6 +317,15 @@ cxx_block_may_fallthru (const_tree stmt)
 	return true;
       return block_may_fallthru (ELSE_CLAUSE (stmt));
 
+    case CLEANUP_STMT:
+      /* Just handle the try/finally cases.  */
+      if (!CLEANUP_EH_ONLY (stmt))
+	{
+	  return (block_may_fallthru (CLEANUP_BODY (stmt))
+		  && block_may_fallthru (CLEANUP_EXPR (stmt)));
+	}
+      return true;
+
     default:
       return c_block_may_fallthru (stmt);
     }
diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-5.C b/gcc/testsuite/g++.dg/warn/Wreturn-5.C
new file mode 100644
index 00000000000..543e33e905d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wreturn-5.C
@@ -0,0 +1,15 @@
+// PR C++/66590
+// { dg-do compile }
+// { dg-options "-Wall" }
+
+struct A{ ~A();};
+
+int f(int x)
+{
+    A a;
+    switch (x)
+    {
+        case 1: { A tmp; return 1; } break;
+        default: return 0;
+    }
+}	// { dg-bogus "control reaches end of non-void function" }
-- 
2.27.0


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

* Re: [PATCH] Fix PR c++/66590: incorrect warning "reaches end of non-void function" for switch
  2021-08-13 18:40 [PATCH] Fix PR c++/66590: incorrect warning "reaches end of non-void function" for switch apinski
@ 2021-08-25 21:15 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-08-25 21:15 UTC (permalink / raw)
  To: apinski, gcc-patches

On 8/13/21 2:40 PM, apinski--- via Gcc-patches wrote:
> From: Andrew Pinski <apinski@marvell.com>
> 
> So the problem here is there is code in the C++ front-end not to add a
> break statement (to the IR) if the previous block does not fall through.
> The problem is the code which does the check to see if the block
> may fallthrough does not check a CLEANUP_STMT; it assumes it is always
> fall through.  Anyways this adds the code for the case of a CLEANUP_STMT
> that is only for !CLEANUP_EH_ONLY (the try/finally case).
> 
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK, thanks.

> gcc/cp/ChangeLog:
> 
> 	* cp-objcp-common.c (cxx_block_may_fallthru): Handle
> 	CLEANUP_STMT for the case which will be try/finally.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wreturn-5.C: New test.
> ---
>   gcc/cp/cp-objcp-common.c              |  9 +++++++++
>   gcc/testsuite/g++.dg/warn/Wreturn-5.C | 15 +++++++++++++++
>   2 files changed, 24 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wreturn-5.C
> 
> diff --git a/gcc/cp/cp-objcp-common.c b/gcc/cp/cp-objcp-common.c
> index beef0123b04..43888507b85 100644
> --- a/gcc/cp/cp-objcp-common.c
> +++ b/gcc/cp/cp-objcp-common.c
> @@ -317,6 +317,15 @@ cxx_block_may_fallthru (const_tree stmt)
>   	return true;
>         return block_may_fallthru (ELSE_CLAUSE (stmt));
>   
> +    case CLEANUP_STMT:
> +      /* Just handle the try/finally cases.  */
> +      if (!CLEANUP_EH_ONLY (stmt))
> +	{
> +	  return (block_may_fallthru (CLEANUP_BODY (stmt))
> +		  && block_may_fallthru (CLEANUP_EXPR (stmt)));
> +	}
> +      return true;
> +
>       default:
>         return c_block_may_fallthru (stmt);
>       }
> diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-5.C b/gcc/testsuite/g++.dg/warn/Wreturn-5.C
> new file mode 100644
> index 00000000000..543e33e905d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wreturn-5.C
> @@ -0,0 +1,15 @@
> +// PR C++/66590
> +// { dg-do compile }
> +// { dg-options "-Wall" }
> +
> +struct A{ ~A();};
> +
> +int f(int x)
> +{
> +    A a;
> +    switch (x)
> +    {
> +        case 1: { A tmp; return 1; } break;
> +        default: return 0;
> +    }
> +}	// { dg-bogus "control reaches end of non-void function" }
> 


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

end of thread, other threads:[~2021-08-25 21:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-13 18:40 [PATCH] Fix PR c++/66590: incorrect warning "reaches end of non-void function" for switch apinski
2021-08-25 21:15 ` 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).