public inbox for linux-sparse@vger.kernel.org
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: linux-sparse@vger.kernel.org
Cc: chriscli@google.com, torvalds@linux-foundation.org,
	zxh@xh-zhang.com, ben.dooks@codethink.co.uk,
	dan.carpenter@linaro.org, rf@opensource.cirrus.com
Subject: [PATCH 09/21] steal 2 bits from argnum for argument kind
Date: Mon, 16 Mar 2026 07:04:03 +0000	[thread overview]
Message-ID: <20260316070415.768839-9-viro@zeniv.linux.org.uk> (raw)
In-Reply-To: <20260316070415.768839-1-viro@zeniv.linux.org.uk>

We have 3 separate token types (TOKEN_{MACRO,QUOTED,STR}_ARGUMENT),
with fairly similar handling at expansion time.  Let's steal two bits
from ->argnum and use them to represent the kind of occurrence; that
simplifies substitute() and allows for better code generation there.

The object we use to store the argument state at expansion time (struct
arg) is already a structure with 3 pointers to token lists (unexpanded,
expanded and stringified forms of the argument) and 3 integer counters -
the number of remaining occurrencies of each kind.  Gather those into
3-element arrays indexed by the kind; counts will be gone soon, token
lists will remain.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
---
 pre-process.c | 106 +++++++++++++++++++++-----------------------------
 token.h       |  14 +++++--
 2 files changed, 55 insertions(+), 65 deletions(-)

diff --git a/pre-process.c b/pre-process.c
index efb208e7..b45688d5 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -304,12 +304,8 @@ static struct token *collect_arg(struct token *prev, bool vararg, const struct p
  */
 
 struct arg {
-	struct token *arg;
-	struct token *expanded;
-	struct token *str;
-	int n_normal;
-	int n_quoted;
-	int n_str;
+	struct token *arg[3];
+	int count[3];
 };
 
 static int collect_arguments(struct token *start, struct symbol *sym, struct arg *args, struct token *what)
@@ -330,10 +326,10 @@ static int collect_arguments(struct token *start, struct symbol *sym, struct arg
 			goto Eclosing;
 		p = &arglist->next->count;
 		arglist = arglist->next->next;
-		args[commas].arg = start->next;
-		args[commas].n_normal = p->normal;
-		args[commas].n_quoted = p->quoted;
-		args[commas].n_str = p->str;
+		args[commas].arg[ARG_QUOTED] = start->next;
+		args[commas].count[ARG_NORMAL] = p->normal;
+		args[commas].count[ARG_QUOTED] = p->quoted;
+		args[commas].count[ARG_STR] = p->str;
 		if (!match_op(next, ',')) {
 			if (commas < fixed - 1)
 				goto Efew;
@@ -353,10 +349,10 @@ static int collect_arguments(struct token *start, struct symbol *sym, struct arg
 		goto Eexcess;
 	if (vararg) {
 		p = &arglist->next->count;
-		args[fixed].arg = v;
-		args[fixed].n_normal = p->normal;
-		args[fixed].n_quoted = p->quoted;
-		args[fixed].n_str = p->str;
+		args[fixed].arg[ARG_QUOTED] = v;
+		args[fixed].count[ARG_NORMAL] = p->normal;
+		args[fixed].count[ARG_QUOTED] = p->quoted;
+		args[fixed].count[ARG_STR] = p->str;
 	}
 	what->next = next->next;
 	return 1;
@@ -440,21 +436,21 @@ static void expand_arguments(int count, struct arg *args)
 {
 	int i;
 	for (i = 0; i < count; i++) {
-		struct token *arg = args[i].arg;
+		struct token *arg = args[i].arg[ARG_QUOTED];
 		if (!arg)
 			arg = &eof_token_entry;
-		if (args[i].n_str)
-			args[i].str = stringify(arg);
-		if (args[i].n_normal) {
-			if (!args[i].n_quoted) {
-				args[i].expanded = arg;
-				args[i].arg = NULL;
+		if (args[i].count[ARG_STR])
+			args[i].arg[ARG_STR] = stringify(arg);
+		if (args[i].count[ARG_NORMAL]) {
+			if (!args[i].count[ARG_QUOTED]) {
+				args[i].arg[ARG_NORMAL] = arg;
+				args[i].arg[ARG_QUOTED] = NULL;
 			} else if (eof_token(arg)) {
-				args[i].expanded = arg;
+				args[i].arg[ARG_NORMAL] = arg;
 			} else {
-				args[i].expanded = dup_list(arg);
+				args[i].arg[ARG_NORMAL] = dup_list(arg);
 			}
-			expand_list(&args[i].expanded);
+			expand_list(&args[i].arg[ARG_NORMAL]);
 		}
 	}
 }
@@ -629,13 +625,18 @@ static inline int argnum(const struct token *arg)
 	return arg->argnum >> ARGNUM_BITS_STOLEN;
 }
 
+static inline enum arg_kind argkind(const struct token *arg)
+{
+	return arg->argnum & ARGNUM_KIND_MASK;
+}
+
 static int handle_kludge(const struct token **p, struct arg *args)
 {
 	const struct token *t = (*p)->next->next;
 	while (1) {
-		struct arg *v = &args[argnum(t)];
+		struct token *v = args[argnum(t)].arg[ARG_QUOTED];
 		if (token_type(t->next) != TOKEN_CONCAT) {
-			if (v->arg) {
+			if (v) {
 				/* ignore the first ## */
 				*p = (*p)->next;
 				return 0;
@@ -644,7 +645,7 @@ static int handle_kludge(const struct token **p, struct arg *args)
 			*p = t;
 			return 1;
 		}
-		if (v->arg && !eof_token(v->arg))
+		if (v && !eof_token(v))
 			return 0; /* no magic */
 		t = t->next->next;
 	}
@@ -685,14 +686,9 @@ static struct token **substitute(struct token **list, const struct token *body,
 			tail = &added->next;
 			break;
 
-		case TOKEN_STR_ARGUMENT:
-			arg = args[argnum(body)].str;
-			count = &args[argnum(body)].n_str;
-			goto copy_arg;
-
-		case TOKEN_QUOTED_ARGUMENT:
-			arg = args[argnum(body)].arg;
-			count = &args[argnum(body)].n_quoted;
+		case TOKEN_MACRO_ARGUMENT:
+			arg = args[argnum(body)].arg[argkind(body)];
+			count = &args[argnum(body)].count[argkind(body)];
 			if (!arg || eof_token(arg)) {
 				if (state == Concat)
 					state = Normal;
@@ -700,16 +696,6 @@ static struct token **substitute(struct token **list, const struct token *body,
 					state = Placeholder;
 				continue;
 			}
-			goto copy_arg;
-
-		case TOKEN_MACRO_ARGUMENT:
-			arg = args[argnum(body)].expanded;
-			count = &args[argnum(body)].n_normal;
-			if (eof_token(arg)) {
-				state = Normal;
-				continue;
-			}
-		copy_arg:
 			if (!--*count)
 				tail = move_into(&added, arg);
 			else
@@ -1040,8 +1026,6 @@ static int token_different(struct token *t1, struct token *t2)
 		different = t1->special != t2->special;
 		break;
 	case TOKEN_MACRO_ARGUMENT:
-	case TOKEN_QUOTED_ARGUMENT:
-	case TOKEN_STR_ARGUMENT:
 		different = t1->argnum != t2->argnum;
 		break;
 	case TOKEN_CHAR_EMBEDDED_0 ... TOKEN_CHAR_EMBEDDED_3:
@@ -1206,7 +1190,7 @@ Eva_args:
 	return NULL;
 }
 
-static int try_arg(struct token *token, enum token_type type, struct token *arglist)
+static int try_arg(struct token *token, enum arg_kind kind, struct token *arglist)
 {
 	struct ident *ident = token->ident;
 	int nr, n;
@@ -1224,13 +1208,13 @@ static int try_arg(struct token *token, enum token_type type, struct token *argl
 	for (int i = 0; i < nr; i++)
 		arglist = arglist->next->next;
 
-	token->argnum = nr << ARGNUM_BITS_STOLEN;
-	token_type(token) = type;
-	switch (type) {
-	case TOKEN_MACRO_ARGUMENT:
+	token->argnum = (nr << ARGNUM_BITS_STOLEN) | kind;
+	token_type(token) = TOKEN_MACRO_ARGUMENT;
+	switch (kind) {
+	case ARG_NORMAL:
 		n = ++arglist->next->count.normal;
 		break;
-	case TOKEN_QUOTED_ARGUMENT:
+	case ARG_QUOTED:
 		n = ++arglist->next->count.quoted;
 		break;
 	default:
@@ -1251,7 +1235,7 @@ static struct token *handle_hash(struct token **p, struct token *arglist)
 	struct token *token = *p;
 	if (macro_funclike) {
 		struct token *next = token->next;
-		if (!try_arg(next, TOKEN_STR_ARGUMENT, arglist))
+		if (!try_arg(next, ARG_STR, arglist))
 			goto Equote;
 		next->pos.whitespace = token->pos.whitespace;
 		__free_token(token);
@@ -1273,7 +1257,7 @@ static struct token *handle_hashhash(struct token *token, struct token *arglist)
 	struct token *concat;
 	int state = match_op(token, ',');
 	
-	try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
+	try_arg(token, ARG_QUOTED, arglist);
 
 	while (1) {
 		struct token *t;
@@ -1297,7 +1281,7 @@ static struct token *handle_hashhash(struct token *token, struct token *arglist)
 				return NULL;
 		}
 
-		is_arg = try_arg(t, TOKEN_QUOTED_ARGUMENT, arglist);
+		is_arg = try_arg(t, ARG_QUOTED, arglist);
 
 		if (state == 1 && is_arg) {
 			state = is_arg;
@@ -1339,7 +1323,7 @@ static struct token *parse_expansion(struct token *expansion, struct token *argl
 			if (!token)
 				return NULL;
 		} else {
-			try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
+			try_arg(token, ARG_NORMAL, arglist);
 		}
 		if (token_type(token) == TOKEN_ERROR)
 			goto Earg;
@@ -2007,7 +1991,7 @@ static int handle_nondirective(struct stream *stream, struct token **line, struc
 
 static struct token *first_arg(struct arg *args)
 {
-	struct token *arg = args[0].arg;
+	struct token *arg = args[0].arg[ARG_QUOTED];
 	expand_list(&arg);
 	return arg;
 }
@@ -2356,11 +2340,9 @@ static void dump_macro(struct symbol *sym)
 		case TOKEN_CONCAT:
 			printf("##");
 			break;
-		case TOKEN_STR_ARGUMENT:
-			printf("#");
-			/* fall-through */
-		case TOKEN_QUOTED_ARGUMENT:
 		case TOKEN_MACRO_ARGUMENT:
+			if (argkind(token) == ARG_STR)
+				printf("#");
 			printf("%s", show_ident(args[argnum(token)]));
 			break;
 		default:
diff --git a/token.h b/token.h
index fe7c7fe9..273da39a 100644
--- a/token.h
+++ b/token.h
@@ -100,8 +100,6 @@ enum token_type {
 	TOKEN_STREAMBEGIN,
 	TOKEN_STREAMEND,
 	TOKEN_MACRO_ARGUMENT,
-	TOKEN_STR_ARGUMENT,
-	TOKEN_QUOTED_ARGUMENT,
 	TOKEN_CONCAT,
 	TOKEN_GNU_KLUDGE,
 	TOKEN_UNTAINT,
@@ -177,8 +175,18 @@ struct argcount {
 	unsigned str:10;
 };
 
+enum arg_kind {
+	ARG_QUOTED = 0,
+	ARG_NORMAL = 1,
+	ARG_STR = 2,
+};
+
+enum {
+	ARGNUM_BITS_STOLEN = 2
+};
+
 enum {
-	ARGNUM_BITS_STOLEN
+	ARGNUM_KIND_MASK = 3
 };
 
 /*
-- 
2.47.3


  parent reply	other threads:[~2026-03-16  7:01 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
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                       ` Al Viro [this message]
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=20260316070415.768839-9-viro@zeniv.linux.org.uk \
    --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