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
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
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.
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
142 make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
144 will execute the following part of the SmPL script.
147 @r depends on !context && !patch && (org || report)@
152 ERR_PTR@p(PTR_ERR(x))
154 @script:python depends on report@
159 msg="ERR_CAST can be used with %s" % (x)
160 coccilib.report.print_report(p[0], msg)
163 This SmPL excerpt generates entries on the standard output, as
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
180 make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
182 will execute the following part of the SmPL script.
185 @ depends on !context && patch && !org && !report @
189 - ERR_PTR(PTR_ERR(x))
193 This SmPL excerpt generates patch hunks on the standard output, as
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);
203 - return ERR_PTR(PTR_ERR(alg));
204 + return ERR_CAST(alg);
206 /* Block size must be >= 4 bytes. */
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.
224 make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
226 will execute the following part of the SmPL script.
229 @ depends on context && !patch && !org && !report@
233 * ERR_PTR(PTR_ERR(x))
236 This SmPL excerpt generates diff hunks on the standard output, as
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
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);
246 - return ERR_PTR(PTR_ERR(alg));
248 /* Block size must be >= 4 bytes. */
251 Detailed description of the 'org' mode
252 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
254 'org' generates a report in the Org mode format of Emacs.
259 make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
261 will execute the following part of the SmPL script.
264 @r depends on !context && !patch && (org || report)@
269 ERR_PTR@p(PTR_ERR(x))
271 @script:python depends on org@
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)
281 This SmPL excerpt generates Org entries on the standard output, as
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]]