Re: [RFC PATCH] pre-process: add __VA_OPT__ support
Al Viro <[email protected]> Wed, 1 Apr 2026 20:52:51 +0100
| Newsgroups | org.kernel.vger.linux-sparse |
|---|---|
| Message-ID | <20260401195251.GA974193@ZenIV> |
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