Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Julia Lawall <julia.lawall@inria.fr>
To: Palani kodeswaran <palani.k@gmail.com>
Cc: cocci@inria.fr
Subject: Re: [cocci] Replacing struct field references with field references from a different struct
Date: Mon, 8 Aug 2022 13:56:23 +0200 (CEST)	[thread overview]
Message-ID: <alpine.DEB.2.22.394.2208081353080.2707@hadrien> (raw)
In-Reply-To: <CAASoEDTjytDuj6quFJpmhcppBZxEUXpTOSk5eZDhS8gUm6hAzA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2150 bytes --]



On Mon, 8 Aug 2022, Palani kodeswaran wrote:

> Hello,
> I am trying to replace field references inside a struct of given type with
> references from a different struct (that is passed as an input parameter for
> a function).  I have the following coccinelle rule that is not working
>
> @vlan@    
> identifier p,c,fn;
> type t;          
> struct vlan_hdr *v;
> @@                
> t fn(struct xdp_md *ctx){
> ...                      
> - v->h_vlan_encapsulated_proto
> + ctx->vlan_proto            
> }

I thik that you want to do the replacement everywhere that it is relevant
in the function body.  The above rule (sort of) says that it should only
be done at the end of the function.  So:

@vlan@    
identifier p,c,fn;
type t;          
struct vlan_hdr *v;
@@                
t fn(struct xdp_md *ctx){
<...                      
- v->h_vlan_encapsulated_proto
+ ctx->vlan_proto            
...>
}

I would suggest doing the same for your other rule.  The other rule your
wrote only works when there is exactly one occurrence of the changed thing
on every execution path.  This is because ... implicitly checks that there
is no occurrence of the thing before or after the ... in the matched
region.  <... ...> says do the change whereever the matched thing occurs.
Including nowhere.

julia


>
> However, I have a similar rule that works for another struct type.  I am not
> sure what I am missing in the above rule compared to the one below. The only
> difference I can see in the source code is that the above rule needs to
> transform a piece of code inside an if block. I believe the three ellipsis
> (...) would handle that anyways?
>
> @rule3@
> identifier p,c,fn;                                                         
>                                                     type t;
> struct ethhdr *e;
> @@              
> t fn(struct xdp_md *ctx){
> ...                      
> - e->h_proto            
> + ctx->protocol
> ...
> }
>
> Appreciate your help.
>
> Regards,
> Palani.
>
>

      reply	other threads:[~2022-08-08 11:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-08 10:36 [cocci] Replacing struct field references with field references from a different struct Palani kodeswaran
2022-08-08 11:56 ` Julia Lawall [this message]

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=alpine.DEB.2.22.394.2208081353080.2707@hadrien \
    --to=julia.lawall@inria.fr \
    --cc=cocci@inria.fr \
    --cc=palani.k@gmail.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).