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?


Hi, Martin, Marc, Manuel.. You've been of great help.
The warning attribute did the trick.
Thanks.

On 19 May 2015 at 14:34, Martin Sebor <msebor@redhat.com> wrote:
> On 05/19/2015 10:04 AM, Marc Glisse wrote:
>>
>> 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")
>
>
> I hadn't tried attribute warning. It does work and makes for
> a cleaner solution than the workarounds I suggested. Thanks
> for pointing it out! (Unfortunately, there doesn't appear to
> be a way to use the warning message for the static warning,
> but that's only a minor wrinkle.)
>
> Attribute deprecated is not suitable for this purpose because
> it's not subject to dead code elimination and so the warning
> is issued regardless of the value of the constant expression.
>
> Martin


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