Release coccinelle-0.1.8
[bpt/coccinelle.git] / docs / manual / cocci-python.txt
CommitLineData
34e49164
C
1Python extensions for Coccinelle
2================================
3
4Coccinelle embeds a python interpreter to support processing
5matches using the full expressive power of python.
6
7Scripting with python
8---------------------
9
10A python script part to a .cocci file must not be the first rule. A typical sample for searching for constantly sized arrays and its uses could be:
11
12@ rule1 @ type T; identifier I; expression C, E; position p1, p2; @@
13T I[C@p1];
14<+... I[E@p2] ...+>
15@ script:python @ x_mv << rule1.C; y_mv << rule1.E; x_pos << rule1.p1; y_pos << rule1.p2; @@
16x = cocci.combine(x_mv, x_pos)
17y = cocci.combine(y_mv, y_pos)
18print x.location
19print y.location
20
21Here cocci is the interface class for interfacting with Coccinelle's OCaml core. It is part python and part OCaml code. The combine function ensures that the filename, line number and column number of the matches are registered with a meta-variable.
22
23Controlling environments
24------------------------
25
26The python script will be called for each environment generated by Coccinelle with matches to the previous rules. By default, the python script drops each environment unless otherwise is indicated in the script using cocci.include_match(True).
27
28As a short-cut for registering information that "belongs together", the Output class also provides a register_match method that may be overridden in derived classes. This method can be called like: cocci.register_match(True, [(x, 'Array size'), (y, 'Array index size')]). Here the True is automatically passed on to include_match (so one could use False in order to drop the environment, but still print information). In the GTK frontend, this will result in the "Array index size" information being shown as a child node to the "Array size".
29
30Output methods for the python scripts
31-------------------------------------
32
33By default Coccinelle contains two output modes: a console-based output, and a GTK-based output. Which one is used is specified using the -pyoutput switch to spatch.
34
35-pyoutput coccilib.output.Console (this is the default)
36-pyoutput coccilib.output.Gtk
37
38The latter depends on pygtk2, and the dependency will not be evaluated until runtime.
39
40Creating new python output classes
41----------------------------------
42
43If you have a need to tailor special output based on your python script, e.g. using register_match of existing scripts, you can do this by deriving from coccilib.output.Output and override the implementation of register_match(self, include, messages). The method expects that the first thing you call is "self.include_match(include)", but otherwise the details of the method is left up to you.
44
45To use your custom output class, just specify it as an argument to -pyoutput. It needs to exist in the default python execution environment or in PYTHON_PATH.
46
47Running spatch from a different directory than it is stored in
48--------------------------------------------------------------
49
50In order for spatch to be able to find coccilib, PYTHON_PATH must be set to include the directory in which spatch resides.