Patchwork C++ PATCH for c++/20681 (bogus -Wreturn-type warning)

login
register
mail settings
Submitter Jason Merrill
Date Jan. 13, 2012, 8:03 p.m.
Message ID <4F108E17.3060802@redhat.com>
Download mbox | patch
Permalink /patch/135981/
State New
Headers show

Comments

Jason Merrill - Jan. 13, 2012, 8:03 p.m.
Here the issue is that in some cases the compiler warns about control 
reaching the end of the function when a return is followed by a break.

In the audit trail people noted that the C front end worked around this 
issue with

   http://gcc.gnu.org/ml/gcc-patches/2005-01/msg01452.html

and I've just adopted the same approach.

Tested x86_64-pc-linux-gnu, applying to trunk.

Patch

commit 00eeabd8273ce6dca5d2ccba45b302119cd798e6
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jan 13 14:27:04 2012 -0500

    	PR c++/20681
    	* semantics.c (finish_break_stmt): Avoid adding an unreachable
    	BREAK_STMT.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 6f6f0ac..8c976eb 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -995,6 +995,15 @@  finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
 tree
 finish_break_stmt (void)
 {
+  /* In switch statements break is sometimes stylistically used after
+     a return statement.  This can lead to spurious warnings about
+     control reaching the end of a non-void function when it is
+     inlined.  Note that we are calling block_may_fallthru with
+     language specific tree nodes; this works because
+     block_may_fallthru returns true when given something it does not
+     understand.  */
+  if (!block_may_fallthru (cur_stmt_list))
+    return void_zero_node;
   return add_stmt (build_stmt (input_location, BREAK_STMT));
 }
 
diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-type-7.C b/gcc/testsuite/g++.dg/warn/Wreturn-type-7.C
new file mode 100644
index 0000000..62e34a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wreturn-type-7.C
@@ -0,0 +1,16 @@ 
+// PR c++/20681
+// { dg-options -Wreturn-type }
+
+struct a{~a();a();};
+int GetMetaCombination (int a2)
+{
+  a bi;
+  switch (a2)
+  {
+    case 1:
+      return 18;
+      break;//removing this works around the warning
+    default:
+      return 0;
+  }
+}