From 591baeb02089799b46e5d320bc6f403d583fe3e9 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Sat, 6 Aug 2005 12:31:49 +0100 Subject: [PATCH] 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 --- gcc/ChangeLog | 9 +++++++ gcc/c-typeck.c | 8 +++++- gcc/stmt.c | 35 ++++++-------------------- gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/gcc.dg/Wunused-value-1.c | 31 +++++++++++++++++++++++ 5 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wunused-value-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 7f7405124ab..0d0848ddd4d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2005-08-06 Joseph S. Myers + + 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 PR rtl-optimization/23233 diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index c0498955f08..90787305ecf 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -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); diff --git a/gcc/stmt.c b/gcc/stmt.c index e38b96b6c29..ad75392010b 100644 --- a/gcc/stmt.c +++ b/gcc/stmt.c @@ -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; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0097a9d2cfa..6e27aec9c24 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2005-08-06 Joseph S. Myers + + PR c/23113 + * gcc.dg/Wunused-value-1.c: New test. + 2005-08-06 Richard Sandiford PR rtl-optimization/23233 diff --git a/gcc/testsuite/gcc.dg/Wunused-value-1.c b/gcc/testsuite/gcc.dg/Wunused-value-1.c new file mode 100644 index 00000000000..0fc7c364ba8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunused-value-1.c @@ -0,0 +1,31 @@ +/* Test -Wunused-value. Bug 23113. */ +/* Origin: Joseph Myers */ +/* { 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" } */ +} -- 2.30.2