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]

Don't warn about unused volatile static variables


When a variable is declared volatile, it might be used in some
non-obvious way.  For instance, you might have some kind of garbage
collection system in your linker but want to make sure that some
variable isn't collected, and write:

extern int x;
static const volatile void *x_ref = &x;

Or, maybe you put 'x_ref' in a special section and use it to locate
'x' (like the way that constructors are called under ELF).  Or put a
variable in a special section and use it to initialise a hardware
register.

I think this sort of thing is perfectly reasonable and it'd be
annoying to produce a warning for this variable; the 'volatile' is
sufficient to inform the compiler that it's used in some way the
compiler doesn't know about.

Bootstrap & test in progress on powerpc-darwin; I'll wait for a bit to
see if there are any strenuous objections.

-- 
- Geoffrey Keating <geoffk@apple.com>

===File ~/patches/gcc-volatileunusedstatic.patch============
2003-05-03  Geoffrey Keating  <geoffk@apple.com>

	* toplev.c (check_global_declarations): Suppress not-used warning
	for volatile variables.
 
Index: testsuite/ChangeLog
2003-05-03  Geoffrey Keating  <geoffk@apple.com>

	* gcc.dg/unused-5.c: New test.

Index: toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.749
diff -u -p -u -p -r1.749 toplev.c
--- toplev.c	2 May 2003 11:33:03 -0000	1.749
+++ toplev.c	3 May 2003 21:48:41 -0000
@@ -2113,6 +2113,8 @@ check_global_declarations (vec, len)
 	  && ! TREE_USED (DECL_NAME (decl))
 	  && ! DECL_EXTERNAL (decl)
 	  && ! TREE_PUBLIC (decl)
+	  /* A volatile variable might be used in some non-obvious way.  */
+	  && ! TREE_THIS_VOLATILE (decl)
 	  /* Global register variables must be declared to reserve them.  */
 	  && ! (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
 	  /* Otherwise, ask the language.  */
Index: testsuite/gcc.dg/unused-5.c
===================================================================
RCS file: testsuite/gcc.dg/unused-5.c
diff -N testsuite/gcc.dg/unused-5.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/unused-5.c	3 May 2003 21:50:05 -0000
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-Wunused" } */
+/* { dg-final { scan-assembler "string_to_look_for" } } */
+
+/* 'volatile' variables get output and don't produce a warning about being
+   unused.  */
+static volatile char string[] 
+  = "string_to_look_for";  /* { dg-bogus "not used" } */
============================================================


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