Coccinelle applies the rules in order, with the first rule applied to every toplevel object (function, declaration, macro definition, etc) in the C source file, then the second rule applied to every toplevel object, etc. Furthermore, as Coccinelle proceeds through the rules, it applies a given rule once for each permutation of the bindings of the inherited metavariables. For a SmPL rule, it tries the rule even if some of the referenced metavariables are not bound, although a match on an unbound variable will fail. This is useful when there is a sequence of rules containing disjunctions; disjuncts may depend on inherited metavariables that are not always defined. For a Python rule, however, because the interpretation of the Python code is done entirely independently of Coccinelle, a rule is applied exactly once for each possible permutation of the bindings of its inherited metavariables, whether or not these metavariables are used in the Python code. It is sometimes useful to add extra metavariables to cause the Python code to be executed more often, even when the Python code does not use these variables, as illustrated by the following example.

Suppose you want to know the names of the set of functions that are called with 3 as the first argument. This could be done as follows:

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

@script:python@
f << r.f;
@@
print f

On the other hand, suppose you want to know how many calls there are that have 3 as the first argument. This could be done as follows:

@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)

The position metavariable r.p is not used in the Python code, but it causes this code to be invoked once for each function call that has 3 as the first argument.

 
controlling_the_execution_of_python_rules.txt · Last modified: 2009/07/23 18:04 by jll
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki