Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / docs / manual / introduction.tex
CommitLineData
708f4980
C
1\chapter{Introduction}
2%src: cocci website, LWN article
3
4Coccinelle is a tool to help automate repetitive
5source-to-source style-preserving program transformations
6on C source code, like for instance to perform some refactorings.
7%coupling: readme.txt
8Coccinelle is presented as a command line tool called \spatch that takes
9as input the name of a file containing the specification of a program
10transformation, called a {\em semantic patch}, and a set of C files,
11and then performs the transformation on all those C files.
12%synopsis ?
13
14To make it easy to express those transformations,
15Coccinelle proposes a WYSISWYG approach where the C programmer
16can leverage the things he already knows: the C syntax
17and the patch syntax. Indeed, with Coccinelle transformations
18are written in specific language called SmPL, for
19Semantic Patch Language, which as the name suggests is very
20close to the syntax of a patch, but which does not
21work at a line level, than traditional patches do.
22but a more high level, or semantic level.
23
24Here is an example of a simple program transformation.
25To replace every calls to \verb+foo+ of any expression $x$
26to a call to \verb+bar+, create a semantic patch file \verb+ex1.cocci+
27(semantic patches usually ends with the \verb+.cocci+ filename extension)
28containing:
29\begin{verbatim}
30@@ expression x; @@
31
32- foo(x)
33+ bar(x)
34
35\end{verbatim}
36
37Then to ``apply'' the specified program transformation to a set of C files,
38simply do:
39\begin{verbatim}
40$ spatch -sp_file ex1.cocci *.c
41\end{verbatim}
5636bb2c
C
42
43
44Coccinelle primarily targets ANSI C, and supports some GCC extensions. It
45has only partial support for K\&R C. K\&R function declarations are only
46recognized if the parameter declarations are indented. Furthermore, the
47parameter names are subsequently considered to be type names, due to
48confusion with function prototypes, in which a name by itself is indeed the
49name of a type.
50
51
708f4980
C
52%command line:
53
54%can do inplace, or with git, cf chapter on developing ...
55
56%Other approaches
57%instead of
58%expressing the transformation on the internal representation
59%of a C frontend, for instance the abstract syntax tree
60%used internally by gcc, which would require for the user
61%to learn how to use this internal data structure,
62
63%if can find and transform, can also find, so semantic grep.
64
65%vs regexp
66%vs ast
67
68%features:
69%src: darcs manual
5636bb2c
C
70
71%%% Local Variables:
72%%% mode: LaTeX
73%%% coding: utf-8
74%%% TeX-PDF-mode: t
75%%% ispell-local-dictionary: "american"
76%%% End: