It can be useful to define a python function, which you can put under 
@initialize:pyt...@.  Then even though you need a separate rule fo each 
case, you can juust call your python function, which is easier than 
duplicating the code.  You may also find it useful to put the 
@initialize:python@ code in a separate file, and then include it using 
#include

julia



On Fri, 18 Jun 2010, Eric Noulard wrote:

> 2010/6/18 Julia Lawall <[email protected]>:
> > On Fri, 18 Jun 2010, Nicolas Palix wrote:
> >
> >> On Friday 18 June 2010 13:38:35 Eric Noulard wrote:
> >> > Hi all,
> >> >
> >> > I do have a problem for writing a python scripts in  a cocci semantic 
> >> > patch.
> >> > I don't really know if it's a "feature" or a bug, the context is the 
> >> > following.
> >> >
> >> > A cocci file (rule1and2.cocci) contains several rules (say 2 for the 
> >> > example),
> >> > and a single python script (at the end).
> >> >
> >> > The python script is using matches of the rules:
> >> >
> >> > @script:python@
> >> > rule1_matches << rule_1.p;
> >> > rule2_matches << rule_2.p;
> >> >
> >> > @@
> >> > if (len(rule1_matches)>0):
> >> >    for m in rule1_matches:
> >> >        print "File <%s>, line %s" % (m.file,m.line)
> >> > if (len(rule2_matches)>0):
> >> >    for m in rule1_matches:
> >> >        print "File <%s>, line %s" % (m.file,m.line)
> >> >
> >> >
> >> > it looks like the script is executed iff both rules (1 & 2) matches.
> >> >
> >> > Find attached  the files to reproduce the "problem":
> >> >
> >> > Commands giving no output
> >> > spatch -sp rule1and2.cocci -no_show_diff 1-1-2.c
> >> > spatch -sp rule1and2.cocci -no_show_diff 1-1-1.c
> >> >
> >> > Command giving output
> >> > spatch -sp rule1and2.cocci -no_show_diff 1-1-1.c 1-1-2.c
> >> >
> >> > Is this an expected behavior?
> >>
> >> Yes. The python code depends on rule_1.p AND rule_2.p. Thus, the python 
> >> code
> >> is only invoked if both rule have matched.
> >
> > In general, python code is only run if all variables are bound.  You can
> > even exploit this to be sure that a variable is bound, even if you don't
> > want to use its value.
> 
> OK I see.
> My needs was to have a python script loop over the set of matches of a
> set of rules
> in order to display something. Basically the same script is run for each rule.
> 
> Currently I end up writing a
> @initialize:python@
> 
> section which defines my "global" check function and then for each rule
> 
> @script:python@
> ruleX_matches << merasa_X.p;
> @@
> MyChecker.check(ruleX_matches)
> 
> > On the other hand, this is not the case for smpl rules.  There a variable
> > only has to be bound if it is needed for a given match.  An unbound
> > variable causes a local failure, just like trying to match two things that
> > are different, but the whole pattern could match in another way, if the
> > unbound variable is in a branch of a disjunction.
> 
> OK I think I understand that.
> 
> Thanks,
> -- 
> Erk
> Membre de l'April - « promouvoir et défendre le logiciel libre » -
> http://www.april.org
> 
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to