This is the mail archive of the gcc-help@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]

Re: _Static_warning?


On 05/16/2015 08:58 AM, Eduardo Piombino wrote:
Hi,

Is there any way to get gcc to generate a warning upon some
compile-time condition?
I'd be aiming at something like _Static_assert, but raising a warning
instead of an error.

Nowadays, to achieve something similar, I have to run 2 builds, one
with the assert enabled just to get notified of such conditions, and
then a another one, without the asserts, to actually compile them.

I don't think there's anything like what you're looking for.
Short of hacking the compiler and implementing it, a similar
effect can be achieved by [ab]using an existing warning. For
example, like so:

$ cat t.c && gcc -Wall -c -o/dev/null t.c
extern int printf (const char*, ...) __attribute__ ((nonnull (1)));

#define Static_Warning(x, txt) \
        (void)((x) ? 0 : printf ("%s%s\n", txt, (char*)x))

void foo (void)
{
    Static_Warning (0, "foo");
    Static_Warning (1, "bar");
}
t.c: In function âfooâ:
t.c:8:5: warning: reading through null pointer (argument 3) [-Wformat=]
     Static_Warning (0, "foo");
     ^

Martin


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