re PR middle-end/17544 (incorrect -Wunreachable-code warning for mains with a return...
authorSteven Bosscher <stevenb@suse.de>
Sat, 1 Jan 2005 16:15:23 +0000 (16:15 +0000)
committerSteven Bosscher <steven@gcc.gnu.org>
Sat, 1 Jan 2005 16:15:23 +0000 (16:15 +0000)
* 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
gcc/c-decl.c
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/emit-rtl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wunreachable-code-1.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/20041231-1.c [new file with mode: 0644]
gcc/tree-cfg.c

index 983ad2ce792f3da86ba771b690d255cec5dc04ba..8bc75edd5048ba9b92c413a65146d6018591b511 100644 (file)
@@ -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
index 2ff015ec7dc15be5605ab8012329a6f9c96a9397..a0053ed2fbe61aeb10129f35f6cbfc7e438e5be7 100644 (file)
@@ -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);
+           }
        }
     }
 
index ccc975afa84a502acf2f702f0be571088053c7a5..b061e69646a0404bff97f16dbeacbe99d0473bc0 100644 (file)
@@ -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
index 61f2d648679ea853d9bf8287d3307d01bb9eaeab..d0d1ff354b34f19ed3de60a8cff13dc702db60a7 100644 (file)
@@ -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.  */
index 793e2df8da466a3bbd87e463f20429af74994ad3..46d9183b92ddeb8b97813c4bce1d3ae4c0f45193 100644 (file)
@@ -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)
index c077b73c787d6c9c747880735bba576da0a765ed..76c2aeaa0873bb83c0f27fde76dd03960405e3ab 100644 (file)
@@ -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 (file)
index 0000000..23fc86d
--- /dev/null
@@ -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 (file)
index 0000000..37c9fe0
--- /dev/null
@@ -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
index e020676f7a457f30940d15b7dc256ca61f5863d7..a68c964c0c866efcd691f308d1946028736300c6 100644 (file)
@@ -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