Re: [PATCH] sparse: add support for __VA_OPT__
Dan Carpenter <[email protected]> Tue, 24 Feb 2026 14:56:34 +0300
| Newsgroups | org.kernel.vger.linux-sparse |
|---|---|
| Message-ID | <[email protected]> |
Oh, sorry, Al's branch changed that code more than I imagined. I would
resend the patch, but I need to figure out this GCC warning...
CC pre-process.o
pre-process.c: In function ‘substitute’:
pre-process.c:785:16: warning: function may return address of local variable [-Wreturn-local-addr]
785 | return list;
| ^~~~
pre-process.c:687:31: note: declared here
687 | struct token *added, *arg;
| ^~~~~
Anyway, here is what I've forward ported.
regards,
dan carpenter
---
ident-list.h | 1 +
pre-process.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 54 insertions(+)
diff --git a/ident-list.h b/ident-list.h
index 3c08e8ca9aa4..556d4050c88d 100644
--- a/ident-list.h
+++ b/ident-list.h
@@ -65,6 +65,7 @@ IDENT(c_generic_selections);
IDENT(c_static_assert);
__IDENT(pragma_ident, "__pragma__", 0);
__IDENT(__VA_ARGS___ident, "__VA_ARGS__", 0);
+__IDENT(__VA_OPT___ident, "__VA_OPT__", 0);
__IDENT(__func___ident, "__func__", 0);
__IDENT(__FUNCTION___ident, "__FUNCTION__", 0);
__IDENT(__PRETTY_FUNCTION___ident, "__PRETTY_FUNCTION__", 0);
diff --git a/pre-process.c b/pre-process.c
index 4e322855d600..364c68489a84 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -638,11 +638,50 @@ static int handle_kludge(const struct token **p, struct arg *args)
}
}
+static struct token *get_VA_OPT(const struct token **p, struct arg *args)
+{
+ struct position base_pos = (*p)->pos;
+ struct token *t = (*p)->next;
+ const char *expected;
+ struct token *ret = NULL;
+ struct token *dup, *tail = NULL;
+
+ if (token_type(t) != TOKEN_SPECIAL || t->special != '(') {
+ expected = "(";
+ goto error;
+ }
+ while (true) {
+ t = t->next;
+ if (eof_token(t)) {
+ expected = ")";
+ goto error;
+ }
+ if (token_type(t) == TOKEN_SPECIAL &&
+ t->special == ')')
+ break;
+ dup = dup_token(t, &base_pos);
+ if (!ret)
+ ret = dup;
+ else
+ tail->next = dup;
+ tail = dup;
+ }
+
+ if (tail)
+ tail->next = &eof_token_entry;
+ *p = t;
+ return ret;
+error:
+ sparse_error(t->pos, "__VA_OPT__ error: expected '%s'", expected);
+ return NULL;
+}
+
static struct token **substitute(struct token **list, const struct token *body, struct arg *args)
{
struct position *base_pos = &(*list)->pos;
int *count;
enum {Normal, Placeholder, Concat} state = Normal;
+ struct token *va_opt = NULL;
for (; !eof_token(body); body = body->next) {
struct token *added, *arg;
@@ -692,6 +731,10 @@ static struct token **substitute(struct token **list, const struct token *body,
case TOKEN_MACRO_ARGUMENT:
arg = args[body->argnum].expanded;
+ if (va_opt && !eof_token(arg)) {
+ list = substitute(list, va_opt, args);
+ va_opt = NULL;
+ }
count = &args[body->argnum].n_normal;
if (eof_token(arg)) {
state = Normal;
@@ -711,6 +754,11 @@ static struct token **substitute(struct token **list, const struct token *body,
continue;
default:
+ if (token_type(body) == TOKEN_IDENT &&
+ body->ident == &__VA_OPT___ident) {
+ va_opt = get_VA_OPT(&body, args);
+ continue;
+ }
added = dup_token(body, base_pos);
if (token_type(body) == TOKEN_IDENT &&
added->ident->tainted)
@@ -1094,6 +1142,8 @@ static struct token *parse_arguments(struct token *list)
while (token_type(arg) == TOKEN_IDENT) {
if (arg->ident == &__VA_ARGS___ident)
goto Eva_args;
+ if (arg->ident == &__VA_OPT___ident)
+ goto Eva_opt;
if (!++count->normal)
goto Eargs;
next = arg->next;
@@ -1172,6 +1222,9 @@ Enotclosed:
Eva_args:
sparse_error(arg->pos, "__VA_ARGS__ can only appear in the expansion of a C99 variadic macro");
return NULL;
+Eva_opt:
+ sparse_error(arg->pos, "__VA_OPT__ can only appear in the expansion of a C99 variadic macro");
+ return NULL;
Eargs:
sparse_error(arg->pos, "too many arguments in macro definition");
return NULL;
--
2.51.0