[PATCH 19/21] simplify the calling conventions of collect_arguments()

Al Viro <[email protected]> Mon, 16 Mar 2026 07:04:13 +0000
Newsgroups org.kernel.vger.linux-sparse
Message-ID <[email protected]>
Currently we call that only after having verified that macro name is
followed by the (, with those two tokens passed as separate arguments.
What's more, collect_arguments() already can tell the caller "don't
expand that" if the arguments are malformed, so there's no reason not to
move the check for opening parenthesis into collect_arguments() - that
makes the calling conventions simpler and it does not incur any cost -
collect_arguments() is going to be inlined into its sole caller anyway.

Signed-off-by: Al Viro <[email protected]>
---
 pre-process.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/pre-process.c b/pre-process.c
index 352f02df..73f4d615 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -307,16 +307,17 @@ struct arg {
 	struct token *arg[3];
 };
 
-static int collect_arguments(struct token *start, struct symbol *sym, struct arg *args, struct token *what)
+static int collect_arguments(struct token *what, int fixed, bool vararg, struct arg *args)
 {
-	int fixed = sym->fixed_args;
-	bool vararg = sym->vararg;
+	struct token *start = scan_next(&what->next);
 	struct token *next = NULL, *v = NULL;
 	const char *err;
 	int commas;
 
 	memset(args, 0, sizeof(struct arg) * (fixed + 1));
 
+	if (!match_op(start, '('))
+		return 0;
 	for (commas = 0; commas < fixed; commas++) {
 		next = collect_arg(start, false, &what->pos);
 		if (token_type(next) != TOKEN_SPECIAL)
@@ -355,7 +356,7 @@ Eexcess:
 Eclosing:
 	err = "unterminated argument list invoking";
 out:
-	sparse_error(what->pos, "%s macro \"%s\"", err, show_ident(sym->ident));
+	sparse_error(what->pos, "%s macro \"%s\"", err, show_ident(what->ident));
 	what->next = next;
 	return 0;
 }
@@ -808,23 +809,20 @@ static struct token **substitute(struct token **list, const struct token *body,
 
 static int expand(struct token **list, struct symbol *sym)
 {
-	struct token *last;
+	struct token *next;
 	struct token *token = *list;
 	struct token **tail;
 	struct token *expansion = sym->expansion;
 	struct arg args[sym->fixed_args + 1];
 
-	if (sym->arglist) {
-		if (!match_op(scan_next(&token->next), '('))
-			return 1;
-		if (!collect_arguments(token->next, sym, args, token))
-			return 1;
-	}
+	if (sym->arglist &&
+	    !collect_arguments(token, sym->fixed_args, sym->vararg, args))
+		return 1;
 
 	if (sym->expand)
 		return sym->expand(token, args) ? 0 : 1;
 
-	last = token->next;
+	next = token->next;
 	tail = substitute(list, expansion, args);
 	/*
 	 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
@@ -834,7 +832,7 @@ static int expand(struct token **list, struct symbol *sym)
 	 */
 	(*list)->pos.newline = token->pos.newline;
 	(*list)->pos.whitespace = token->pos.whitespace;
-	*tail = last;
+	*tail = next;
 
 	return 0;
 }
-- 
2.47.3