On Sun, 22 May 2011, Francis Galiegue wrote:

> Hello again,
> 
> Here is a semantic patch:
> 
> ----
> @r@
> identifer f;
> local idexpression victim;
> position p;
> @@
> 
> amfree@p(victim)
> ... when != victim
> 
> @script:python depends on r@
> victim << r.victim;
> p << r.p;
> @@
> 
> print "%s, line %s: local variable %s is amfree()d but isn't used
> anywhere else in the file" % (p[0].file, p[0].line, str(victim))
> ----
> 
> Now, I have one problem and one question:
> 
> - the problem is, it also prints the warning if "victim" is static,
> which I don't want: how do I tell it about that?

One might consider this to be a bug.  You can get around it by matching 
the static variables and then avoiding them.  You can see how to do this
below.

> - the question is, I also want to print the name of the enclosing
> function and where it is defined, how do I do that?

If you just want the function name, then a position variable has a field 
current_element that has that information.  If you want the line number 
etc of the start of the function then you have to match the function and 
put a position variable there.  This is done in the semantic patch below.  
when any means allow anything to appear in the ...s, including another 
call to amfree.  The default is to consider the shortest path between what 
is before and what is after, which would only give you the first call to 
amfree.  exists means consider each path through the function 
individually.  The default, unless you use * in the leftmost column for 
matching only, is forall, meaning that all control-flow paths have to 
satisfy the pattern.

julia

@bad@
position p;
identifier i;
type T;
@@

static T i;
<...
amfree@p(i)
...>

@r exists@
local idexpression victim;
position p!=bad.p;
identifier f;
position pf;
@@

f@pf(...) {
... when any
amfree@p(victim)
... when != victim
}

@script:python depends on r@
victim << r.victim;
p << r.p;
pf << r.pf;
f << r.f;
@@

print "%s, line %s: local variable %s is amfree()d but isn't used anywhere else 
in the file" % (p[0].file, p[0].line, str(victim)) 
print "problem in function %s starting on line %s" % (f,pf[0].line)
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to