This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[RFA] -Wunused -> -Wunused{,-{function,label,parameter,variable,value}}]
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Subject: [RFA] -Wunused -> -Wunused{,-{function,label,parameter,variable,value}}]
- From: Andrew Cagney <ac131313 at cygnus dot com>
- Date: Tue, 21 Mar 2000 16:47:28 +1100
- CC: Andrew Cagney <ac131313 at cygnus dot com>
- Organization: Cygnus Solutions
Hello,
I'd like to put the attatched patch forward for approval.
Andrew
The attatched patch replaces the simple -Wunused option with:
-Wunused
-Wunused-function
-Wunused-label
-Wunused-parameter
-Wunused-variable
-Wunused-value
-Wunused and -Wall enabling all of the above. The most common use for
this is likely to be:
-Wall -Wno-unused-parameter
The implementation replaces the variable ``warn_unused'' with the array
``warn_unused[enum unused_type]''.
Index: gcc/c-decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-decl.c,v
retrieving revision 1.100
diff -p -r1.100 c-decl.c
*** c-decl.c 2000/02/29 23:33:48 1.100
--- c-decl.c 2000/03/05 04:27:18
*************** c_decode_option (argc, argv)
*** 744,749 ****
--- 744,750 ----
warn_unknown_pragmas = 0;
else if (!strcmp (p, "-Wall"))
{
+ int i;
/* We save the value of warn_uninitialized, since if they put
-Wuninitialized on the command line, we need to generate a
warning about not using it without also specifying -O. */
*************** c_decode_option (argc, argv)
*** 752,758 ****
warn_implicit_int = 1;
mesg_implicit_function_declaration = 1;
warn_return_type = 1;
! warn_unused = 1;
warn_switch = 1;
warn_format = 1;
warn_char_subscripts = 1;
--- 753,760 ----
warn_implicit_int = 1;
mesg_implicit_function_declaration = 1;
warn_return_type = 1;
! for (i = 0; i < NR_UNUSED; i++)
! warn_unused[i] = 1;
warn_switch = 1;
warn_format = 1;
warn_char_subscripts = 1;
*************** poplevel (keep, reverse, functionbody)
*** 1119,1125 ****
define_label (input_filename, lineno,
DECL_NAME (label));
}
! else if (warn_unused && !TREE_USED (label))
warning_with_decl (label, "label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
--- 1121,1127 ----
define_label (input_filename, lineno,
DECL_NAME (label));
}
! else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
warning_with_decl (label, "label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
*************** pop_label_level ()
*** 1280,1286 ****
define_label (input_filename, lineno,
DECL_NAME (TREE_VALUE (link)));
}
! else if (warn_unused && !TREE_USED (TREE_VALUE (link)))
warning_with_decl (TREE_VALUE (link),
"label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
--- 1282,1288 ----
define_label (input_filename, lineno,
DECL_NAME (TREE_VALUE (link)));
}
! else if (warn_unused[UNUSED_LABEL] && !TREE_USED (TREE_VALUE (link)))
warning_with_decl (TREE_VALUE (link),
"label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
Index: gcc/c-typeck.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-typeck.c,v
retrieving revision 1.54
diff -p -r1.54 c-typeck.c
*** c-typeck.c 2000/03/02 18:29:55 1.54
--- c-typeck.c 2000/03/05 04:27:40
*************** internal_build_compound_expr (list, firs
*** 3551,3557 ****
/* The left-hand operand of a comma expression is like an expression
statement: with -W or -Wunused, we should warn if it doesn't have
any side-effects, unless it was explicitly cast to (void). */
! if ((extra_warnings || warn_unused)
&& ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
&& TREE_TYPE (TREE_VALUE (list)) == void_type_node))
warning ("left-hand operand of comma expression has no effect");
--- 3551,3557 ----
/* The left-hand operand of a comma expression is like an expression
statement: with -W or -Wunused, we should warn if it doesn't have
any side-effects, unless it was explicitly cast to (void). */
! if ((extra_warnings || warn_unused[UNUSED_VALUE])
&& ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
&& TREE_TYPE (TREE_VALUE (list)) == void_type_node))
warning ("left-hand operand of comma expression has no effect");
*************** internal_build_compound_expr (list, firs
*** 3566,3572 ****
side-effects, but computes a value which is not used. For example, in
`foo() + bar(), baz()' the result of the `+' operator is not used,
so we should issue a warning. */
! else if (warn_unused)
warn_if_unused_value (TREE_VALUE (list));
return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
--- 3566,3572 ----
side-effects, but computes a value which is not used. For example, in
`foo() + bar(), baz()' the result of the `+' operator is not used,
so we should issue a warning. */
! else if (warn_unused[UNUSED_VALUE])
warn_if_unused_value (TREE_VALUE (list));
return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
Index: gcc/flags.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/flags.h,v
retrieving revision 1.40
diff -p -r1.40 flags.h
*** flags.h 2000/02/20 01:10:48 1.40
--- flags.h 2000/03/05 04:27:56
*************** extern int inhibit_warnings;
*** 71,79 ****
extern int extra_warnings;
! /* Nonzero to warn about unused local variables. */
! extern int warn_unused;
/* Nonzero to warn about code which is never reached. */
--- 71,90 ----
extern int extra_warnings;
! /* Nonzero to warn about unused variables, functions et.al. */
! /* (Also update W_options, gcc.1, invoke.texi) */
! enum unused_type
! {
! UNUSED_FUNCTION,
! UNUSED_LABEL,
! UNUSED_PARAMETER,
! UNUSED_VARIABLE,
! UNUSED_VALUE,
! NR_UNUSED
! };
!
! extern int warn_unused[NR_UNUSED];
/* Nonzero to warn about code which is never reached. */
Index: gcc/function.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/function.c,v
retrieving revision 1.169
diff -p -r1.169 function.c
*** function.c 2000/03/05 03:43:58 1.169
--- function.c 2000/03/05 04:28:11
*************** expand_function_end (filename, line, end
*** 6313,6319 ****
}
/* Warn about unused parms if extra warnings were specified. */
! if (warn_unused && extra_warnings)
{
tree decl;
--- 6313,6319 ----
}
/* Warn about unused parms if extra warnings were specified. */
! if (warn_unused[UNUSED_PARAMETER] && extra_warnings)
{
tree decl;
Index: gcc/gcc.1
===================================================================
RCS file: /cvs/gcc/egcs/gcc/gcc.1,v
retrieving revision 1.13
diff -p -r1.13 gcc.1
*** gcc.1 1999/11/02 10:23:46 1.13
--- gcc.1 2000/03/05 04:28:20
*************** in the following sections.
*** 211,216 ****
--- 211,221 ----
\-Wtrigraphs
\-Wuninitialized
\-Wunused
+ \-Wunused-function
+ \-Wunused-label
+ \-Wunused-parameter
+ \-Wunused-variable
+ \-Wunused-value
\-Wwrite\-strings
.TP
.B Debugging Options
*************** return-value in a function whose return-
*** 1750,1758 ****
\&.
.TP
.B \-Wunused
! Warn whenever a local variable is unused aside from its declaration,
! whenever a function is declared static but never defined, and whenever
! a statement computes a result that is explicitly not used.
.TP
.B \-Wswitch
Warn whenever a \c
--- 1755,1764 ----
\&.
.TP
.B \-Wunused
! Warn whenever a local variable or parameter is unused aside from its
! declaration, whenever a function is declared static but never defined,
! and whenever a statement computes a result that is explicitly not
! used.
.TP
.B \-Wswitch
Warn whenever a \c
Index: gcc/invoke.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/invoke.texi,v
retrieving revision 1.177
diff -p -r1.177 invoke.texi
*** invoke.texi 2000/02/29 01:42:52 1.177
--- invoke.texi 2000/03/05 04:28:46
*************** in the following sections.
*** 132,138 ****
-Wparentheses -Wpointer-arith -Wredundant-decls
-Wreturn-type -Wshadow -Wsign-compare -Wswitch
-Wtrigraphs -Wundef -Wuninitialized -Wunknown-pragmas -Wunreachable-code
! -Wunused -Wwrite-strings
@end smallexample
@item C-only Warning Options
--- 132,139 ----
-Wparentheses -Wpointer-arith -Wredundant-decls
-Wreturn-type -Wshadow -Wsign-compare -Wswitch
-Wtrigraphs -Wundef -Wuninitialized -Wunknown-pragmas -Wunreachable-code
! -Wunused -Wunused-function -Wunused-label -Wunused-parameter
! -Wunused-variable -Wunused-value -Wwrite-strings
@end smallexample
@item C-only Warning Options
*************** provoke warnings when this option is use
*** 1538,1547 ****
Warn if any trigraphs are encountered (assuming they are enabled).
@item -Wunused
! Warn whenever a variable is unused aside from its declaration,
! whenever a function is declared static but never defined, whenever a
! label is declared but not used, and whenever a statement computes a
! result that is explicitly not used.
In order to get a warning about an unused function parameter, you must
specify both @samp{-W} and @samp{-Wunused}.
--- 1539,1549 ----
Warn if any trigraphs are encountered (assuming they are enabled).
@item -Wunused
!
! Warn whenever a variable or parameter is unused aside from its
! declaration, whenever a function is declared static but never defined,
! whenever a label is declared but not used, and whenever a statement
! computes a result that is explicitly not used.
In order to get a warning about an unused function parameter, you must
specify both @samp{-W} and @samp{-Wunused}.
Index: gcc/stmt.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/stmt.c,v
retrieving revision 1.126
diff -p -r1.126 stmt.c
*** stmt.c 2000/03/05 01:57:10 1.126
--- stmt.c 2000/03/05 04:29:00
*************** expand_expr_stmt (exp)
*** 1848,1859 ****
except inside a ({...}) where they may be useful. */
if (expr_stmts_for_value == 0 && exp != error_mark_node)
{
! if (! TREE_SIDE_EFFECTS (exp) && (extra_warnings || warn_unused)
&& !(TREE_CODE (exp) == CONVERT_EXPR
&& TREE_TYPE (exp) == void_type_node))
warning_with_file_and_line (emit_filename, emit_lineno,
"statement with no effect");
! else if (warn_unused)
warn_if_unused_value (exp);
}
--- 1848,1860 ----
except inside a ({...}) where they may be useful. */
if (expr_stmts_for_value == 0 && exp != error_mark_node)
{
! if (! TREE_SIDE_EFFECTS (exp)
! && (extra_warnings || warn_unused[UNUSED_VALUE])
&& !(TREE_CODE (exp) == CONVERT_EXPR
&& TREE_TYPE (exp) == void_type_node))
warning_with_file_and_line (emit_filename, emit_lineno,
"statement with no effect");
! else if (warn_unused[UNUSED_VALUE])
warn_if_unused_value (exp);
}
*************** warn_about_unused_variables (vars)
*** 3570,3576 ****
{
tree decl;
! if (warn_unused)
for (decl = vars; decl; decl = TREE_CHAIN (decl))
if (TREE_CODE (decl) == VAR_DECL
&& ! TREE_USED (decl)
--- 3571,3577 ----
{
tree decl;
! if (warn_unused[UNUSED_VARIABLE])
for (decl = vars; decl; decl = TREE_CHAIN (decl))
if (TREE_CODE (decl) == VAR_DECL
&& ! TREE_USED (decl)
Index: gcc/toplev.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/toplev.c,v
retrieving revision 1.299
diff -p -r1.299 toplev.c
*** toplev.c 2000/03/04 16:24:03 1.299
--- toplev.c 2000/03/05 04:29:13
*************** int extra_warnings = 0;
*** 1198,1206 ****
int warnings_are_errors = 0;
! /* Nonzero to warn about unused local variables. */
! int warn_unused;
/* Nonzero to warn about code which is never reached. */
--- 1198,1206 ----
int warnings_are_errors = 0;
! /* Nonzero to warn about unused variables, functions et.al. */
! int warn_unused[NR_UNUSED];
/* Nonzero to warn about code which is never reached. */
*************** int warn_padded;
*** 1263,1269 ****
lang_independent_options W_options[] =
{
! {"unused", &warn_unused, 1, "Warn when a variable is unused" },
{"error", &warnings_are_errors, 1, ""},
{"shadow", &warn_shadow, 1, "Warn when one local variable shadows another" },
{"switch", &warn_switch, 1,
--- 1263,1273 ----
lang_independent_options W_options[] =
{
! {"unused-function", &warn_unused[UNUSED_FUNCTION], 1, "Warn when a function is unused" },
! {"unused-label", &warn_unused[UNUSED_LABEL], 1, "Warn when an label is unused" },
! {"unused-parameter", &warn_unused[UNUSED_PARAMETER], 1, "Warn when a function parameter is unused" },
! {"unused-variable", &warn_unused[UNUSED_VARIABLE], 1, "Warn when a variable is unused" },
! {"unused-value", &warn_unused[UNUSED_VALUE], 1, "Warn when an expression value is unused" },
{"error", &warnings_are_errors, 1, ""},
{"shadow", &warn_shadow, 1, "Warn when one local variable shadows another" },
{"switch", &warn_switch, 1,
*************** check_global_declarations (vec, len)
*** 1925,1931 ****
because many programs have static variables
that exist only to get some text into the object file. */
if (TREE_CODE (decl) == FUNCTION_DECL
! && (warn_unused
|| TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
&& DECL_INITIAL (decl) == 0
&& DECL_EXTERNAL (decl)
--- 1929,1935 ----
because many programs have static variables
that exist only to get some text into the object file. */
if (TREE_CODE (decl) == FUNCTION_DECL
! && (warn_unused[UNUSED_FUNCTION]
|| TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
&& DECL_INITIAL (decl) == 0
&& DECL_EXTERNAL (decl)
*************** check_global_declarations (vec, len)
*** 1946,1952 ****
/* Warn about static fns or vars defined but not used,
but not about inline functions or static consts
since defining those in header files is normal practice. */
! if (warn_unused
&& ((TREE_CODE (decl) == FUNCTION_DECL && ! DECL_INLINE (decl))
|| (TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
&& ! DECL_IN_SYSTEM_HEADER (decl)
--- 1950,1956 ----
/* Warn about static fns or vars defined but not used,
but not about inline functions or static consts
since defining those in header files is normal practice. */
! if (warn_unused[UNUSED_FUNCTION]
&& ((TREE_CODE (decl) == FUNCTION_DECL && ! DECL_INLINE (decl))
|| (TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl)))
&& ! DECL_IN_SYSTEM_HEADER (decl)
*************** display_help ()
*** 3840,3845 ****
--- 3844,3850 ----
W_options[i].string, description);
}
+ printf (" -Wunused Warn when a funciton, parameter, variable or expression value is unused\n");
printf (" -Wid-clash-<num> Warn if 2 identifiers have the same first <num> chars\n");
printf (" -Wlarger-than-<number> Warn if an object is larger than <number> bytes\n");
printf (" -p Enable function profiling\n");
*************** decode_W_option (arg)
*** 4222,4228 ****
}
}
! if (!strncmp (arg, "id-clash-", 9))
{
id_clash_len = read_integral_parameter (arg + 9, arg - 2, -1);
--- 4227,4245 ----
}
}
! if (!strcmp (arg, "unused"))
! {
! int i;
! for (i = 0; i < NR_UNUSED; i++)
! warn_unused[i] = 1;
! }
! if (!strcmp (arg, "no-unused"))
! {
! int i;
! for (i = 0; i < NR_UNUSED; i++)
! warn_unused[i] = 0;
! }
! else if (!strncmp (arg, "id-clash-", 9))
{
id_clash_len = read_integral_parameter (arg + 9, arg - 2, -1);
Index: gcc/ch/decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/ch/decl.c,v
retrieving revision 1.21
diff -p -r1.21 decl.c
*** decl.c 2000/02/29 23:33:49 1.21
--- decl.c 2000/03/05 04:30:01
*************** c_decode_option (argc, argv)
*** 793,798 ****
--- 793,799 ----
warn_missing_braces = 0;
else if (!strcmp (p, "-Wall"))
{
+ int i;
extra_warnings = 1;
/* We save the value of warn_uninitialized, since if they put
-Wuninitialized on the command line, we need to generate a
*************** c_decode_option (argc, argv)
*** 801,807 ****
warn_uninitialized = 2;
warn_implicit = 1;
warn_return_type = 1;
! warn_unused = 1;
warn_char_subscripts = 1;
warn_parentheses = 1;
warn_missing_braces = 1;
--- 802,809 ----
warn_uninitialized = 2;
warn_implicit = 1;
warn_return_type = 1;
! for (i = 0; i < NR_UNUSED; i++)
! warn_unused[i] = 1;
warn_char_subscripts = 1;
warn_parentheses = 1;
warn_missing_braces = 1;
*************** poplevel (keep, reverse, functionbody)
*** 2975,2981 ****
define_label (input_filename, lineno,
DECL_NAME (label));
}
! else if (warn_unused && !TREE_USED (label))
warning_with_decl (label, "label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
--- 2977,2983 ----
define_label (input_filename, lineno,
DECL_NAME (label));
}
! else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
warning_with_decl (label, "label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
Index: gcc/cp/decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl.c,v
retrieving revision 1.558
diff -p -r1.558 decl.c
*** decl.c 2000/03/04 00:45:23 1.558
--- decl.c 2000/03/05 04:31:33
*************** pop_label (link)
*** 1078,1084 ****
/* Avoid crashing later. */
define_label (input_filename, 1, DECL_NAME (label));
}
! else if (warn_unused && !TREE_USED (label))
cp_warning_at ("label `%D' defined but not used", label);
}
--- 1078,1084 ----
/* Avoid crashing later. */
define_label (input_filename, 1, DECL_NAME (label));
}
! else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
cp_warning_at ("label `%D' defined but not used", label);
}
Index: gcc/cp/decl2.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl2.c,v
retrieving revision 1.315
diff -p -r1.315 decl2.c
*** decl2.c 2000/03/04 00:45:23 1.315
--- decl2.c 2000/03/05 04:31:45
*************** lang_decode_option (argc, argv)
*** 754,761 ****
; /* cpp handles this one. */
else if (!strcmp (p, "all"))
{
warn_return_type = setting;
! warn_unused = setting;
warn_implicit = setting;
warn_switch = setting;
warn_format = setting;
--- 754,763 ----
; /* cpp handles this one. */
else if (!strcmp (p, "all"))
{
+ int i;
warn_return_type = setting;
! for (i = 0; i < NR_UNUSED; i++)
! warn_unused[i] = setting;
warn_implicit = setting;
warn_switch = setting;
warn_format = setting;
Index: gcc/cp/typeck.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/typeck.c,v
retrieving revision 1.256
diff -p -r1.256 typeck.c
*** typeck.c 2000/03/03 02:27:15 1.256
--- typeck.c 2000/03/05 04:32:01
*************** build_x_compound_expr (list)
*** 5086,5098 ****
/* the left-hand operand of a comma expression is like an expression
statement: we should warn if it doesn't have any side-effects,
unless it was explicitly cast to (void). */
! if ((extra_warnings || warn_unused)
&& !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
&& TREE_TYPE (TREE_VALUE(list)) == void_type_node))
warning("left-hand operand of comma expression has no effect");
}
#if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
! else if (warn_unused)
warn_if_unused_value (TREE_VALUE(list));
#endif
--- 5086,5098 ----
/* the left-hand operand of a comma expression is like an expression
statement: we should warn if it doesn't have any side-effects,
unless it was explicitly cast to (void). */
! if ((extra_warnings || warn_unused[UNUSED_VALUE])
&& !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
&& TREE_TYPE (TREE_VALUE(list)) == void_type_node))
warning("left-hand operand of comma expression has no effect");
}
#if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
! else if (warn_unused[UNUSED_VALUE])
warn_if_unused_value (TREE_VALUE(list));
#endif
Index: gcc/f/top.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/f/top.c,v
retrieving revision 1.20
diff -p -r1.20 top.c
*** top.c 2000/02/26 20:02:01 1.20
--- top.c 2000/03/05 04:32:07
*************** ffe_decode_option (argc, argv)
*** 505,516 ****
ffe_set_is_warn_surprising (FALSE);
else if (!strcmp (&opt[2], "all"))
{
/* We save the value of warn_uninitialized, since if they put
-Wuninitialized on the command line, we need to generate a
warning about not using it without also specifying -O. */
if (warn_uninitialized != 1)
warn_uninitialized = 2;
! warn_unused = 1;
}
else
return 0;
--- 505,518 ----
ffe_set_is_warn_surprising (FALSE);
else if (!strcmp (&opt[2], "all"))
{
+ int i;
/* We save the value of warn_uninitialized, since if they put
-Wuninitialized on the command line, we need to generate a
warning about not using it without also specifying -O. */
if (warn_uninitialized != 1)
warn_uninitialized = 2;
! for (i = 0; i < NR_UNUSED; i++)
! warn_unused[i] = 1;
}
else
return 0;
Index: gcc/java/decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/decl.c,v
retrieving revision 1.51
diff -p -r1.51 decl.c
*** decl.c 2000/03/04 18:27:48 1.51
--- decl.c 2000/03/05 04:32:36
*************** poplevel (keep, reverse, functionbody)
*** 1332,1338 ****
define_label (input_filename, lineno,
DECL_NAME (label));
}
! else if (warn_unused && !TREE_USED (label))
warning_with_decl (label, "label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
--- 1332,1338 ----
define_label (input_filename, lineno,
DECL_NAME (label));
}
! else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
warning_with_decl (label, "label `%s' defined but not used");
IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;