Re: detecting integer constant expressions in macros

From: Linus Torvalds
Date: Tue Mar 20 2018 - 20:31:28 EST


On Tue, Mar 20, 2018 at 5:10 PM, Uecker, Martin
<Martin.Uecker@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
>>
>> So I see two issues:
>>
>> - "sizeof(*(void *)1)" is not necessalily well-defined. For gcc it
>> is
>> 1. But it could cause warnings.
>
> It is a documented extension which enables pointer arithmetic
> on void pointers, so I am sure neither gcc nor
> clang has any problem with it.

Well, so sparse does the whole "void pointer arithmetic" thing too,
but sparse actually complains about use of sizeof(void).

We could remove that warning, though. But:

> But one could also use __builtin_types_compatible_p instead.

That might be the right approach, even if I like how it only used
standard C (although _disgusting_ standard C) without it apart from
the small issue of sizeof(void)

So something like

#define __is_constant(a) \
__builtin_types_compatible_p(int *, typeof(1 ? ((void*)((a) *
0l)) : (int*)1 ) )

if I counted the parentheses right..

Linus