public inbox for linux-sparse@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: Ben Dooks <ben.dooks@codethink.co.uk>
Cc: linux-sparse@vger.kernel.org, Chris Li <chriscli@google.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Ricardo Ribalda <ribalda@chromium.org>,
	Hans Verkuil <hverkuil@kernel.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Richard Fitzgerald <rf@opensource.cirrus.com>
Subject: Re: [PATCH] sparse: add support for __VA_OPT__
Date: Tue, 24 Feb 2026 14:56:34 +0300	[thread overview]
Message-ID: <aZ2R8k1MRiPwJwOC@stanley.mountain> (raw)
In-Reply-To: <29424a96-4c7c-4076-99bc-903cd857f89a@codethink.co.uk>

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


  reply	other threads:[~2026-02-24 11:56 UTC|newest]

Thread overview: 42+ 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 [this message]
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-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=aZ2R8k1MRiPwJwOC@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=ben.dooks@codethink.co.uk \
    --cc=chriscli@google.com \
    --cc=hverkuil@kernel.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=rf@opensource.cirrus.com \
    --cc=ribalda@chromium.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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