Skip to contentSkip to conversation

sparse/parse: update decl->pos when direct_declarator() sees TOKEN_IDENT

PATCH New Oleg Nesterov 1 participant 1 message Dec 21, 2025 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparse/parse: update decl->pos when direct_declarator() sees TOKEN_IDENT

Test-case:

	$ cat -n DECL_POS.c
	     1	void
	     2	 *       ptr,
	     3	 *   (*  fptr  ) (int),
	     4	 *       ary[2];
	     5
	     6	struct T {
	     7	 void
	     8	  *      mptr1[],
	     9	  *      mptr2[];
	    10	 int     dummy;
	    11	};
	    12
	    13	static void func(struct T *t)
	    14	{
	    15		__builtin_offsetof(typeof(*t), dummy);
	    16	}

Note that all variables/members are defined at column 10.

However, before this patch

	$ ./sparse DECL_POS.c
	DECL_POS.c:2:2: warning: symbol 'ptr' was not declared. Should it be static?
	DECL_POS.c:3:2: warning: symbol 'fptr' was not declared. Should it be static?
	DECL_POS.c:4:2: warning: symbol 'ary' was not declared. Should it be static?
	DECL_POS.c:8:3: error: flexible array member 'mptr1' is not last
	DECL_POS.c:9:3: error: flexible array member 'mptr2' is not last

every reported position is wrong.

This is because the users of declarator() do alloc_symbol(token->pos), but the current token is not necessarily the ident found later in the declarator() paths.

With this patch:

	$ ./sparse DECL_POS.c
	DECL_POS.c:2:10: warning: symbol 'ptr' was not declared. Should it be static?
	DECL_POS.c:3:10: warning: symbol 'fptr' was not declared. Should it be static?
	DECL_POS.c:4:10: warning: symbol 'ary' was not declared. Should it be static?
	DECL_POS.c:8:10: error: flexible array member 'mptr1' is not last
	DECL_POS.c:9:10: error: flexible array member 'mptr2' is not last

See also the fixes in validation/{function-redecl,reserved}.c, some of the old positions in the check-error-start/end blocks were wrong.

TODO: there are more users of users of declarator() which should probably be updated the same way.

NOTE: unlike external_declaration(), declaration_list() doesn't check ident != NULL, so ./sparse doesn't complain about "struct { int; };", this should probably be fixed.

---
 parse.c                      |   5 ++
 symbol.h                     |   1 +
 validation/function-redecl.c |  16 ++--
 validation/reserved.c        | 144 +++++++++++++++++------------------
 4 files changed, 86 insertions(+), 80 deletions(-)
parse.c +5 -0
index 9389079e..1166ab7b 100644
@@ -1821,6 +1821,8 @@ static struct token *direct_declarator(struct token *token, struct decl_state *c
 
 	if (ctx->ident && token_type(token) == TOKEN_IDENT) {
 		*ctx->ident = token->ident;
+		if (ctx->d_pos)
+			*ctx->d_pos = token->pos;
 		token = token->next;
 	} else if (match_op(token, '(') &&
 	    is_nested(token, &next, ctx->prefer_abstract)) {
@@ -1945,6 +1947,7 @@ static struct token *declaration_list(struct token *token, struct symbol_list **
 		struct symbol *decl = alloc_symbol(token->pos, SYM_NODE);
 		ctx.cleanup = NULL;
 		ctx.ident = &decl->ident;
+		ctx.d_pos = &decl->pos;
 
 		token = declarator(token, &ctx);
 		if (match_op(token, ':'))
@@ -2963,6 +2966,7 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 	}
 
 	saved = ctx.ctype;
+	ctx.d_pos = &decl->pos;
 	token = declarator(token, &ctx);
 	token = handle_asm_name(token, &ctx);
 	token = handle_attributes(token, &ctx);
@@ -3089,6 +3093,7 @@ struct token *external_declaration(struct token *token, struct symbol_list **lis
 		decl = alloc_symbol(token->pos, SYM_NODE);
 		ctx.ctype = saved;
 		ctx.cleanup = NULL;
+		ctx.d_pos = &decl->pos;
 		token = handle_attributes(token, &ctx);
 		token = declarator(token, &ctx);
 		token = handle_asm_name(token, &ctx);
symbol.h +1 -0
index 3552d439..52f0977a 100644
@@ -107,6 +107,7 @@ struct ctype {
 struct decl_state {
 	struct ctype ctype;
 	struct ident **ident;
+	struct position *d_pos;
 	struct symbol_op *mode;
 	struct expression *cleanup;
 	unsigned long f_modifiers;		// function attributes
validation/function-redecl.c +8 -8
index d0d844a5..272d4ac9 100644
@@ -56,14 +56,14 @@ function-redecl.c:9:11: error: symbol 'ret_const' redeclared with different type
 function-redecl.c:9:11:    int extern const [addressable] [signed] [toplevel] ret_const( ... )
 function-redecl.c:8:5: note: previously declared as:
 function-redecl.c:8:5:    int extern [addressable] [signed] [toplevel] ret_const( ... )
-function-redecl.c:13:13: error: symbol 'ret_as' redeclared with different type (different address spaces):
-function-redecl.c:13:13:    void <asn:1> *extern [addressable] [toplevel] ret_as( ... )
-function-redecl.c:12:6: note: previously declared as:
-function-redecl.c:12:6:    void *extern [addressable] [toplevel] ret_as( ... )
-function-redecl.c:17:12: error: symbol 'ret_mod' redeclared with different type (different modifiers):
-function-redecl.c:17:12:    void const *extern [addressable] [toplevel] ret_mod( ... )
-function-redecl.c:16:6: note: previously declared as:
-function-redecl.c:16:6:    void *extern [addressable] [toplevel] ret_mod( ... )
+function-redecl.c:13:14: error: symbol 'ret_as' redeclared with different type (different address spaces):
+function-redecl.c:13:14:    void <asn:1> *extern [addressable] [toplevel] ret_as( ... )
+function-redecl.c:12:7: note: previously declared as:
+function-redecl.c:12:7:    void *extern [addressable] [toplevel] ret_as( ... )
+function-redecl.c:17:13: error: symbol 'ret_mod' redeclared with different type (different modifiers):
+function-redecl.c:17:13:    void const *extern [addressable] [toplevel] ret_mod( ... )
+function-redecl.c:16:7: note: previously declared as:
+function-redecl.c:16:7:    void *extern [addressable] [toplevel] ret_mod( ... )
 function-redecl.c:21:6: error: symbol 'arg_type' redeclared with different type (incompatible argument 1 (different base types)):
 function-redecl.c:21:6:    void extern [addressable] [toplevel] arg_type( ... )
 function-redecl.c:20:6: note: previously declared as:
validation/reserved.c +72 -72

149 lines not on this page · raw message

Signature · 1 line
2.52.0