From c23f72f25fde8d5583a368b903d1e46bab9f2ff8 Mon Sep 17 00:00:00 2001 From: jakub Date: Wed, 7 Apr 2010 20:33:36 +0000 Subject: [PATCH] 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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@158086 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 35 +++++++ gcc/c-typeck.c | 1 + gcc/doc/invoke.texi | 30 +++++- gcc/objc/ChangeLog | 7 ++ gcc/testsuite/ChangeLog | 9 ++ gcc/testsuite/gcc.dg/Wunused-var-1.c | 179 ++++++++++++++++++++++++++++++++--- gcc/testsuite/gcc.dg/Wunused-var-2.c | 25 ++--- gcc/testsuite/gcc.dg/Wunused-var-3.c | 37 ++++++++ gcc/testsuite/gcc.dg/Wunused-var-4.c | 26 +++++ gcc/testsuite/gcc.dg/Wunused-var-5.c | 33 +++++++ gcc/testsuite/gcc.dg/Wunused-var-6.c | 19 ++++ 11 files changed, 374 insertions(+), 27 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wunused-var-3.c create mode 100644 gcc/testsuite/gcc.dg/Wunused-var-4.c create mode 100644 gcc/testsuite/gcc.dg/Wunused-var-5.c create mode 100644 gcc/testsuite/gcc.dg/Wunused-var-6.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 1f32f76d46f..9db02bcfc60 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,40 @@ 2010-04-07 Jakub Jelinek + 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. + * tree-ssa-pre.c (my_rev_post_order_compute): Remove set but not used count variable. * genemit.c (gen_expand, gen_split): Avoid set but not used warnings diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index a9cd10691ac..326fa86ee32 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -8869,6 +8869,7 @@ c_process_expr_stmt (location_t loc, tree expr) number, wrap the thing in a no-op NOP_EXPR. */ if (DECL_P (expr) || CONSTANT_CLASS_P (expr)) { + mark_exp_read (expr); expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr); SET_EXPR_LOCATION (expr, loc); } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index d356daaf021..67a1b60d15c 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -262,7 +262,7 @@ Objective-C and Objective-C++ Dialects}. -Wunknown-pragmas -Wno-pragmas @gol -Wunsuffixed-float-constants -Wunused -Wunused-function @gol -Wunused-label -Wunused-parameter -Wno-unused-result -Wunused-value -Wunused-variable @gol --Wvariadic-macros -Wvla @gol +-Wunused-but-set-parameter -Wunused-but-set-variable -Wvariadic-macros -Wvla @gol -Wvolatile-register-var -Wwrite-strings} @item C and Objective-C-only Warning Options @@ -2926,7 +2926,8 @@ name is still supported, but the newer name is more descriptive.) -Wsign-compare @gol -Wtype-limits @gol -Wuninitialized @gol --Wunused-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)} @gol +-Wunused-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)} @gol +-Wunused-but-set-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)} @gol } The option @option{-Wextra} also prints warning messages for the @@ -3321,6 +3322,31 @@ Warn if any trigraphs are encountered that might change the meaning of the program (trigraphs within comments are not warned about). This warning is enabled by @option{-Wall}. +@item -Wunused-but-set-parameter +@opindex Wunused-but-set-parameter +@opindex Wno-unused-but-set-parameter +Warn whenever a function parameter is assigned to, but otherwise unused +(aside from its declaration). + +To suppress this warning use the @samp{unused} attribute +(@pxref{Variable Attributes}). + +This warning is also enabled by @option{-Wunused} together with +@option{-Wextra}. + +@item -Wunused-but-set-variable +@opindex Wunused-but-set-variable +@opindex Wno-unused-but-set-variable +Warn whenever a local variable is assigned to, but otherwise unused +(aside from its declaration). +This warning is enabled by @option{-Wall}. + +To suppress this warning use the @samp{unused} attribute +(@pxref{Variable Attributes}). + +This warning is also enabled by @option{-Wunused}, which is enabled +by @option{-Wall}. + @item -Wunused-function @opindex Wunused-function @opindex Wno-unused-function diff --git a/gcc/objc/ChangeLog b/gcc/objc/ChangeLog index 3063af9f3af..4307001dffc 100644 --- a/gcc/objc/ChangeLog +++ b/gcc/objc/ChangeLog @@ -1,3 +1,10 @@ +2010-04-07 Jakub Jelinek + + PR c/18624 + * 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. + 2010-04-07 Iain Sandoe PR objc/35996 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b7fb0c5b2ec..d4ea2787568 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,14 @@ 2010-04-07 Jakub Jelinek + PR c/18624 + * 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. + * gcc.dg/builtin-choose-expr.c: Avoid set but not used warnings. * gcc.dg/trunc-1.c: Likewise. * gcc.dg/vla-9.c: Likewise. diff --git a/gcc/testsuite/gcc.dg/Wunused-var-1.c b/gcc/testsuite/gcc.dg/Wunused-var-1.c index d60dd70e573..bee34d88031 100644 --- a/gcc/testsuite/gcc.dg/Wunused-var-1.c +++ b/gcc/testsuite/gcc.dg/Wunused-var-1.c @@ -1,26 +1,179 @@ /* { dg-do compile } */ /* { dg-options "-Wunused" } */ -int +void f1 (void) { + int a; /* { dg-warning "set but not used" } */ + int b; + int c; + c = 1; + a = b = c; +} + +void +f2 (int x) +{ + int a; /* { dg-warning "set but not used" } */ + int b; + int c; /* { dg-warning "set but not used" } */ + c = (a = x, b = x); +} + +int +f3 (int x) +{ + int a; + return a = x; +} + +int +f4 (int x) +{ + int a; + a = x; + return a; +} + +void +f5 (int x) +{ + int a[2]; /* { dg-warning "set but not used" } */ + int b; + int *c, d[2]; + c = d; + b = x; + a[b] = 1; + c[b] = 1; +} + +int +f6 (int x) +{ + int a[2]; + int b; + b = x; + a[b] = 1; + return a[b]; +} + +void +f7 (int x, int *p) +{ + int *a[2]; + a[x] = p; + a[x][x] = x; +} + +struct S { int i; }; + +void +f8 (void) +{ + struct S s; /* { dg-warning "set but not used" } */ + s.i = 6; +} + +int +f9 (void) +{ + struct S s; + s.i = 6; + return s.i; +} + +struct S +f10 (void) +{ + struct S s; + s.i = 6; + return s; +} + +extern int foo11 (int *); + +void +f11 (void) +{ + int a[2]; + foo11 (a); +} + +void +f12 (void) +{ int a; - int foo (void) - { - return a; - } a = 1; - return foo (); + a; /* { dg-warning "statement with no effect" } */ } void -f2 (void) +f13 (void (*x) (void)) +{ + void (*a) (void); + a = x; + a (); +} + +void +f14 (void (*x) (void)) +{ + void (*a) (void); /* { dg-warning "set but not used" } */ + a = x; +} + +extern void foo15 (int *); + +void +f15 (void) +{ + int a[10]; + int *b = a + 2; + foo15 (b); +} + +extern void foo16 (int **); + +void +f16 (void) +{ + int a[10]; + int *b[] = { a, a + 2 }; + foo16 (b); +} + +void +f17 (int x) +{ + long a; /* { dg-warning "set but not used" } */ + int b; + a = b = x; +} + +void +f18 (int x) { int a; /* { dg-warning "set but not used" } */ - void foo (void) - { - a = 2; - } - a = 1; - foo (); + int b; + a = (char) (b = x); +} + +int +f19 (int x, int y, int z) +{ + int a; + int b; + a = x; + b = y; + return z ? a : b; +} + +int * +f20 (int x) +{ + static int a[] = { 3, 4, 5, 6 }; + static int b[] = { 4, 5, 6, 7 }; + static int c[] = { 5, 6, 7, 8 }; /* { dg-warning "set but not used" } */ + c[1] = 1; + return x ? a : b; } diff --git a/gcc/testsuite/gcc.dg/Wunused-var-2.c b/gcc/testsuite/gcc.dg/Wunused-var-2.c index b5a22f86545..281825900b5 100644 --- a/gcc/testsuite/gcc.dg/Wunused-var-2.c +++ b/gcc/testsuite/gcc.dg/Wunused-var-2.c @@ -4,16 +4,17 @@ int f1 (void) { - int a; - int b; - int c; - int d; - int e; - a = 1; - b = 2; - c = 3; - d = 4; - e = 5; - return sizeof (a) + ((__typeof (b)) 1) + __alignof__ (c) - + __builtin_choose_expr (1, d, e); + int c = ({ + int a; + a = 1; + a; }); + return c; +} + +void +f2 (void) +{ + int f; + f = 0; + __asm__ __volatile__ ("" : "+r" (f)); } diff --git a/gcc/testsuite/gcc.dg/Wunused-var-3.c b/gcc/testsuite/gcc.dg/Wunused-var-3.c new file mode 100644 index 00000000000..9b1fce77ad3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunused-var-3.c @@ -0,0 +1,37 @@ +/* { dg-do compile } */ +/* { dg-options "-Wunused" } */ + +void +f1 (void) +{ + _Complex int a; /* { dg-warning "set but not used" } */ + _Complex double b; /* { dg-warning "set but not used" } */ + __real__ a = 1; + __imag__ a = 2; + __real__ b = 3.0; + __imag__ b = 4.0; +} + +int +f2 (void) +{ + _Complex int a; + _Complex double b; + __real__ a = 1; + __imag__ a = 2; + __real__ b = 3.0; + __imag__ b = 4.0; + return __real__ a + __imag__ b; +} + +_Complex double +f3 (void) +{ + _Complex int a; + _Complex double b; + __real__ a = 1; + __imag__ a = 2; + __real__ b = 3.0; + __imag__ b = 4.0; + return a + b; +} diff --git a/gcc/testsuite/gcc.dg/Wunused-var-4.c b/gcc/testsuite/gcc.dg/Wunused-var-4.c new file mode 100644 index 00000000000..d60dd70e573 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunused-var-4.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-Wunused" } */ + +int +f1 (void) +{ + int a; + int foo (void) + { + return a; + } + a = 1; + return foo (); +} + +void +f2 (void) +{ + int a; /* { dg-warning "set but not used" } */ + void foo (void) + { + a = 2; + } + a = 1; + foo (); +} diff --git a/gcc/testsuite/gcc.dg/Wunused-var-5.c b/gcc/testsuite/gcc.dg/Wunused-var-5.c new file mode 100644 index 00000000000..747f58d04c0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunused-var-5.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-Wunused" } */ + +void +f1 (void) +{ + extern int extvari; + extvari = 1; +} + +int extvarj; + +void +f2 (void) +{ + extern int extvarj; + extvarj = 1; +} + +static int extvark; + +void +f3 (void) +{ + extern int extvark; + extvark = 1; +} + +int +f4 (void) +{ + return extvark; +} diff --git a/gcc/testsuite/gcc.dg/Wunused-var-6.c b/gcc/testsuite/gcc.dg/Wunused-var-6.c new file mode 100644 index 00000000000..b5a22f86545 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunused-var-6.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-Wunused" } */ + +int +f1 (void) +{ + int a; + int b; + int c; + int d; + int e; + a = 1; + b = 2; + c = 3; + d = 4; + e = 5; + return sizeof (a) + ((__typeof (b)) 1) + __alignof__ (c) + + __builtin_choose_expr (1, d, e); +} -- 2.11.0