Release coccinelle-0.2.4rc6
[bpt/coccinelle.git] / docs / manual / cocci_syntax.tex
CommitLineData
faf9a90c
C
1
2%\section{The SmPL Grammar}
3
4% This section presents the SmPL grammar. This definition follows closely
5% our implementation using the Menhir parser generator \cite{menhir}.
6
7This document presents the grammar of the SmPL language used by the
7f004419 8\href{http://coccinelle.lip6.fr/}{Coccinelle tool}. For the most
faf9a90c
C
9part, the grammar is written using standard notation. In some rules,
10however, the left-hand side is in all uppercase letters. These are
11macros, which take one or more grammar rule right-hand-sides as
12arguments. The grammar also uses some unspecified nonterminals, such
b1b2de81
C
13as \T{id}, \T{const}, etc. These refer to the sets suggested by
14the name, {\em i.e.}, \T{id} refers to the set of possible
15C-language identifiers, while \T{const} refers to the set of
978fd7e5 16possible C-language constants.
708f4980 17%
978fd7e5 18\ifhevea
708f4980 19A PDF version of this documentation is available at
951c7801 20\url{http://coccinelle.lip6.fr/docs/main_grammar.pdf}.
708f4980 21\else
faf9a90c 22A HTML version of this documentation is available online at
951c7801 23\url{http://coccinelle.lip6.fr/docs/main_grammar.html}.
708f4980 24\fi
faf9a90c 25
faf9a90c
C
26\section{Program}
27
28\begin{grammar}
29 \RULE{\rt{program}}
30 \CASE{\any{\NT{include\_cocci}} \some{\NT{changeset}}}
31
32 \RULE{\rt{include\_cocci}}
33 \CASE{using \NT{string}}
34 \CASE{using \NT{pathToIsoFile}}
5636bb2c 35 \CASE{virtual \T{id} \ANY{, \T{id}}}
faf9a90c
C
36
37 \RULE{\rt{changeset}}
38 \CASE{\NT{metavariables} \NT{transformation}}
b1b2de81 39 \CASE{\NT{script\_metavariables} \T{script\_code}}
faf9a90c 40% \CASE{\NT{metavariables} \ANY{--- filename +++ filename} \NT{transformation}}
faf9a90c
C
41\end{grammar}
42
b1b2de81
C
43\noindent
44\T{script\_code} is any code in the chosen scripting language. Parsing of
45the semantic patch does not check the validity of this code; any errors are
978fd7e5
C
46first detected when the code is executed. Furthermore, \texttt{@} should
47not be use in this code. Spatch scans the script code for the next
48\texttt{@} and considers that to be the beginning of the next rule, even if
49\texttt{@} occurs within e.g., a string or a comment.
b1b2de81 50
5636bb2c
C
51\texttt{virtual} keyword is used to declare virtual rules. Virtual
52rules may be subsequently used as a dependency for the rules in the
53SmPL file. Whether a virtual rule is defined or not is controlled by
54the \texttt{-D} option on the command line.
55
faf9a90c
C
56% Between the metavariables and the transformation rule, there can be a
57% specification of constraints on the names of the old and new files,
58% analogous to the filename specifications in the standard patch syntax.
59% (see Figure \ref{scsiglue_patch}).
60
b1b2de81 61\section{Metavariables for transformations}
faf9a90c
C
62
63The \NT{rulename} portion of the metavariable declaration can specify
64properties of a rule such as its name, the names of the rules that it
65depends on, the isomorphisms to be used in processing the rule, and whether
66quantification over paths should be universal or existential. The optional
67annotation {\tt expression} indicates that the pattern is to be considered
68as matching an expression, and thus can be used to avoid some parsing
69problems.
70
71The \NT{metadecl} portion of the metavariable declaration defines various
72types of metavariables that will be used for matching in the transformation
73section.
74
75\begin{grammar}
76 \RULE{\rt{metavariables}}
77 \CASE{@@ \any{\NT{metadecl}} @@}
78 \CASE{@ \NT{rulename} @ \any{\NT{metadecl}} @@}
79
80 \RULE{\rt{rulename}}
81 \CASE{\T{id} \OPT{extends \T{id}} \OPT{depends on \NT{dep}} \opt{\NT{iso}}
82 \opt{\NT{disable-iso}} \opt{\NT{exists}} \opt{expression}}
b1b2de81 83
faf9a90c
C
84 \RULE{\rt{dep}}
85 \CASE{\NT{pnrule}}
86 \CASE{\NT{dep} \&\& \NT{dep}}
87 \CASE{\NT{dep} || \NT{dep}}
88
89 \RULE{\rt{pnrule}}
90 \CASE{\T{id}}
91 \CASE{!\T{id}}
92 \CASE{ever \T{id}}
93 \CASE{never \T{id}}
94 \CASE{(\NT{dep})}
95
96 \RULE{\rt{iso}}
97 \CASE{using \NT{string} \ANY{, \NT{string}}}
98
99 \RULE{\rt{disable-iso}}
100 \CASE{disable \NT{COMMA\_LIST}\mth{(}\T{id}\mth{)}}
101
102 \RULE{\rt{exists}}
103 \CASE{exists}
104 \CASE{forall}
105% \CASE{\opt{reverse} forall}
106
107 \RULE{\rt{COMMA\_LIST}\mth{(}\rt{elem}\mth{)}}
108 \CASE{\NT{elem} \ANY{, \NT{elem}}}
109\end{grammar}
110
b1b2de81 111The keyword \KW{disable} is normally used with the names of
faf9a90c
C
112isomorphisms defined in standard.iso or whatever isomorphism file has been
113included. There are, however, some other isomorphisms that are built into
114the implementation of Coccinelle and that can be disabled as well. Their
413ffc02 115names are given below. In each case, the text describes the standard
faf9a90c
C
116behavior. Using \NT{disable-iso} with the given name disables this behavior.
117
118\begin{itemize}
119\item \KW{optional\_storage}: A SmPL function definition that does not
120 specify any visibility (i.e., static or extern), or a SmPL variable
121 declaration that does not specify any storage (i.e., auto, static,
122 register, or extern), matches a function declaration or variable
123 declaration with any visibility or storage, respectively.
124\item \KW{optional\_qualifier}: This is similar to \KW{optional\_storage},
125 except that here is it the qualifier (i.e., const or volatile) that does
126 not have to be specified in the SmPL code, but may be present in the C code.
127\item \KW{value\_format}: Integers in various formats, e.g., 1 and 0x1, are
128 considered to be equivalent in the matching process.
129\item \KW{comm\_assoc}: An expression of the form \NT{exp} \NT{bin\_op}
130 \KW{...}, where \NT{bin\_op} is commutative and associative, is
131 considered to match any top-level sequence of \NT{bin\_op} operators
132 containing \NT{exp} as the top-level argument.
133\end{itemize}
134
135The possible types of metavariable declarations are defined by the grammar
136rule below. Metavariables should occur at least once in the transformation
137immediately following their declaration. Fresh metavariables must only be
138used in {\tt +} code. These properties are not expressed in the grammar,
139but are checked by a subsequent analysis. The metavariables are designated
140according to the kind of terms they can match, such as a statement, an
141identifier, or an expression. An expression metavariable can be further
413ffc02
C
142constrained by its type. A declaration metavariable matches the
143declaration of one or more variables, all sharing the same type
144specification ({\em e.g.}, {\tt int a,b,c=3;}). A field metavariable does
145the same, but for structure fields.
faf9a90c
C
146
147\begin{grammar}
148 \RULE{\rt{metadecl}}
149 \CASE{fresh identifier \NT{ids} ;}
951c7801 150 \CASE{identifier \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_regexp}\mth{)} ;}
ae4735db 151 \CASE{identifier \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_virt\_or\_not\_eq}\mth{)} ;}
faf9a90c
C
152 \CASE{parameter \opt{list} \NT{ids} ;}
153 \CASE{parameter list [ \NT{id} ] \NT{ids} ;}
88e71198 154 \CASE{parameter list [ \NT{const} ] \NT{ids} ;}
faf9a90c
C
155 \CASE{type \NT{ids} ;}
156 \CASE{statement \opt{list} \NT{ids} ;}
413ffc02
C
157 \CASE{declaration \opt{list} \NT{ids} ;}
158 \CASE{field \opt{list} \NT{ids} ;}
faf9a90c
C
159 \CASE{typedef \NT{ids} ;}
160 \CASE{declarer name \NT{ids} ;}
161% \CASE{\opt{local} function \NT{pmid\_with\_not\_eq\_list} ;}
951c7801 162 \CASE{declarer \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_regexp}\mth{)} ;}
faf9a90c
C
163 \CASE{declarer \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
164 \CASE{iterator name \NT{ids} ;}
951c7801 165 \CASE{iterator \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_regexp}\mth{)} ;}
faf9a90c
C
166 \CASE{iterator \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
167% \CASE{error \NT{pmid\_with\_not\_eq\_list} ; }
168 \CASE{\opt{local} idexpression \opt{\NT{ctype}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
169 \CASE{\opt{local} idexpression \OPT{\ttlb \NT{ctypes}\ttrb~\any{*}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
170 \CASE{\opt{local} idexpression \some{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
171 \CASE{expression list \NT{ids} ;}
172 \CASE{expression \some{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
e6509c05
C
173 \CASE{expression enum \any{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
174 \CASE{expression struct \any{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
175 \CASE{expression union \any{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
faf9a90c 176 \CASE{expression \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_ceq}\mth{)} ;}
88e71198
C
177 \CASE{expression list [ \NT{id} ] \NT{ids} ;}
178 \CASE{expression list [ \NT{const} ] \NT{ids} ;}
faf9a90c
C
179 \CASE{\NT{ctype} [ ] \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
180 \CASE{\NT{ctype} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_ceq}\mth{)} ;}
181 \CASE{\ttlb \NT{ctypes}\ttrb~\any{*} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_ceq}\mth{)} ;}
182 \CASE{\ttlb \NT{ctypes}\ttrb~\any{*} [ ] \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
183 \CASE{constant \opt{\NT{ctype}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
184 \CASE{constant \OPT{\ttlb \NT{ctypes}\ttrb~\any{*}} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq}\mth{)} ;}
185 \CASE{position \opt{any} \NT{COMMA\_LIST}\mth{(}\NT{pmid\_with\_not\_eq\_mid}\mth{)} ;}
186\end{grammar}
187
88e71198
C
188It is possible to specify that an expression list or a parameter list
189metavariable should match a specific number of expressions or parameters.
190
faf9a90c
C
191\begin{grammar}
192 \RULE{\rt{ids}}
193 \CASE{\NT{COMMA\_LIST}\mth{(}\NT{pmid}\mth{)}}
194
195 \RULE{\rt{pmid}}
196 \CASE{\T{id}}
197 \CASE{\NT{mid}}
198% \CASE{list}
199% \CASE{error}
200% \CASE{type}
201
202 \RULE{\rt{mid}} \CASE{\T{rulename\_id}.\T{id}}
203
951c7801
C
204 \RULE{\rt{pmid\_with\_regexp}}
205 \CASE{\NT{pmid} \~{}= \NT{regexp}}
206
faf9a90c 207 \RULE{\rt{pmid\_with\_not\_eq}}
5636bb2c
C
208 \CASE{\NT{pmid} \OPT{!= \NT{id\_or\_meta}}}
209 \CASE{\NT{pmid}
210 \OPT{!= \ttlb~\NT{COMMA\_LIST}\mth{(}\NT{id\_or\_meta}\mth{)} \ttrb}}
faf9a90c 211
55d38388 212 \RULE{\rt{pmid\_with\_virt\_or\_not\_eq}}
ae4735db 213 \CASE{virtual.\T{id}}
55d38388
C
214 \CASE{\NT{pmid\_with\_not\_eq}}
215
216 \RULE{\rt{pmid\_with\_not\_ceq}}
faf9a90c
C
217 \CASE{\NT{pmid} \OPT{!= \NT{id\_or\_cst}}}
218 \CASE{\NT{pmid} \OPT{!= \ttlb~\NT{COMMA\_LIST}\mth{(}\NT{id\_or\_cst}\mth{)} \ttrb}}
219
220 \RULE{\rt{id\_or\_cst}}
221 \CASE{\T{id}}
222 \CASE{\T{integer}}
223
5636bb2c
C
224 \RULE{\rt{id\_or\_meta}}
225 \CASE{\T{id}}
226 \CASE{\T{rulename\_id}.\T{id}}
227
faf9a90c
C
228 \RULE{\rt{pmid\_with\_not\_eq\_mid}}
229 \CASE{\NT{pmid} \OPT{!= \NT{mid}}}
230 \CASE{\NT{pmid} \OPT{!= \ttlb~\NT{COMMA\_LIST}\mth{(}\NT{mid}\mth{)} \ttrb}}
231\end{grammar}
232
233Subsequently, we refer to arbitrary metavariables as
234\mth{\msf{metaid}^{\mbox{\scriptsize{\it{ty}}}}}, where {\it{ty}}
235indicates the {\it metakind} used in the declaration of the variable.
236For example, \mth{\msf{metaid}^{\ssf{Type}}} refers to a metavariable
237that was declared using \texttt{type} and stands for any type.
238
239The \NT{ctype} and \NT{ctypes} nonterminals are used by both the grammar of
240metavariable declarations and the grammar of transformations, and are
241defined on page~\pageref{types}.
242
ae4735db
C
243An identifier metavariable with {\tt virtual} as its ``rule name'' is given
244a value on the command line. For example, if a semantic patch contains a
245rule that declares an identifier metavariable with the name {\tt
246 virtual.alloc}, then the command line could contain {\tt -D
247 alloc=kmalloc}. There should not be space around the {\tt =}. An
248example is in {\tt demos/vm.cocci} and {\tt demos/vm.c}.
249
b1b2de81
C
250\section{Metavariables for scripts}
251
252Metavariables for scripts can only be inherited from transformation rules.
253In the spirit of scripting languages such as Python that use dynamic
254typing, metavariables for scripts do not include type declarations.
255
256\begin{grammar}
257 \RULE{\rt{script\_metavariables}}
413ffc02 258 \CASE{@ script:\NT{language} \OPT{\NT{rulename}} \OPT{depends on \NT{dep}} @
b1b2de81 259 \any{\NT{script\_metadecl}} @@}
5636bb2c
C
260 \CASE{@ initialize:\NT{language} \OPT{depends on \NT{dep}} @}
261 \CASE{@ finalize:\NT{language} \OPT{depends on \NT{dep}} @}
b1b2de81 262
413ffc02 263 \RULE{\rt{language}} \CASE{python} \CASE{ocaml}
b1b2de81 264
413ffc02
C
265 \RULE{\rt{script\_metadecl}}
266 \CASE{\T{id} <{}< \T{rulename\_id}.\T{id} ;}
267 \CASE{\T{id} ;}
b1b2de81
C
268\end{grammar}
269
174d1640
C
270Currently, the only scripting languages that are supported are Python and
271OCaml, indicated using {\tt python} and {\tt ocaml}, respectively. The
b1b2de81
C
272set of available scripting languages may be extended at some point.
273
274Script rules declared with \KW{initialize} are run before the treatment of
275any file. Script rules declared with \KW{finalize} are run when the
276treatment of all of the files has completed. There can be at most one of
277each per scripting language (thus currently at most one of each).
278Initialize and finalize script rules do not have access to SmPL
279metavariables. Nevertheless, a finalize script rule can access any
280variables initialized by the other script rules, allowing information to be
281transmitted from the matching process to the finalize rule.
282
413ffc02
C
283A script metavariable that does not specify an origin, using \texttt{<<},
284is newly declared by the script. This metavariable should be assigned to a
285string and can be inherited by subsequent rules as an identifier. In
286Python, the assignment of such a metavariable $x$ should refer to the
287metavariable as {\tt coccinelle.\(x\)}. Examples are in the files
288\texttt{demos/pythontococci.cocci} and \texttt{demos/camltococci.cocci}.
289
290In an ocaml script, the following extended form of \textit{script\_metadecl}
291may be used:
292
293\begin{grammar}
294 \RULE{\rt{script\_metadecl}}
295 \CASE{(\T{id},\T{id}) <{}< \T{rulename\_id}.\T{id} ;}
296 \CASE{\T{id} <{}< \T{rulename\_id}.\T{id} ;}
297 \CASE{\T{id} ;}
298\end{grammar}
299
300\noindent
301In a declaration of the form \texttt{(\T{id},\T{id}) <{}<
302 \T{rulename\_id}.\T{id} ;}, the left component of \texttt{(\T{id},\T{id})}
303receives a string representation of the value of the inherited metavariable
304while the right component receives its abstract syntax tree. The file
305\texttt{parsing\_c/ast\_c.ml} in the Coccinelle implementation gives some
306information about the structure of the abstract syntax tree. Either the
307left or right component may be replaced by \verb+_+, indicating that the
308string representation or abstract syntax trees representation is not
309wanted, respectively.
310
faf9a90c
C
311\section{Transformation}
312
313The transformation specification essentially has the form of C code,
314except that lines to remove are annotated with \verb+-+ in the first
315column, and lines to add are annotated with \verb-+-. A
316transformation specification can also use {\em dots}, ``\verb-...-'',
317describing an arbitrary sequence of function arguments or instructions
318within a control-flow path. Dots may be modified with a {\tt when}
319clause, indicating a pattern that should not occur anywhere within the
320matched sequence. Finally, a transformation can specify a disjunction
321of patterns, of the form \mtt{( \mth{\mita{pat}_1} | \mita{\ldots} |
322 \mth{\mita{pat}_n} )} where each \texttt{(}, \texttt{|} or
323\texttt{)} is in column 0 or preceded by \texttt{\textbackslash}.
324
325The grammar that we present for the transformation is not actually the
326grammar of the SmPL code that can be written by the programmer, but is
327instead the grammar of the slice of this consisting of the {\tt -}
328annotated and the unannotated code (the context of the transformed lines),
329or the {\tt +} annotated code and the unannotated code. For example, for
330parsing purposes, the following transformation
331%presented in Section \ref{sec:seq2}
332is split into the two variants shown below and each is parsed
333separately.
334
335\begin{center}
336\begin{tabular}{c}
337\begin{lstlisting}[language=Cocci]
338 proc_info_func(...) {
339 <...
340@-- hostno
341@++ hostptr->host_no
342 ...>
343 }
344\end{lstlisting}\\
345\end{tabular}
346\end{center}
347
348{%\sizecodebis
349\begin{center}
350\begin{tabular}{p{5cm}p{3cm}p{5cm}}
351\begin{lstlisting}[language=Cocci]
352 proc_info_func(...) {
353 <...
354@-- hostno
355 ...>
356 }
357\end{lstlisting}
358&&
359\begin{lstlisting}[language=Cocci]
360 proc_info_func(...) {
361 <...
362@++ hostptr->host_no
363 ...>
364 }
365\end{lstlisting}
366\end{tabular}
367\end{center}
368}
369
370\noindent
371Requiring that both slices parse correctly ensures that the rule matches
372syntactically valid C code and that it produces syntactically valid C code.
373The generated parse trees are then merged for use in the subsequent
374matching and transformation process.
375
376The grammar for the minus or plus slice of a transformation is as follows:
377
378\begin{grammar}
379
380 \RULE{\rt{transformation}}
381 \CASE{\some{\NT{include}}}
382 \CASE{\NT{OPTDOTSEQ}\mth{(}\NT{expr}, \NT{when}\mth{)}}
383 \CASE{\NT{OPTDOTSEQ}\mth{(}\some{\NT{decl\_stmt}}, \NT{when}\mth{)}}
384 \CASE{\NT{OPTDOTSEQ}\mth{(}\NT{fundecl}, \NT{when}\mth{)}}
385
386 \RULE{\rt{include}}
387 \CASE{\#include \T{include\_string}}
388
389% \RULE{\rt{fun\_decl\_stmt}}
390% \CASE{\NT{decl\_stmt}}
391% \CASE{\NT{fundecl}}
392
393% \CASE{\NT{ctype}}
394% \CASE{\ttlb \NT{initialize\_list} \ttrb}
395% \CASE{\NT{toplevel\_seq\_start\_after\_dots\_init}}
396%
397% \RULE{\rt{toplevel\_seq\_start\_after\_dots\_init}}
398% \CASE{\NT{stmt\_dots} \NT{toplevel\_after\_dots}}
399% \CASE{\NT{expr} \opt{\NT{toplevel\_after\_exp}}}
400% \CASE{\NT{decl\_stmt\_expr} \opt{\NT{toplevel\_after\_stmt}}}
401%
402% \RULE{\rt{stmt\_dots}}
403% \CASE{... \any{\NT{when}}}
404% \CASE{<... \any{\NT{when}} \NT{nest\_after\_dots} ...>}
405% \CASE{<+... \any{\NT{when}} \NT{nest\_after\_dots} ...+>}
406
407 \RULE{\rt{when}}
408 \CASE{when != \NT{when\_code}}
409 \CASE{when = \NT{rule\_elem\_stmt}}
410 \CASE{when \NT{COMMA\_LIST}\mth{(}\NT{any\_strict}\mth{)}}
411 \CASE{when true != \NT{expr}}
412 \CASE{when false != \NT{expr}}
413
414 \RULE{\rt{when\_code}}
415 \CASE{\NT{OPTDOTSEQ}\mth{(}\some{\NT{decl\_stmt}}, \NT{when}\mth{)}}
416 \CASE{\NT{OPTDOTSEQ}\mth{(}\NT{expr}, \NT{when}\mth{)}}
417
418 \RULE{\rt{rule\_elem\_stmt}}
419 \CASE{\NT{one\_decl}}
420 \CASE{\NT{expr};}
421 \CASE{return \opt{\NT{expr}};}
422 \CASE{break;}
423 \CASE{continue;}
424 \CASE{\bs(\NT{rule\_elem\_stmt} \SOME{\bs| \NT{rule\_elem\_stmt}}\bs)}
425
426 \RULE{\rt{any\_strict}}
427 \CASE{any}
428 \CASE{strict}
429 \CASE{forall}
430 \CASE{exists}
431
432% \RULE{\rt{nest\_after\_dots}}
433% \CASE{\NT{decl\_stmt\_exp} \opt{\NT{nest\_after\_stmt}}}
434% \CASE{\opt{\NT{exp}} \opt{\NT{nest\_after\_exp}}}
435%
436% \RULE{\rt{nest\_after\_stmt}}
437% \CASE{\NT{stmt\_dots} \NT{nest\_after\_dots}}
438% \CASE{\NT{decl\_stmt} \opt{\NT{nest\_after\_stmt}}}
439%
440% \RULE{\rt{nest\_after\_exp}}
441% \CASE{\NT{stmt\_dots} \NT{nest\_after\_dots}}
442%
443% \RULE{\rt{toplevel\_after\_dots}}
444% \CASE{\opt{\NT{toplevel\_after\_exp}}}
445% \CASE{\NT{exp} \opt{\NT{toplevel\_after\_exp}}}
446% \CASE{\NT{decl\_stmt\_expr} \NT{toplevel\_after\_stmt}}
447%
448% \RULE{\rt{toplevel\_after\_exp}}
449% \CASE{\NT{stmt\_dots} \opt{\NT{toplevel\_after\_dots}}}
450%
451% \RULE{\rt{decl\_stmt\_expr}}
452% \CASE{TMetaStmList$^\ddag$}
453% \CASE{\NT{decl\_var}}
454% \CASE{\NT{stmt}}
455% \CASE{(\NT{stmt\_seq} \ANY{| \NT{stmt\_seq}})}
456%
457% \RULE{\rt{toplevel\_after\_stmt}}
458% \CASE{\NT{stmt\_dots} \opt{\NT{toplevel\_after\_dots}}}
459% \CASE{\NT{decl\_stmt} \NT{toplevel\_after\_stmt}}
460
461\end{grammar}
462
463\begin{grammar}
464 \RULE{\rt{OPTDOTSEQ}\mth{(}\rt{grammar\_ds}, \rt{when\_ds}\mth{)}}
465 \CASE{}\multicolumn{3}{r}{\hspace{1cm}
466 \KW{\opt{... \opt{\NT{when\_ds}}} \NT{grammar\_ds}
467 \ANY{... \opt{\NT{when\_ds}} \NT{grammar\_ds}}
468 \opt{... \opt{\NT{when\_ds}}}}
469 }
470
471% \CASE{\opt{... \opt{\NT{when\_ds}}} \NT{grammar}
472% \ANY{... \opt{\NT{when\_ds}} \NT{grammar}}
473% \opt{... \opt{\NT{when\_ds}}}}
474% \CASE{<... \any{\NT{when\_ds}} \NT{grammar} ...>}
475% \CASE{<+... \any{\NT{when\_ds}} \NT{grammar} ...+>}
476
477\end{grammar}
478
479\noindent
480Lines may be annotated with an element of the set $\{\mtt{-}, \mtt{+},
481\mtt{*}\}$ or the singleton $\mtt{?}$, or one of each set. \mtt{?}
482represents at most one match of the given pattern. \mtt{*} is used for
483semantic match, \emph{i.e.}, a pattern that highlights the fragments
484annotated with \mtt{*}, but does not perform any modification of the
485matched code. \mtt{*} cannot be mixed with \mtt{-} and \mtt{+}. There are
486some constraints on the use of these annotations:
487\begin{itemize}
488\item Dots, {\em i.e.} \texttt{...}, cannot occur on a line marked
489 \texttt{+}.
490\item Nested dots, {\em i.e.}, dots enclosed in {\tt <} and {\tt >}, cannot
491 occur on a line with any marking.
492\end{itemize}
493
0708f913
C
494Each element of a disjunction must be a proper term like an
495expression, a statement, an identifier or a declaration. Thus, the
413ffc02 496rule on the left below is not a syntactically correct SmPL rule. One may
0708f913
C
497use the rule on the right instead.
498
499\begin{center}
500 \begin{tabular}{l@{\hspace{5cm}}r}
501\begin{lstlisting}[language=Cocci]
502@@
503type T;
504T b;
505@@
506
507(
508 writeb(...,
509|
510 readb(
511)
512@--(T)
513 b)
514\end{lstlisting}
515 &
516\begin{lstlisting}[language=Cocci]
517@@
518type T;
519T b;
520@@
521
522(
523read
524|
525write
526)
527 (...,
528@-- (T)
529 b)
530\end{lstlisting}
531 \\
532 \end{tabular}
533\end{center}
534
faf9a90c
C
535\section{Types}
536\label{types}
537
538\begin{grammar}
539
540 \RULE{\rt{ctypes}}
541 \CASE{\NT{COMMA\_LIST}\mth{(}\NT{ctype}\mth{)}}
542
543 \RULE{\rt{ctype}}
544 \CASE{\opt{\NT{const\_vol}} \NT{generic\_ctype} \any{*}}
545 \CASE{\opt{\NT{const\_vol}} void \some{*}}
546 \CASE{(\NT{ctype} \ANY{| \NT{ctype}})}
547
548 \RULE{\rt{const\_vol}}
549 \CASE{const}
550 \CASE{volatile}
551
552 \RULE{\rt{generic\_ctype}}
553 \CASE{\NT{ctype\_qualif}}
554 \CASE{\opt{\NT{ctype\_qualif}} char}
555 \CASE{\opt{\NT{ctype\_qualif}} short}
556 \CASE{\opt{\NT{ctype\_qualif}} int}
557 \CASE{\opt{\NT{ctype\_qualif}} long}
558 \CASE{\opt{\NT{ctype\_qualif}} long long}
559 \CASE{double}
560 \CASE{float}
1eddfd50 561 \CASE{size\_t} \CASE{ssize\_t} \CASE{ptrdiff\_t}
c491d8ee 562 \CASE{enum \NT{id} \{ \NT{PARAMSEQ}\mth{(}\NT{dot\_expr}, \NT{exp\_whencode}\mth{)} \OPT{,} \}}
faf9a90c
C
563 \CASE{\OPT{struct\OR union} \T{id} \OPT{\{ \any{\NT{struct\_decl\_list}} \}}}
564
565 \RULE{\rt{ctype\_qualif}}
566 \CASE{unsigned}
567 \CASE{signed}
568
569 \RULE{\rt{struct\_decl\_list}}
570 \CASE{\NT{struct\_decl\_list\_start}}
571
572 \RULE{\rt{struct\_decl\_list\_start}}
573 \CASE{\NT{struct\_decl}}
574 \CASE{\NT{struct\_decl} \NT{struct\_decl\_list\_start}}
575 \CASE{... \opt{when != \NT{struct\_decl}}$^\dag$ \opt{\NT{continue\_struct\_decl\_list}}}
576
577 \RULE{\rt{continue\_struct\_decl\_list}}
578 \CASE{\NT{struct\_decl} \NT{struct\_decl\_list\_start}}
579 \CASE{\NT{struct\_decl}}
580
581 \RULE{\rt{struct\_decl}}
582 \CASE{\NT{ctype} \NT{d\_ident};}
583 \CASE{\NT{fn\_ctype} (* \NT{d\_ident}) (\NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)});)}
584 \CASE{\opt{\NT{const\_vol}} \T{id} \NT{d\_ident};}
585
586 \RULE{\rt{d\_ident}}
c491d8ee 587 \CASE{\T{id} \any{[\opt{\NT{expr}}]}}
faf9a90c
C
588
589 \RULE{\rt{fn\_ctype}}
590 \CASE{\NT{generic\_ctype} \any{*}}
591 \CASE{void \any{*}}
592
593 \RULE{\rt{name\_opt\_decl}}
594 \CASE{\NT{decl}}
595 \CASE{\NT{ctype}}
596 \CASE{\NT{fn\_ctype}}
597\end{grammar}
598
599$^\dag$ The optional \texttt{when} construct ends at the end of the line.
600
601\section{Function declarations}
602
603\begin{grammar}
604
605 \RULE{\rt{fundecl}}
606 \CASE{\opt{\NT{fn\_ctype}} \any{\NT{funinfo}} \NT{funid}
607 (\opt{\NT{PARAMSEQ}\mth{(}\NT{param}, \mth{\varepsilon)}})
608 \ttlb~\opt{\NT{stmt\_seq}} \ttrb}
609
610 \RULE{\rt{funproto}}
611 \CASE{\opt{\NT{fn\_ctype}} \any{\NT{funinfo}} \NT{funid}
612 (\opt{\NT{PARAMSEQ}\mth{(}\NT{param}, \mth{\varepsilon)}});}
613
614 \RULE{\rt{funinfo}}
615 \CASE{inline}
616 \CASE{\NT{storage}}
617% \CASE{\NT{attr}}
618
619 \RULE{\rt{storage}}
620 \CASE{static}
621 \CASE{auto}
622 \CASE{register}
623 \CASE{extern}
624
625 \RULE{\rt{funid}}
626 \CASE{\T{id}}
627 \CASE{\mth{\T{metaid}^{\ssf{Id}}}}
628% \CASE{\mth{\T{metaid}^{\ssf{Func}}}}
629% \CASE{\mth{\T{metaid}^{\ssf{LocalFunc}}}}
630
631 \RULE{\rt{param}}
632 \CASE{\NT{type} \T{id}}
633 \CASE{\mth{\T{metaid}^{\ssf{Param}}}}
634 \CASE{\mth{\T{metaid}^{\ssf{ParamList}}}}
635
636 \RULE{\rt{decl}}
637 \CASE{\NT{ctype} \NT{id}}
638 \CASE{\NT{fn\_ctype} (* \NT{id}) (\NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)})}
639 \CASE{void}
640 \CASE{\mth{\T{metaid}^{\ssf{Param}}}}
641\end{grammar}
642
643\begin{grammar}
644 \RULE{\rt{PARAMSEQ}\mth{(}\rt{gram\_p}, \rt{when\_p}\mth{)}}
645 \CASE{\NT{COMMA\_LIST}\mth{(}\NT{gram\_p} \OR \ldots \opt{\NT{when\_p}}\mth{)}}
646\end{grammar}
647
90aeb998
C
648To match a function it is not necessary to provide all of the annotations
649that appear before the function name. For example, the following semantic
650patch:
651
652\begin{lstlisting}[language=Cocci]
653@@
654@@
655
656foo() { ... }
657\end{lstlisting}
658
659\noindent
660matches a function declared as follows:
661
662\begin{lstlisting}[language=C]
663static int foo() { return 12; }
664\end{lstlisting}
665
666\noindent
667This behavior can be turned off by disabling the \KW{optional\_storage}
668isomorphism. If one adds code before a function declaration, then the
669effect depends on the kind of code that is added. If the added code is a
670function definition or CPP code, then the new code is placed before
671all information associated with the function definition, including any
672comments preceeding the function definition. On the other hand, if the new
673code is associated with the function, such as the addition of the keyword
674{\tt static}, the new code is placed exactly where it appears with respect
675to the rest of the function definition in the semantic patch. For example,
676
677\begin{lstlisting}[language=Cocci]
678@@
679@@
680
681+ static
682foo() { ... }
683\end{lstlisting}
684
685\noindent
686causes static to be placed just before the function name. The following
687causes it to be placed just before the type
688
689\begin{lstlisting}[language=Cocci]
690@@
691type T;
692@@
693
694+ static
695T foo() { ... }
696\end{lstlisting}
697
698\noindent
413ffc02 699It may be necessary to consider several cases to ensure that the added ode
90aeb998
C
700is placed in the right position. For example, one may need one pattern
701that considers that the function is declared {\tt inline} and another that
702considers that it is not.
703
faf9a90c
C
704%\newpage
705
706\section{Declarations}
707
708\begin{grammar}
709 \RULE{\rt{decl\_var}}
710% \CASE{\NT{type} \opt{\NT{id} \opt{[\opt{\NT{dot\_expr}}]}
711% \ANY{, \NT{id} \opt{[ \opt{\NT{dot\_expr}}]}}};}
712 \CASE{\NT{common\_decl}}
713 \CASE{\opt{\NT{storage}} \NT{ctype} \NT{COMMA\_LIST}\mth{(}\NT{d\_ident}\mth{)} ;}
714 \CASE{\opt{\NT{storage}} \opt{\NT{const\_vol}} \T{id} \NT{COMMA\_LIST}\mth{(}\NT{d\_ident}\mth{)} ;}
715 \CASE{\opt{\NT{storage}} \NT{fn\_ctype} ( * \NT{d\_ident} ) ( \NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)} ) = \NT{initialize} ;}
716 \CASE{typedef \NT{ctype} \NT{typedef\_ident} ;}
717
718 \RULE{\rt{one\_decl}}
719 \CASE{\NT{common\_decl}}
720 \CASE{\opt{\NT{storage}} \NT{ctype} \NT{id};}
721% \CASE{\NT{storage} \NT{ctype} \NT{id} \opt{[\opt{\NT{dot\\_expr}}]} = \NT{nest\\_expr};}
722 \CASE{\opt{\NT{storage}} \opt{\NT{const\_vol}} \T{id} \NT{d\_ident} ;}
723
724 \RULE{\rt{common\_decl}}
725 \CASE{\NT{ctype};}
726 \CASE{\NT{funproto}}
727 \CASE{\opt{\NT{storage}} \NT{ctype} \NT{d\_ident} = \NT{initialize} ;}
728 \CASE{\opt{\NT{storage}} \opt{\NT{const\_vol}} \T{id} \NT{d\_ident} = \NT{initialize} ;}
729 \CASE{\opt{\NT{storage}} \NT{fn\_ctype} ( * \NT{d\_ident} ) ( \NT{PARAMSEQ}\mth{(}\NT{name\_opt\_decl}, \mth{\varepsilon)} ) ;}
730 \CASE{\NT{decl\_ident} ( \OPT{\NT{COMMA\_LIST}\mth{(}\NT{expr}\mth{)}} ) ;}
731
732 \RULE{\rt{initialize}}
733 \CASE{\NT{dot\_expr}}
c491d8ee 734 \CASE{\mth{\T{metaid}^{\ssf{Initialiser}}}}
faf9a90c
C
735 \CASE{\ttlb~\opt{\NT{COMMA\_LIST}\mth{(}\NT{dot\_expr}\mth{)}}~\ttrb}
736
c491d8ee
C
737 \RULE{\rt{init\_list\_elem}}
738 \CASE{\NT{dot\_expr}}
739 \CASE{\NT{designator} = \NT{dot\_expr}}
740 \CASE{\NT{id} : \NT{dot\_expr}}
741
742 \RULE{\rt{designator}}
743 \CASE{. \NT{id}}
744 \CASE{[ \NT{dot\_expr} ]}
745 \CASE{[ \NT{dot\_expr} ... \NT{dot\_expr} ]}
746
faf9a90c
C
747 \RULE{\rt{decl\_ident}}
748 \CASE{\T{DeclarerId}}
749 \CASE{\mth{\T{metaid}^{\ssf{Declarer}}}}
750\end{grammar}
751
752\section{Statements}
753
754The first rule {\em statement} describes the various forms of a statement.
755The remaining rules implement the constraints that are sensitive to the
756context in which the statement occurs: {\em single\_statement} for a
757context in which only one statement is allowed, and {\em decl\_statement}
758for a context in which a declaration, statement, or sequence thereof is
759allowed.
760
761\begin{grammar}
762 \RULE{\rt{stmt}}
763 \CASE{\NT{include}}
764 \CASE{\mth{\T{metaid}^{\ssf{Stmt}}}}
765 \CASE{\NT{expr};}
766 \CASE{if (\NT{dot\_expr}) \NT{single\_stmt} \opt{else \NT{single\_stmt}}}
767 \CASE{for (\opt{\NT{dot\_expr}}; \opt{\NT{dot\_expr}}; \opt{\NT{dot\_expr}})
768 \NT{single\_stmt}}
769 \CASE{while (\NT{dot\_expr}) \NT{single\_stmt}}
770 \CASE{do \NT{single\_stmt} while (\NT{dot\_expr});}
771 \CASE{\NT{iter\_ident} (\any{\NT{dot\_expr}}) \NT{single\_stmt}}
772 \CASE{switch (\opt{\NT{dot\_expr}}) \ttlb \any{\NT{case\_line}} \ttrb}
773 \CASE{return \opt{\NT{dot\_expr}};}
774 \CASE{\ttlb~\opt{\NT{stmt\_seq}} \ttrb}
775 \CASE{\NT{NEST}\mth{(}\some{\NT{decl\_stmt}}, \NT{when}\mth{)}}
776 \CASE{\NT{NEST}\mth{(}\NT{expr}, \NT{when}\mth{)}}
777 \CASE{break;}
778 \CASE{continue;}
779 \CASE{\NT{id}:}
780 \CASE{goto \NT{id};}
781 \CASE{\ttlb \NT{stmt\_seq} \ttrb}
782
783 \RULE{\rt{single\_stmt}}
784 \CASE{\NT{stmt}}
785 \CASE{\NT{OR}\mth{(}\NT{stmt}\mth{)}}
786
787 \RULE{\rt{decl\_stmt}}
788 \CASE{\mth{\T{metaid}^{\ssf{StmtList}}}}
789 \CASE{\NT{decl\_var}}
790 \CASE{\NT{stmt}}
791 \CASE{\NT{OR}\mth{(}\NT{stmt\_seq}\mth{)}}
792
793 \RULE{\rt{stmt\_seq}}
794 \CASE{\any{\NT{decl\_stmt}}
795 \opt{\NT{DOTSEQ}\mth{(}\some{\NT{decl\_stmt}},
796 \NT{when}\mth{)} \any{\NT{decl\_stmt}}}}
797 \CASE{\any{\NT{decl\_stmt}}
798 \opt{\NT{DOTSEQ}\mth{(}\NT{expr},
799 \NT{when}\mth{)} \any{\NT{decl\_stmt}}}}
800
801 \RULE{\rt{case\_line}}
802 \CASE{default :~\NT{stmt\_seq}}
803 \CASE{case \NT{dot\_expr} :~\NT{stmt\_seq}}
804
805 \RULE{\rt{iter\_ident}}
806 \CASE{\T{IteratorId}}
807 \CASE{\mth{\T{metaid}^{\ssf{Iterator}}}}
808\end{grammar}
809
810\begin{grammar}
811 \RULE{\rt{OR}\mth{(}\rt{gram\_o}\mth{)}}
812 \CASE{( \NT{gram\_o} \ANY{\ttmid \NT{gram\_o}})}
813
814 \RULE{\rt{DOTSEQ}\mth{(}\rt{gram\_d}, \rt{when\_d}\mth{)}}
815 \CASE{\ldots \opt{\NT{when\_d}} \ANY{\NT{gram\_d} \ldots \opt{\NT{when\_d}}}}
816
817 \RULE{\rt{NEST}\mth{(}\rt{gram\_n}, \rt{when\_n}\mth{)}}
818 \CASE{<\ldots \opt{\NT{when\_n}} \NT{gram\_n} \ANY{\ldots \opt{\NT{when\_n}} \NT{gram\_n}} \ldots>}
819 \CASE{<+\ldots \opt{\NT{when\_n}} \NT{gram\_n} \ANY{\ldots \opt{\NT{when\_n}} \NT{gram\_n}} \ldots+>}
820\end{grammar}
821
822\noindent
823OR is a macro that generates a disjunction of patterns. The three
824tokens \T{(}, \T{\ttmid}, and \T{)} must appear in the leftmost
825column, to differentiate them from the parentheses and bit-or tokens
826that can appear within expressions (and cannot appear in the leftmost
827column). These token may also be preceded by \texttt{\bs}
828when they are used in an other column. These tokens are furthermore
829different from (, \(\mid\), and ), which are part of the grammar
830metalanguage.
831
832\section{Expressions}
833
834A nest or a single ellipsis is allowed in some expression contexts, and
835causes ambiguity in others. For example, in a sequence \mtt{\ldots
836\mita{expr} \ldots}, the nonterminal \mita{expr} must be instantiated as an
837explicit C-language expression, while in an array reference,
838\mtt{\mth{\mita{expr}_1} \mtt{[} \mth{\mita{expr}_2} \mtt{]}}, the
839nonterminal \mth{\mita{expr}_2}, because it is delimited by brackets, can
840be also instantiated as \mtt{\ldots}, representing an arbitrary expression. To
841distinguish between the various possibilities, we define three nonterminals
842for expressions: {\em expr} does not allow either top-level nests or
843ellipses, {\em nest\_expr} allows a nest but not an ellipsis, and {\em
844dot\_expr} allows both. The EXPR macro is used to express these variants
845in a concise way.
846
847\begin{grammar}
848 \RULE{\rt{expr}}
849 \CASE{\NT{EXPR}\mth{(}\NT{expr}\mth{)}}
850
851 \RULE{\rt{nest\_expr}}
852 \CASE{\NT{EXPR}\mth{(}\NT{nest\_expr}\mth{)}}
853 \CASE{\NT{NEST}\mth{(}\NT{nest\_expr}, \NT{exp\_whencode}\mth{)}}
854
855 \RULE{\rt{dot\_expr}}
856 \CASE{\NT{EXPR}\mth{(}\NT{dot\_expr}\mth{)}}
857 \CASE{\NT{NEST}\mth{(}\NT{dot\_expr}, \NT{exp\_whencode}\mth{)}}
858 \CASE{...~\opt{\NT{exp\_whencode}}}
859
860 \RULE{\rt{EXPR}\mth{(}\rt{exp}\mth{)}}
861 \CASE{\NT{exp} \NT{assign\_op} \NT{exp}}
862 \CASE{\NT{exp}++}
863 \CASE{\NT{exp}--}
864 \CASE{\NT{unary\_op} \NT{exp}}
865 \CASE{\NT{exp} \NT{bin\_op} \NT{exp}}
866 \CASE{\NT{exp} ?~\NT{dot\_expr} :~\NT{exp}}
867 \CASE{(\NT{type}) \NT{exp}}
868 \CASE{\NT{exp} [\NT{dot\_expr}]}
869 \CASE{\NT{exp} .~\NT{id}}
870 \CASE{\NT{exp} -> \NT{id}}
871 \CASE{\NT{exp}(\opt{\NT{PARAMSEQ}\mth{(}\NT{arg}, \NT{exp\_whencode}\mth{)}})}
872 \CASE{\NT{id}}
873% \CASE{\mth{\T{metaid}^{\ssf{Func}}}}
874% \CASE{\mth{\T{metaid}^{\ssf{LocalFunc}}}}
875 \CASE{\mth{\T{metaid}^{\ssf{Exp}}}}
876% \CASE{\mth{\T{metaid}^{\ssf{Err}}}}
877 \CASE{\mth{\T{metaid}^{\ssf{Const}}}}
878 \CASE{\NT{const}}
879 \CASE{(\NT{dot\_expr})}
880 \CASE{\NT{OR}\mth{(}\NT{exp}\mth{)}}
881
882 \RULE{\rt{arg}}
883 \CASE{\NT{nest\_expr}}
884 \CASE{\mth{\T{metaid}^{\ssf{ExpList}}}}
885
886 \RULE{\rt{exp\_whencode}}
887 \CASE{when != \NT{expr}}
888
889 \RULE{\rt{assign\_op}}
890 \CASE{= \OR -= \OR += \OR *= \OR /= \OR \%=}
891 \CASE{\&= \OR |= \OR \caret= \OR \lt\lt= \OR \gt\gt=}
892
893 \RULE{\rt{bin\_op}}
894 \CASE{* \OR / \OR \% \OR + \OR -}
895 \CASE{\lt\lt \OR \gt\gt \OR \caret\xspace \OR \& \OR \ttmid}
896 \CASE{< \OR > \OR <= \OR >= \OR == \OR != \OR \&\& \OR \ttmid\ttmid}
897
898 \RULE{\rt{unary\_op}}
899 \CASE{++ \OR -- \OR \& \OR * \OR + \OR - \OR !}
900
901\end{grammar}
902
903\section{Constant, Identifiers and Types for Transformations}
904
905\begin{grammar}
906 \RULE{\rt{const}}
907 \CASE{\NT{string}}
908 \CASE{[0-9]+}
909 \CASE{\mth{\cdots}}
910
911 \RULE{\rt{string}}
912 \CASE{"\any{[\^{}"]}"}
913
914 \RULE{\rt{id}}
915 \CASE{\T{id} \OR \mth{\T{metaid}^{\ssf{Id}}}}
916
917 \RULE{\rt{typedef\_ident}}
918 \CASE{\T{id} \OR \mth{\T{metaid}^{\ssf{Type}}}}
919
920 \RULE{\rt{type}}
921 \CASE{\NT{ctype} \OR \mth{\T{metaid}^{\ssf{Type}}}}
922
923 \RULE{\rt{pathToIsoFile}}
924 \CASE{<.*>}
951c7801
C
925
926 \RULE{\rt{regexp}}
927 \CASE{"\any{[\^{}"]}"}
faf9a90c
C
928\end{grammar}
929
faf9a90c
C
930
931%%% Local Variables:
932%%% mode: LaTeX
708f4980 933%%% TeX-master: "main_grammar"
5636bb2c 934%%% coding: utf-8
faf9a90c
C
935%%% TeX-PDF-mode: t
936%%% ispell-local-dictionary: "american"
937%%% End: