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
This commit is contained in:
Steven Bosscher 2005-01-01 16:15:23 +00:00 committed by Steven Bosscher
parent 8e1d2e8278
commit 43e05e45bc
9 changed files with 93 additions and 13 deletions

View 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

View 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);
}
}
}

View 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

View 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. */

View 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)

View 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

View File

@ -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;
}

View File

@ -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

View 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