Newer gcc swallow version control keywords

Newer gcc swallow version control keywords

Nadav Har'El nyh at math.technion.ac.il
Tue Oct 18 17:58:49 IST 2011


On Tue, Oct 18, 2011, Nadav Har'El wrote about "Re: Newer gcc swallow version control keywords":
> On Tue, Oct 18, 2011, Oleg Goldshmidt wrote about "Re: Newer gcc swallow version control keywords":
> > It was about C++. C and C++ compilers behave the same.
> 
> I was very surprised to discover that this is indeed the case.

I was originally right, and my last statement was wrong - the constructor
DOES matter. I probably wasn't paying attention when I ran my previous
test. Actually, I checked again, and constructor does cause the unused
static object NOT to be optimized out.

In the following example, the "id" object is not optimized out, just as
I thought. You see "yo" in the printout, and ident(1) shows the ident string
you wanted, even with -O2. If you're using C++, and are reluctant to use
__attribute__((used)) because it's not standard, how about using this trick?

BTW, unfortunately, you need the "global" below, because if Ident's constructor
doesn't read its argument, the optimizer ends up optimizing away the string
constant given to it as argument! Instead of using a global variable, you can
probably consider doing other things with the argument which the optimizer
considers as using it.

#include <cstdio>
extern const char *global;
class Ident {
public:
	Ident(const char *ident){
		// We want it to touch the ident parameter, so the caller
		// doesn't think its ignored and can be optimized out.
		global=ident;
		printf("yo\n");
	}
};

static Ident id("$Id: hello $");

main(){
	printf("hello\n");
}

// Put this in a single file, probably main.cc
const char *global;



-- 
Nadav Har'El                        |                   Tuesday, Oct 18 2011, 
nyh at math.technion.ac.il             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Unlike Microsoft, a restaurant would not
http://nadav.harel.org.il           |charge me for food with a bug!



More information about the Linux-il mailing list