Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / docs / manual / spatch_options.tex
CommitLineData
faf9a90c
C
1\section{Introduction}
2
3This document describes the options provided by Coccinelle. The options
4have an impact on various phases of the semantic patch application
5process. These are:
6
7\begin{enumerate}
8\item Selecting and parsing the semantic patch.
9\item Selecting and parsing the C code.
10\item Application of the semantic patch to the C code.
11\item Transformation.
12\item Generation of the result.
13\end{enumerate}
14
15\noindent
16One can either initiate the complete process from step 1, or
17to perform step 1 or step 2 individually.
18
19Coccinelle has quite a lot of options. The most common usages are as
20follows, for a semantic match {\tt foo.cocci}, a C file {\tt foo.c}, and a
21directory {\tt foodir}:
22
23\begin{itemize}
24\item {\tt spatch -parse\_cocci foo.cocci}: Check that the semantic patch
25 is syntactically correct.
26\item {\tt spatch -parse\_c foo.c}: Check that the C file
27 is syntactically correct. The Coccinelle C parser tries to recover
28 during the parsing process, so if one function does not parse, it will
29 start up again with the next one. Thus, a parse error is often not a
30 cause for concern, unless it occurs in a function that is relevant to the
31 semantic patch.
32\item {\tt spatch -sp\_file foo.cocci foo.c}: Apply the semantic patch {\tt
33 foo.cocci} to the file {\tt foo.c} and print out any transformations as a
34 diff.
35\item {\tt spatch -sp\_file foo.cocci foo.c -debug}: The same as the
36 previous case, but print out some information about the matching process.
37\item {\tt spatch -sp\_file foo.cocci -dir foodir}: Apply the semantic
38 patch {\tt foo.cocci} to all of the C files in the directory {\tt foodir}.
39\item {\tt spatch -sp\_file foo.cocci -dir foodir -include\_headers}: Apply
40 the semantic patch {\tt foo.cocci} to all of the C files and header files
41 in the directory {\tt foodir}.
42\end{itemize}
43
44In the rest of this document, the options are annotated as follows:
45\begin{itemize}
46\item \FilledBigDiamondshape: a basic option, that is most likely of
47 interest to all users.
48\item \BigLowerDiamond: an option that is frequently used, often for better
49understanding the effect of a semantic patch.
50\item \BigDiamondshape: an option that is likely to be rarely used, but
51 whose effect is still comprehensible to a user.
52\item An option with no annotation is likely of interest only to
53 developers.
54\end{itemize}
55
56\section{Selecting and parsing the semantic patch}
57
58\subsection{Standalone options}
59
60\normal{-parse\_cocci $\langle$file$\rangle$}{ Parse a semantic
61patch file and print out some information about it.}
62
63\subsection{The semantic patch}
64
65\minimum{-sp\_file $\langle$file$\rangle$, -c $\langle$file$\rangle$,
66-cocci\_file $\langle$file$\rangle$}{ Specify the name of the file
67 containing the semantic patch. The file name should end in {\tt .cocci}.
68All three options do the same thing; the last two are deprecated.}
69
70\subsection{Isomorphisms}
71
72\rare{-iso, -iso\_file}{ Specify a file containing isomorphisms to be used in
73place of the standard one. Normally one should use the {\tt using}
74construct within a semantic patch to specify isomorphisms to be used {\em
75 in addition to} the standard ones.}
76
b1b2de81
C
77\rare{-iso\_limit $\langle$int$\rangle$} Limit the depth of application of
78isomorphisms to the specified integer.
79
80\rare{-no\_iso\_limit} Put no limit on the number of times that
81isomorphisms can be applied. This is the default.
82
faf9a90c
C
83\developer{-track\_iso}{ Gather information about isomorphism usage.}
84
85\developer{-profile\_iso}{ Gather information about the time required for
86isomorphism expansion.}
87
88\subsection{Display options}
89
90\rare{-show\_cocci}{Show the semantic patch that is being processed before
91 expanding isomorphisms.}
92
93\rare{-show\_SP}{Show the semantic patch that is being processed after
94 expanding isomorphisms.}
95
96\rare{-show\_ctl\_text}{ Show the representation
97of the semantic patch in CTL.}
98
99\rare{-ctl\_inline\_let}{ Sometimes {\tt let} is used to name
100intermediate terms CTL representation. This option causes the let-bound
101terms to be inlined at the point of their reference.
102This option implicitly sets {\bf -show\_ctl\_text}.}
103
104\rare{-ctl\_show\_mcodekind}{ Show
105transformation information within the CTL representation
106of the semantic patch. This option implicitly sets {\bf -show\_ctl\_text}.}
107
108\rare{-show\_ctl\_tex}{ Create a LaTeX files showing the representation
109of the semantic patch in CTL.}
110
111\section{Selecting and parsing the C files}
112
113\subsection{Standalone options}
114
115\normal{-parse\_c $\langle$file/dir$\rangle$}{ Parse a {\tt .c} file or all
116 of the {\tt .c} files in a directory. This generates information about
117 any parse errors encountered.}
118
119\normal{-parse\_h $\langle$file/dir$\rangle$}{ Parse a {\tt .h} file or all
120 of the {\tt .h} files in a directory. This generates information about
121 any parse errors encountered.}
122
123\normal{-parse\_ch $\langle$file/dir$\rangle$}{ Parse a {\tt .c} or {\tt
124 .h} file or all of the {\tt .c} or {\tt .h} files in a directory. This
125 generates information about any parse errors encountered.}
126
127\normal{-control\_flow $\langle$file$\rangle$, -control\_flow
128$\langle$file$\rangle$:$\langle$function$\rangle$}{ Print a control-flow
129graph for all of the functions in a file or for a specific function in a
130file. This requires {\tt dot} (http://www.graphviz.org/) and {\tt gv}.}
131
132\rare{-type\_c $\langle$file$\rangle$}{ Parse a C file and pretty-print a
133version including type information.}
134
135\developer{-tokens\_c $\langle$file$\rangle$}{Prints the tokens in a C
136 file.}
137
138\developer{-parse\_unparse $\langle$file$\rangle$}{Parse and then reconstruct
139 a C file.}
140
141\developer{-compare\_c $\langle$file$\rangle$ $\langle$file$\rangle$,
142 -compare\_c\_hardcoded}{Compares one C file to another, or compare the
143file tests/compare1.c to the file tests/compare2.c.}
144
145\developer{-test\_cfg\_ifdef $\langle$file$\rangle$}{Do some special
146processing of \#ifdef and display the resulting control-flow graph. This
147requires {\tt dot} and {\tt gv}.}
148
149\developer{-test\_attributes $\langle$file$\rangle$,
150 -test\_cpp $\langle$file$\rangle$}{
151Test the parsing of cpp code and attributes, respectively.}
152
153\subsection{Selecting C files}
154
708f4980
C
155An argument that ends in {\tt .c} is assumed to be a C file to process.
156Normally, only one C file or one directory is specified. If multiple C
157files are specified, they are treated in parallel, {\em i.e.}, the first
158semantic patch rule is applied to all functions in all files, then the
159second semantic patch rule is applied to all functions in all files, etc.
160If a directory is specified then no files may be specified and only the
faf9a90c
C
161rightmost directory specified is used.
162
faf9a90c
C
163\normal{-include\_headers}{ This option causes header files to be processed
164independently. This option only makes sense if a directory is specified
165using {\bf -dir}.}
166
167\normal{-use\_glimpse}{ Use a glimpse index to select the files to which
168a semantic patch may be relevant. This option requires that a directory is
169specified. The index may be created using the script {\tt
170 coccinelle/scripts/ glimpseindex\_cocci.sh}. Glimpse is available at
171http://webglimpse.net/. In conjunction with the option {\bf -patch\_cocci}
172this option prints the regular expression that will be passed to glimpse.}
173
708f4980
C
174\rare{-dir}{ Specify a directory containing C files to process. By
175 default, the include path will be set to the ``include'' subdirectory of
176 this directory. A different include path can be specified using the
177 option {\bf -I}. {\bf -dir} only considers the rightmost directory in
178 the argument list. This behavior is convenient for creating a script
179 that always works on a single directory, but allows the user of the
180 script to override the provided directory with another one. Spatch
181 collects the files in the directory using {\tt find} and does not follow
182 symbolic links.}
183
faf9a90c
C
184\developer{-kbuild\_info $\langle$file$\rangle$}{ The specified file
185 contains information about which sets of files should be considered in
186 parallel.}
187
188\developer{-disable\_worth\_trying\_opt}{Normally, a C file is only
189 processed if it contains some keywords that have been determined to be
190 essential for the semantic patch to match somewhere in the file. This
191 option disables this optimization and tries the semantic patch on all files.}
192
193\developer{-test $\langle$file$\rangle$}{ A shortcut for running Coccinelle
194on the semantic patch ``file{\tt{.cocci}}'' and the C file ``file{\tt{.c}}''.}
195
196\developer{-testall}{A shortcut for running Coccinelle on all files in a
197 subdirectory {\tt tests} such that there are all of a {\tt .cocci} file, a {\tt
198 .c} file, and a {\tt .res} file, where the {\tt .res} contains the
199 expected result.}
200
201\developer{-test\_okfailed, -test\_regression\_okfailed} Other options for
202keeping track of tests that have succeeded and failed.
203
204\developer{-compare\_with\_expected}{Compare the result of applying
205 Coccinelle to file{\tt{.c}} to the file file{\tt{.res}} representing the
206 expected result.}
207
708f4980
C
208\developer{-expected\_score\_file $\langle$file$\rangle$}{
209which score file to compare with in the testall run}
210
faf9a90c
C
211\subsection{Parsing C files}
212
213\rare{-show\_c}{Show the C code that is being processed.}
214
215\rare{-parse\_error\_msg}{Show parsing errors in the C file.}
216
217\rare{-type\_error\_msg}{Show information about where the C type checker
218 was not able to determine the type of an expression.}
219
708f4980
C
220\rare{-int\_bits $\langle$n$\rangle$, -long\_bits
221$\langle$n$\rangle$}{Provide integer size information. n is the number of
222bits in an unsigned integer or unsigned long, respectively. If only the
223option {\bf -int\_bits} is used, unsigned longs will be assumed to have
224twice as many bits as unsigned integers. If only the option {\bf
225-long\_bits} is used, unsigned ints will be assumed to have half as many
226bits as unsigned integers. This information is only used in determining
227the types of integer constants, according to the ANSI C standard (C89). If
228neither is provided, the type of an integer constant is determined by the
229sequence of ``u'' and ``l'' annotations following the constant. If there
230is none, the constant is assumed to be a signed integer. If there is only
231``u'', the constant is assumed to be an unsigned integer, etc.}
232
002099fc
C
233\rare{-no\_loops}{Drop back edges for loops. This may make a semantic
234 patch/match run faster, at the cost of not finding matches that wrap
235 around loops.}
236
faf9a90c
C
237\developer{-use\_cache} Use preparsed versions of the C files that are
238stored in a cache.
239
240\developer{-debug\_cpp, -debug\_lexer, -debug\_etdt,
241 -debug\_typedef}{Various options for debugging the C parser.}
242
243\developer{-filter\_msg, -filter\_define\_error,
244 -filter\_passed\_level}{Various options for debugging the C parser.}
245
246\developer{-only\_return\_is\_error\_exit}{In matching ``{\tt{\ldots}}'' in
247 a semantic patch or when forall is specified, a rule must match all
248 control-flow paths starting from a node matching the beginning of the
249 rule. This is relaxed, however, for error handling code. Normally, error
250 handling code is considered to be a conditional with only a then branch
251 that ends in goto, break, continue, or return. If this option is set,
252 then only a then branch ending in a return is considered to be error
253 handling code. Usually a better strategy is to use {\tt when strict} in
254 the semantic patch, and then match explicitly the case where there is a
255 conditional whose then branch ends in a return.}
256
257\subsubsection*{Macros and other preprocessor code}
258
708f4980 259\normal{-macro\_file $\langle$file$\rangle$}{
faf9a90c
C
260 Extra macro definitions to be taken into account when parsing the C
261 files.}
262
708f4980
C
263\normal{-macro\_file\_builtins $\langle$file$\rangle$}{
264 Builtin macro definitions to be taken into account when parsing the C
265 files.}
266
267\rare{-ifdef\_to\_if,-no\_ifdef\_to\_if}{
268The option {\bf -ifdef\_to\_if}
269represents an {\tt \#ifdef} in the source code as a conditional in the
270control-flow graph when doing so represents valid code. {\bf
271-no\_ifdef\_to\_if} disables this feature. {\bf -ifdef\_to\_if} is the
272default.
273}
faf9a90c
C
274
275\rare{-use\_if0\_code}{ Normally code under \#if 0 is ignored. If this
276option is set then the code is considered, just like the code under any
277other \#ifdef.}
278
279\developer{-noadd\_typedef\_root}{This seems to reduce the scope of a
280 typedef declaration found in the C code.}
281
282\subsubsection*{Include files}
283
284\normal{-all\_includes, -local\_includes, -no\_includes}{
285These options control which include files mentioned in a C file are taken into
286account. {\bf -all\_includes} indicates that all included files will be
287processed. {\bf -local\_includes} indicates that only included files in
288the current directory will be processed. {\bf -no\_includes} indicates
289that no included files will be processed. If the semantic patch contains
290type specifications on expression metavariables, then the default is {\bf
291-local\_includes}. Otherwise the default is {\bf -no\_includes}. At most
292one of these options can be specified.}
293
294\normal{-I $\langle$path$\rangle$}{ This option specifies the directory in
295 which to find non-local include files. This option should be used only
296 once, as each use will overwrite the preceding one.}
297
7f004419
C
298\rare{-relax\_include\_path}{This option when combined with -all\_includes
299 causes the search for local
faf9a90c
C
300 include files to consider the directory specified using {\bf -I} if the
301 included file is not found in the current directory.}
302
303\section{Application of the semantic patch to the C code}
304
305\subsection{Feedback at the rule level during the application of the
306 semantic patch}
307
308\normal{-show\_bindings}{
309Show the environments with respect to which each rule is applied and the
310bindings that result from each such application.}
311
312\normal{-show\_dependencies}{ Show the status (matched or unmatched) of the
313rules on which a given rule depends. {\bf -show\_dependencies} implicitly
314sets {\bf -show\_bindings}, as the values of the dependencies are
315environment-specific.}
316
317\normal{-show\_trying}{
318Show the name of each program element to which each rule is applied.}
319
320\normal{-show\_transinfo}{
321Show information about each transformation that is performed.
322The node numbers that are referenced are the number of the nodes in the
323control-flow graph, which can be seen using the option {\bf -control\_flow}
324(the initial control-flow graph only) or the option {\bf -show\_flow} (the
325control-flow graph before and after each rule application).}
326
327\normal{-show\_misc}{Show some miscellaneous information.}
328
329\rare{-show\_flow $\langle$file$\rangle$, -show\_flow
330 $\langle$file$\rangle$:$\langle$function$\rangle$} Show the control-flow
331graph before and after the application of each rule.
332
333\developer{-show\_before\_fixed\_flow}{This is similar to {\bf
334 -show\_flow}, but shows a preliminary version of the control-flow graph.}
335
336\subsection{Feedback at the CTL level during the application of the
337 semantic patch}
338
339\normal{-verbose\_engine}{Show a trace of the matching of atomic terms to C
340 code.}
341
342\rare{-verbose\_ctl\_engine}{Show a trace of the CTL matching process.
343 This is unfortunately rather voluminous and not so helpful for someone
344 who is not familiar with CTL in general and the translation of SmPL into
345 CTL specifically. This option implicitly sets the option {\bf
346 -show\_ctl\_text}.}
347
348\rare{-graphical\_trace}{Create a pdf file containing the control flow
349 graph annotated with the various nodes matched during the CTL matching
350 process. Unfortunately, except for the most simple examples, the output
351 is voluminous, and so the option is not really practical for most
352 examples. This requires {\tt dot} (http://www.graphviz.org/) and {\tt
353 pdftk}.}
354
355\rare{-gt\_without\_label}{The same as {\bf -graphical\_trace}, but the PDF
356 file does not contain the CTL code.}
357
358\rare{-partial\_match}{
359Report partial matches of the semantic patch on the C file. This can
360 be substantially slower than normal matching.}
361
362\rare{-verbose\_match}{
363Report on when CTL matching is not applied to a function or other program
364unit because it does not contain some required atomic pattern.
365This can be viewed as a simpler, more efficient, but less informative
366version of {\bf -partial\_match}.}
367
368\subsection{Actions during the application of the semantic patch}
369
370\rare{-allow\_inconsistent\_paths}{Normally, a term that is transformed
371 should only be accessible from other terms that are matched by the
372 semantic patch. This option removes this constraint. Doing so, is
373 unsafe, however, because the properties that hold along the matched path
374 might not hold at all along the unmatched path.}
375
376\rare{-disallow\_nested\_exps}{In an expression that contains repeated
377 nested subterms, {\em e.g.} of the form {\tt f(f(x))}, a pattern can
378 match a single expression in multiple ways, some nested inside others.
379 This option causes the matching process to stop immediately at the
380 outermost match. Thus, in the example {\tt f(f(x))}, the possibility
381 that the pattern {\tt f(E)}, with metavariable {\tt E}, matches with {\tt
382 E} as {\tt x} will not be considered.}
383
978fd7e5
C
384\rare{-no\_safe\_expressions}{normally, we check that an expression does
385 not match something earlier in the disjunction. But for large
386 disjunctions, this can result in a very big CTL formula. So this
387 option give the user the option to say he doesn't want this feature,
388 if that is the case.}
389
faf9a90c
C
390\rare{-pyoutput coccilib.output.Gtk, -pyoutput coccilib.output.Console}{
391This controls whether Python output is sent to Gtk or to the console. {\bf
392 -pyoutput coccilib.output.Console} is the default. The Gtk option is
393currently not well supported.}
394
395\developer{-loop}{When there is ``{\tt{\ldots}}'' in the semantic patch,
396 the CTL operator {\sf AU} is used if the current function does not
397 contain a loop, and {\sf AW} may be used if it does. This option causes
398 {\sf AW} always to be used.}
399
400\developer{-steps $\langle$int$\rangle$}{
401This limits the number of steps performed by the CTL engine to the
402specified number. This option is unsafe as it might cause a rule to fail
403due to running out of steps rather than due to not matching.}
404
405\developer{-bench $\langle$int$\rangle$}{This collects various information
406 about the operations performed during the CTL matching process.}
407
408\developer{-popl, -popl\_mark\_all, -popl\_keep\_all\_wits}{
409These options use a simplified version of the SmPL language. {\bf
410 -popl\_mark\_all} and {\bf -popl\_keep\_all\_wits} implicitly set {\bf
411 -popl}.}
412
413\section{Generation of the result}
414
415Normally, the only output is a diff printed to standard output.
416
7f004419
C
417\normal{-keep\_comments}{Don't remove comments adjacent to removed code.}
418
708f4980
C
419\normal{-linux\_spacing, -smpl\_spacing}{Control the spacing within the code
420 added by the semantic patch. The option {\bf -linux\_spacing} causes
421 spatch to follow the conventions of Linux, regardless of the spacing in
422 the semantic patch. This is the default. The option {\bf
423 -smpl\_spacing} causes spatch to follow the spacing given in the semantic
424 patch, within individual lines.}
425
faf9a90c
C
426\rare{-o $\langle$file$\rangle$}{ The output file.}
427
428\rare{-inplace}{ Modify the input file.}
429
430\rare{-outplace}{ Store modifications in a .cocci\_res file.}
431
432\rare{-no\_show\_diff}{ Normally, a diff between the original and transformed
433code is printed on the standard output. This option causes this not to be
434done.}
435
436\rare{-U}{ Set number of diff context lines.}
437
438\rare{-save\_tmp\_files}{Coccinelle creates some temporary
439 files in {\tt /tmp} that it deletes after use. This option causes these
440 files to be saved.}
441
442\developer{-debug\_unparsing}{Show some debugging information about the
443 generation of the transformed code. This has the side-effect of
444 deleting the transformed code.}
445
446\developer{-patch}{ Deprecated option.}
447
448
449\section{Other options}
450
451\subsection{Version information}
452
453\normal{-version}{ The version of Coccinelle. No other options are
454allowed.}
455
456\normal{-date}{ The date of the current version of Coccinelle. No other
457options are allowed.}
458
459\subsection{Help}
460
461\minimum{-h, -shorthelp}{ The most useful commands.}
462
463\minimum{-help, --help, -longhelp}{ A complete listing of the available
464commands.}
465
466\subsection{Controlling the execution of Coccinelle}
467
468\normal{-timeout $\langle$int$\rangle$}{ The maximum time in seconds for
469 processing a single file.}
470
471\rare{-max $\langle$int$\rangle$}{This option informs Coccinelle of the
472 number of instances of Coccinelle that will be run concurrently. This
473 option requires {\bf -index}. It is usually used with {\bf -dir}.}
474
475\rare{-index $\langle$int$\rangle$}{This option informs Coccinelle of
476 which of the concurrent instances is the current one. This option
477 requires {\bf -max}.}
478
479\rare{-mod\_distrib}{When multiple instances of Coccinelle are run in
480 parallel, normally the first instance processes the first $n$ files, the
481 second instance the second $n$ files, etc. With this option, the files
482 are distributed among the instances in a round-robin fashion.}
483
484\developer{-debugger}{Option for running Coccinelle from within the OCaml
485 debugger.}
486
487\developer{-profile}{ Gather timing information about the main Coccinelle
488functions.}
489
490\developer{-disable\_once}{Print various warning messages every time some
491condition occurs, rather than only once.}
492
493\subsection{Miscellaneous}
494
495\rare{-quiet}{Suppress most output. This is the default.}
496
497\developer{-pad, -hrule $\langle$dir$\rangle$, -xxx, -l1}{}
498