This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: _Static_warning?
- From: Martin Sebor <msebor at redhat dot com>
- To: Eduardo Piombino <drakorg at gmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Mon, 18 May 2015 17:14:53 -0600
- Subject: Re: _Static_warning?
- Authentication-results: sourceware.org; auth=none
- References: <CAGHqW79SSbF1QEddCChJFfzvm6tk5XoMHfcM0jwdpUUzAUmokw at mail dot gmail dot com>
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