From: Al Viro <viro@zeniv.linux.org.uk>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: chriscli@google.com, linux-sparse@vger.kernel.org,
dan.carpenter@linaro.org, ben.dooks@codethink.co.uk,
rf@opensource.cirrus.com, Eric Zhang <zxh@xh-zhang.com>
Subject: Re: [RFC PATCH] pre-process: add __VA_OPT__ support
Date: Wed, 1 Apr 2026 20:52:51 +0100 [thread overview]
Message-ID: <20260401195251.GA974193@ZenIV> (raw)
In-Reply-To: <CAHk-=whC+8jX=t9NsF3uG_=BoLDNG7q0+FCm4ZrQDFdirWaasA@mail.gmail.com>
On Wed, Apr 01, 2026 at 09:18:34AM -0700, Linus Torvalds wrote:
> I suspect this part of the patch is more likely to actually be
> something that somebody may actually want some day:
>
> +// We ought to add __builtin_strcmp() support to constant expressions;
> +// what's more, "foo"[0] should also be recognized as one...
> +// Sloppy length check for now...
>
> because yeah, we've actually depended on the compiler just figuring
> some of those kinds of build-time optimizations out before to remove
> dead code etc.
Something like delta below ought to take care of __builtin_strcmp(); figuring
out the things like "ab"[1] being equal to 'b' is something I'd rather
leave for later - at the very least after I resurrect and repost the
busy-wait in shrink_dcache_parent() patchset. I've really spent way
too much of the last couple of months on digging in sparse ;-/
As for the whitespace handling in preprocessor, I suspect that it'll
end up with several bug reports for gcc and a DR for ISO C folks re
clarifying that part of 6.10; not until summer, probably...
diff --git a/builtin.c b/builtin.c
index a704c5e8..21b41045 100644
--- a/builtin.c
+++ b/builtin.c
@@ -631,6 +631,49 @@ static struct symbol_op strlen_op = {
.expand = expand_strlen,
};
+static int evaluate_strcmp(struct expression *expr)
+{
+ struct expression *arg;
+ bool non_const = false;
+
+ FOR_EACH_PTR(expr->args, arg) {
+ if (arg && arg->type == EXPR_SYMBOL)
+ arg = arg->symbol->initializer;
+ if (!arg || arg->type != EXPR_STRING || !arg->string->length)
+ non_const = true;
+ } END_FOR_EACH_PTR(arg);
+ if (!non_const)
+ expr->flags |= CEF_ICE;
+ return 1;
+}
+
+static int expand_strcmp(struct expression *expr, int cost)
+{
+ struct expression *arg;
+ const char *v[2];
+ int n = 0;
+
+ FOR_EACH_PTR(expr->args, arg) {
+ if (!arg)
+ return UNSAFE;
+ if (arg->type == EXPR_SYMBOL)
+ arg = arg->symbol->initializer;
+ if (!arg || arg->type != EXPR_STRING || !arg->string->length)
+ return UNSAFE;
+ v[n++] = arg->string->data;
+ } END_FOR_EACH_PTR(arg);
+ expr->flags |= CEF_SET_ICE;
+ expr->type = EXPR_VALUE;
+ expr->value = strcmp(v[0], v[1]);
+ expr->taint = 0;
+ return 0;
+}
+
+static struct symbol_op strcmp_op = {
+ .evaluate = evaluate_strcmp,
+ .expand = expand_strcmp,
+};
+
/*
* Builtin functions
*/
@@ -806,7 +849,7 @@ static const struct builtin_fn builtins_common[] = {
{ "__builtin_strcasestr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
{ "__builtin_strcat", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
{ "__builtin_strchr", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
- { "__builtin_strcmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype }},
+ { "__builtin_strcmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype }, .op = &strcmp_op},
{ "__builtin_strcpy", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
{ "__builtin_strcspn", size_t_ctype, 0, { &const_string_ctype, &const_string_ctype }},
{ "__builtin_strdup", &string_ctype, 0, { &const_string_ctype }},
diff --git a/validation/preprocessor/whitespace.c b/validation/preprocessor/whitespace.c
index 27bbea14..4396035b 100644
--- a/validation/preprocessor/whitespace.c
+++ b/validation/preprocessor/whitespace.c
@@ -1,23 +1,9 @@
#define S(X) #X
-#if notyet
#define EXPECT(X,Y) _Static_assert(__builtin_strcmp(S(X),Y) == 0, \
#X " => " S(X) ", " Y "expected");
#define Y(M) EXPECT(M(,]),"[ ]")
#define N(M) EXPECT(M(,]),"[]")
-#else
-
-// We ought to add __builtin_strcmp() support to constant expressions;
-// what's more, "foo"[0] should also be recognized as one...
-// Sloppy length check for now...
-
-#define __Y(X) _Static_assert(__builtin_strlen(S(X)) == 3, \
- #X " => " S(X) ", [ ] expected");
-#define Y(M) __Y(M(,]))
-#define __N(X) _Static_assert(__builtin_strlen(S(X)) == 2, \
- #X " => " S(X) ", [] expected");
-#define N(M) __N(M(,]))
-#endif
#define T1(X,R) [] // T
#define A1(X,R) [R // A
next prev parent reply other threads:[~2026-04-01 19:49 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1771930766.git.dan.carpenter@linaro.org>
2026-02-24 11:07 ` [PATCH] sparse: add support for __VA_OPT__ Dan Carpenter
2026-02-24 11:16 ` Ben Dooks
2026-02-24 11:56 ` Dan Carpenter
2026-02-24 12:42 ` Richard Fitzgerald
2026-02-24 13:15 ` Ben Dooks
2026-02-25 2:39 ` Chris Li
2026-02-25 3:36 ` Al Viro
2026-02-25 5:29 ` [RFC PATCH] pre-process: add __VA_OPT__ support Eric Zhang
2026-02-25 6:40 ` Al Viro
2026-02-25 7:27 ` Al Viro
2026-02-25 8:14 ` Eric Zhang
2026-02-25 22:18 ` Al Viro
2026-02-26 7:29 ` Al Viro
2026-03-16 6:56 ` Al Viro
2026-03-16 7:03 ` [PATCH 01/21] split copy() into "need to copy" and "can move in place" cases Al Viro
2026-03-16 7:03 ` [PATCH 02/21] expand and simplify the call of dup_token() in copy() Al Viro
2026-03-16 7:03 ` [PATCH 03/21] more dup_token() optimizations Al Viro
2026-03-16 7:03 ` [PATCH 04/21] parsing #define: saner handling of argument count, part 1 Al Viro
2026-03-16 7:03 ` [PATCH 05/21] simplify collect_arguments() and fix error handling there Al Viro
2026-03-16 7:04 ` [PATCH 06/21] try_arg(): don't use arglist for argument name lookups Al Viro
2026-03-16 7:04 ` [PATCH 07/21] make expand_has_...() responsible for expanding its argument Al Viro
2026-03-16 7:04 ` [PATCH 08/21] preparing to change argument number encoding for TOKEN_..._ARGUMENT Al Viro
2026-03-16 7:04 ` [PATCH 09/21] steal 2 bits from argnum for argument kind Al Viro
2026-03-16 7:04 ` [PATCH 10/21] on-demand argument expansion Al Viro
2026-03-16 7:04 ` [PATCH 11/21] kill create_arglist() Al Viro
2026-03-16 7:04 ` [PATCH 12/21] stop mangling arglist, get rid of TOKEN_ARG_COUNT Al Viro
2026-03-16 7:04 ` [PATCH 13/21] deal with ## on arguments separately Al Viro
2026-03-16 7:04 ` [PATCH 14/21] preparations for __VA_OPT__ support: reshuffle argument slot assignments Al Viro
2026-03-16 7:04 ` [PATCH 15/21] pre-process.c: split try_arg() Al Viro
2026-03-16 7:04 ` [PATCH 16/21] __VA_OPT__: parsing Al Viro
2026-03-16 7:04 ` [PATCH 17/21] expansion-time va_opt handling Al Viro
2026-03-16 7:04 ` [PATCH 18/21] merge(): saner handling of ->noexpand Al Viro
2026-03-16 7:04 ` [PATCH 19/21] simplify the calling conventions of collect_arguments() Al Viro
2026-03-16 7:04 ` [PATCH 20/21] make expand_one_symbol() inline Al Viro
2026-03-16 7:04 ` [PATCH 21/21] substitute(): convert switch() into cascade of ifs Al Viro
2026-03-16 16:42 ` [RFC PATCH] pre-process: add __VA_OPT__ support Linus Torvalds
2026-03-19 3:53 ` Al Viro
2026-03-19 4:07 ` Linus Torvalds
2026-03-19 5:34 ` Al Viro
2026-03-17 7:41 ` Chris Li
2026-03-18 6:35 ` Eric Zhang
2026-03-31 8:06 ` Al Viro
2026-03-31 8:07 ` [PATCH 1/6] nextchar(): get rid of special[] Al Viro
2026-03-31 8:07 ` [PATCH 2/6] simplify the inlined side of nextchar() Al Viro
2026-03-31 8:07 ` [PATCH 3/6] tokenize_stream(): don't bother with isspace() Al Viro
2026-03-31 8:07 ` [PATCH 4/6] TOKEN_DIRECTIVE: recognize directive-introducing # in tokenizer Al Viro
2026-03-31 8:07 ` [PATCH 5/6] saner collect_arg() code generation Al Viro
2026-03-31 8:07 ` [PATCH 6/6] try to get whitespaces right Al Viro
2026-04-01 10:39 ` [RFC PATCH] pre-process: add __VA_OPT__ support Al Viro
2026-04-01 16:18 ` Linus Torvalds
2026-04-01 19:52 ` Al Viro [this message]
2026-04-01 20:22 ` Al Viro
2026-02-25 7:05 ` [PATCH] sparse: add support for __VA_OPT__ Chris Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260401195251.GA974193@ZenIV \
--to=viro@zeniv.linux.org.uk \
--cc=ben.dooks@codethink.co.uk \
--cc=chriscli@google.com \
--cc=dan.carpenter@linaro.org \
--cc=linux-sparse@vger.kernel.org \
--cc=rf@opensource.cirrus.com \
--cc=torvalds@linux-foundation.org \
--cc=zxh@xh-zhang.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox