* 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
2010-04-07 Jakub Jelinek <jakub@redhat.com>
+ 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
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);
}
-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
-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
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
+2010-04-07 Jakub Jelinek <jakub@redhat.com>
+
+ 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 <iains@gcc.gnu.org>
PR objc/35996
2010-04-07 Jakub Jelinek <jakub@redhat.com>
+ 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.
/* { 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;
}
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));
}
--- /dev/null
+/* { 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;
+}
--- /dev/null
+/* { 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 ();
+}
--- /dev/null
+/* { 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;
+}
--- /dev/null
+/* { 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);
+}