From 056928b248b7e3f879c2b3ac4be935e67ad3a09a Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 11 May 2010 20:12:28 +0200 Subject: [PATCH] re PR c++/44062 ((void)var; doesn't prevent 'set but not used' warning) PR c++/44062 * c-parser.c (c_parser_expression): Mark LHS of a comma expression as read if it is a decl, handled component or COMPOUND_EXPR with that on the RHS. * c-typeck.c (c_process_expr_stmt): Mark RHS of COMPOUND_EXPR if it is a decl or handled component. * semantics.c (finish_expr_stmt): Don't call mark_exp_read here... * cvt.c (convert_to_void): ... but here. If expr is a COMPOUND_EXPR, look at its second operand. * c-c++-common/Wunused-var-7.c: New test. * g++.dg/warn/Wunused-var-9.C: New test. From-SVN: r159286 --- gcc/ChangeLog | 9 ++++++ gcc/c-parser.c | 6 ++++ gcc/c-typeck.c | 9 ++++-- gcc/cp/ChangeLog | 7 +++++ gcc/cp/cvt.c | 16 +++++++++- gcc/cp/semantics.c | 9 +----- gcc/testsuite/ChangeLog | 6 ++++ gcc/testsuite/c-c++-common/Wunused-var-7.c | 48 ++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/warn/Wunused-var-9.C | 13 ++++++++ 9 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wunused-var-7.c create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-9.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4f370ef..712b437 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,12 @@ +2010-05-11 Jakub Jelinek + + PR c++/44062 + * c-parser.c (c_parser_expression): Mark LHS of a comma + expression as read if it is a decl, handled component or + COMPOUND_EXPR with that on the RHS. + * c-typeck.c (c_process_expr_stmt): Mark RHS of COMPOUND_EXPR + if it is a decl or handled component. + 2010-05-11 Jan Hubicka * lto-symtab.c (lto_symtab_free): New function. diff --git a/gcc/c-parser.c b/gcc/c-parser.c index e5440d4..a0f1bea 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -6028,10 +6028,16 @@ c_parser_expression (c_parser *parser) while (c_parser_next_token_is (parser, CPP_COMMA)) { struct c_expr next; + tree lhsval; location_t loc = c_parser_peek_token (parser)->location; location_t expr_loc; c_parser_consume_token (parser); expr_loc = c_parser_peek_token (parser)->location; + lhsval = expr.value; + while (TREE_CODE (lhsval) == COMPOUND_EXPR) + lhsval = TREE_OPERAND (lhsval, 1); + if (DECL_P (lhsval) || handled_component_p (lhsval)) + mark_exp_read (lhsval); next = c_parser_expr_no_commas (parser, NULL); next = default_function_array_conversion (expr_loc, next); expr.value = build_compound_expr (loc, expr.value, next.value); diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 1bac4fd..083b5a1 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -8841,6 +8841,8 @@ emit_side_effect_warnings (location_t loc, tree expr) tree c_process_expr_stmt (location_t loc, tree expr) { + tree exprv; + if (!expr) return NULL_TREE; @@ -8861,8 +8863,11 @@ c_process_expr_stmt (location_t loc, tree expr) && warn_unused_value) emit_side_effect_warnings (loc, expr); - if (DECL_P (expr) || handled_component_p (expr)) - mark_exp_read (expr); + exprv = expr; + while (TREE_CODE (exprv) == COMPOUND_EXPR) + exprv = TREE_OPERAND (exprv, 1); + if (DECL_P (exprv) || handled_component_p (exprv)) + mark_exp_read (exprv); /* If the expression is not of a type to which we cannot assign a line number, wrap the thing in a no-op NOP_EXPR. */ diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 31749dd..597f8f1 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2010-05-11 Jakub Jelinek + + PR c++/44062 + * semantics.c (finish_expr_stmt): Don't call mark_exp_read here... + * cvt.c (convert_to_void): ... but here. If expr is a COMPOUND_EXPR, + look at its second operand. + 2010-05-10 Jason Merrill PR c++/44017 diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index b357084..efef5c2 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -1,6 +1,6 @@ /* Language-level data type conversion for GNU C++. Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. Hacked by Michael Tiemann (tiemann@cygnus.com) @@ -822,9 +822,23 @@ ocp_convert (tree type, tree expr, int convtype, int flags) tree convert_to_void (tree expr, const char *implicit, tsubst_flags_t complain) { + tree exprv; + if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node) return error_mark_node; + + exprv = expr; + while (TREE_CODE (exprv) == COMPOUND_EXPR) + exprv = TREE_OPERAND (exprv, 1); + if (DECL_P (exprv) || handled_component_p (exprv)) + /* Expr is not being 'used' here, otherwise we whould have + called mark_{rl}value_use use here, which would have in turn + called mark_exp_read. Rather, we call mark_exp_read directly + to avoid some warnings when + -Wunused-but-set-{variable,parameter} is in effect. */ + mark_exp_read (exprv); + if (!TREE_TYPE (expr)) return expr; if (invalid_nonstatic_memfn_p (expr, complain)) diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index cbbcf96..135de46 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -4,7 +4,7 @@ and during the instantiation of template functions. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, - 2008, 2009 Free Software Foundation, Inc. + 2008, 2009, 2010 Free Software Foundation, Inc. Written by Mark Mitchell (mmitchell@usa.net) based on code found formerly in parse.y and pt.c. @@ -610,13 +610,6 @@ finish_expr_stmt (tree expr) { if (warn_sequence_point) verify_sequence_points (expr); - if (TREE_CODE (expr) != MODIFY_EXPR) - /* Expr is not being 'used' here, otherwise we whould have - called mark_{rl}value_use use here, which would have in turn - called mark_exp_read. Rather, we call mark_exp_read directly - to avoid some warnings when - -Wunused-but-set-{variable,parameter} is in effect. */ - mark_exp_read (expr); expr = convert_to_void (expr, "statement", tf_warning_or_error); } else if (!type_dependent_expression_p (expr)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f42ccba..8a74fd1 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2010-05-11 Jakub Jelinek + + PR c++/44062 + * c-c++-common/Wunused-var-7.c: New test. + * g++.dg/warn/Wunused-var-9.C: New test. + 2010-05-11 Daniel Franke PR fortran/43711 diff --git a/gcc/testsuite/c-c++-common/Wunused-var-7.c b/gcc/testsuite/c-c++-common/Wunused-var-7.c new file mode 100644 index 0000000..7419643 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wunused-var-7.c @@ -0,0 +1,48 @@ +/* PR c++/44062 */ +/* { dg-do compile } */ +/* { dg-options "-Wunused" } */ + +void +foo (void) +{ + int a, b, c, d, e, f, g; + a = 1; + b = 2; + c = 3; + d = 4; + e = 5; + f = 6; + g = 7; + a; /* { dg-warning "no effect" } */ + b, 1; /* { dg-warning "no effect" } */ + (void) c; + (void) d, 1; /* { dg-warning "no effect" } */ + e, f, 1; /* { dg-warning "no effect" } */ + (void) g, f, 1; /* { dg-warning "no effect" } */ +} + +void +bar (void) +{ + int a; + int b; + int c; /* { dg-warning "set but not used" } */ + a = 1; + b = 2; + c = 3; + c = ({ a++, b; }); +} + +void +baz (void) +{ + int a; + int b; + int c; + int d; + a = 1; + b = 2; + c = 3; + d = 4; + d, ( a++, b ), c; /* { dg-warning "no effect" } */ +} diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-9.C b/gcc/testsuite/g++.dg/warn/Wunused-var-9.C new file mode 100644 index 0000000..cb422f4 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-9.C @@ -0,0 +1,13 @@ +/* PR c++/44062 */ +/* { dg-do compile } */ +/* { dg-options "-Wunused" } */ + +void +f () +{ + int i = 4; + static_cast (i); + int j; + j = 5; + static_cast (j); +} -- 2.7.4