From 43e05e45bcd3092ef7fb02e537ca7645607dbede Mon Sep 17 00:00:00 2001
From: Steven Bosscher <stevenb@suse.de>
Date: Sat, 1 Jan 2005 16:15:23 +0000
Subject: [PATCH] re PR middle-end/17544 (incorrect -Wunreachable-code warning
 for mains with a return statement)

	* emit-rtl.c (add_insn_before): Fix comment typo.

	PR middle-end/17544
	* c-decl.c (finish_function): If compiling C99, annotate the
	compiler generated return with the current file name and line 0.
	* tree-cfg.c (remove_useless_stmts_warn_notreached): Only warn if
	the source line is greater than 0.
	(remove_bb): Likewise.

cp/
	PR middle-end/17544
	* decl.c (finish_function): Fix comment.  Annotate the compiler
	generated return with the current file name and line 0.

testsuite/
	* gcc.dg/20041231-1.C: New test.
	* g++.dg/warn/Wunreachable-code-1.C: New test.

From-SVN: r92784
---
 gcc/ChangeLog                                 |  9 ++++++++
 gcc/c-decl.c                                  | 11 ++++++++--
 gcc/cp/ChangeLog                              |  6 +++++
 gcc/cp/decl.c                                 | 15 +++++++++----
 gcc/emit-rtl.c                                |  4 ++--
 gcc/testsuite/ChangeLog                       |  5 +++++
 .../g++.dg/warn/Wunreachable-code-1.C         | 22 +++++++++++++++++++
 gcc/testsuite/gcc.dg/20041231-1.c             | 15 +++++++++++++
 gcc/tree-cfg.c                                | 19 +++++++++++-----
 9 files changed, 93 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wunreachable-code-1.C
 create mode 100644 gcc/testsuite/gcc.dg/20041231-1.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 983ad2ce792f..8bc75edd5048 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2005-01-01  Steven Bosscher  <stevenb@suse.de>
+
+	PR middle-end/17544
+	* c-decl.c (finish_function): If compiling C99, annotate the
+	compiler generated return with the current file name and line 0.
+	* tree-cfg.c (remove_useless_stmts_warn_notreached): Only warn if
+	the source line is greater than 0.
+	(remove_bb): Likewise.
+
 2004-12-31  Richard Henderson  <rth@redhat.com>
 
 	PR tree-opt/19042
diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 2ff015ec7dc1..a0053ed2fbe6 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -1,6 +1,6 @@
 /* Process declarations and variables for C compiler.
    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -29,6 +29,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "input.h"
 #include "tm.h"
 #include "intl.h"
 #include "tree.h"
@@ -6287,7 +6288,13 @@ finish_function (void)
       else
 	{
 	  if (flag_isoc99)
-	    c_finish_return (integer_zero_node);
+	    {
+	      tree stmt = c_finish_return (integer_zero_node);
+	      /* Hack.  We don't want the middle-end to warn that this
+		 return is unreachable, so put the statement on the
+		 special line 0.  */
+	      annotate_with_file_line (stmt, input_filename, 0);
+	    }
 	}
     }
 
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index ccc975afa84a..b061e69646a0 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2005-01-01  Steven Bosscher  <stevenb@suse.de>
+
+	PR middle-end/17544
+	* decl.c (finish_function): Fix comment.  Annotate the compiler
+	generated return with the current file name and line 0.
+
 2004-12-31  Richard Henderson  <rth@redhat.com>
 
 	PR middle-end/17799
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 61f2d648679e..d0d1ff354b34 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -1,6 +1,6 @@
 /* Process declarations and variables for C++ compiler.
    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004  Free Software Foundation, Inc.
+   2001, 2002, 2003, 2004,2005  Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GCC.
@@ -10635,12 +10635,19 @@ finish_function (int flags)
     {
       if (DECL_MAIN_P (current_function_decl))
 	{
-	  /* Make it so that `main' always returns 0 by default.  */
+	  tree stmt;
+
+	  /* Make it so that `main' always returns 0 by default (or
+	     1 for VMS).  */
 #if VMS_TARGET
-	  finish_return_stmt (integer_one_node);
+	  stmt = finish_return_stmt (integer_one_node);
 #else
-	  finish_return_stmt (integer_zero_node);
+	  stmt = finish_return_stmt (integer_zero_node);
 #endif
+	  /* Hack.  We don't want the middle-end to warn that this
+	     return is unreachable, so put the statement on the
+	     special line 0.  */
+	  annotate_with_file_line (stmt, input_filename, 0);
 	}
 
       /* Finish dealing with exception specifiers.  */
diff --git a/gcc/emit-rtl.c b/gcc/emit-rtl.c
index 793e2df8da46..46d9183b92dd 100644
--- a/gcc/emit-rtl.c
+++ b/gcc/emit-rtl.c
@@ -1,6 +1,6 @@
 /* Emit RTL for the GCC expander.
    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -3473,7 +3473,7 @@ add_insn_before (rtx insn, rtx before)
       if (INSN_P (insn))
 	bb->flags |= BB_DIRTY;
       /* Should not happen as first in the BB is always either NOTE or
-	 LABEl.  */
+	 LABEL.  */
       gcc_assert (BB_HEAD (bb) != insn
 		  /* Avoid clobbering of structure when creating new BB.  */
 		  || BARRIER_P (insn)
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index c077b73c787d..76c2aeaa0873 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2005-01-01  Steven Bosscher  <stevenb@suse.de>
+
+	* gcc.dg/20041231-1.C: New test.
+	* g++.dg/warn/Wunreachable-code-1.C: New test.
+
 2004-12-30  David Edelsohn  <edelsohn@gnu.org>
 
 	* gfortran.fortran-torture/execute/read_eof.f90: Open scratch
diff --git a/gcc/testsuite/g++.dg/warn/Wunreachable-code-1.C b/gcc/testsuite/g++.dg/warn/Wunreachable-code-1.C
new file mode 100644
index 000000000000..23fc86dfbb28
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunreachable-code-1.C
@@ -0,0 +1,22 @@
+/* PR17544 Incorrect -Wunreachable-code warning
+   Origin: sebor@roguewave.com
+
+   G++ appends a "return 0;" when finishing a function, but it was not
+   given a source location.  The gimplifier thinks a return statement
+   needs a locus so it would add one, making the compiler generated code
+   visible to the unreachable code warning.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O -Wunreachable-code" } */
+
+int
+main (int argc, char *argv[])
+{
+  const char* const s = argc < 2 ? "" : argv [1];
+  int i = 0;
+  do {
+    ++i;
+  } while (i < s [0]);
+  return i;
+}
+
diff --git a/gcc/testsuite/gcc.dg/20041231-1.c b/gcc/testsuite/gcc.dg/20041231-1.c
new file mode 100644
index 000000000000..37c9fe04a1f8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/20041231-1.c
@@ -0,0 +1,15 @@
+/* PR17544 Incorrect -Wunreachable-code warning
+   Origin: Giovanni Bajo
+
+   In C99 we append a "return 0;" when finishing a function, but it was
+   not given a source location.  The gimplifier thinks a return statement
+   needs a locus so it would add one, making the compiler generated code
+   visible to the unreachable code warning.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -O -Wunreachable-code" } */
+
+int main (void)    // 1
+{                  // 2
+  return 0;        // 3
+}                  // 4
diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
index e020676f7a45..a68c964c0c86 100644
--- a/gcc/tree-cfg.c
+++ b/gcc/tree-cfg.c
@@ -1,5 +1,5 @@
 /* Control flow functions for trees.
-   Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
 This file is part of GCC.
@@ -1323,8 +1323,11 @@ remove_useless_stmts_warn_notreached (tree stmt)
   if (EXPR_HAS_LOCATION (stmt))
     {
       location_t loc = EXPR_LOCATION (stmt);
-      warning ("%Hwill never be executed", &loc);
-      return true;
+      if (LOCATION_LINE (loc) > 0)
+	{
+	  warning ("%Hwill never be executed", &loc);
+	  return true;
+	}
     }
 
   switch (TREE_CODE (stmt))
@@ -2021,11 +2024,17 @@ remove_bb (basic_block bb)
 	 since this way we lose warnings for gotos in the original
 	 program that are indeed unreachable.  */
       if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
+	{
+	  source_locus t;
+
 #ifdef USE_MAPPED_LOCATION
-	loc = EXPR_LOCATION (stmt);
+	  t = EXPR_LOCATION (stmt);
 #else
-	loc = EXPR_LOCUS (stmt);
+	  t = EXPR_LOCUS (stmt);
 #endif
+	  if (t && LOCATION_LINE (*t) > 0)
+	    loc = t;
+	}
     }
 
   /* If requested, give a warning that the first statement in the
-- 
GitLab