drm/i915|intel-gtt: consolidate intel-gtt.h headers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / Documentation / coccinelle.txt
blob4a276ea7001c660a8f0dcbe7ff968abcff22a102
1 Copyright 2010 Nicolas Palix <npalix@diku.dk>
2 Copyright 2010 Julia Lawall <julia@diku.dk>
3 Copyright 2010 Gilles Muller <Gilles.Muller@lip6.fr>
6  Getting Coccinelle
7 ~~~~~~~~~~~~~~~~~~~~
9 The semantic patches included in the kernel use the 'virtual rule'
10 feature which was introduced in Coccinelle version 0.1.11.
12 Coccinelle (>=0.2.0) is available through the package manager
13 of many distributions, e.g. :
15  - Debian (>=squeeze)
16  - Fedora (>=13)
17  - Ubuntu (>=10.04 Lucid Lynx)
18  - OpenSUSE
19  - Arch Linux
20  - NetBSD
21  - FreeBSD
24 You can get the latest version released from the Coccinelle homepage at
25 http://coccinelle.lip6.fr/
27 Information and tips about Coccinelle are also provided on the wiki
28 pages at http://cocci.ekstranet.diku.dk/wiki/doku.php
30 Once you have it, run the following command:
32         ./configure
33         make
35 as a regular user, and install it with
37         sudo make install
40  Using Coccinelle on the Linux kernel
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43 A Coccinelle-specific target is defined in the top level
44 Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
45 front-end in the 'scripts' directory.
47 Four modes are defined: patch, report, context, and org. The mode to
48 use is specified by setting the MODE variable with 'MODE=<mode>'.
50 'patch' proposes a fix, when possible.
52 'report' generates a list in the following format:
53   file:line:column-column: message
55 'context' highlights lines of interest and their context in a
56 diff-like style.Lines of interest are indicated with '-'.
58 'org' generates a report in the Org mode format of Emacs.
60 Note that not all semantic patches implement all modes. For easy use
61 of Coccinelle, the default mode is "chain" which tries the previous
62 modes in the order above until one succeeds.
64 To make a report for every semantic patch, run the following command:
66         make coccicheck MODE=report
68 NB: The 'report' mode is the default one.
70 To produce patches, run:
72         make coccicheck MODE=patch
75 The coccicheck target applies every semantic patch available in the
76 sub-directories of 'scripts/coccinelle' to the entire Linux kernel.
78 For each semantic patch, a commit message is proposed.  It gives a
79 description of the problem being checked by the semantic patch, and
80 includes a reference to Coccinelle.
82 As any static code analyzer, Coccinelle produces false
83 positives. Thus, reports must be carefully checked, and patches
84 reviewed.
87  Using Coccinelle with a single semantic patch
88 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90 The optional make variable COCCI can be used to check a single
91 semantic patch. In that case, the variable must be initialized with
92 the name of the semantic patch to apply.
94 For instance:
96         make coccicheck COCCI=<my_SP.cocci> MODE=patch
98         make coccicheck COCCI=<my_SP.cocci> MODE=report
101  Using Coccinelle on (modified) files
102 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104 To apply Coccinelle on a file basis, instead of a directory basis, the
105 following command may be used:
107     make C=1 CHECK="scripts/coccicheck"
109 To check only newly edited code, use the value 2 for the C flag, i.e.
111     make C=2 CHECK="scripts/coccicheck"
113 This runs every semantic patch in scripts/coccinelle by default. The
114 COCCI variable may additionally be used to only apply a single
115 semantic patch as shown in the previous section.
117 The "chain" mode is the default. You can select another one with the
118 MODE variable explained above.
120 In this mode, there is no information about semantic patches
121 displayed, and no commit message proposed.
124  Proposing new semantic patches
125 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127 New semantic patches can be proposed and submitted by kernel
128 developers. For sake of clarity, they should be organized in the
129 sub-directories of 'scripts/coccinelle/'.
132  Detailed description of the 'report' mode
133 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135 'report' generates a list in the following format:
136   file:line:column-column: message
138 Example:
140 Running
142         make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
144 will execute the following part of the SmPL script.
146 <smpl>
147 @r depends on !context && !patch && (org || report)@
148 expression x;
149 position p;
152  ERR_PTR@p(PTR_ERR(x))
154 @script:python depends on report@
155 p << r.p;
156 x << r.x;
159 msg="ERR_CAST can be used with %s" % (x)
160 coccilib.report.print_report(p[0], msg)
161 </smpl>
163 This SmPL excerpt generates entries on the standard output, as
164 illustrated below:
166 /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
167 /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
168 /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
171  Detailed description of the 'patch' mode
172 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174 When the 'patch' mode is available, it proposes a fix for each problem
175 identified.
177 Example:
179 Running
180         make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
182 will execute the following part of the SmPL script.
184 <smpl>
185 @ depends on !context && patch && !org && !report @
186 expression x;
189 - ERR_PTR(PTR_ERR(x))
190 + ERR_CAST(x)
191 </smpl>
193 This SmPL excerpt generates patch hunks on the standard output, as
194 illustrated below:
196 diff -u -p a/crypto/ctr.c b/crypto/ctr.c
197 --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
198 +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
199 @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
200         alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
201                                   CRYPTO_ALG_TYPE_MASK);
202         if (IS_ERR(alg))
203 -               return ERR_PTR(PTR_ERR(alg));
204 +               return ERR_CAST(alg);
206         /* Block size must be >= 4 bytes. */
207         err = -EINVAL;
209  Detailed description of the 'context' mode
210 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 'context' highlights lines of interest and their context
213 in a diff-like style.
215 NOTE: The diff-like output generated is NOT an applicable patch. The
216       intent of the 'context' mode is to highlight the important lines
217       (annotated with minus, '-') and gives some surrounding context
218       lines around. This output can be used with the diff mode of
219       Emacs to review the code.
221 Example:
223 Running
224         make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
226 will execute the following part of the SmPL script.
228 <smpl>
229 @ depends on context && !patch && !org && !report@
230 expression x;
233 * ERR_PTR(PTR_ERR(x))
234 </smpl>
236 This SmPL excerpt generates diff hunks on the standard output, as
237 illustrated below:
239 diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
240 --- /home/user/linux/crypto/ctr.c       2010-05-26 10:49:38.000000000 +0200
241 +++ /tmp/nothing
242 @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
243         alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
244                                   CRYPTO_ALG_TYPE_MASK);
245         if (IS_ERR(alg))
246 -               return ERR_PTR(PTR_ERR(alg));
248         /* Block size must be >= 4 bytes. */
249         err = -EINVAL;
251  Detailed description of the 'org' mode
252 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
254 'org' generates a report in the Org mode format of Emacs.
256 Example:
258 Running
259         make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
261 will execute the following part of the SmPL script.
263 <smpl>
264 @r depends on !context && !patch && (org || report)@
265 expression x;
266 position p;
269  ERR_PTR@p(PTR_ERR(x))
271 @script:python depends on org@
272 p << r.p;
273 x << r.x;
276 msg="ERR_CAST can be used with %s" % (x)
277 msg_safe=msg.replace("[","@(").replace("]",")")
278 coccilib.org.print_todo(p[0], msg_safe)
279 </smpl>
281 This SmPL excerpt generates Org entries on the standard output, as
282 illustrated below:
284 * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
285 * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
286 * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]