Skip to contentSkip to conversation

Inserting code after specific set of declarations?

Cocci Ville Syrjälä 2 participants 3 messages Feb 19, 2020 – Mar 20, 2020

lore
Share & tools
lore
#0 lore raw

Hi all,

I was trying to do the following transformation:
@@
identifier M;
expression E;
@@
- struct foo M = E;
+ struct foo M;
+ copy_struct(&M, &E);

but without inserting the function call in the middle of the declarations and instead pushing it past them.

This is one attempt:
@decl@
identifier M;
expression E;
@@
- struct foo M = E;
+ struct foo M;

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S;
@@
struct foo M;
...
D
+ copy_struct(&M, &E);
S

The copy rule fails to match anything when I have != 1 declarations after the struct. So the ... doesn't seem to eat the extra declarations for some reason.

Also tried some other tricks with <... ...> but that just resulted in the code being inserted into every if block in the function. Not what I wanted.

I guess what I might need is something along the lines of:
@copy@
...
declaration list[N] D;
@@
struct foo M;
D
+ copy_struct(&M, &E);
S

but that's not supported it seems.

Is there any way to achieve this atm?

Signature · 6 lines
Ville Syrjälä
Intel
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#1 lore raw

--- Please note the new email address ---

Quoted 34 lines · Ville Syrjälä

Hi all,

I was trying to do the following transformation:
@@
identifier M;
expression E;
@@
- struct foo M = E;
+ struct foo M;
+ copy_struct(&M, &E);

but without inserting the function call in the middle of the declarations and instead pushing it past them.

This is one attempt:
@decl@
identifier M;
expression E;
@@
- struct foo M = E;
+ struct foo M;

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S;
@@
struct foo M;
...
D
+ copy_struct(&M, &E);
S

Quoted 3 lines

The copy rule fails to match anything when I have != 1 declarations after the struct. So the ... doesn't seem to eat the extra declarations for some reason.

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S;
@@
struct foo M;
... when any
D
+ copy_struct(&M, &E);
S

Otherwise, ... doesn't match code that includes what is before or after it, ie struct foo M or D.

Aternatively:

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S,S1;
@@
struct foo M;
... when != S1
+ copy_struct(&M, &E);
S

julia

Quoted 19 lines

Also tried some other tricks with <... ...> but that just resulted in the code being inserted into every if block in the function. Not what I wanted.

I guess what I might need is something along the lines of:
@copy@
...
declaration list[N] D;
@@
struct foo M;
D
+ copy_struct(&M, &E);
S

but that's not supported it seems.

Is there any way to achieve this atm?

Quoted 8 lines

--
Ville Syrjälä
Intel
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw
Quoted 57 lines · Julia Lawall

--- Please note the new email address ---

Quoted 34 lines · Ville Syrjälä

Hi all,

I was trying to do the following transformation:
@@
identifier M;
expression E;
@@
- struct foo M = E;
+ struct foo M;
+ copy_struct(&M, &E);

but without inserting the function call in the middle of the declarations and instead pushing it past them.

This is one attempt:
@decl@
identifier M;
expression E;
@@
- struct foo M = E;
+ struct foo M;

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S;
@@
struct foo M;
...
D
+ copy_struct(&M, &E);
S

Quoted 3 lines

The copy rule fails to match anything when I have != 1 declarations after the struct. So the ... doesn't seem to eat the extra declarations for some reason.

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S;
@@
struct foo M;
... when any
D
+ copy_struct(&M, &E);
S

Quoted 2 lines

Otherwise, ... doesn't match code that includes what is before or after it, ie struct foo M or D.

This one still inserts the code after all declarations in nested blocks. Also misses cases where there are no extra declarations. So not quite what I need.

Quoted 13 lines

Aternatively:

@copy@
identifier decl.M;
expression decl.E;
declaration D;
statement S,S1;
@@
struct foo M;
... when != S1
+ copy_struct(&M, &E);
S

This on the other hand seems to work very nicely. Thanks.

Earlier I tried to play around with some '... when !=' stuff, but this double statement trick did not occur to me.

Signature · 6 lines
Ville Syrjälä
Intel
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci