All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] Adding a line to a function after all variable declarations
@ 2021-05-27 10:14 Fuad Tabba
  2021-05-27 10:25 ` Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Fuad Tabba @ 2021-05-27 10:14 UTC (permalink / raw)
  To: cocci

Hi,

I'm trying to write a semantic patch that would add a new line to a
function, but would like that line to come after all local variables
(if any) have been declared. For example (distilled from what I am
actually trying to accomplish):

@@
identifier f, s;
@@
f(..., struct mystruct *s, ...) {
/* after any variable declarations*/
+ x = s->x;
 ...
 }

I would like it to patch as follows:

int function(struct mystruct *s) {
+ x = s->x;
return 0;
}

as well as

int function(struct mystruct *s)
{
int y;
+ x = s->x;
return 0;
}

or

int function(struct mystruct *s)
{
int y, z = 10;
float f;
+ x = s->x;
return 0;
}

Any suggestions on how I could do that?

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

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 10:14 [Cocci] Adding a line to a function after all variable declarations Fuad Tabba
@ 2021-05-27 10:25 ` Julia Lawall
  2021-05-27 10:26   ` Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2021-05-27 10:25 UTC (permalink / raw)
  To: Fuad Tabba; +Cc: cocci



On Thu, 27 May 2021, Fuad Tabba wrote:

> Hi,
>
> I'm trying to write a semantic patch that would add a new line to a
> function, but would like that line to come after all local variables
> (if any) have been declared. For example (distilled from what I am
> actually trying to accomplish):
>
> @@
> identifier f, s;
> @@
> f(..., struct mystruct *s, ...) {
> /* after any variable declarations*/
> + x = s->x;
>  ...
>  }
>
> I would like it to patch as follows:
>
> int function(struct mystruct *s) {
> + x = s->x;
> return 0;
> }
>
> as well as
>
> int function(struct mystruct *s)
> {
> int y;
> + x = s->x;
> return 0;
> }
>
> or
>
> int function(struct mystruct *s)
> {
> int y, z = 10;
> float f;
> + x = s->x;
> return 0;
> }
>
> Any suggestions on how I could do that?

@@
@@

f(...) {
... when != S
+ new_code
S
... when any
}

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

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 10:25 ` Julia Lawall
@ 2021-05-27 10:26   ` Julia Lawall
  2021-05-27 10:55     ` Fuad Tabba
  0 siblings, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2021-05-27 10:26 UTC (permalink / raw)
  To: Fuad Tabba; +Cc: cocci



On Thu, 27 May 2021, Julia Lawall wrote:

>
>
> On Thu, 27 May 2021, Fuad Tabba wrote:
>
> > Hi,
> >
> > I'm trying to write a semantic patch that would add a new line to a
> > function, but would like that line to come after all local variables
> > (if any) have been declared. For example (distilled from what I am
> > actually trying to accomplish):
> >
> > @@
> > identifier f, s;
> > @@
> > f(..., struct mystruct *s, ...) {
> > /* after any variable declarations*/
> > + x = s->x;
> >  ...
> >  }
> >
> > I would like it to patch as follows:
> >
> > int function(struct mystruct *s) {
> > + x = s->x;
> > return 0;
> > }
> >
> > as well as
> >
> > int function(struct mystruct *s)
> > {
> > int y;
> > + x = s->x;
> > return 0;
> > }
> >
> > or
> >
> > int function(struct mystruct *s)
> > {
> > int y, z = 10;
> > float f;
> > + x = s->x;
> > return 0;
> > }
> >
> > Any suggestions on how I could do that?
>
> @@
> @@
>
> f(...) {
> ... when != S
> + new_code
> S
> ... when any
> }

Sorry, that was not quite right.  The first S should be S1 and the second
S should be S2, ie they should be different.  S1 and S2 should be
statement metavariables and f should be an identifier metavariable.

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

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 10:26   ` Julia Lawall
@ 2021-05-27 10:55     ` Fuad Tabba
  2021-05-27 11:30       ` Fuad Tabba
  2021-05-27 11:44       ` Julia Lawall
  0 siblings, 2 replies; 8+ messages in thread
From: Fuad Tabba @ 2021-05-27 10:55 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Thu, May 27, 2021 at 11:26 AM Julia Lawall <julia.lawall@inria.fr> wrote:
>
>
>
> On Thu, 27 May 2021, Julia Lawall wrote:
>
> >
> >
> > On Thu, 27 May 2021, Fuad Tabba wrote:
> >
> > > Hi,
> > >
> > > I'm trying to write a semantic patch that would add a new line to a
> > > function, but would like that line to come after all local variables
> > > (if any) have been declared. For example (distilled from what I am
> > > actually trying to accomplish):
> > >
> > > @@
> > > identifier f, s;
> > > @@
> > > f(..., struct mystruct *s, ...) {
> > > /* after any variable declarations*/
> > > + x = s->x;
> > >  ...
> > >  }
> > >
> > > I would like it to patch as follows:
> > >
> > > int function(struct mystruct *s) {
> > > + x = s->x;
> > > return 0;
> > > }
> > >
> > > as well as
> > >
> > > int function(struct mystruct *s)
> > > {
> > > int y;
> > > + x = s->x;
> > > return 0;
> > > }
> > >
> > > or
> > >
> > > int function(struct mystruct *s)
> > > {
> > > int y, z = 10;
> > > float f;
> > > + x = s->x;
> > > return 0;
> > > }
> > >
> > > Any suggestions on how I could do that?
> >
> > @@
> > @@
> >
> > f(...) {
> > ... when != S
> > + new_code
> > S
> > ... when any
> > }
>
> Sorry, that was not quite right.  The first S should be S1 and the second
> S should be S2, ie they should be different.  S1 and S2 should be
> statement metavariables and f should be an identifier metavariable.

Thanks Julia. That works for my distilled example, but teaches me a
lesson not to assume that the parts I've omitted aren't relevant. :)
Here's the actual semantic patch I'm trying to write, and I can't get
that quite to work:

@@
expression a, b;
identifier vcpu;
fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
iterator name kvm_for_each_vcpu;
identifier fc;
@@
kvm_for_each_vcpu(a, vcpu, b)
 {
/* hop over any declarations */
+ vcpu_ctxt = &vcpu->arch.ctxt;
<+...
fc(..., vcpu, ...)
...+>
 }

So I'm trying to add the line "vcpu_ctxt = &vcpu->arch.ctxt" after any
declarations, only if there's a function in that block that's using
vcpu. This works, but the vcpu_ctxt assignment is of course always
added first, before any declarations.

Doing the following partially works, but it of course misses the case
where the function call with vcpu comes immediately after the
vcpu_ctxt assignment. Removing S2 altogether gives me a parse error.

@@
expression a, b;
identifier vcpu;
fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
iterator name kvm_for_each_vcpu;
identifier fc;
statement S1, S2;
@@
kvm_for_each_vcpu(a, vcpu, b)
 {
 ... when != S1
+ vcpu_ctxt = &vcpu->arch.ctxt;
 S2;
<+...
fc(..., vcpu, ...)
...+>
 }

Thanks again,
/fuad

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

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 10:55     ` Fuad Tabba
@ 2021-05-27 11:30       ` Fuad Tabba
  2021-05-27 11:44       ` Julia Lawall
  1 sibling, 0 replies; 8+ messages in thread
From: Fuad Tabba @ 2021-05-27 11:30 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Thu, May 27, 2021 at 11:55 AM Fuad Tabba <tabba@google.com> wrote:
>
> On Thu, May 27, 2021 at 11:26 AM Julia Lawall <julia.lawall@inria.fr> wrote:
> >
> >
> >
> > On Thu, 27 May 2021, Julia Lawall wrote:
> >
> > >
> > >
> > > On Thu, 27 May 2021, Fuad Tabba wrote:
> > >
> > > > Hi,
> > > >
> > > > I'm trying to write a semantic patch that would add a new line to a
> > > > function, but would like that line to come after all local variables
> > > > (if any) have been declared. For example (distilled from what I am
> > > > actually trying to accomplish):
> > > >
> > > > @@
> > > > identifier f, s;
> > > > @@
> > > > f(..., struct mystruct *s, ...) {
> > > > /* after any variable declarations*/
> > > > + x = s->x;
> > > >  ...
> > > >  }
> > > >
> > > > I would like it to patch as follows:
> > > >
> > > > int function(struct mystruct *s) {
> > > > + x = s->x;
> > > > return 0;
> > > > }
> > > >
> > > > as well as
> > > >
> > > > int function(struct mystruct *s)
> > > > {
> > > > int y;
> > > > + x = s->x;
> > > > return 0;
> > > > }
> > > >
> > > > or
> > > >
> > > > int function(struct mystruct *s)
> > > > {
> > > > int y, z = 10;
> > > > float f;
> > > > + x = s->x;
> > > > return 0;
> > > > }
> > > >
> > > > Any suggestions on how I could do that?
> > >
> > > @@
> > > @@
> > >
> > > f(...) {
> > > ... when != S
> > > + new_code
> > > S
> > > ... when any
> > > }
> >
> > Sorry, that was not quite right.  The first S should be S1 and the second
> > S should be S2, ie they should be different.  S1 and S2 should be
> > statement metavariables and f should be an identifier metavariable.
>
> Thanks Julia. That works for my distilled example, but teaches me a
> lesson not to assume that the parts I've omitted aren't relevant. :)
> Here's the actual semantic patch I'm trying to write, and I can't get
> that quite to work:
>
> @@
> expression a, b;
> identifier vcpu;
> fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> iterator name kvm_for_each_vcpu;
> identifier fc;
> @@
> kvm_for_each_vcpu(a, vcpu, b)
>  {
> /* hop over any declarations */
> + vcpu_ctxt = &vcpu->arch.ctxt;
> <+...
> fc(..., vcpu, ...)
> ...+>
>  }
>

I managed to get it done by doing it in two steps; first using the
original semantic patch I had, then using yours essentially to move
the line I added.

Thanks again!
/fuad
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 10:55     ` Fuad Tabba
  2021-05-27 11:30       ` Fuad Tabba
@ 2021-05-27 11:44       ` Julia Lawall
  2021-05-27 13:44         ` Fuad Tabba
  1 sibling, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2021-05-27 11:44 UTC (permalink / raw)
  To: Fuad Tabba; +Cc: cocci

> @@
> expression a, b;
> identifier vcpu;
> fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> iterator name kvm_for_each_vcpu;
> identifier fc;
> @@
> kvm_for_each_vcpu(a, vcpu, b)
>  {
> /* hop over any declarations */
> + vcpu_ctxt = &vcpu->arch.ctxt;
> <+...
> fc(..., vcpu, ...)
> ...+>
>  }
>
> So I'm trying to add the line "vcpu_ctxt = &vcpu->arch.ctxt" after any
> declarations, only if there's a function in that block that's using
> vcpu. This works, but the vcpu_ctxt assignment is of course always
> added first, before any declarations.
>
> Doing the following partially works, but it of course misses the case
> where the function call with vcpu comes immediately after the
> vcpu_ctxt assignment. Removing S2 altogether gives me a parse error.
>
> @@
> expression a, b;
> identifier vcpu;
> fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> iterator name kvm_for_each_vcpu;
> identifier fc;
> statement S1, S2;
> @@
> kvm_for_each_vcpu(a, vcpu, b)
>  {
>  ... when != S1
> + vcpu_ctxt = &vcpu->arch.ctxt;
>  S2;
> <+...
> fc(..., vcpu, ...)
> ...+>
>  }

@@
identifier f;
statement S1,S2;
@@

f(...) {
... when != S1
(
+ new_code
S2
... when any
&
<+...
fc(..., vcpu, ...)
...+>
)
}

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

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 11:44       ` Julia Lawall
@ 2021-05-27 13:44         ` Fuad Tabba
  2021-05-27 13:50           ` Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Fuad Tabba @ 2021-05-27 13:44 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

On Thu, May 27, 2021 at 12:44 PM Julia Lawall <julia.lawall@inria.fr> wrote:
>
> > @@
> > expression a, b;
> > identifier vcpu;
> > fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> > iterator name kvm_for_each_vcpu;
> > identifier fc;
> > @@
> > kvm_for_each_vcpu(a, vcpu, b)
> >  {
> > /* hop over any declarations */
> > + vcpu_ctxt = &vcpu->arch.ctxt;
> > <+...
> > fc(..., vcpu, ...)
> > ...+>
> >  }
> >
> > So I'm trying to add the line "vcpu_ctxt = &vcpu->arch.ctxt" after any
> > declarations, only if there's a function in that block that's using
> > vcpu. This works, but the vcpu_ctxt assignment is of course always
> > added first, before any declarations.
> >
> > Doing the following partially works, but it of course misses the case
> > where the function call with vcpu comes immediately after the
> > vcpu_ctxt assignment. Removing S2 altogether gives me a parse error.
> >
> > @@
> > expression a, b;
> > identifier vcpu;
> > fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> > iterator name kvm_for_each_vcpu;
> > identifier fc;
> > statement S1, S2;
> > @@
> > kvm_for_each_vcpu(a, vcpu, b)
> >  {
> >  ... when != S1
> > + vcpu_ctxt = &vcpu->arch.ctxt;
> >  S2;
> > <+...
> > fc(..., vcpu, ...)
> > ...+>
> >  }
>
> @@
> identifier f;
> statement S1,S2;
> @@
>
> f(...) {
> ... when != S1
> (
> + new_code
> S2
> ... when any
> &
> <+...
> fc(..., vcpu, ...)
> ...+>
> )
> }

I get a parse error when I try that (both copying it verbatim, or
adapting it to my code):

minus: parse error:
  File "test.cocci", line 18, column 0, charpos = 209
  around = '<+...',
  whole content = <+...

Thanks!
/fuad
>
> julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Adding a line to a function after all variable declarations
  2021-05-27 13:44         ` Fuad Tabba
@ 2021-05-27 13:50           ` Julia Lawall
  0 siblings, 0 replies; 8+ messages in thread
From: Julia Lawall @ 2021-05-27 13:50 UTC (permalink / raw)
  To: Fuad Tabba; +Cc: cocci



On Thu, 27 May 2021, Fuad Tabba wrote:

> On Thu, May 27, 2021 at 12:44 PM Julia Lawall <julia.lawall@inria.fr> wrote:
> >
> > > @@
> > > expression a, b;
> > > identifier vcpu;
> > > fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> > > iterator name kvm_for_each_vcpu;
> > > identifier fc;
> > > @@
> > > kvm_for_each_vcpu(a, vcpu, b)
> > >  {
> > > /* hop over any declarations */
> > > + vcpu_ctxt = &vcpu->arch.ctxt;
> > > <+...
> > > fc(..., vcpu, ...)
> > > ...+>
> > >  }
> > >
> > > So I'm trying to add the line "vcpu_ctxt = &vcpu->arch.ctxt" after any
> > > declarations, only if there's a function in that block that's using
> > > vcpu. This works, but the vcpu_ctxt assignment is of course always
> > > added first, before any declarations.
> > >
> > > Doing the following partially works, but it of course misses the case
> > > where the function call with vcpu comes immediately after the
> > > vcpu_ctxt assignment. Removing S2 altogether gives me a parse error.
> > >
> > > @@
> > > expression a, b;
> > > identifier vcpu;
> > > fresh identifier vcpu_ctxt = vcpu ## "_ctxt";
> > > iterator name kvm_for_each_vcpu;
> > > identifier fc;
> > > statement S1, S2;
> > > @@
> > > kvm_for_each_vcpu(a, vcpu, b)
> > >  {
> > >  ... when != S1
> > > + vcpu_ctxt = &vcpu->arch.ctxt;
> > >  S2;
> > > <+...
> > > fc(..., vcpu, ...)
> > > ...+>
> > >  }
> >
> > @@
> > identifier f;
> > statement S1,S2;
> > @@
> >
> > f(...) {
> > ... when != S1
> > (
> > + new_code
> > S2
> > ... when any
> > &
> > <+...
> > fc(..., vcpu, ...)
> > ...+>
> > )
> > }
>
> I get a parse error when I try that (both copying it verbatim, or
> adapting it to my code):
>
> minus: parse error:
>   File "test.cocci", line 18, column 0, charpos = 209
>   around = '<+...',
>   whole content = <+...

OK, perhaps that is a corner that is not supported.  It might work bettwe
with

... when any
    when exists
fc(..., vcpu, ...)
... when any

in place of the <+... ...+>

But since you have a satisfactory solution, that is fine too.

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

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

end of thread, other threads:[~2021-05-27 13:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-27 10:14 [Cocci] Adding a line to a function after all variable declarations Fuad Tabba
2021-05-27 10:25 ` Julia Lawall
2021-05-27 10:26   ` Julia Lawall
2021-05-27 10:55     ` Fuad Tabba
2021-05-27 11:30       ` Fuad Tabba
2021-05-27 11:44       ` Julia Lawall
2021-05-27 13:44         ` Fuad Tabba
2021-05-27 13:50           ` Julia Lawall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.