[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241206193836.GA26860@1wt.eu>
Date: Fri, 6 Dec 2024 20:38:36 +0100
From: Willy Tarreau <w@....eu>
To: Linus Torvalds <torvalds@...ux-foundation.org>
Cc: David Laight <David.Laight@...lab.com>,
Vincent Mailhol <vincent.mailhol@...il.com>,
Luc Van Oostenryck <luc.vanoostenryck@...il.com>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Bill Wendling <morbo@...gle.com>,
Justin Stitt <justinstitt@...gle.com>,
Yury Norov <yury.norov@...il.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Kees Cook <kees@...nel.org>,
"Gustavo A. R. Silva" <gustavoars@...nel.org>,
Jani Nikula <jani.nikula@...ux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@...ux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@...el.com>,
Tvrtko Ursulin <tursulin@...ulin.net>,
David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
Suzuki K Poulose <suzuki.poulose@....com>,
Mike Leach <mike.leach@...aro.org>,
James Clark <james.clark@...aro.org>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Rikard Falkeborn <rikard.falkeborn@...il.com>,
"linux-sparse@...r.kernel.org" <linux-sparse@...r.kernel.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"llvm@...ts.linux.dev" <llvm@...ts.linux.dev>,
"linux-hardening@...r.kernel.org" <linux-hardening@...r.kernel.org>,
"intel-gfx@...ts.freedesktop.org" <intel-gfx@...ts.freedesktop.org>,
"dri-devel@...ts.freedesktop.org" <dri-devel@...ts.freedesktop.org>,
"coresight@...ts.linaro.org" <coresight@...ts.linaro.org>,
"linux-arm-kernel@...ts.infradead.org" <linux-arm-kernel@...ts.infradead.org>,
"uecker@...raz.at" <uecker@...raz.at>
Subject: Re: [PATCH 02/10] compiler.h: add is_const() as a replacement of
__is_constexpr()
On Fri, Dec 06, 2024 at 11:15:20AM -0800, Linus Torvalds wrote:
> On Fri, 6 Dec 2024 at 11:07, David Laight <David.Laight@...lab.com> 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.
I've already used "+0" to shut certain warnings, I don't really remember
which one, but also remember it was OK everywhere I needed.
Another trick I've been using to shut up the compiler is a cast via typeof
and an intermediary variable:
#define shut_up(expr) \
({ \
typeof(expr) _expr_ = expr; \
_expr_; \
})
int fn1(int a)
{
return shut_up(a << 2) ? 1 : 2;
}
int fn2(int a)
{
return (a << 2) ? 1 : 2;
}
$ gcc -Wall -S t2.c
t2.c: In function 'fn2':
t2.c:15:19: warning: '<<' in boolean context, did you mean '<'? [-Wint-in-bool-context]
15 | return (a << 2) ? 1 : 2;
| ~~~^~~~~
See ? It only complains on fn2() but not fn1(). Similarly I found it
to be quite portable (at least I don't remember seeing it fail on me).
It produces the exact same code, except at -O0 where it does really
create a local variable.
Willy
Powered by blists - more mailing lists