xref: /OK3568_Linux_fs/kernel/Documentation/dev-tools/coccinelle.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. Copyright 2010 Nicolas Palix <npalix@diku.dk>
2*4882a593Smuzhiyun.. Copyright 2010 Julia Lawall <julia@diku.dk>
3*4882a593Smuzhiyun.. Copyright 2010 Gilles Muller <Gilles.Muller@lip6.fr>
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun.. highlight:: none
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun.. _devtools_coccinelle:
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunCoccinelle
10*4882a593Smuzhiyun==========
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunCoccinelle is a tool for pattern matching and text transformation that has
13*4882a593Smuzhiyunmany uses in kernel development, including the application of complex,
14*4882a593Smuzhiyuntree-wide patches and detection of problematic programming patterns.
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunGetting Coccinelle
17*4882a593Smuzhiyun------------------
18*4882a593Smuzhiyun
19*4882a593SmuzhiyunThe semantic patches included in the kernel use features and options
20*4882a593Smuzhiyunwhich are provided by Coccinelle version 1.0.0-rc11 and above.
21*4882a593SmuzhiyunUsing earlier versions will fail as the option names used by
22*4882a593Smuzhiyunthe Coccinelle files and coccicheck have been updated.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunCoccinelle is available through the package manager
25*4882a593Smuzhiyunof many distributions, e.g. :
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun - Debian
28*4882a593Smuzhiyun - Fedora
29*4882a593Smuzhiyun - Ubuntu
30*4882a593Smuzhiyun - OpenSUSE
31*4882a593Smuzhiyun - Arch Linux
32*4882a593Smuzhiyun - NetBSD
33*4882a593Smuzhiyun - FreeBSD
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunSome distribution packages are obsolete and it is recommended
36*4882a593Smuzhiyunto use the latest version released from the Coccinelle homepage at
37*4882a593Smuzhiyunhttp://coccinelle.lip6.fr/
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunOr from Github at:
40*4882a593Smuzhiyun
41*4882a593Smuzhiyunhttps://github.com/coccinelle/coccinelle
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunOnce you have it, run the following commands::
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun        ./autogen
46*4882a593Smuzhiyun        ./configure
47*4882a593Smuzhiyun        make
48*4882a593Smuzhiyun
49*4882a593Smuzhiyunas a regular user, and install it with::
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun        sudo make install
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunMore detailed installation instructions to build from source can be
54*4882a593Smuzhiyunfound at:
55*4882a593Smuzhiyun
56*4882a593Smuzhiyunhttps://github.com/coccinelle/coccinelle/blob/master/install.txt
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunSupplemental documentation
59*4882a593Smuzhiyun--------------------------
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunFor supplemental documentation refer to the wiki:
62*4882a593Smuzhiyun
63*4882a593Smuzhiyunhttps://bottest.wiki.kernel.org/coccicheck
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunThe wiki documentation always refers to the linux-next version of the script.
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunFor Semantic Patch Language(SmPL) grammar documentation refer to:
68*4882a593Smuzhiyun
69*4882a593Smuzhiyunhttp://coccinelle.lip6.fr/documentation.php
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunUsing Coccinelle on the Linux kernel
72*4882a593Smuzhiyun------------------------------------
73*4882a593Smuzhiyun
74*4882a593SmuzhiyunA Coccinelle-specific target is defined in the top level
75*4882a593SmuzhiyunMakefile. This target is named ``coccicheck`` and calls the ``coccicheck``
76*4882a593Smuzhiyunfront-end in the ``scripts`` directory.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunFour basic modes are defined: ``patch``, ``report``, ``context``, and
79*4882a593Smuzhiyun``org``. The mode to use is specified by setting the MODE variable with
80*4882a593Smuzhiyun``MODE=<mode>``.
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun- ``patch`` proposes a fix, when possible.
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun- ``report`` generates a list in the following format:
85*4882a593Smuzhiyun  file:line:column-column: message
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun- ``context`` highlights lines of interest and their context in a
88*4882a593Smuzhiyun  diff-like style. Lines of interest are indicated with ``-``.
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun- ``org`` generates a report in the Org mode format of Emacs.
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunNote that not all semantic patches implement all modes. For easy use
93*4882a593Smuzhiyunof Coccinelle, the default mode is "report".
94*4882a593Smuzhiyun
95*4882a593SmuzhiyunTwo other modes provide some common combinations of these modes.
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun- ``chain`` tries the previous modes in the order above until one succeeds.
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun- ``rep+ctxt`` runs successively the report mode and the context mode.
100*4882a593Smuzhiyun  It should be used with the C option (described later)
101*4882a593Smuzhiyun  which checks the code on a file basis.
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunExamples
104*4882a593Smuzhiyun~~~~~~~~
105*4882a593Smuzhiyun
106*4882a593SmuzhiyunTo make a report for every semantic patch, run the following command::
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun		make coccicheck MODE=report
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunTo produce patches, run::
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun		make coccicheck MODE=patch
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunThe coccicheck target applies every semantic patch available in the
116*4882a593Smuzhiyunsub-directories of ``scripts/coccinelle`` to the entire Linux kernel.
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunFor each semantic patch, a commit message is proposed.  It gives a
119*4882a593Smuzhiyundescription of the problem being checked by the semantic patch, and
120*4882a593Smuzhiyunincludes a reference to Coccinelle.
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunAs with any static code analyzer, Coccinelle produces false
123*4882a593Smuzhiyunpositives. Thus, reports must be carefully checked, and patches
124*4882a593Smuzhiyunreviewed.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunTo enable verbose messages set the V= variable, for example::
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun   make coccicheck MODE=report V=1
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunCoccinelle parallelization
131*4882a593Smuzhiyun--------------------------
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunBy default, coccicheck tries to run as parallel as possible. To change
134*4882a593Smuzhiyunthe parallelism, set the J= variable. For example, to run across 4 CPUs::
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun   make coccicheck MODE=report J=4
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunAs of Coccinelle 1.0.2 Coccinelle uses Ocaml parmap for parallelization;
139*4882a593Smuzhiyunif support for this is detected you will benefit from parmap parallelization.
140*4882a593Smuzhiyun
141*4882a593SmuzhiyunWhen parmap is enabled coccicheck will enable dynamic load balancing by using
142*4882a593Smuzhiyun``--chunksize 1`` argument. This ensures we keep feeding threads with work
143*4882a593Smuzhiyunone by one, so that we avoid the situation where most work gets done by only
144*4882a593Smuzhiyuna few threads. With dynamic load balancing, if a thread finishes early we keep
145*4882a593Smuzhiyunfeeding it more work.
146*4882a593Smuzhiyun
147*4882a593SmuzhiyunWhen parmap is enabled, if an error occurs in Coccinelle, this error
148*4882a593Smuzhiyunvalue is propagated back, and the return value of the ``make coccicheck``
149*4882a593Smuzhiyuncommand captures this return value.
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunUsing Coccinelle with a single semantic patch
152*4882a593Smuzhiyun---------------------------------------------
153*4882a593Smuzhiyun
154*4882a593SmuzhiyunThe optional make variable COCCI can be used to check a single
155*4882a593Smuzhiyunsemantic patch. In that case, the variable must be initialized with
156*4882a593Smuzhiyunthe name of the semantic patch to apply.
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunFor instance::
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun	make coccicheck COCCI=<my_SP.cocci> MODE=patch
161*4882a593Smuzhiyun
162*4882a593Smuzhiyunor::
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun	make coccicheck COCCI=<my_SP.cocci> MODE=report
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunControlling Which Files are Processed by Coccinelle
168*4882a593Smuzhiyun---------------------------------------------------
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunBy default the entire kernel source tree is checked.
171*4882a593Smuzhiyun
172*4882a593SmuzhiyunTo apply Coccinelle to a specific directory, ``M=`` can be used.
173*4882a593SmuzhiyunFor example, to check drivers/net/wireless/ one may write::
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun    make coccicheck M=drivers/net/wireless/
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunTo apply Coccinelle on a file basis, instead of a directory basis, the
178*4882a593SmuzhiyunC variable is used by the makefile to select which files to work with.
179*4882a593SmuzhiyunThis variable can be used to run scripts for the entire kernel, a
180*4882a593Smuzhiyunspecific directory, or for a single file.
181*4882a593Smuzhiyun
182*4882a593SmuzhiyunFor example, to check drivers/bluetooth/bfusb.c, the value 1 is
183*4882a593Smuzhiyunpassed to the C variable to check files that make considers
184*4882a593Smuzhiyunneed to be compiled.::
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun    make C=1 CHECK=scripts/coccicheck drivers/bluetooth/bfusb.o
187*4882a593Smuzhiyun
188*4882a593SmuzhiyunThe value 2 is passed to the C variable to check files regardless of
189*4882a593Smuzhiyunwhether they need to be compiled or not.::
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun    make C=2 CHECK=scripts/coccicheck drivers/bluetooth/bfusb.o
192*4882a593Smuzhiyun
193*4882a593SmuzhiyunIn these modes, which work on a file basis, there is no information
194*4882a593Smuzhiyunabout semantic patches displayed, and no commit message proposed.
195*4882a593Smuzhiyun
196*4882a593SmuzhiyunThis runs every semantic patch in scripts/coccinelle by default. The
197*4882a593SmuzhiyunCOCCI variable may additionally be used to only apply a single
198*4882a593Smuzhiyunsemantic patch as shown in the previous section.
199*4882a593Smuzhiyun
200*4882a593SmuzhiyunThe "report" mode is the default. You can select another one with the
201*4882a593SmuzhiyunMODE variable explained above.
202*4882a593Smuzhiyun
203*4882a593SmuzhiyunDebugging Coccinelle SmPL patches
204*4882a593Smuzhiyun---------------------------------
205*4882a593Smuzhiyun
206*4882a593SmuzhiyunUsing coccicheck is best as it provides in the spatch command line
207*4882a593Smuzhiyuninclude options matching the options used when we compile the kernel.
208*4882a593SmuzhiyunYou can learn what these options are by using V=1; you could then
209*4882a593Smuzhiyunmanually run Coccinelle with debug options added.
210*4882a593Smuzhiyun
211*4882a593SmuzhiyunAlternatively you can debug running Coccinelle against SmPL patches
212*4882a593Smuzhiyunby asking for stderr to be redirected to stderr. By default stderr
213*4882a593Smuzhiyunis redirected to /dev/null; if you'd like to capture stderr you
214*4882a593Smuzhiyuncan specify the ``DEBUG_FILE="file.txt"`` option to coccicheck. For
215*4882a593Smuzhiyuninstance::
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun    rm -f cocci.err
218*4882a593Smuzhiyun    make coccicheck COCCI=scripts/coccinelle/free/kfree.cocci MODE=report DEBUG_FILE=cocci.err
219*4882a593Smuzhiyun    cat cocci.err
220*4882a593Smuzhiyun
221*4882a593SmuzhiyunYou can use SPFLAGS to add debugging flags; for instance you may want to
222*4882a593Smuzhiyunadd both --profile --show-trying to SPFLAGS when debugging. For example
223*4882a593Smuzhiyunyou may want to use::
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun    rm -f err.log
226*4882a593Smuzhiyun    export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
227*4882a593Smuzhiyun    make coccicheck DEBUG_FILE="err.log" MODE=report SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
228*4882a593Smuzhiyun
229*4882a593Smuzhiyunerr.log will now have the profiling information, while stdout will
230*4882a593Smuzhiyunprovide some progress information as Coccinelle moves forward with
231*4882a593Smuzhiyunwork.
232*4882a593Smuzhiyun
233*4882a593SmuzhiyunDEBUG_FILE support is only supported when using coccinelle >= 1.0.2.
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun.cocciconfig support
236*4882a593Smuzhiyun--------------------
237*4882a593Smuzhiyun
238*4882a593SmuzhiyunCoccinelle supports reading .cocciconfig for default Coccinelle options that
239*4882a593Smuzhiyunshould be used every time spatch is spawned. The order of precedence for
240*4882a593Smuzhiyunvariables for .cocciconfig is as follows:
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun- Your current user's home directory is processed first
243*4882a593Smuzhiyun- Your directory from which spatch is called is processed next
244*4882a593Smuzhiyun- The directory provided with the --dir option is processed last, if used
245*4882a593Smuzhiyun
246*4882a593SmuzhiyunSince coccicheck runs through make, it naturally runs from the kernel
247*4882a593Smuzhiyunproper dir; as such the second rule above would be implied for picking up a
248*4882a593Smuzhiyun.cocciconfig when using ``make coccicheck``.
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun``make coccicheck`` also supports using M= targets. If you do not supply
251*4882a593Smuzhiyunany M= target, it is assumed you want to target the entire kernel.
252*4882a593SmuzhiyunThe kernel coccicheck script has::
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun    if [ "$KBUILD_EXTMOD" = "" ] ; then
255*4882a593Smuzhiyun        OPTIONS="--dir $srctree $COCCIINCLUDE"
256*4882a593Smuzhiyun    else
257*4882a593Smuzhiyun        OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
258*4882a593Smuzhiyun    fi
259*4882a593Smuzhiyun
260*4882a593SmuzhiyunKBUILD_EXTMOD is set when an explicit target with M= is used. For both cases
261*4882a593Smuzhiyunthe spatch --dir argument is used, as such third rule applies when whether M=
262*4882a593Smuzhiyunis used or not, and when M= is used the target directory can have its own
263*4882a593Smuzhiyun.cocciconfig file. When M= is not passed as an argument to coccicheck the
264*4882a593Smuzhiyuntarget directory is the same as the directory from where spatch was called.
265*4882a593Smuzhiyun
266*4882a593SmuzhiyunIf not using the kernel's coccicheck target, keep the above precedence
267*4882a593Smuzhiyunorder logic of .cocciconfig reading. If using the kernel's coccicheck target,
268*4882a593Smuzhiyunoverride any of the kernel's .coccicheck's settings using SPFLAGS.
269*4882a593Smuzhiyun
270*4882a593SmuzhiyunWe help Coccinelle when used against Linux with a set of sensible default
271*4882a593Smuzhiyunoptions for Linux with our own Linux .cocciconfig. This hints to coccinelle
272*4882a593Smuzhiyunthat git can be used for ``git grep`` queries over coccigrep. A timeout of 200
273*4882a593Smuzhiyunseconds should suffice for now.
274*4882a593Smuzhiyun
275*4882a593SmuzhiyunThe options picked up by coccinelle when reading a .cocciconfig do not appear
276*4882a593Smuzhiyunas arguments to spatch processes running on your system. To confirm what
277*4882a593Smuzhiyunoptions will be used by Coccinelle run::
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun      spatch --print-options-only
280*4882a593Smuzhiyun
281*4882a593SmuzhiyunYou can override with your own preferred index option by using SPFLAGS. Take
282*4882a593Smuzhiyunnote that when there are conflicting options Coccinelle takes precedence for
283*4882a593Smuzhiyunthe last options passed. Using .cocciconfig is possible to use idutils, however
284*4882a593Smuzhiyungiven the order of precedence followed by Coccinelle, since the kernel now
285*4882a593Smuzhiyuncarries its own .cocciconfig, you will need to use SPFLAGS to use idutils if
286*4882a593Smuzhiyundesired. See below section "Additional flags" for more details on how to use
287*4882a593Smuzhiyunidutils.
288*4882a593Smuzhiyun
289*4882a593SmuzhiyunAdditional flags
290*4882a593Smuzhiyun----------------
291*4882a593Smuzhiyun
292*4882a593SmuzhiyunAdditional flags can be passed to spatch through the SPFLAGS
293*4882a593Smuzhiyunvariable. This works as Coccinelle respects the last flags
294*4882a593Smuzhiyungiven to it when options are in conflict. ::
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun    make SPFLAGS=--use-glimpse coccicheck
297*4882a593Smuzhiyun
298*4882a593SmuzhiyunCoccinelle supports idutils as well but requires coccinelle >= 1.0.6.
299*4882a593SmuzhiyunWhen no ID file is specified coccinelle assumes your ID database file
300*4882a593Smuzhiyunis in the file .id-utils.index on the top level of the kernel. Coccinelle
301*4882a593Smuzhiyuncarries a script scripts/idutils_index.sh which creates the database with::
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun    mkid -i C --output .id-utils.index
304*4882a593Smuzhiyun
305*4882a593SmuzhiyunIf you have another database filename you can also just symlink with this
306*4882a593Smuzhiyunname. ::
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun    make SPFLAGS=--use-idutils coccicheck
309*4882a593Smuzhiyun
310*4882a593SmuzhiyunAlternatively you can specify the database filename explicitly, for
311*4882a593Smuzhiyuninstance::
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun    make SPFLAGS="--use-idutils /full-path/to/ID" coccicheck
314*4882a593Smuzhiyun
315*4882a593SmuzhiyunSee ``spatch --help`` to learn more about spatch options.
316*4882a593Smuzhiyun
317*4882a593SmuzhiyunNote that the ``--use-glimpse`` and ``--use-idutils`` options
318*4882a593Smuzhiyunrequire external tools for indexing the code. None of them is
319*4882a593Smuzhiyunthus active by default. However, by indexing the code with
320*4882a593Smuzhiyunone of these tools, and according to the cocci file used,
321*4882a593Smuzhiyunspatch could proceed the entire code base more quickly.
322*4882a593Smuzhiyun
323*4882a593SmuzhiyunSmPL patch specific options
324*4882a593Smuzhiyun---------------------------
325*4882a593Smuzhiyun
326*4882a593SmuzhiyunSmPL patches can have their own requirements for options passed
327*4882a593Smuzhiyunto Coccinelle. SmPL patch-specific options can be provided by
328*4882a593Smuzhiyunproviding them at the top of the SmPL patch, for instance::
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun	// Options: --no-includes --include-headers
331*4882a593Smuzhiyun
332*4882a593SmuzhiyunSmPL patch Coccinelle requirements
333*4882a593Smuzhiyun----------------------------------
334*4882a593Smuzhiyun
335*4882a593SmuzhiyunAs Coccinelle features get added some more advanced SmPL patches
336*4882a593Smuzhiyunmay require newer versions of Coccinelle. If an SmPL patch requires
337*4882a593Smuzhiyuna minimum version of Coccinelle, this can be specified as follows,
338*4882a593Smuzhiyunas an example if requiring at least Coccinelle >= 1.0.5::
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun	// Requires: 1.0.5
341*4882a593Smuzhiyun
342*4882a593SmuzhiyunProposing new semantic patches
343*4882a593Smuzhiyun------------------------------
344*4882a593Smuzhiyun
345*4882a593SmuzhiyunNew semantic patches can be proposed and submitted by kernel
346*4882a593Smuzhiyundevelopers. For sake of clarity, they should be organized in the
347*4882a593Smuzhiyunsub-directories of ``scripts/coccinelle/``.
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun
350*4882a593SmuzhiyunDetailed description of the ``report`` mode
351*4882a593Smuzhiyun-------------------------------------------
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun``report`` generates a list in the following format::
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun  file:line:column-column: message
356*4882a593Smuzhiyun
357*4882a593SmuzhiyunExample
358*4882a593Smuzhiyun~~~~~~~
359*4882a593Smuzhiyun
360*4882a593SmuzhiyunRunning::
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun	make coccicheck MODE=report COCCI=scripts/coccinelle/api/err_cast.cocci
363*4882a593Smuzhiyun
364*4882a593Smuzhiyunwill execute the following part of the SmPL script::
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun   <smpl>
367*4882a593Smuzhiyun   @r depends on !context && !patch && (org || report)@
368*4882a593Smuzhiyun   expression x;
369*4882a593Smuzhiyun   position p;
370*4882a593Smuzhiyun   @@
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun     ERR_PTR@p(PTR_ERR(x))
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun   @script:python depends on report@
375*4882a593Smuzhiyun   p << r.p;
376*4882a593Smuzhiyun   x << r.x;
377*4882a593Smuzhiyun   @@
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun   msg="ERR_CAST can be used with %s" % (x)
380*4882a593Smuzhiyun   coccilib.report.print_report(p[0], msg)
381*4882a593Smuzhiyun   </smpl>
382*4882a593Smuzhiyun
383*4882a593SmuzhiyunThis SmPL excerpt generates entries on the standard output, as
384*4882a593Smuzhiyunillustrated below::
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun    /home/user/linux/crypto/ctr.c:188:9-16: ERR_CAST can be used with alg
387*4882a593Smuzhiyun    /home/user/linux/crypto/authenc.c:619:9-16: ERR_CAST can be used with auth
388*4882a593Smuzhiyun    /home/user/linux/crypto/xts.c:227:9-16: ERR_CAST can be used with alg
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunDetailed description of the ``patch`` mode
392*4882a593Smuzhiyun------------------------------------------
393*4882a593Smuzhiyun
394*4882a593SmuzhiyunWhen the ``patch`` mode is available, it proposes a fix for each problem
395*4882a593Smuzhiyunidentified.
396*4882a593Smuzhiyun
397*4882a593SmuzhiyunExample
398*4882a593Smuzhiyun~~~~~~~
399*4882a593Smuzhiyun
400*4882a593SmuzhiyunRunning::
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun	make coccicheck MODE=patch COCCI=scripts/coccinelle/api/err_cast.cocci
403*4882a593Smuzhiyun
404*4882a593Smuzhiyunwill execute the following part of the SmPL script::
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun    <smpl>
407*4882a593Smuzhiyun    @ depends on !context && patch && !org && !report @
408*4882a593Smuzhiyun    expression x;
409*4882a593Smuzhiyun    @@
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun    - ERR_PTR(PTR_ERR(x))
412*4882a593Smuzhiyun    + ERR_CAST(x)
413*4882a593Smuzhiyun    </smpl>
414*4882a593Smuzhiyun
415*4882a593SmuzhiyunThis SmPL excerpt generates patch hunks on the standard output, as
416*4882a593Smuzhiyunillustrated below::
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun    diff -u -p a/crypto/ctr.c b/crypto/ctr.c
419*4882a593Smuzhiyun    --- a/crypto/ctr.c 2010-05-26 10:49:38.000000000 +0200
420*4882a593Smuzhiyun    +++ b/crypto/ctr.c 2010-06-03 23:44:49.000000000 +0200
421*4882a593Smuzhiyun    @@ -185,7 +185,7 @@ static struct crypto_instance *crypto_ct
422*4882a593Smuzhiyun 	alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
423*4882a593Smuzhiyun 				  CRYPTO_ALG_TYPE_MASK);
424*4882a593Smuzhiyun 	if (IS_ERR(alg))
425*4882a593Smuzhiyun    -		return ERR_PTR(PTR_ERR(alg));
426*4882a593Smuzhiyun    +		return ERR_CAST(alg);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun 	/* Block size must be >= 4 bytes. */
429*4882a593Smuzhiyun 	err = -EINVAL;
430*4882a593Smuzhiyun
431*4882a593SmuzhiyunDetailed description of the ``context`` mode
432*4882a593Smuzhiyun--------------------------------------------
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun``context`` highlights lines of interest and their context
435*4882a593Smuzhiyunin a diff-like style.
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun      **NOTE**: The diff-like output generated is NOT an applicable patch. The
438*4882a593Smuzhiyun      intent of the ``context`` mode is to highlight the important lines
439*4882a593Smuzhiyun      (annotated with minus, ``-``) and gives some surrounding context
440*4882a593Smuzhiyun      lines around. This output can be used with the diff mode of
441*4882a593Smuzhiyun      Emacs to review the code.
442*4882a593Smuzhiyun
443*4882a593SmuzhiyunExample
444*4882a593Smuzhiyun~~~~~~~
445*4882a593Smuzhiyun
446*4882a593SmuzhiyunRunning::
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun	make coccicheck MODE=context COCCI=scripts/coccinelle/api/err_cast.cocci
449*4882a593Smuzhiyun
450*4882a593Smuzhiyunwill execute the following part of the SmPL script::
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun    <smpl>
453*4882a593Smuzhiyun    @ depends on context && !patch && !org && !report@
454*4882a593Smuzhiyun    expression x;
455*4882a593Smuzhiyun    @@
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun    * ERR_PTR(PTR_ERR(x))
458*4882a593Smuzhiyun    </smpl>
459*4882a593Smuzhiyun
460*4882a593SmuzhiyunThis SmPL excerpt generates diff hunks on the standard output, as
461*4882a593Smuzhiyunillustrated below::
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun    diff -u -p /home/user/linux/crypto/ctr.c /tmp/nothing
464*4882a593Smuzhiyun    --- /home/user/linux/crypto/ctr.c	2010-05-26 10:49:38.000000000 +0200
465*4882a593Smuzhiyun    +++ /tmp/nothing
466*4882a593Smuzhiyun    @@ -185,7 +185,6 @@ static struct crypto_instance *crypto_ct
467*4882a593Smuzhiyun 	alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
468*4882a593Smuzhiyun 				  CRYPTO_ALG_TYPE_MASK);
469*4882a593Smuzhiyun 	if (IS_ERR(alg))
470*4882a593Smuzhiyun    -		return ERR_PTR(PTR_ERR(alg));
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun 	/* Block size must be >= 4 bytes. */
473*4882a593Smuzhiyun 	err = -EINVAL;
474*4882a593Smuzhiyun
475*4882a593SmuzhiyunDetailed description of the ``org`` mode
476*4882a593Smuzhiyun----------------------------------------
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun``org`` generates a report in the Org mode format of Emacs.
479*4882a593Smuzhiyun
480*4882a593SmuzhiyunExample
481*4882a593Smuzhiyun~~~~~~~
482*4882a593Smuzhiyun
483*4882a593SmuzhiyunRunning::
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun	make coccicheck MODE=org COCCI=scripts/coccinelle/api/err_cast.cocci
486*4882a593Smuzhiyun
487*4882a593Smuzhiyunwill execute the following part of the SmPL script::
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun    <smpl>
490*4882a593Smuzhiyun    @r depends on !context && !patch && (org || report)@
491*4882a593Smuzhiyun    expression x;
492*4882a593Smuzhiyun    position p;
493*4882a593Smuzhiyun    @@
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun      ERR_PTR@p(PTR_ERR(x))
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun    @script:python depends on org@
498*4882a593Smuzhiyun    p << r.p;
499*4882a593Smuzhiyun    x << r.x;
500*4882a593Smuzhiyun    @@
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun    msg="ERR_CAST can be used with %s" % (x)
503*4882a593Smuzhiyun    msg_safe=msg.replace("[","@(").replace("]",")")
504*4882a593Smuzhiyun    coccilib.org.print_todo(p[0], msg_safe)
505*4882a593Smuzhiyun    </smpl>
506*4882a593Smuzhiyun
507*4882a593SmuzhiyunThis SmPL excerpt generates Org entries on the standard output, as
508*4882a593Smuzhiyunillustrated below::
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun    * TODO [[view:/home/user/linux/crypto/ctr.c::face=ovl-face1::linb=188::colb=9::cole=16][ERR_CAST can be used with alg]]
511*4882a593Smuzhiyun    * TODO [[view:/home/user/linux/crypto/authenc.c::face=ovl-face1::linb=619::colb=9::cole=16][ERR_CAST can be used with auth]]
512*4882a593Smuzhiyun    * TODO [[view:/home/user/linux/crypto/xts.c::face=ovl-face1::linb=227::colb=9::cole=16][ERR_CAST can be used with alg]]
513