Date: Wed, 31 Jul 1996 21:34:37 +1000 From: Bruce Evans <bde@zeta.org.au> To: freebsd-current@FreeBSD.ORG, syssgm@devetir.qld.gov.au Subject: Re: `const char rcsid[]' vs -traditional Message-ID: <199607311134.VAA24793@godzilla.zeta.org.au>
next in thread | raw e-mail | index | archive | help
>I find:
> static const char rcsid[] = ...
>How long will it be before the ANSI compilers start discarding unreferenced
>static data? Or is "rcsid" already a special case in gcc? :-)
There's nothing to stop ANSI compilers from discarding unreferenced data,
but declaring static variables as `const' is a documented way of stopping
gcc from discarding them. At least, the following macro is a documented
way for gcc-2.6.3:
#define USE(var) \
static void *const use_##var = (&use_##var, &var, 0)
This method also works for functions. It is used in MAKE_SET() in
<sys/kernel.h>.
Unfortunately, gcc with maximal warnings generates many warnings for
this macro. E.g., when it is applied to
static int x;
USE(x);
z.c:4: warning: initialization makes pointer from integer without a cast
z.c:4: initializer element is not constant
There are even more warnings (which actually occur thousands of times for
compiling the kernel) for gcc-2.7.2. The USE() macro is no longer documented
for gcc-2.7.2; you are supposed to use the `unused' attribute instead:
static const int x __attribute__((__unused__));
The `unused' attribute doesn't work for gcc-2.6.3 so it will need to be
used in a macro that expands to the attribute version only for recent
versions of gcc.
The `unused' attribute doesn't work completely for functions:
static void x(void) __attribute__((__unused__));
static void x(void) {}
This works right for -O2 but the function still gets deleted for -O3.
The function probably needs to referenced via a pointer to keep it.
MAKE_SET() fails to do this for the function pointer arg because the
function pointer arg is only referenced in assembler.
>Should we switch to #ident? It doesn't do anything useful on 2.1.5, but
>we could fix that.
> #ident "$Id: frobble.c,v ..."
>What does the C standard say about #ident? Has anyone put a copy of the
>standard on the web?
#ident is a syntax error. The standard won't be put on the web until its
owners figure out how to charge for it there :-(.
Anyway, we shouldn't use #ident because it is impossible to hide the details
in a macro.
Bruce
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199607311134.VAA24793>
