_Static_warning?

Martin Sebor msebor@redhat.com
Mon May 18 23:14:00 GMT 2015


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



More information about the Gcc-help mailing list