[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAHk-=whVDzempHTWHMRpbSdCpFhSyfcLyfx2XvSTZNT-MNSS0A@mail.gmail.com>
Date: Sun, 17 Nov 2024 11:02:50 -0800
From: Linus Torvalds <torvalds@...ux-foundation.org>
To: David Laight <David.Laight@...lab.com>
Cc: Vincent Mailhol <mailhol.vincent@...adoo.fr>, Yury Norov <yury.norov@...il.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>, Luc Van Oostenryck <luc.vanoostenryck@...il.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"linux-sparse@...r.kernel.org" <linux-sparse@...r.kernel.org>,
Rikard Falkeborn <rikard.falkeborn@...il.com>
Subject: Re: [PATCH v4 1/2] compiler.h: add const_true()
On Sun, 17 Nov 2024 at 10:00, Linus Torvalds
<torvalds@...ux-foundation.org> wrote:
>
> IOW, something like this:
>
> /*
> * iff 'x' is a non-zero constant integer expression,
> * then '!(x)' will be a zero constant integer expression,
> * and casting that to 'void *' will result in a NULL
> * pointer. Otherwise casting it to 'void *' will be just
> * a regular 'void *'.
> *
> * The type of '0 ? NULL : (char *)' is 'char *'
> * The type of '0 ? (void *) : (char *) is 'void *'
> */
> #define const_true(x) \
> _Generic(0 ? (void *)((long)!(x)) : (char *)0, char *: 1, void *: 0)
>
> should work, and doesn't do any double expansion of complex arguments.
Always good to test things, and it does seem to actually work.
Interestingly, while testing it, I found what looks like a (harmless)
bug in gcc.
Gcc seems to think that "!(void *)1" is an integer constant expression.
But technically, only *integer* casts can be part of an integer
constant expression.
Both sparse and clang get that odd case right.
Practically speaking, this doesn't matter, but I'll claim that my test
coverage was at least interesting since it seems to have found a
compiler issue.
Maybe it's a documented gcc thing, I'm not sure. Regardless, I think I
actually prefer the gcc behavior, but I don't see that it really makes
much of a difference.
Linus
Powered by blists - more mailing lists