On Thu, May 27, 2021 at 11:55 AM Fuad Tabba <ta...@google.com> wrote: > > On Thu, May 27, 2021 at 11:26 AM Julia Lawall <julia.law...@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