[Cocci] Writing accessors

Terry Wilson twilson at digium.com
Tue Feb 14 18:55:36 CET 2012


Let's say I want to write a patch that adds accessor functions for all the fields in a struct. When I try to pass both a type and an identifier from one rule to a Python script rule, it seems that I end up in an infinite loop (or at least, 100% CPU usage that doesn't seem to go away).

What I tried to do was something like:

@s@
type t;
identifier x;
@@

struct thing {
...
t x;
...
};

@script:python p@
t << s.t;
x << s.x;
@@

print "type:", t, "identifier:", x

at which point things grind to a halt. Passing only the type or the identifier prints what I would expect, except type rule only hits once for each type while the identifier rule hits for each (yet, by their nature always unique) match (which, I assume means that I completely misunderstand what it is that coccinelle is doing!) Is there a way to do this that I've just missed?

Also, is there a way of matching in the code any expression involving a particular type of field of a struct? Say you have something like the following (which doesn't work):

@@
struct *thing;
int i;
expression E;
@@

- thing->i = E
+ thing_${i}_set(thing, E)

I can do the python re-writing of the function name without problem, but am having trouble figuring out how to just match thing->something when it evaluates to an int.

Thanks for helping out a coccinelle newbie.

Terry


More information about the Cocci mailing list