On Tue, 14 Feb 2012, Terry Wilson wrote:

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

Could you send some C code that causes the problem (grinding to a
halt)?
Intuitively the above looks fine...  You can use the option
-show_trying
to find out what thing it is working on.  That only gives function
names,
unfortunately, but it should help you narrow it down.

It looks to be an algorithmic issue. With a short struct, no problem. But add a 
lot of fields, and it takes much longer each addition. Moving the #if 0 after 
l, then m, then n was instructive (9s vs 36s, vs 160s seems rather factorial). 
The struct I'm currently working on has about 80 fields, so that would explain 
why it was never finishing. :-)

struct thing {
   int a;
   int b;
   int c;
   int d;
   int e;
   int f;
   int g;
   int h;
   int i;
   int j;
   int k;
   int l;
#if 0
   int m;
   int n;
   int o;
   int p;
   int q;
   int r;
   int s;
   int t;
   int u;
   int v;
   int w;
   int x;
   int y;
   int z;
#endif
};

OK, thanks for the example. I will look into it. I'm surprised that type only works but identifier + type does not. I would think that type only would not work either.

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.

I'm not sure to understand the problem.  You want to specify the type
of
thing, I guess, not thing->something?

Yes, I'm moving away from directly accessing the struct fields to using 
accessors. The accessors for the different types end up being very similar, so 
just doing the types together works very well. I can go through and find the 
names of all the types and limit by regex on the names of the identifiers, it 
would just be easier if I could specify the type--certainly not that big of a 
deal though.

Probably if the rule is not working
it is just because it doesn't know the type of thing, because that is
in a
header file that is not getting included.  You could try
-all_includes or
-recursive_includes.  Or add more -I options.  You can see where
types are
known by running spatch -type_c and your C file.

Actually, doing
@@
struct thing *t;
int i;
@@

- thing->i

yields the following syntax error because I guess it doesn't think of "int i" 
as an identifier?:

Fatal error: exception Failure("minus: parse error:
= File "test2.cocci", line 6, column 9,  charpos = 40
   around = 'i', whole content = - thing->i
")

OK, I see now. The name of a field has to be an identifier. Maybe today I will release a new version that will solve your problem :). You will be able to say:

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

thing ->@E i

Currently there is no easy way to do what you want.

julia
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to