All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] Add code to functions that use a certain variable
@ 2015-04-02 16:33 ron minnich
  2015-04-02 18:27 ` Julia Lawall
  0 siblings, 1 reply; 7+ messages in thread
From: ron minnich @ 2015-04-02 16:33 UTC (permalink / raw)
  To: cocci

I have this typedef in Plan 9
typedef struct Mach Mach;

a global
Mach *m;

and functions:
a(...){
...
m->whatever = x;
...
}

I want to add some code after the {
+Mach *m=machp();

I.e. for any function that uses the "global" (it's not really that, it's a
bit more complex) I want to add a local variable that is initialized.

My efforts so far have failed badly.

ron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20150402/231fbe2c/attachment.html>

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

* [Cocci] Add code to functions that use a certain variable
  2015-04-02 16:33 [Cocci] Add code to functions that use a certain variable ron minnich
@ 2015-04-02 18:27 ` Julia Lawall
  2015-04-02 21:10   ` ron minnich
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Lawall @ 2015-04-02 18:27 UTC (permalink / raw)
  To: cocci

On Thu, 2 Apr 2015, ron minnich wrote:

> I have this typedef in Plan 9typedef struct Mach Mach;
> 
> a global?
> Mach *m;
> 
> and functions:
> a(...){
> ...
> m->whatever = x;
> ...
> }
> 
> I want to add some code after the {
> +Mach *m=machp();
> 
> I.e. for any function that uses the "global" (it's not really that, it's a
> bit more complex) I want to add a local variable that is initialized.
> 
> My efforts so far have failed badly.

Could you send an example of a badly failing effort?

thans,
julia

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

* [Cocci] Add code to functions that use a certain variable
  2015-04-02 18:27 ` Julia Lawall
@ 2015-04-02 21:10   ` ron minnich
  2015-04-02 21:22     ` Julia Lawall
  0 siblings, 1 reply; 7+ messages in thread
From: ron minnich @ 2015-04-02 21:10 UTC (permalink / raw)
  To: cocci

Here are two that work and one that fails, hopefully it will make more
sense.
poperror is a macro that uses m, so I know we need it there.

@@
function f;
@@
f(...){
+Mach *m = machp();
...
poperror();
}

Or I can add it to everything :-)
@@
function f;
@@
f(...){
+Mach *m = machp();
...
}

Here is one I can not get to work:
@@
typedef Mach;
Mach *m;
function f;
identifier x;
statement s;
@@
f(...){
+ Mach *m = machp();
...
x = m
...
}

The x = m could also be m->x, or if (m) s, and so on

thanks again

ron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20150402/31a16442/attachment.html>

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

* [Cocci] Add code to functions that use a certain variable
  2015-04-02 21:10   ` ron minnich
@ 2015-04-02 21:22     ` Julia Lawall
  2015-04-02 21:34       ` ron minnich
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Lawall @ 2015-04-02 21:22 UTC (permalink / raw)
  To: cocci

> f(...){
> + Mach *m = machp();
> ...
> x = m
> ...
> }

This requires there to be exactly one occurrence of x = m on every (non 
failing) execution path.

> 
> The x = m could also be m->x, or if (m) s, and so on

Do you just mean any reference to m at all?  Or do you mean any reference 
that is not an assignment to m, ie not m = x.

If you want to allow any kind of reference to m, you could do:

@r exists@
typedef Mach; // only needed once per semantic patch
idexpression Mach *m;
function f;
position p;
@@
f at p(...){
<+...
m
...+>
}

@@
identifier r.f;
position r.p;
idexpression Mach *r.m;
@@

f at p(...) {
++Mach *m;
...
}

There could be more than one Mach * typed variable in a function.  The ++ 
lets them all get added, but there is no way to know in what order they 
will appear.

Otherwise, the idea here is that if there ever exists an m in the 
function, then add the declaration to the function.  It might be possible 
to merge the two rules, as:

@r exists@
typedef Mach; // only needed once per semantic patch
idexpression Mach *m;
function f;
@@
f(...){
++ Mach *m;
<+...
m
...+>
}  

But that might give multiple declarations of the same variable, if it is 
used in more than one execution path.

Also, it might be possible to say global idexpression Mach *m.  I don't 
remember if that is in the current release or in the upcoming one.

julia

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

* [Cocci] Add code to functions that use a certain variable
  2015-04-02 21:22     ` Julia Lawall
@ 2015-04-02 21:34       ` ron minnich
  2015-04-02 21:40         ` Julia Lawall
  0 siblings, 1 reply; 7+ messages in thread
From: ron minnich @ 2015-04-02 21:34 UTC (permalink / raw)
  To: cocci

This is not working with my 1.0.0-rc21 on ubuntu, is there a version you'd
recommend?

Many thanks!

ron

On Thu, Apr 2, 2015 at 2:22 PM Julia Lawall <julia.lawall@lip6.fr> wrote:

> > f(...){
> > + Mach *m = machp();
> > ...
> > x = m
> > ...
> > }
>
> This requires there to be exactly one occurrence of x = m on every (non
> failing) execution path.
>
> >
> > The x = m could also be m->x, or if (m) s, and so on
>
> Do you just mean any reference to m at all?  Or do you mean any reference
> that is not an assignment to m, ie not m = x.
>
> If you want to allow any kind of reference to m, you could do:
>
> @r exists@
> typedef Mach; // only needed once per semantic patch
> idexpression Mach *m;
> function f;
> position p;
> @@
> f at p(...){
> <+...
> m
> ...+>
> }
>
> @@
> identifier r.f;
> position r.p;
> idexpression Mach *r.m;
> @@
>
> f at p(...) {
> ++Mach *m;
> ...
> }
>
> There could be more than one Mach * typed variable in a function.  The ++
> lets them all get added, but there is no way to know in what order they
> will appear.
>
> Otherwise, the idea here is that if there ever exists an m in the
> function, then add the declaration to the function.  It might be possible
> to merge the two rules, as:
>
> @r exists@
> typedef Mach; // only needed once per semantic patch
> idexpression Mach *m;
> function f;
> @@
> f(...){
> ++ Mach *m;
> <+...
> m
> ...+>
> }
>
> But that might give multiple declarations of the same variable, if it is
> used in more than one execution path.
>
> Also, it might be possible to say global idexpression Mach *m.  I don't
> remember if that is in the current release or in the upcoming one.
>
> julia
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20150402/40d350c4/attachment.html>

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

* [Cocci] Add code to functions that use a certain variable
  2015-04-02 21:34       ` ron minnich
@ 2015-04-02 21:40         ` Julia Lawall
  2015-04-07  3:13           ` ron minnich
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Lawall @ 2015-04-02 21:40 UTC (permalink / raw)
  To: cocci



On Thu, 2 Apr 2015, ron minnich wrote:

> This is not working with my 1.0.0-rc21 on ubuntu, is there a version you'd
> recommend?

The current release is rc24, but I don't think there is anything relevant 
that is new, except the global idea.  Does it crash, or just not find 
anything?  Could you send the exact semantic patch and some representative 
C code?

thanks,
julia

> 
> Many thanks!
> 
> ron
> 
> On Thu, Apr 2, 2015 at 2:22 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
>       > f(...){
>       > + Mach *m = machp();
>       > ...
>       > x = m
>       > ...
>       > }
> 
>       This requires there to be exactly one occurrence of x = m on
>       every (non
>       failing) execution path.
> 
>       >
>       > The x = m could also be m->x, or if (m) s, and so on
> 
>       Do you just mean any reference to m at all?? Or do you mean any
>       reference
>       that is not an assignment to m, ie not m = x.
> 
>       If you want to allow any kind of reference to m, you could do:
> 
>       @r exists@
>       typedef Mach; // only needed once per semantic patch
>       idexpression Mach *m;
>       function f;
>       position p;
>       @@
>       f at p(...){
>       <+...
>       m
>       ...+>
>       }
> 
>       @@
>       identifier r.f;
>       position r.p;
>       idexpression Mach *r.m;
>       @@
> 
>       f at p(...) {
>       ++Mach *m;
>       ...
>       }
> 
>       There could be more than one Mach * typed variable in a
>       function.? The ++
>       lets them all get added, but there is no way to know in what
>       order they
>       will appear.
> 
>       Otherwise, the idea here is that if there ever exists an m in
>       the
>       function, then add the declaration to the function.? It might be
>       possible
>       to merge the two rules, as:
> 
>       @r exists@
>       typedef Mach; // only needed once per semantic patch
>       idexpression Mach *m;
>       function f;
>       @@
>       f(...){
>       ++ Mach *m;
>       <+...
>       m
>       ...+>
>       }
> 
>       But that might give multiple declarations of the same variable,
>       if it is
>       used in more than one execution path.
> 
>       Also, it might be possible to say global idexpression Mach *m.?
>       I don't
>       remember if that is in the current release or in the upcoming
>       one.
> 
>       julia
> 
> 
> 

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

* [Cocci] Add code to functions that use a certain variable
  2015-04-02 21:40         ` Julia Lawall
@ 2015-04-07  3:13           ` ron minnich
  0 siblings, 0 replies; 7+ messages in thread
From: ron minnich @ 2015-04-07  3:13 UTC (permalink / raw)
  To: cocci

OK, here is an example .c, a sample .h, and a script that works part of the
time.

I can spatch --all-includes -I . --sp-file <code in this email> x.c and it
gets 3 and fails 3.

I've boiled this down as much as I can from plan 9 code. BTW, the position
stuff would not work at all for me.

.c

#include "x.h"

int x(int a)
{
if (m)
return 0;
poperror();
return m->x;
}

int y(int a)
{
if (m)
return 1;
return 0;
}

void * z(int a)
{
x = m;
return &x[1];
}

int w(int a)
{
poperror();
return 1;
}

int ww(int a)
{
if (a) {
if (waserror()) {
nexterror();
}
poperror();
}
return 1;
}

.h
#define NULL (void *)0
struct Mach {
int x;
};

typedef struct Mach Mach;

Mach *m;
#define poperror() m = NULL

.cocci
@r exists@
typedef Mach; // only needed once per semantic patch
idexpression Mach *m;
function f;
@@
f(...){
<+...
m
...+>
}

@@
function r.f;
@@

f(...) {
++ Mach *m = machp();
...
}



On Thu, Apr 2, 2015 at 2:40 PM Julia Lawall <julia.lawall@lip6.fr> wrote:

>
>
> On Thu, 2 Apr 2015, ron minnich wrote:
>
> > This is not working with my 1.0.0-rc21 on ubuntu, is there a version
> you'd
> > recommend?
>
> The current release is rc24, but I don't think there is anything relevant
> that is new, except the global idea.  Does it crash, or just not find
> anything?  Could you send the exact semantic patch and some representative
> C code?
>
> thanks,
> julia
>
> >
> > Many thanks!
> >
> > ron
> >
> > On Thu, Apr 2, 2015 at 2:22 PM Julia Lawall <julia.lawall@lip6.fr>
> wrote:
> >       > f(...){
> >       > + Mach *m = machp();
> >       > ...
> >       > x = m
> >       > ...
> >       > }
> >
> >       This requires there to be exactly one occurrence of x = m on
> >       every (non
> >       failing) execution path.
> >
> >       >
> >       > The x = m could also be m->x, or if (m) s, and so on
> >
> >       Do you just mean any reference to m at all?  Or do you mean any
> >       reference
> >       that is not an assignment to m, ie not m = x.
> >
> >       If you want to allow any kind of reference to m, you could do:
> >
> >       @r exists@
> >       typedef Mach; // only needed once per semantic patch
> >       idexpression Mach *m;
> >       function f;
> >       position p;
> >       @@
> >       f at p(...){
> >       <+...
> >       m
> >       ...+>
> >       }
> >
> >       @@
> >       identifier r.f;
> >       position r.p;
> >       idexpression Mach *r.m;
> >       @@
> >
> >       f at p(...) {
> >       ++Mach *m;
> >       ...
> >       }
> >
> >       There could be more than one Mach * typed variable in a
> >       function.  The ++
> >       lets them all get added, but there is no way to know in what
> >       order they
> >       will appear.
> >
> >       Otherwise, the idea here is that if there ever exists an m in
> >       the
> >       function, then add the declaration to the function.  It might be
> >       possible
> >       to merge the two rules, as:
> >
> >       @r exists@
> >       typedef Mach; // only needed once per semantic patch
> >       idexpression Mach *m;
> >       function f;
> >       @@
> >       f(...){
> >       ++ Mach *m;
> >       <+...
> >       m
> >       ...+>
> >       }
> >
> >       But that might give multiple declarations of the same variable,
> >       if it is
> >       used in more than one execution path.
> >
> >       Also, it might be possible to say global idexpression Mach *m.
> >       I don't
> >       remember if that is in the current release or in the upcoming
> >       one.
> >
> >       julia
> >
> >
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20150407/47fef8d6/attachment.html>

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

end of thread, other threads:[~2015-04-07  3:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-02 16:33 [Cocci] Add code to functions that use a certain variable ron minnich
2015-04-02 18:27 ` Julia Lawall
2015-04-02 21:10   ` ron minnich
2015-04-02 21:22     ` Julia Lawall
2015-04-02 21:34       ` ron minnich
2015-04-02 21:40         ` Julia Lawall
2015-04-07  3:13           ` ron minnich

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.