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 Once you have it, run the following command:
32 as a regular user, and install it with
37 Using Coccinelle on the Linux kernel
38 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 A Coccinelle-specific target is defined in the top level
41 Makefile. This target is named 'coccicheck' and calls the 'coccicheck'
42 front-end in the 'scripts' directory.
44 Four modes are defined: report, patch, context, and org. The mode to
45 use is specified by setting the MODE variable with 'MODE=<mode>'.
47 'report' generates a list in the following format:
48 file:line:column-column: message
50 'patch' proposes a fix, when possible.
52 'context' highlights lines of interest and their context in a
53 diff-like style.Lines of interest are indicated with '-'.
55 'org' generates a report in the Org mode format of Emacs.
57 Note that not all semantic patches implement all modes.
59 To make a report for every semantic patch, run the following command:
61 make coccicheck MODE=report
63 NB: The 'report' mode is the default one.
65 To produce patches, run:
67 make coccicheck MODE=patch
70 The coccicheck target applies every semantic patch available in the
71 subdirectories of 'scripts/coccinelle' to the entire Linux kernel.
73 For each semantic patch, a changelog message is proposed. It gives a
74 description of the problem being checked by the semantic patch, and
75 includes a reference to Coccinelle.
77 As any static code analyzer, Coccinelle produces false
78 positives. Thus, reports must be carefully checked, and patches
82 Using Coccinelle with a single semantic patch
83 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85 The optional make variable COCCI can be used to check a single
86 semantic patch. In that case, the variable must be initialized with
87 the name of the semantic patch to apply.
91 make coccicheck COCCI=<my_SP.cocci> MODE=patch
93 make coccicheck COCCI=<my_SP.cocci> MODE=report
96 Proposing new semantic patches
97 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99 New semantic patches can be proposed and submitted by kernel
100 developers. For sake of clarity, they should be organized in the
101 subdirectories of 'scripts/coccinelle/'.
104 Detailed description of the 'report' mode
105 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 'report' generates a list in the following format:
108 file:line:column-column: message
114 make coccicheck MODE=report COCCI=scripts/coccinelle/err_cast.cocci
116 will execute the following part of the SmPL script.
119 @r depends on !context && !patch && (org || report)@
124 ERR_PTR@p(PTR_ERR(x))
126 @script:python depends on report@
131 msg="ERR_CAST can be used with %s" % (x)
132 coccilib.report.print_report(p[0], msg)
135 This SmPL excerpt generates entries on the standard output, as
138 /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
139 /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
140 /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
143 Detailed description of the 'patch' mode
144 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146 When the 'patch' mode is available, it proposes a fix for each problem
152 make coccicheck MODE=patch COCCI=scripts/coccinelle/err_cast.cocci
154 will execute the following part of the SmPL script.
157 @ depends on !context && patch && !org && !report @
161 - ERR_PTR(PTR_ERR(x))
165 This SmPL excerpt generates patch hunks on the standard output, as
168 diff -u -p a/crypto/ctr.c b/crypto/ctr.c
169 --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
170 +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
171 @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
172 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
173 CRYPTO_ALG_TYPE_MASK);
175 - return ERR_PTR(PTR_ERR(alg));
176 + return ERR_CAST(alg);
178 /* Block size must be >= 4 bytes. */
181 Detailed description of the 'context' mode
182 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 'context' highlights lines of interest and their context
185 in a diff-like style.
187 NOTE: The diff-like output generated is NOT an applicable patch. The
188 intent of the 'context' mode is to highlight the important lines
189 (annotated with minus, '-') and gives some surrounding context
190 lines around. This output can be used with the diff mode of
191 Emacs to review the code.
196 make coccicheck MODE=context COCCI=scripts/coccinelle/err_cast.cocci
198 will execute the following part of the SmPL script.
201 @ depends on context && !patch && !org && !report@
205 * ERR_PTR(PTR_ERR(x))
208 This SmPL excerpt generates diff hunks on the standard output, as
211 diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
212 --- /home/user/linux/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
214 @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
215 alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
216 CRYPTO_ALG_TYPE_MASK);
218 - return ERR_PTR(PTR_ERR(alg));
220 /* Block size must be >= 4 bytes. */
223 Detailed description of the 'org' mode
224 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226 'org' generates a report in the Org mode format of Emacs.
231 make coccicheck MODE=org COCCI=scripts/coccinelle/err_cast.cocci
233 will execute the following part of the SmPL script.
236 @r depends on !context && !patch && (org || report)@
241 ERR_PTR@p(PTR_ERR(x))
243 @script:python depends on org@
248 msg="ERR_CAST can be used with %s" % (x)
249 msg_safe=msg.replace("[","@(").replace("]",")")
250 coccilib.org.print_todo(p[0], msg_safe)
253 This SmPL excerpt generates Org entries on the standard output, as
256 * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
257 * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
258 * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]