Messages in this thread | | | From | Linus Torvalds <> | Date | Tue, 20 Mar 2018 17:30:38 -0700 | Subject | Re: detecting integer constant expressions in macros |
| |
On Tue, Mar 20, 2018 at 5:10 PM, Uecker, Martin <Martin.Uecker@med.uni-goettingen.de> 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
| |