Newer gcc swallow version control keywords

Newer gcc swallow version control keywords

Nadav Har'El nyh at math.technion.ac.il
Mon Oct 17 21:09:35 IST 2011


On Mon, Oct 17, 2011, Oleg Goldshmidt wrote about "Newer gcc swallow version control keywords":
> static const char foo_src_id[] = "$Id$";

I remember many years ago (when I was probably still using SCCS with its %..%
macros, and SCCS's what(1) instead of ident(1)), there was already an argument
on whether what you're trying to do should work, or whether proper compiler
behavior is to optimize these declarations away. In any case, people who used
this trick (rather than comments) *wanted* them not to be optimized away, and
they always were *not* optimized away. I guess that now that changed,
according to your report.

In any case, because there was always a fear that the compiler might optimize
these out, someone invented a new directive, #ident, as in:

#ident "$Id$"

The C preprocessor would pass this statement unchanged to the C compiler
(as it also does with #line), and the C compiler changed it to some static
constant that doesn't get optimized away.
Last time I checked, this worked, but I suggest you give it a shot again.
I think it has been in all C compilers I've seen in the last two decades,
but perhaps it rusted away because of disuse ;-)


> but this is non-portable, and preprocessor acrobatics such as
> 
> #if (__GNUC__ >= 4) && (__GNUC_MINOR__ > 4)
> #define USED(x) x __attribute__((used))
> #else
> #define USED(x) x
> #endif
> 
> static const char USED(foo_src_id[]) = "$Id$";
> 
> is incredibly ugly and verbose

Why is this incredebly ugly? You can even make it nicer by putting in ident.h:
	#if (__GNUC__ >= 4) && (__GNUC_MINOR__ > 4)
	#define USED(x) x __attribute__((used))
	#else
	#define USED(x) x
	#endif
	#define IDENT(x) static const char USED(foo_src_id[]) = x;

and then in each file just do
#include "ident.h"
IDENT("$Id");

Doesn't look that bad...

> (such as -fkeep-static-consts) but they did not work, e.g.,
> -fkeep-static-consts emits static consts only when optimization is not
> on.

Apparently, the optimizer behavior you're reporting and the odd behavior of
-fkeep-static-consts you report is NOT new. Check out this bug report
from 6 (!) years ago:

	http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20319

Ironically, this bug is still marked "NEW" :-)

> check whether it works? If it is a RedHat-specific or generic for all
> GCCs is important.

According to the aforementioned bug report, it has been this way in all
GCCs for over 6 years, if optimization was turned on... I didn't check myself
in years, though.


-- 
Nadav Har'El                        |                    Monday, Oct 17 2011, 
nyh at math.technion.ac.il             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Committee: A group of people that keeps
http://nadav.harel.org.il           |minutes and wastes hours.



More information about the Linux-il mailing list