Let gcc generate a warning or an error?

Jan Engelhardt jengelh@medozas.de
Fri Dec 26 20:06:00 GMT 2008


On Friday 2008-12-26 07:26, Ian Lance Taylor wrote:
>
>> Hi, I would like to do something like
>>
>> static inline void foo (unsigned long x)
>> {
>>     if (__builtin_constant_p (x))
>>     {
>>         asm volatile ("... %0" :: "n" (x));
>>     }
>>     else
>>     {
>>         __builtin_error ("foo must be used with a compile time constant");
>>     }
>> }
>>
>> Is there a way to generate an error/warning/info by making gcc
>> call error/warning/info?
>>
>> So that in the above case an understandable error message is printed.
>
>Not really, no.  It would be a useful feature.  In C++ you can use
>template tricks to implement this.

Does not even need a template.

#define foo(x) ({ \
	BUILD_BUG_ON(!__builtin_constant_p(x)); \
	__foo(x); \
})
void __foo(unsigned long x)
{
	asm ...
}

with BUILD_BUG_ON from
http://lxr.linux.no/linux+v2.6.28/include/linux/kernel.h#L509



More information about the Gcc-help mailing list