Re: [PATCH] sparse: add the new m_pos member into struct expression{EXPR_DEREF}

Chris Li <[email protected]> Fri, 16 Jan 2026 14:55:34 -0800
Newsgroups org.kernel.vger.linux-sparse
Message-ID <CACePvbWfwsu_gEUqfg47joLpD=UmO5Rj7hyoJFgiLzv5LyRFCw@mail.gmail.com>
Hi Oleg,

Applied to sparse-dev.

Chris



Chris

On Tue, Dec 16, 2025 at 5:40 AM Oleg Nesterov <[email protected]> wrote:
>
> 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;
>
> Signed-off-by: Oleg Nesterov <[email protected]>
> ---
>  dissect.c    | 2 +-
>  expression.c | 1 +
>  expression.h | 1 +
>  3 files changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/dissect.c b/dissect.c
> index 9419c593..a6003afa 100644
> --- a/dissect.c
> +++ b/dissect.c
> @@ -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));
>         }
>
> diff --git a/expression.c b/expression.c
> index 727e7056..b23107da 100644
> --- a/expression.c
> +++ b/expression.c
> @@ -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;
> diff --git a/expression.h b/expression.h
> index 8bf40d32..ce8a29ce 100644
> --- a/expression.h
> +++ b/expression.h
> @@ -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
>
>