cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] Replacing struct field references with field references from a different struct
@ 2022-08-08 10:36 Palani kodeswaran
  2022-08-08 11:56 ` Julia Lawall
  0 siblings, 1 reply; 2+ messages in thread
From: Palani kodeswaran @ 2022-08-08 10:36 UTC (permalink / raw)
  To: cocci

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

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
}

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.

[-- Attachment #2: Type: text/html, Size: 1360 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [cocci] Replacing struct field references with field references from a different struct
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Julia Lawall @ 2022-08-08 11:56 UTC (permalink / raw)
  To: Palani kodeswaran; +Cc: cocci

[-- 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.
>
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-08 11:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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 NNTP newsgroup(s).