diff options
author | Jakub Jelinek <jakub@redhat.com> | 2010-04-07 22:33:36 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@gcc.gnu.org> | 2010-04-07 22:33:36 +0200 |
commit | ebfbbdc556443d1ea95923b596f59411256bd9b5 (patch) | |
tree | 2559870c4c1dbe82ba98485de0b41abd6bed7ca2 /gcc/c-parser.c | |
parent | 0bb8c1342ffedb0d012f007342f802e0fb692e4d (diff) | |
download | gcc-ebfbbdc556443d1ea95923b596f59411256bd9b5.zip gcc-ebfbbdc556443d1ea95923b596f59411256bd9b5.tar.gz gcc-ebfbbdc556443d1ea95923b596f59411256bd9b5.tar.bz2 |
re PR c/18624 (GCC does not detect local variable set but never used)
PR c/18624
* tree.h (DECL_READ_P): Define.
(struct tree_decl_common): Add decl_read_flag.
* c-decl.c (pop_scope): If TREE_USED but !DECL_READ_P, issue
a set but not used warning.
(merge_decls): Merge DECL_READ_P flag.
(finish_decl, build_compound_literal): Set DECL_READ_P flag.
(finish_function): Issue -Wunused-but-set-parameter diagnostics.
* c-common.c (handle_used_attribute, handle_unused_attribute):
Likewise.
* c-tree.h (default_function_array_read_conversion, mark_exp_read):
New prototypes.
* c-typeck.c (default_function_array_read_conversion, mark_exp_read):
New functions.
(default_conversion, c_process_expr_stmt): Call mark_exp_read.
* c-parser.c (c_parser_initializer, c_parser_expr_no_commas,
c_parser_binary_expression, c_parser_cast_expression,
c_parser_expr_list, c_parser_omp_atomic, c_parser_omp_for_loop):
Call default_function_array_read_conversion instead of
default_function_array_conversion where needed.
(c_parser_unary_expression, c_parser_conditional_expression,
c_parser_postfix_expression_after_primary, c_parser_initelt):
Likewise. Call mark_exp_read where needed.
(c_parser_statement_after_labels, c_parser_asm_operands,
c_parser_typeof_specifier, c_parser_sizeof_expression,
c_parser_alignof_expression, c_parser_initval): Call mark_exp_read
where needed.
* common.opt (Wunused-but-set-variable, Wunused-but-set-parameter):
New.
* toplev.c (warn_unused_but_set_variable): Default to warn_unused.
(warn_unused_but_set_parameter): Default to warn_unused
&& extra_warnings.
* doc/invoke.texi: Document -Wunused-but-set-variable and
-Wunused-but-set-parameter.
* objc-act.c (finish_var_decl, objc_begin_catch_clause,
really_start_method, get_super_receiver, handle_class_ref): Set
DECL_READ_P in addition to TREE_USED.
* gcc.dg/Wunused-var-1.c: New test.
* gcc.dg/Wunused-var-2.c: New test.
* gcc.dg/Wunused-var-3.c: New test.
* gcc.dg/Wunused-var-4.c: New test.
* gcc.dg/Wunused-var-5.c: New test.
* gcc.dg/Wunused-var-6.c: New test.
* gcc.dg/Wunused-parm-1.c: New test.
From-SVN: r158086
Diffstat (limited to 'gcc/c-parser.c')
-rw-r--r-- | gcc/c-parser.c | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/gcc/c-parser.c b/gcc/c-parser.c index bf66a58..27f0b81 100644 --- a/gcc/c-parser.c +++ b/gcc/c-parser.c @@ -1,6 +1,6 @@ /* Parser for C and Objective-C. Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 + 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. Parser actions based on the old Bison parser; structure somewhat @@ -2171,6 +2171,7 @@ c_parser_typeof_specifier (c_parser *parser) if (TREE_CODE (expr.value) == COMPONENT_REF && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) error_at (here, "%<typeof%> applied to a bit-field"); + mark_exp_read (expr.value); ret.spec = TREE_TYPE (expr.value); was_vm = variably_modified_type_p (ret.spec, NULL_TREE); /* This is returned with the type so that when the type is @@ -3073,7 +3074,7 @@ c_parser_initializer (c_parser *parser) ret = c_parser_expr_no_commas (parser, NULL); if (TREE_CODE (ret.value) != STRING_CST && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR) - ret = default_function_array_conversion (loc, ret); + ret = default_function_array_read_conversion (loc, ret); return ret; } } @@ -3229,6 +3230,7 @@ c_parser_initelt (c_parser *parser) goto parse_message_args; } first = c_parser_expr_no_commas (parser, NULL).value; + mark_exp_read (first); if (c_parser_next_token_is (parser, CPP_ELLIPSIS) || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) goto array_desig_after_first; @@ -3244,7 +3246,8 @@ c_parser_initelt (c_parser *parser) c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; next = c_parser_expr_no_commas (parser, NULL); - next = default_function_array_conversion (exp_loc, next); + next = default_function_array_read_conversion (exp_loc, + next); rec = build_compound_expr (comma_loc, rec, next.value); } parse_message_args: @@ -3264,12 +3267,14 @@ c_parser_initelt (c_parser *parser) } c_parser_consume_token (parser); first = c_parser_expr_no_commas (parser, NULL).value; + mark_exp_read (first); array_desig_after_first: if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) { ellipsis_loc = c_parser_peek_token (parser)->location; c_parser_consume_token (parser); second = c_parser_expr_no_commas (parser, NULL).value; + mark_exp_read (second); } else second = NULL_TREE; @@ -3337,7 +3342,7 @@ c_parser_initval (c_parser *parser, struct c_expr *after) if (init.value != NULL_TREE && TREE_CODE (init.value) != STRING_CST && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR) - init = default_function_array_conversion (loc, init); + init = default_function_array_read_conversion (loc, init); } process_init_element (init, false); } @@ -3840,6 +3845,7 @@ c_parser_statement_after_labels (c_parser *parser) else { struct c_expr expr = c_parser_expression_conv (parser); + mark_exp_read (expr.value); stmt = c_finish_return (loc, expr.value, expr.original_type); goto expect_semicolon; } @@ -4455,6 +4461,7 @@ c_parser_asm_operands (c_parser *parser, bool convert_p) } loc = c_parser_peek_token (parser)->location; expr = c_parser_expression (parser); + mark_exp_read (expr.value); if (convert_p) expr = default_function_array_conversion (loc, expr); expr.value = c_fully_fold (expr.value, false, NULL); @@ -4605,7 +4612,7 @@ c_parser_expr_no_commas (c_parser *parser, struct c_expr *after) c_parser_consume_token (parser); exp_location = c_parser_peek_token (parser)->location; rhs = c_parser_expr_no_commas (parser, NULL); - rhs = default_function_array_conversion (exp_location, rhs); + rhs = default_function_array_read_conversion (exp_location, rhs); ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type, code, exp_location, rhs.value, rhs.original_type); @@ -4647,7 +4654,7 @@ c_parser_conditional_expression (c_parser *parser, struct c_expr *after) if (c_parser_next_token_is_not (parser, CPP_QUERY)) return cond; cond_loc = c_parser_peek_token (parser)->location; - cond = default_function_array_conversion (cond_loc, cond); + cond = default_function_array_read_conversion (cond_loc, cond); c_parser_consume_token (parser); if (c_parser_next_token_is (parser, CPP_COLON)) { @@ -4674,6 +4681,7 @@ c_parser_conditional_expression (c_parser *parser, struct c_expr *after) (cond_loc, default_conversion (cond.value)); c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node; exp1 = c_parser_expression_conv (parser); + mark_exp_read (exp1.value); c_inhibit_evaluation_warnings += ((cond.value == truthvalue_true_node) - (cond.value == truthvalue_false_node)); @@ -4691,7 +4699,7 @@ c_parser_conditional_expression (c_parser *parser, struct c_expr *after) { location_t exp2_loc = c_parser_peek_token (parser)->location; exp2 = c_parser_conditional_expression (parser, NULL); - exp2 = default_function_array_conversion (exp2_loc, exp2); + exp2 = default_function_array_read_conversion (exp2_loc, exp2); } c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node; ret.value = build_conditional_expr (colon_loc, cond.value, @@ -4844,10 +4852,11 @@ c_parser_binary_expression (c_parser *parser, struct c_expr *after) break; \ } \ stack[sp - 1].expr \ - = default_function_array_conversion (stack[sp - 1].loc, \ - stack[sp - 1].expr); \ + = default_function_array_read_conversion (stack[sp - 1].loc, \ + stack[sp - 1].expr); \ stack[sp].expr \ - = default_function_array_conversion (stack[sp].loc, stack[sp].expr); \ + = default_function_array_read_conversion (stack[sp].loc, \ + stack[sp].expr); \ stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \ stack[sp].op, \ stack[sp - 1].expr, \ @@ -4952,8 +4961,8 @@ c_parser_binary_expression (c_parser *parser, struct c_expr *after) { case TRUTH_ANDIF_EXPR: stack[sp].expr - = default_function_array_conversion (stack[sp].loc, - stack[sp].expr); + = default_function_array_read_conversion (stack[sp].loc, + stack[sp].expr); stack[sp].expr.value = c_objc_common_truthvalue_conversion (stack[sp].loc, default_conversion (stack[sp].expr.value)); c_inhibit_evaluation_warnings += (stack[sp].expr.value @@ -4961,8 +4970,8 @@ c_parser_binary_expression (c_parser *parser, struct c_expr *after) break; case TRUTH_ORIF_EXPR: stack[sp].expr - = default_function_array_conversion (stack[sp].loc, - stack[sp].expr); + = default_function_array_read_conversion (stack[sp].loc, + stack[sp].expr); stack[sp].expr.value = c_objc_common_truthvalue_conversion (stack[sp].loc, default_conversion (stack[sp].expr.value)); c_inhibit_evaluation_warnings += (stack[sp].expr.value @@ -5032,7 +5041,7 @@ c_parser_cast_expression (c_parser *parser, struct c_expr *after) { location_t expr_loc = c_parser_peek_token (parser)->location; expr = c_parser_cast_expression (parser, NULL); - expr = default_function_array_conversion (expr_loc, expr); + expr = default_function_array_read_conversion (expr_loc, expr); } ret.value = c_cast_expr (cast_loc, type_name, expr.value); ret.original_code = ERROR_MARK; @@ -5085,23 +5094,24 @@ c_parser_unary_expression (c_parser *parser) c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op); case CPP_MINUS_MINUS: c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op); case CPP_AND: c_parser_consume_token (parser); - return parser_build_unary_op (op_loc, ADDR_EXPR, - c_parser_cast_expression (parser, NULL)); + op = c_parser_cast_expression (parser, NULL); + mark_exp_read (op.value); + return parser_build_unary_op (op_loc, ADDR_EXPR, op); case CPP_MULT: c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR); return ret; case CPP_PLUS: @@ -5112,25 +5122,25 @@ c_parser_unary_expression (c_parser *parser) c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); return parser_build_unary_op (op_loc, CONVERT_EXPR, op); case CPP_MINUS: c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); return parser_build_unary_op (op_loc, NEGATE_EXPR, op); case CPP_COMPL: c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op); case CPP_NOT: c_parser_consume_token (parser); exp_loc = c_parser_peek_token (parser)->location; op = c_parser_cast_expression (parser, NULL); - op = default_function_array_conversion (exp_loc, op); + op = default_function_array_read_conversion (exp_loc, op); return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op); case CPP_AND_AND: /* Refer to the address of a label as a pointer. */ @@ -5230,6 +5240,7 @@ c_parser_sizeof_expression (c_parser *parser) sizeof_expr: c_inhibit_evaluation_warnings--; in_sizeof--; + mark_exp_read (expr.value); if (TREE_CODE (expr.value) == COMPONENT_REF && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) error_at (expr_loc, "%<sizeof%> applied to a bit-field"); @@ -5290,6 +5301,7 @@ c_parser_alignof_expression (c_parser *parser) struct c_expr ret; expr = c_parser_unary_expression (parser); alignof_expr: + mark_exp_read (expr.value); c_inhibit_evaluation_warnings--; in_alignof--; ret.value = c_alignof_expr (loc, expr.value); @@ -5495,6 +5507,7 @@ c_parser_postfix_expression (c_parser *parser) break; } e1 = c_parser_expr_no_commas (parser, NULL); + mark_exp_read (e1.value); e1.value = c_fully_fold (e1.value, false, NULL); if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) { @@ -5639,6 +5652,8 @@ c_parser_postfix_expression (c_parser *parser) tree c; c = e1.value; + mark_exp_read (e2.value); + mark_exp_read (e3.value); if (TREE_CODE (c) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (c))) error_at (loc, @@ -5883,6 +5898,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser, c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); orig_expr = expr; + mark_exp_read (expr.value); /* FIXME diagnostics: Ideally we want the FUNCNAME, not the "(" after the FUNCNAME, which is what we have now. */ expr.value = build_function_call_vec (op_loc, expr.value, exprlist, @@ -5965,7 +5981,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser, case CPP_PLUS_PLUS: /* Postincrement. */ c_parser_consume_token (parser); - expr = default_function_array_conversion (expr_loc, expr); + expr = default_function_array_read_conversion (expr_loc, expr); expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR, expr.value, 0); expr.original_code = ERROR_MARK; @@ -5974,7 +5990,7 @@ c_parser_postfix_expression_after_primary (c_parser *parser, case CPP_MINUS_MINUS: /* Postdecrement. */ c_parser_consume_token (parser); - expr = default_function_array_conversion (expr_loc, expr); + expr = default_function_array_read_conversion (expr_loc, expr); expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR, expr.value, 0); expr.original_code = ERROR_MARK; @@ -6052,7 +6068,7 @@ c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p, expr = c_parser_expr_no_commas (parser, NULL); if (convert_p) - expr = default_function_array_conversion (loc, expr); + expr = default_function_array_read_conversion (loc, expr); if (fold_p) expr.value = c_fully_fold (expr.value, false, NULL); VEC_quick_push (tree, ret, expr.value); @@ -6064,7 +6080,7 @@ c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p, loc = c_parser_peek_token (parser)->location; expr = c_parser_expr_no_commas (parser, NULL); if (convert_p) - expr = default_function_array_conversion (loc, expr); + expr = default_function_array_read_conversion (loc, expr); if (fold_p) expr.value = c_fully_fold (expr.value, false, NULL); VEC_safe_push (tree, gc, ret, expr.value); @@ -7887,7 +7903,7 @@ c_parser_omp_atomic (location_t loc, c_parser *parser) { location_t rhs_loc = c_parser_peek_token (parser)->location; rhs_expr = c_parser_expression (parser); - rhs_expr = default_function_array_conversion (rhs_loc, rhs_expr); + rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr); } rhs = rhs_expr.value; rhs = c_fully_fold (rhs, false, NULL); @@ -8034,7 +8050,8 @@ c_parser_omp_for_loop (location_t loc, init_loc = c_parser_peek_token (parser)->location; init_exp = c_parser_expr_no_commas (parser, NULL); - init_exp = default_function_array_conversion (init_loc, init_exp); + init_exp = default_function_array_read_conversion (init_loc, + init_exp); init = build_modify_expr (init_loc, decl, decl_exp.original_type, NOP_EXPR, init_loc, init_exp.value, init_exp.original_type); |