Skip to contentSkip to conversation

sparse: add the new m_pos member into struct expression{EXPR_DEREF}

PATCH Applied Oleg Nesterov 2 participants 2 messages Dec 16, 2025 – Jan 16 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparse: add the new m_pos member into struct expression{EXPR_DEREF}

Test-case:

	$ cat -n MEMPOS.c
	     1	struct T {
	     2	 int mem;
	     3	} X;
	     4
	     5	void func(struct T *x)
	     6	{
	     7	 x -> mem = 1;
	     8	 X .  mem = 2;
	     9	}
	$ ./test-dissect MEMPOS.c | grep -F T.mem
	   2:6                    def   m T.mem                            int
	   7:4   func             -w-   m T.mem                            int
	   8:4   func             -w-   m T.mem                            int

Note that the reported position of .mem usage is wrong. This is because do_expression(EXPR_DEREF) uses &expr->pos which is position of TOKEN_SPECIAL, not the position of the next token (mem).

This also breaks "semind search -l" (search by location)

	$ ./semind add MEMPOS.c
	$ ./semind search -l MEMPOS.c:7:7

reports nothing.

With this patch:

	$ ./test-dissect MEMPOS.c | grep -F T.mem
	   2:6                    def   m T.mem                            int
	   7:7   func             -w-   m T.mem                            int
	   8:7   func             -w-   m T.mem                            int
	$ ./semind add MEMPOS.c
	$ ./semind search -l MEMPOS.c:7:7
	(def) MEMPOS.c	2	6		 int mem;
	(-w-) MEMPOS.c	7	7	func	 x -> mem = 1;
	(-w-) MEMPOS.c	8	7	func	 X .  mem = 2;
---
 dissect.c    | 2 +-
 expression.c | 1 +
 expression.h | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
dissect.c +1 -1
index 9419c593..a6003afa 100644
@@ -454,7 +454,7 @@ again:
 			p_mode = U_R_VAL;
 		p_type = do_expression(p_mode, expr->deref);
 
-		ret = report_member(mode, &expr->pos, p_type,
+		ret = report_member(mode, &expr->m_pos, p_type,
 			lookup_member(p_type, expr->member, NULL));
 	}
 
expression.c +1 -0
index 727e7056..b23107da 100644
@@ -605,6 +605,7 @@ static struct token *postfix_expression(struct token *token, struct expression *
 				break;
 			}
 			deref->member = token->ident;
+			deref->m_pos = token->pos;
 			token = token->next;
 			expr = deref;
 			continue;
expression.h +1 -0
index 8bf40d32..ce8a29ce 100644
@@ -202,6 +202,7 @@ struct expression {
 		struct /* deref_arg */ {
 			struct expression *deref;
 			struct ident *member;
+			struct position m_pos;
 		};
 		// EXPR_SLICE
 		struct /* slice */ {
Signature · 1 line
2.52.0
maintainer Applied
#1 lore raw

Hi Oleg,

Applied to sparse-dev.

Chris

Chris

Quoted 91 lines · Oleg Nesterov

Test-case:

        $ cat -n MEMPOS.c
             1  struct T {
             2   int mem;
             3  } X;
             4
             5  void func(struct T *x)
             6  {
             7   x -> mem = 1;
             8   X .  mem = 2;
             9  }
        $ ./test-dissect MEMPOS.c | grep -F T.mem
           2:6                    def   m T.mem                            int
           7:4   func             -w-   m T.mem                            int
           8:4   func             -w-   m T.mem                            int

Note that the reported position of .mem usage is wrong. This is because do_expression(EXPR_DEREF) uses &expr->pos which is position of TOKEN_SPECIAL, not the position of the next token (mem).

This also breaks "semind search -l" (search by location)

        $ ./semind add MEMPOS.c
        $ ./semind search -l MEMPOS.c:7:7

reports nothing.

With this patch:

        $ ./test-dissect MEMPOS.c | grep -F T.mem
           2:6                    def   m T.mem                            int
           7:7   func             -w-   m T.mem                            int
           8:7   func             -w-   m T.mem                            int
        $ ./semind add MEMPOS.c
        $ ./semind search -l MEMPOS.c:7:7
        (def) MEMPOS.c  2       6                int mem;
        (-w-) MEMPOS.c  7       7       func     x -> mem = 1;
        (-w-) MEMPOS.c  8       7       func     X .  mem = 2;
---
 dissect.c    | 2 +-
 expression.c | 1 +
 expression.h | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
dissect.c +1 -1
index 9419c593..a6003afa 100644
@@ -454,7 +454,7 @@ again:
                        p_mode = U_R_VAL;
                p_type = do_expression(p_mode, expr->deref);
 
-               ret = report_member(mode, &expr->pos, p_type,
+               ret = report_member(mode, &expr->m_pos, p_type,
                        lookup_member(p_type, expr->member, NULL));
        }
 
expression.c +1 -0
index 727e7056..b23107da 100644
@@ -605,6 +605,7 @@ static struct token *postfix_expression(struct token *token, struct expression *
                                break;
                        }
                        deref->member = token->ident;
+                       deref->m_pos = token->pos;
                        token = token->next;
                        expr = deref;
                        continue;
expression.h +1 -1
index 8bf40d32..ce8a29ce 100644
@@ -202,6 +202,7 @@ struct expression {
                struct /* deref_arg */ {
                        struct expression *deref;
                        struct ident *member;
+                       struct position m_pos;
                };
                // EXPR_SLICE
                struct /* slice */ {
--

2.52.0