This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH (head): -Wunused-static-variable


In reference to the message thread starting at
http://gcc.gnu.org/ml/gcc/2003-04/msg01494.html

how about the following patch. This is against the head, but I can
easily redo this on the 3.3 branch if that is desired and someone
can take care of the merging back to the head.

2003-05-01  Kean Johnston  <jkj@sco.com>

	* c-opts.c (COMMAND_LINE_OPTIONS): Add -Wunused-static-variable
	for finer grained warning control.
	* doc/invoke.texi: Document it.
	* flags.h: Declare warn_unused_static_variable.
	* toplev.c (W_options): Add it.
	(check_global_declarations): Use it.

Index: c-opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-opts.c,v
retrieving revision 1.41
diff -u -r1.41 c-opts.c
--- c-opts.c	1 May 2003 16:13:27 -0000	1.41
+++ c-opts.c	2 May 2003 02:33:01 -0000
@@ -233,6 +233,7 @@
   OPT("Wundef",			CL_ALL,   OPT_Wundef)
\
   OPT("Wunknown-pragmas",	CL_ALL,   OPT_Wunknown_pragmas)
\
   OPT("Wunused-macros",		CL_ALL,   OPT_Wunused_macros)
\
+  OPT("Wunused-static-variable",CL_ALL,   OPT_Wunused_static_variable)
\
   OPT("Wwrite-strings",		CL_ALL,   OPT_Wwrite_strings)
\
   OPT("ansi",			CL_ALL,   OPT_ansi)
\
   OPT("d",                      CL_ALL | CL_JOINED, OPT_d)
\
@@ -1082,6 +1083,10 @@
 
     case OPT_Wunused_macros:
       warn_unused_macros = on;
+      break;
+
+    case OPT_Wunused_static_variable:
+      warn_unused_static_variable = on;
       break;
 
     case OPT_Wwrite_strings:
Index: flags.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/flags.h,v
retrieving revision 1.106
diff -u -r1.106 flags.h
--- flags.h	26 Apr 2003 15:19:05 -0000	1.106
+++ flags.h	2 May 2003 02:33:11 -0000
@@ -102,6 +102,9 @@
 extern int warn_unused_variable;
 extern int warn_unused_value;
 
+/* Nonzero to warn about unused static variables.  */
+extern int warn_unused_static_variable;
+
 /* Nonzero to warn about code which is never reached.  */
 
 extern int warn_notreached;
Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.748
diff -u -r1.748 toplev.c
--- toplev.c	1 May 2003 16:13:29 -0000	1.748
+++ toplev.c	2 May 2003 02:33:20 -0000
@@ -1468,6 +1468,9 @@
 int warn_unused_variable;
 int warn_unused_value;
 
+/* Nonzero to warn about unused static variables.  */
+int warn_unused_static_variable;
+
 /* Nonzero to warn about code which is never reached.  */
 
 int warn_notreached;
@@ -1561,6 +1564,8 @@
    N_("Warn when a variable is unused") },
   {"unused-value", &warn_unused_value, 1,
    N_("Warn when an expression value is unused") },
+  {"unused-static-variable", &warn_unused_static_variable, 1,
+   N_("Warn when a variable with static scope is unused") },
   {"system-headers", &warn_system_headers, 1,
    N_("Do not suppress warnings from system headers") },
   {"error", &warnings_are_errors, 1,
@@ -2111,7 +2116,9 @@
 
       /* Warn about static fns or vars defined but not used.  */
       if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
-	   || (warn_unused_variable && TREE_CODE (decl) == VAR_DECL))
+	   || ((TREE_CODE (decl) == VAR_DECL) &&
+	       ((warn_unused_variable && !TREE_STATIC (decl)) ||
+		(warn_unused_static_variable && TREE_STATIC (decl)))))
 	  && ! TREE_USED (decl)
 	  /* The TREE_USED bit for file-scope decls is kept in the
identifier,
 	     to handle multiple external decls in different scopes.  */
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.271
diff -u -r1.271 invoke.texi
--- doc/invoke.texi	29 Apr 2003 20:45:55 -0000	1.271
+++ doc/invoke.texi	2 May 2003 02:33:33 -0000
@@ -227,7 +227,8 @@
 -Wsystem-headers  -Wtrigraphs  -Wundef  -Wuninitialized @gol
 -Wunknown-pragmas  -Wunreachable-code @gol
 -Wunused  -Wunused-function  -Wunused-label  -Wunused-parameter @gol
--Wunused-value  -Wunused-variable  -Wwrite-strings}
+-Wunused-value  -Wunused-variable  -Wunused-static-variable @gol
+-Wwrite-strings}
 
 @item C-only Warning Options
 @gccoptlist{-Wbad-function-cast  -Wmissing-declarations @gol
@@ -2494,6 +2495,24 @@
 Do not warn about compile-time integer division by zero.  Floating
point
 division by zero is not warned about, as it can be a legitimate way of
 obtaining infinities and NaNs.
+
+@item -Wunused-static-variable
+@opindex Wunused-static-variable
+Print a warning message for unused variables with @samp{static} scope.
+This warning is explicitly not enabled with @option{-Wunused} because
+a lot of older code uses some variant of the following in order to
+stamp object files and executables with file version information:
+
+@smallexample
+@group
+#ifndef LINT
+static char *sccsid = "@@(#)somefile.c 1.2.3.4";
+#endif
+@end group
+@end smallexample
+
+Using @option{-Wall} or @option{-Wunused} will print warning messages
+for unused variables with local scope.
 
 @item -Wsystem-headers
 @opindex Wsystem-headers


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]