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>
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. :
17 - Ubuntu (>=10.04 Lucid Lynx)
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:
35 as a regular user, and install it with
39 The semantic patches in the kernel will work best with Coccinelle version
40 0.2.4 or later. Using earlier versions may incur some parse errors in the
41 semantic patch code, but any results that are obtained should still be
44 Using Coccinelle on the Linux kernel
45 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47 A Coccinelle-specific target is defined in the top level
48 Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
49 front-end in the 'scripts' directory.
51 Four modes are defined: patch, report, context, and org. The mode to
52 use is specified by setting the MODE variable with 'MODE=<mode>'.
54 'patch' proposes a fix, when possible.
56 'report' generates a list in the following format:
57 file:line:column-column: message
59 'context' highlights lines of interest and their context in a
60 diff-like style.Lines of interest are indicated with '-'.
62 'org' generates a report in the Org mode format of Emacs.
64 Note that not all semantic patches implement all modes. For easy use
65 of Coccinelle, the default mode is "chain" which tries the previous
66 modes in the order above until one succeeds.
68 To make a report for every semantic patch, run the following command:
70 make coccicheck MODE=report
72 NB: The 'report' mode is the default one.
74 To produce patches, run:
76 make coccicheck MODE=patch
79 The coccicheck target applies every semantic patch available in the
80 sub-directories of 'scripts/coccinelle' to the entire Linux kernel.
82 For each semantic patch, a commit message is proposed. It gives a
83 description of the problem being checked by the semantic patch, and
84 includes a reference to Coccinelle.
86 As any static code analyzer, Coccinelle produces false
87 positives. Thus, reports must be carefully checked, and patches
90 To enable verbose messages set the V= variable, for example:
92 make coccicheck MODE=report V=1
95 Using Coccinelle with a single semantic patch
96 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98 The optional make variable COCCI can be used to check a single
99 semantic patch. In that case, the variable must be initialized with
100 the name of the semantic patch to apply.
104 make coccicheck COCCI=<my_SP.cocci> MODE=patch
106 make coccicheck COCCI=<my_SP.cocci> MODE=report
109 Controlling Which Files are Processed by Coccinelle
110 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111 By default the entire kernel source tree is checked.
113 To apply Coccinelle to a specific directory, M= can be used.
114 For example, to check drivers/net/wireless/ one may write:
116 make coccicheck M=drivers/net/wireless/
118 To apply Coccinelle on a file basis, instead of a directory basis, the
119 following command may be used:
121 make C=1 CHECK="scripts/coccicheck"
123 To check only newly edited code, use the value 2 for the C flag, i.e.
125 make C=2 CHECK="scripts/coccicheck"
127 This runs every semantic patch in scripts/coccinelle by default. The
128 COCCI variable may additionally be used to only apply a single
129 semantic patch as shown in the previous section.
131 The "chain" mode is the default. You can select another one with the
132 MODE variable explained above.
134 In this mode, there is no information about semantic patches
135 displayed, and no commit message proposed.
138 Proposing new semantic patches
139 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141 New semantic patches can be proposed and submitted by kernel
142 developers. For sake of clarity, they should be organized in the
143 sub-directories of 'scripts/coccinelle/'.
146 Detailed description of the 'report' mode
147 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149 'report' generates a list in the following format:
150 file:line:column-column: message
156 make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
158 will execute the following part of the SmPL script.
161 @r depends on !context && !patch && (org || report)@
166 ERR_PTR@p(PTR_ERR(x))
168 @script:python depends on report@
173 msg="ERR_CAST can be used with %s" % (x)
174 coccilib.report.print_report(p[0], msg)
177 This SmPL excerpt generates entries on the standard output, as
180 /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
181 /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
182 /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
185 Detailed description of the 'patch' mode
186 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188 When the 'patch' mode is available, it proposes a fix for each problem
194 make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
196 will execute the following part of the SmPL script.
199 @ depends on !context && patch && !org && !report @
203 - ERR_PTR(PTR_ERR(x))
207 This SmPL excerpt generates patch hunks on the standard output, as
210 diff -u -p a/crypto/ctr.c b/crypto/ctr.c
211 --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
212 +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
213 @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
214 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
215 CRYPTO_ALG_TYPE_MASK);
217 - return ERR_PTR(PTR_ERR(alg));
218 + return ERR_CAST(alg);
220 /* Block size must be >= 4 bytes. */
223 Detailed description of the 'context' mode
224 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226 'context' highlights lines of interest and their context
227 in a diff-like style.
229 NOTE: The diff-like output generated is NOT an applicable patch. The
230 intent of the 'context' mode is to highlight the important lines
231 (annotated with minus, '-') and gives some surrounding context
232 lines around. This output can be used with the diff mode of
233 Emacs to review the code.
238 make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
240 will execute the following part of the SmPL script.
243 @ depends on context && !patch && !org && !report@
247 * ERR_PTR(PTR_ERR(x))
250 This SmPL excerpt generates diff hunks on the standard output, as
253 diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
254 --- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
256 @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
257 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
258 CRYPTO_ALG_TYPE_MASK);
260 - return ERR_PTR(PTR_ERR(alg));
262 /* Block size must be >= 4 bytes. */
265 Detailed description of the 'org' mode
266 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
268 'org' generates a report in the Org mode format of Emacs.
273 make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
275 will execute the following part of the SmPL script.
278 @r depends on !context && !patch && (org || report)@
283 ERR_PTR@p(PTR_ERR(x))
285 @script:python depends on org@
290 msg="ERR_CAST can be used with %s" % (x)
291 msg_safe=msg.replace("[","@(").replace("]",")")
292 coccilib.org.print_todo(p[0], msg_safe)
295 This SmPL excerpt generates Org entries on the standard output, as
298 * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
299 * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
300 * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]