re PR c/23113 (The -Wunused (value computed is not used) option missed an important case)

PR c/23113
	* stmt.c (warn_if_unused_value): Check TREE_NO_WARNING at start.
	Don't handle NOP_EXPR, CONVERT_EXPR and NON_LVALUE_EXPR
	specially.  Check for side effects only for COND_EXPR.
	* c-typeck.c (c_finish_stmt_expr): Mark statement expression
	return with TREE_NO_WARNING.

testsuite:
	* gcc.dg/Wunused-value-1.c: New test.

From-SVN: r102805
This commit is contained in:
Joseph Myers 2005-08-06 12:31:49 +01:00 committed by Joseph Myers
parent f7a064b514
commit 591baeb020
5 changed files with 59 additions and 29 deletions

View file

@ -1,3 +1,12 @@
2005-08-06 Joseph S. Myers <joseph@codesourcery.com>
PR c/23113
* stmt.c (warn_if_unused_value): Check TREE_NO_WARNING at start.
Don't handle NOP_EXPR, CONVERT_EXPR and NON_LVALUE_EXPR
specially. Check for side effects only for COND_EXPR.
* c-typeck.c (c_finish_stmt_expr): Mark statement expression
return with TREE_NO_WARNING.
2005-08-06 Richard Sandiford <richard@codesourcery.com>
PR rtl-optimization/23233

View file

@ -7315,7 +7315,13 @@ c_finish_stmt_expr (tree body)
if (last == error_mark_node
|| (last == BIND_EXPR_BODY (body)
&& BIND_EXPR_VARS (body) == NULL))
return last;
{
/* Do not warn if the return value of a statement expression is
unused. */
if (EXPR_P (last))
TREE_NO_WARNING (last) = 1;
return last;
}
/* Extract the type of said expression. */
type = TREE_TYPE (last);

View file

@ -1373,7 +1373,7 @@ int
warn_if_unused_value (tree exp, location_t locus)
{
restart:
if (TREE_USED (exp))
if (TREE_USED (exp) || TREE_NO_WARNING (exp))
return 0;
/* Don't warn about void constructs. This includes casting to void,
@ -1416,8 +1416,6 @@ warn_if_unused_value (tree exp, location_t locus)
goto restart;
case COMPOUND_EXPR:
if (TREE_NO_WARNING (exp))
return 0;
if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
return 1;
/* Let people do `(foo (), 0)' without a warning. */
@ -1426,27 +1424,12 @@ warn_if_unused_value (tree exp, location_t locus)
exp = TREE_OPERAND (exp, 1);
goto restart;
case NOP_EXPR:
case CONVERT_EXPR:
case NON_LVALUE_EXPR:
/* Don't warn about conversions not explicit in the user's program. */
if (TREE_NO_WARNING (exp))
case COND_EXPR:
/* If this is an expression with side effects, don't warn; this
case commonly appears in macro expansions. */
if (TREE_SIDE_EFFECTS (exp))
return 0;
/* Assignment to a cast usually results in a cast of a modify.
Don't complain about that. There can be an arbitrary number of
casts before the modify, so we must loop until we find the first
non-cast expression and then test to see if that is a modify. */
{
tree tem = TREE_OPERAND (exp, 0);
while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
tem = TREE_OPERAND (tem, 0);
if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
|| TREE_CODE (tem) == CALL_EXPR)
return 0;
}
goto maybe_warn;
goto warn;
case INDIRECT_REF:
/* Don't warn about automatic dereferencing of references, since
@ -1470,11 +1453,7 @@ warn_if_unused_value (tree exp, location_t locus)
if (EXPRESSION_CLASS_P (exp) && TREE_CODE_LENGTH (TREE_CODE (exp)) == 0)
return 0;
maybe_warn:
/* If this is an expression with side effects, don't warn. */
if (TREE_SIDE_EFFECTS (exp))
return 0;
warn:
warning (0, "%Hvalue computed is not used", &locus);
return 1;
}

View file

@ -1,3 +1,8 @@
2005-08-06 Joseph S. Myers <joseph@codesourcery.com>
PR c/23113
* gcc.dg/Wunused-value-1.c: New test.
2005-08-06 Richard Sandiford <richard@codesourcery.com>
PR rtl-optimization/23233

View file

@ -0,0 +1,31 @@
/* Test -Wunused-value. Bug 23113. */
/* Origin: Joseph Myers <joseph@codesourcery.com> */
/* { dg-do compile } */
/* { dg-options "-Wunused-value" } */
int f (void);
void g (void);
int *p;
void
h (void)
{
1 + f (); /* { dg-warning "value computed is not used" } */
f () + f (); /* { dg-warning "value computed is not used" } */
f () + f (), f (); /* { dg-warning "value computed is not used" } */
(char) f (); /* { dg-warning "value computed is not used" } */
g ();
f ();
(void) f ();
*p++; /* { dg-warning "value computed is not used" } */
++*p;
(*p ? f() : 0);
({ f(); });
/* Statement expressions may be used in macro expansions which like
functions return values which may or may not be of use, so don't
warn for them but do warn inside them. */
({ f() + 1; });
({ f(); 0; });
({ f() + 1; 0; }); /* { dg-warning "value computed is not used" } */
1 + ({ f(); }); /* { dg-warning "value computed is not used" } */
}