Re: _Static_warning?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 18 May 2015, Martin Sebor wrote:

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");
    ^

Does the "warning" function attribute not work? (or maybe "deprecated")

--
Marc Glisse




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux