Re: [PATCH 02/10] compiler.h: add is_const() as a replacement of __is_constexpr()

From: Linus Torvalds
Date: Fri Dec 06 2024 - 14:15:54 EST


On Fri, 6 Dec 2024 at 11:07, David Laight <David.Laight@xxxxxxxxxx> wrote:
>
> I'm missing the compiler version and options to generate the error.

Just -Wall with most recent gcc versions seems to do it. At least I
can repro it with gcc-14.2.1 and something silly like this:

$ cat t.c
int fn(int a) { return (a<<2)?1:2; }
$ gcc -Wall -S t.c
t.c: In function ‘fn’:
t.c:1:26: warning: ‘<<’ in boolean context, did you mean ‘<’?
[-Wint-in-bool-context]

> Does a '+ 0' help? "(var << 2) + 0 ? 0 : 0"

Yeah, that actually works.

And "+0" is nice in that it should work in any context.

> #define const_NULL(x) _Generic(0 ? (x) : (char *)0, char *: 1, void *: 0)
> #define const_true(x) const_NULL((x) ? NULL : (void *)1L))
> #define const_expr(x) const_NULL((x) ? NULL : NULL))
> I send this morning.
> Needs 's/char/struct kjkjkjkjui/' applied.

Oh Christ. You really are taking this whole ugly to another level.

Linus