Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Inserting code after specific set of declarations?
@ 2020-02-19 17:28 Ville Syrjälä
  2020-02-19 22:37 ` Julia Lawall
  0 siblings, 1 reply; 3+ messages in thread
From: Ville Syrjälä @ 2020-02-19 17:28 UTC (permalink / raw)
  To: cocci

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?

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

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

* Re: [Cocci] Inserting code after specific set of declarations?
  2020-02-19 17:28 [Cocci] Inserting code after specific set of declarations? Ville Syrjälä
@ 2020-02-19 22:37 ` Julia Lawall
  2020-02-20 13:27   ` Ville Syrjälä
  0 siblings, 1 reply; 3+ messages in thread
From: Julia Lawall @ 2020-02-19 22:37 UTC (permalink / raw)
  To: Ville Syrjälä; +Cc: cocci

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



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


On Wed, 19 Feb 2020, Ville Syrjälä wrote:

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

@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


>
> 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?
>
> --
> Ville Syrjälä
> Intel
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Inserting code after specific set of declarations?
  2020-02-19 22:37 ` Julia Lawall
@ 2020-02-20 13:27   ` Ville Syrjälä
  0 siblings, 0 replies; 3+ messages in thread
From: Ville Syrjälä @ 2020-02-20 13:27 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Wed, Feb 19, 2020 at 11:37:03PM +0100, Julia Lawall wrote:
> 
> 
> --- Please note the new email address ---
> 
> 
> On Wed, 19 Feb 2020, Ville Syrjälä wrote:
> 
> > 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.
> 
> @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.

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.

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

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

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

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-19 17:28 [Cocci] Inserting code after specific set of declarations? Ville Syrjälä
2020-02-19 22:37 ` Julia Lawall
2020-02-20 13:27   ` Ville Syrjälä

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git