You can do this in Coccinelle, by declaring a variable in the python code
that is inherited as an identifier but actually contains a number
represented as a string.

I get this part.

Currently, your python rule that increments count is being called once per
file.  I don't know if that is what you want.  If you want it to be once
per the occurrence of something, put a position variable on that thing and
interit the position variable in the python rule.  You don't have to use
it in the python rule for anyhting.

I want count to be incremented once per occurrence of a function call.
So I am now using a position variable.

The example in Coccinelle wiki (shown below)

@initialize:python@
count = 0

@r@
identifier f;
position p;
@@
f@p(3,...)

@script:python@
p << r.p;
@@
count = count + 1

@finalize:python@
print "%d" % (count)

I need the exact same thing with an addition, i.e. whenever I match the
function call (count is incremented) I need to add a variable declaration
above it

+ int x = (value of count)

I am not able to figure out how to use the value of count in this case.

Thanks
Ajay

On Tue, Jul 5, 2011 at 11:17 AM, Julia Lawall <[email protected]> wrote:

> On Tue, 5 Jul 2011, Ajay Panyala wrote:
>
> > Hello,
> >
> > I have a simple rule to add a variable declaration
> > whenever I see a function call with a certain name
> > as shown below.
> >
> > @initialize:python@
> > count = 0
> >
> > @@
> > identifier bCall ~= "^\(foo\|bar\|abc\)$"
> >
> > @@
> > + int x = 42;
> > bCall(...);
> >
> > @script:python@
> > @@
> > count = count + 1
> >
> >
> > @@
> > identifier cCall ~="^\(foo1\|bar1\|abc1\)$"
> >
> > @@
> > + int y = 42;
> > cCall(...);
> >
> > @script:python@
> > @@
> > count = count + 1
> >
> > Is it possible to add the variable declarations (x,y) in such a way
> > that they are initialized to *count *rather than 42 ?
>
> You can do this in Coccinelle, by declaring a variable in the python code
> that is inherited as an identifier but actually contains a number
> represented as a string.
>
> Look at demos/pythontococci.cocci to see how to declare variables in
> python code.
>
> Currently, your python rule that increments count is being called once per
> file.  I don't know if that is what you want.  If you want it to be once
> per the occurrence of something, put a position variable on that thing and
> interit the position variable in the python rule.  You don't have to use
> it in the python rule for anyhting.
>
> julia
>
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to