From 078721e1a2e6f361f4c7ed6b9017b6fb0d7211a1 Mon Sep 17 00:00:00 2001 From: Andrew Cagney Date: Wed, 17 May 2000 08:15:29 +0000 Subject: [PATCH] Add options -Wunused-variable, -Wunused-function, -Wunused-label, -Wunused-parameter. Retain existing -Wunused behavour. Document. From-SVN: r33953 --- gcc/ChangeLog | 31 +++++++++++++++++++++++++++ gcc/c-decl.c | 6 +++--- gcc/c-typeck.c | 4 ++-- gcc/ch/ChangeLog | 6 ++++++ gcc/ch/decl.c | 4 ++-- gcc/cp/ChangeLog | 9 ++++++++ gcc/cp/decl.c | 2 +- gcc/cp/decl2.c | 2 +- gcc/cp/typeck.c | 4 ++-- gcc/f/ChangeLog | 5 +++++ gcc/f/top.c | 2 +- gcc/flags.h | 12 +++++++++-- gcc/function.c | 6 +++++- gcc/gcc.1 | 53 ++++++++++++++++++++++++++++++++++++++++++---- gcc/invoke.texi | 43 ++++++++++++++++++++++++++++--------- gcc/java/ChangeLog | 8 ++++++- gcc/java/decl.c | 2 +- gcc/java/lang.c | 2 +- gcc/stmt.c | 7 +++--- gcc/toplev.c | 49 ++++++++++++++++++++++++++++++++++++------ 20 files changed, 215 insertions(+), 42 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 17dd13d330a..425ab785599 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,34 @@ +Wed May 17 17:27:44 2000 Andrew Cagney + + * flags.h (warn_unused_function, warn_unused_label, + warn_unused_parameter, warn_unused_variable, warn_unused_value): + Replace ``warn_unused''. + (set_Wunused): Add declaration. + * toplev.c (set_Wunused): New function. + (warn_unused_function, warn_unused_label, warn_unused_parameter, + warn_unused_variable, warn_unused_value): New variables. + (W_options): Add -Wunused-function, -Wunused-function, + -Wunused-label, -Wunused-parameter, -Wunused-variable and + -Wunused-value. Delete -Wunused. Handled in decode_W_option. + + * toplev.c (decode_W_option): Update -Wunused flags by calling + set_Wunused. + * c-decl.c (c_decode_option): Ditto for -Wall. + + * stmt.c (expand_expr_stmt, expand_expr_stmt, + warn_about_unused_variables): Replace warn_unused with more + explicit warn_unused_value et.al. + * function.c (expand_function_end): Ditto. + * c-typeck.c (internal_build_compound_expr, + internal_build_compound_expr): Ditto. + * c-decl.c (poplevel, pop_label_level): Ditto. + * toplev.c (check_global_declarations): Replace warn_unused with + check for either warn_unused_function or warn_unused_variable. + + * gcc.1, invoke.texi (Warning Options): Document + -Wunused-function, -Wunused-function, -Wunused-label, + -Wunused-parameter, -Wunused-variable and -Wunused-value options. + 2000-05-16 Richard Henderson * config/ia64/crtbegin.asm (__dso_handle): Mark hidden if the diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 301a3227977..040b695f889 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -754,7 +754,7 @@ c_decode_option (argc, argv) warn_implicit_int = 1; mesg_implicit_function_declaration = 1; warn_return_type = 1; - warn_unused = 1; + set_Wunused (1); warn_switch = 1; warn_format = 1; warn_char_subscripts = 1; @@ -1121,7 +1121,7 @@ poplevel (keep, reverse, functionbody) define_label (input_filename, lineno, DECL_NAME (label)); } - else if (warn_unused && !TREE_USED (label)) + else if (warn_unused_label && !TREE_USED (label)) warning_with_decl (label, "label `%s' defined but not used"); IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0; @@ -1282,7 +1282,7 @@ pop_label_level () define_label (input_filename, lineno, DECL_NAME (TREE_VALUE (link))); } - else if (warn_unused && !TREE_USED (TREE_VALUE (link))) + else if (warn_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; diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 25c19e25784..0f01c578d4d 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -3531,7 +3531,7 @@ internal_build_compound_expr (list, first_p) /* 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) + if ((extra_warnings || warn_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"); @@ -3546,7 +3546,7 @@ internal_build_compound_expr (list, first_p) 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) + else if (warn_unused_value) warn_if_unused_value (TREE_VALUE (list)); return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest); diff --git a/gcc/ch/ChangeLog b/gcc/ch/ChangeLog index 45262b340e7..a491bb376eb 100644 --- a/gcc/ch/ChangeLog +++ b/gcc/ch/ChangeLog @@ -1,3 +1,9 @@ +Wed May 17 17:27:44 2000 Andrew Cagney + + * decl.c (c_decode_option): Update -Wall unused flags by + calling set_Wunused. + (poplevel): Replace warn_unused with warn_unused_label. + 2000-05-09 Zack Weinberg * ch-tree.h: Update prototypes. Remove prototypes for diff --git a/gcc/ch/decl.c b/gcc/ch/decl.c index 9d5fd0dcfef..45f2656e567 100644 --- a/gcc/ch/decl.c +++ b/gcc/ch/decl.c @@ -799,7 +799,7 @@ c_decode_option (argc, argv) warn_uninitialized = 2; warn_implicit = 1; warn_return_type = 1; - warn_unused = 1; + set_Wunused (1); warn_char_subscripts = 1; warn_parentheses = 1; warn_missing_braces = 1; @@ -2973,7 +2973,7 @@ poplevel (keep, reverse, functionbody) define_label (input_filename, lineno, DECL_NAME (label)); } - else if (warn_unused && !TREE_USED (label)) + else if (warn_unused_label && !TREE_USED (label)) warning_with_decl (label, "label `%s' defined but not used"); IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0; diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 1400181faa1..d6b8431782d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +Wed May 17 17:27:44 2000 Andrew Cagney + + * decl.c (pop_label): Replace warn_unused with warn_unused_label. + * typeck.c (build_x_compound_expr): Replace warn_unused with + warn_unused_value. + + * decl2.c (lang_decode_option): Update -Wall unused flags by + calling set_Wunused. + 2000-05-16 Mark Mitchell * cp-treeh (BINFO_NEW_VTABLE_MARKED): Update documentation. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index e32f2435612..6f09a7aee73 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -1194,7 +1194,7 @@ pop_label (link) /* Avoid crashing later. */ define_label (input_filename, 1, DECL_NAME (label)); } - else if (warn_unused && !TREE_USED (label)) + else if (warn_unused_label && !TREE_USED (label)) cp_warning_at ("label `%D' defined but not used", label); } diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 39faa5d7e8d..fb399dbf6e8 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -773,7 +773,7 @@ lang_decode_option (argc, argv) else if (!strcmp (p, "all")) { warn_return_type = setting; - warn_unused = setting; + set_Wunused (setting); warn_implicit = setting; warn_switch = setting; warn_format = setting; diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 671a66659ee..45c144530c5 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5058,13 +5058,13 @@ build_x_compound_expr (list) /* 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) + if ((extra_warnings || warn_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) + else if (warn_unused_value) warn_if_unused_value (TREE_VALUE(list)); #endif diff --git a/gcc/f/ChangeLog b/gcc/f/ChangeLog index c33a7904e3a..9eb3ccee06b 100644 --- a/gcc/f/ChangeLog +++ b/gcc/f/ChangeLog @@ -1,3 +1,8 @@ +Wed May 17 17:27:44 2000 Andrew Cagney + + * top.c (ffe_decode_option): Update -Wall unused flags by calling + set_Wunused. + 2000-05-09 Zack Weinberg * com.c (ffecom_subscript_check_): Constify array_name diff --git a/gcc/f/top.c b/gcc/f/top.c index c48e9f1b7d4..91db6d01803 100644 --- a/gcc/f/top.c +++ b/gcc/f/top.c @@ -510,7 +510,7 @@ ffe_decode_option (argc, argv) warning about not using it without also specifying -O. */ if (warn_uninitialized != 1) warn_uninitialized = 2; - warn_unused = 1; + set_Wunused (1); } else return 0; diff --git a/gcc/flags.h b/gcc/flags.h index 9ca120c2020..3c543bc5bcd 100644 --- a/gcc/flags.h +++ b/gcc/flags.h @@ -71,9 +71,17 @@ extern int inhibit_warnings; extern int extra_warnings; -/* Nonzero to warn about unused local variables. */ +/* Nonzero to warn about unused variables, functions et.al. Use + set_Wunused() to update the -Wunused-* flags that correspond to the + -Wunused option. */ -extern int warn_unused; +extern void set_Wunused PARAMS ((int setting)); + +extern int warn_unused_function; +extern int warn_unused_label; +extern int warn_unused_parameter; +extern int warn_unused_variable; +extern int warn_unused_value; /* Nonzero to warn about code which is never reached. */ diff --git a/gcc/function.c b/gcc/function.c index 9fd40cc2379..0f6785b9bdc 100644 --- a/gcc/function.c +++ b/gcc/function.c @@ -6492,7 +6492,11 @@ expand_function_end (filename, line, end_bindings) } /* Warn about unused parms if extra warnings were specified. */ - if (warn_unused && extra_warnings) + /* Either ``-W -Wunused'' or ``-Wunused-parameter'' enables this + warning. WARN_UNUSED_PARAMETER is negative when set by + -Wunused. */ + if (warn_unused_parameter > 0 + || (warn_unused_parameter < 0 && extra_warnings)) { tree decl; diff --git a/gcc/gcc.1 b/gcc/gcc.1 index d9543fa6edc..e1907510e39 100644 --- a/gcc/gcc.1 +++ b/gcc/gcc.1 @@ -20,7 +20,7 @@ .if n .sp .if t .sp 0.4 .. -.Id $Id: gcc.1,v 1.13 1999/11/02 10:23:46 law Exp $ +.Id $Id: gcc.1,v 1.14 2000/04/11 06:01:52 loewis Exp $ .TH GCC 1 "\*(Dt" "GNU Tools" "GNU Tools" .SH NAME gcc, g++ \- GNU project C and C++ Compiler (gcc-2.96) @@ -206,6 +206,11 @@ in the following sections. \-Wtrigraphs \-Wuninitialized \-Wunused +\-Wunused-function +\-Wunused-label +\-Wunused-parameter +\-Wunused-variable +\-Wunused-value \-Wwrite\-strings .TP .B Debugging Options @@ -1694,10 +1699,50 @@ return-value in a function whose return-type is not \c .B void\c \&. .TP +.B \-Wunused\-function +Warn whenever a static function is declared but not defined or a +non\-inline static function is unused. +.TP +.B \-Wunused\-label +Warn whenever a label is declared but not used. + +To suppress this warning use the +.B unused +attribute. +.TP +.B \-Wunused\-parameter +Warn whenever a function parameter is unused aside from its declaration. + +To suppress this warning use the +.B unused +attribute. +.TP +.B \-Wunused\-variable +Warn whenever a local variable or non\-constant static variable +is unused aside from its declaration + +To suppress this warning use the +.B unused +attribute. +.TP +.B \-Wunused\-value +Warn whenever a statement computes a result that is explicitly not used. + +To suppress this warning cast the expression to +.B void\c +\&. +.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. +All all the above `\|\c +.B \-Wunused\c +\&\|' options combined. + +In order to get a warning about an unused function parameter, you must +either specify `\|\c +.B \-W \-Wunused\c +\&\|' or separatly specify `\|\c +.B \-Wunused\-parameter\c +\&\|'. .TP .B \-Wswitch Warn whenever a \c diff --git a/gcc/invoke.texi b/gcc/invoke.texi index c008ed037df..4261cc6f22e 100644 --- a/gcc/invoke.texi +++ b/gcc/invoke.texi @@ -132,7 +132,8 @@ in the following sections. -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wundef -Wuninitialized -Wunknown-pragmas -Wunreachable-code --Wunused -Wwrite-strings +-Wunused -Wunused-function -Wunused-label -Wunused-parameter +-Wunused-variable -Wunused-value -Wwrite-strings @end smallexample @item C-only Warning Options @@ -1538,18 +1539,40 @@ provoke warnings when this option is used. @item -Wtrigraphs Warn if any trigraphs are encountered (assuming they are enabled). +@item -Wunused-function +Warn whenever a static function is declared but not defined or a +non\-inline static function is unused. + +@item -Wunused-label +Warn whenever a label is declared but not used. + +To suppress this warning use the @samp{unused} attribute +(@pxref{Variable Attributes}). + +@item -Wunused-parameter +Warn whenever a function parameter is unused aside from its declaration. + +To suppress this warning use the @samp{unused} attribute +(@pxref{Variable Attributes}). + +@item -Wunused-variable +Warn whenever a local variable or non-constant static variable is unused +aside from its declaration + +To suppress this warning use the @samp{unused} attribute +(@pxref{Variable Attributes}). + +@item -Wunused-value +Warn whenever a statement computes a result that is explicitly not used. + +To suppress this warning cast the expression to @samp{void}. + @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. +All all the above @samp{-Wunused} options combined. In order to get a warning about an unused function parameter, you must -specify both @samp{-W} and @samp{-Wunused}. - -To suppress this warning for an expression, simply cast it to void. For -unused variables, parameters and labels, use the @samp{unused} attribute -(@pxref{Variable Attributes}). +either specify @samp{-W -Wunused} or separatly specify +@samp{-Wunused-parameter}. @item -Wuninitialized Warn if an automatic variable is used without first being initialized or diff --git a/gcc/java/ChangeLog b/gcc/java/ChangeLog index 2f7aff924a0..f30afabbb4d 100644 --- a/gcc/java/ChangeLog +++ b/gcc/java/ChangeLog @@ -1,4 +1,10 @@ -2000-05-09 Zack Weinberg +Wed May 17 17:27:44 2000 Andrew Cagney + + * lang.c (lang_decode_option): Update -Wunused flags by calling + set_Wunused. + * decl.c (poplevel): Replace warn_unused with warn_unused_label. + +2000-04-19 Tom Tromey <tromey@cygnus.com> * check_init.c (check_init): Constify local char *. * class.c (push_class): Constify local char *. diff --git a/gcc/java/decl.c b/gcc/java/decl.c index 5757b2c8f32..a79bb911252 100644 --- a/gcc/java/decl.c +++ b/gcc/java/decl.c @@ -1387,7 +1387,7 @@ poplevel (keep, reverse, functionbody) define_label (input_filename, lineno, DECL_NAME (label)); } - else if (warn_unused && !TREE_USED (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; diff --git a/gcc/java/lang.c b/gcc/java/lang.c index 4bef553a95a..2a1d142ed38 100644 --- a/gcc/java/lang.c +++ b/gcc/java/lang.c @@ -258,7 +258,7 @@ lang_decode_option (argc, argv) flag_redundant = 1; /* When -Wall given, enable -Wunused. We do this because the C compiler does it, and people expect it. */ - warn_unused = 1; + set_Wunused (1); return 1; } diff --git a/gcc/stmt.c b/gcc/stmt.c index 0626e2c3e34..a3b72ea3259 100644 --- a/gcc/stmt.c +++ b/gcc/stmt.c @@ -1857,12 +1857,13 @@ expand_expr_stmt (exp) 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) + if (! TREE_SIDE_EFFECTS (exp) + && (extra_warnings || warn_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) + else if (warn_unused_value) warn_if_unused_value (exp); } @@ -3575,7 +3576,7 @@ warn_about_unused_variables (vars) { tree decl; - if (warn_unused) + if (warn_unused_variable) for (decl = vars; decl; decl = TREE_CHAIN (decl)) if (TREE_CODE (decl) == VAR_DECL && ! TREE_USED (decl) diff --git a/gcc/toplev.c b/gcc/toplev.c index bcb81f34bdf..470a6ab6314 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -1289,9 +1289,30 @@ int extra_warnings = 0; int warnings_are_errors = 0; -/* Nonzero to warn about unused local variables. */ +/* Nonzero to warn about unused variables, functions et.al. */ -int warn_unused; +int warn_unused_function; +int warn_unused_label; +int warn_unused_parameter; +int warn_unused_variable; +int warn_unused_value; + +void +set_Wunused (setting) + int setting; +{ + warn_unused_function = setting; + warn_unused_label = setting; + /* Unused function parameter warnings are reported when either ``-W + -Wunused'' or ``-Wunused-parameter'' is specified. Differentiate + -Wunused by setting WARN_UNUSED_PARAMETER to -1 */ + if (!setting) + warn_unused_parameter = 0; + else if (!warn_unused_parameter) + warn_unused_parameter = -1; + warn_unused_variable = setting; + warn_unused_value = setting; +} /* Nonzero to warn about code which is never reached. */ @@ -1354,7 +1375,11 @@ int warn_padded; lang_independent_options W_options[] = { - {"unused", &warn_unused, 1, "Warn when a variable is unused" }, + {"unused-function", &warn_unused_function, 1, "Warn when a function is unused" }, + {"unused-label", &warn_unused_label, 1, "Warn when a label is unused" }, + {"unused-parameter", &warn_unused_parameter, 1, "Warn when a function parameter is unused" }, + {"unused-variable", &warn_unused_variable, 1, "Warn when a variable is unused" }, + {"unused-value", &warn_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, @@ -1905,7 +1930,7 @@ check_global_declarations (vec, len) 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 + && (warn_unused_function || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))) && DECL_INITIAL (decl) == 0 && DECL_EXTERNAL (decl) @@ -1926,9 +1951,10 @@ check_global_declarations (vec, len) /* 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))) + if (((warn_unused_function + && TREE_CODE (decl) == FUNCTION_DECL && ! DECL_INLINE (decl)) + || (warn_unused_variable + && TREE_CODE (decl) == VAR_DECL && ! TREE_READONLY (decl))) && ! DECL_IN_SYSTEM_HEADER (decl) && ! DECL_EXTERNAL (decl) && ! TREE_PUBLIC (decl) @@ -3685,6 +3711,7 @@ display_help () W_options[i].string, description); } + printf (" -Wunused Enable unused warnings\n"); printf (" -Wid-clash- Warn if 2 identifiers have the same first chars\n"); printf (" -Wlarger-than- Warn if an object is larger than bytes\n"); printf (" -p Enable function profiling\n"); @@ -3995,6 +4022,14 @@ decode_W_option (arg) if (larger_than_size != -1) warn_larger_than = 1; } + else if (!strcmp (arg, "unused")) + { + set_Wunused (1); + } + else if (!strcmp (arg, "no-unused")) + { + set_Wunused (0); + } else return 0;