~ubuntu-branches/ubuntu/trusty/coccinelle/trusty-proposed

« back to all changes in this revision

Viewing changes to docs/manual/cocci_syntax.tex

  • Committer: Package Import Robot
  • Author(s): Євгеній Мещеряков
  • Date: 2011-12-06 23:17:50 UTC
  • mfrom: (7.2.4 experimental) (7.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20111206231750-m6n4b9jh0d48dlxo
Tags: 1.0.0~rc7.deb-5
Merge Build-Depends-Indep into Build-Depends, the package does not build
on autobuilders otherwise

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
first detected when the code is executed.  Furthermore, \texttt{@} should
47
47
not be use in this code.  Spatch scans the script code for the next
48
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.
 
49
\texttt{@} occurs within e.g., a comment.
50
50
 
51
51
\texttt{virtual} keyword is used to declare virtual rules. Virtual
52
52
rules may be subsequently used as a dependency for the rules in the
126
126
  not have to be specified in the SmPL code, but may be present in the C code.
127
127
\item \KW{value\_format}: Integers in various formats, e.g., 1 and 0x1, are
128
128
  considered to be equivalent in the matching process.
 
129
\item \KW{optional\_declarer\_semicolon}: Some declarers (top-level terms
 
130
  that look like function calls but serve to declare some variable) don't
 
131
  require a semicolon.  This isomorphism allows a SmPL declarer with a semicolon
 
132
  to match such a C declarer, if no transformation is specified on the SmPL
 
133
  semicolon.
129
134
\item \KW{comm\_assoc}: An expression of the form \NT{exp} \NT{bin\_op}
130
135
  \KW{...}, where \NT{bin\_op} is commutative and associative, is
131
136
  considered to match any top-level sequence of \NT{bin\_op} operators
204
209
It is possible to specify that an expression list or a parameter list
205
210
metavariable should match a specific number of expressions or parameters.
206
211
 
 
212
It is possible to specify some information about the definition of a fresh
 
213
identifier.  See the wiki.
 
214
 
 
215
 
207
216
\begin{grammar}
208
217
  \RULE{\rt{ids}}
209
218
  \CASE{\NT{COMMA\_LIST}\mth{(}\NT{pmid}\mth{)}}
218
227
  \RULE{\rt{mid}}  \CASE{\T{rulename\_id}.\T{id}}
219
228
 
220
229
  \RULE{\rt{pmid\_with\_regexp}}
221
 
  \CASE{\NT{pmid} \~{}= \NT{regexp}}
222
 
  \CASE{\NT{pmid} !\~{}= \NT{regexp}}
 
230
  \CASE{\NT{pmid} =\~{} \NT{regexp}}
 
231
  \CASE{\NT{pmid} !\~{} \NT{regexp}}
223
232
 
224
233
  \RULE{\rt{pmid\_with\_not\_eq}}
225
234
  \CASE{\NT{pmid} \OPT{!= \NT{id\_or\_meta}}}
276
285
  alloc=kmalloc}.  There should not be space around the {\tt =}.  An
277
286
example is in {\tt demos/vm.cocci} and {\tt demos/vm.c}.
278
287
 
 
288
 
 
289
\paragraph*{Warning:} Each metavariable declaration causes the declared
 
290
metavariables to be immediately usable, without any inheritance
 
291
indication.  Thus the following are correct:
 
292
 
 
293
\begin{quote}
 
294
\begin{verbatim}
 
295
@@
 
296
type r.T;
 
297
T x;
 
298
@@
 
299
 
 
300
[...] // some semantic patch code
 
301
\end{verbatim}
 
302
\end{quote}
 
303
 
 
304
\begin{quote}
 
305
\begin{verbatim}
 
306
@@
 
307
r.T x;
 
308
type r.T;
 
309
@@
 
310
 
 
311
[...] // some semantic patch code
 
312
\end{verbatim}
 
313
\end{quote}
 
314
 
 
315
\noindent
 
316
But the following is not correct:
 
317
 
 
318
\begin{quote}
 
319
\begin{verbatim}
 
320
@@
 
321
type r.T;
 
322
r.T x;
 
323
@@
 
324
 
 
325
[...] // some semantic patch code
 
326
\end{verbatim}
 
327
\end{quote}
 
328
 
 
329
This applies to position variables, type metavariables, identifier
 
330
metavariables that may be used in specifying a structure type, and
 
331
metavariables used in the initialization of a fresh identifier.  In the
 
332
case of a structure type, any identifier metavariable indeed has to be
 
333
declared as an identifier metavariable in advance.  The syntax does not
 
334
permit {\tt r.n} as the name of a structure or union type in such a
 
335
declaration.
 
336
 
279
337
\section{Metavariables for scripts}
280
338
 
281
339
Metavariables for scripts can only be inherited from transformation rules.
588
646
  \CASE{\NT{ctype\_qualif}}
589
647
  \CASE{\opt{\NT{ctype\_qualif}} char}
590
648
  \CASE{\opt{\NT{ctype\_qualif}} short}
 
649
  \CASE{\opt{\NT{ctype\_qualif}} short int}
591
650
  \CASE{\opt{\NT{ctype\_qualif}} int}
592
651
  \CASE{\opt{\NT{ctype\_qualif}} long}
 
652
  \CASE{\opt{\NT{ctype\_qualif}} long int}
593
653
  \CASE{\opt{\NT{ctype\_qualif}} long long}
 
654
  \CASE{\opt{\NT{ctype\_qualif}} long long int}
594
655
  \CASE{double}
 
656
  \CASE{long double}
595
657
  \CASE{float}
596
658
  \CASE{size\_t} \CASE{ssize\_t} \CASE{ptrdiff\_t}
597
659
  \CASE{enum \NT{id} \{ \NT{PARAMSEQ}\mth{(}\NT{dot\_expr}, \NT{exp\_whencode}\mth{)} \OPT{,} \}}
768
830
  \RULE{\rt{initialize}}
769
831
  \CASE{\NT{dot\_expr}}
770
832
  \CASE{\mth{\T{metaid}^{\ssf{Initialiser}}}}
771
 
  \CASE{\ttlb~\opt{\NT{COMMA\_LIST}\mth{(}\NT{dot\_expr}\mth{)}}~\ttrb}
 
833
  \CASE{\ttlb~\opt{\NT{COMMA\_LIST}\mth{(}\NT{init\_list\_elem}\mth{)}}~\ttrb}
772
834
 
773
835
  \RULE{\rt{init\_list\_elem}}
774
836
  \CASE{\NT{dot\_expr}}
775
837
  \CASE{\NT{designator} = \NT{dot\_expr}}
 
838
  \CASE{\mth{\T{metaid}^{\ssf{Initialiser}}}}
 
839
  \CASE{\mth{\T{metaid}^{\ssf{InitialiserList}}}}
776
840
  \CASE{\NT{id} : \NT{dot\_expr}}
777
841
 
778
842
  \RULE{\rt{designator}}
785
849
  \CASE{\mth{\T{metaid}^{\ssf{Declarer}}}}
786
850
\end{grammar}
787
851
 
 
852
An initializer for a structure can be ordered or unordered.  It is
 
853
considered to be unordered if there is at least one key-value pair
 
854
initializer, e.g., \texttt{.x = e}.
 
855
 
788
856
\section{Statements}
789
857
 
790
858
The first rule {\em statement} describes the various forms of a statement.
906
974
  \CASE{\NT{exp} -> \NT{id}}
907
975
  \CASE{\NT{exp}(\opt{\NT{PARAMSEQ}\mth{(}\NT{arg}, \NT{exp\_whencode}\mth{)}})}
908
976
  \CASE{\NT{id}}
 
977
  \CASE{(\NT{type}) \ttlb~{\NT{COMMA\_LIST}\mth{(}\NT{init\_list\_elem}\mth{)}}~\ttrb}
909
978
%   \CASE{\mth{\T{metaid}^{\ssf{Func}}}}
910
979
%   \CASE{\mth{\T{metaid}^{\ssf{LocalFunc}}}}
911
980
  \CASE{\mth{\T{metaid}^{\ssf{Exp}}}}
964
1033
  \CASE{"\any{[\^{}"]}"}
965
1034
\end{grammar}
966
1035
 
 
1036
\section{Comments}
 
1037
 
 
1038
A \verb+//+ or \verb+/* */+ comment that is annotated with + in the
 
1039
leftmost column is considered to be added code.  A \verb+//+ or
 
1040
\verb+/* */+ comment wthout such an annotation is considered to be a
 
1041
comment about the SmPL code, and thus is not matched in the C code.
 
1042
 
 
1043
\section{Command-line semantic match}
 
1044
 
 
1045
It is possible to specify a semantic match on the spatch command line,
 
1046
using the argument {\tt -sp}.  In such a semantic match, any token
 
1047
beginning with a capital letter is assumed to be a metavariable of type
 
1048
{\tt metavariable}.  In this case, the parser must be able to figure out what
 
1049
kind of metavariable it is.  It is also possible to specify the type of a
 
1050
metavariable by enclosing the type in :'s, concatenated directly to the
 
1051
metavariable name.
 
1052
 
 
1053
Some examples of semantic matches that can be given as an argument to {\tt
 
1054
  -sp} are as follows:
 
1055
 
 
1056
\begin{itemize}
 
1057
\item \texttt{f(e)}: This only matches the expression \texttt{f(e)}.
 
1058
\item \texttt{f(E)}: This matches a call to f with any argument.
 
1059
\item \texttt{F(E)}: This gives a parse error; the semantic patch parser
 
1060
  cannot figure out what kind of metavariable \texttt{F} is.
 
1061
\item \texttt{F:identifier:(E)}: This matches any one argument function
 
1062
  call.
 
1063
\item \texttt{f:identifier:(e:struct foo *:)}: This matches any one
 
1064
  argument function call where the argument has type \texttt{struct foo
 
1065
    *}.  Since the types of the metavariables are specified, it is not
 
1066
  necessary for the metavariable names to begin with a capital letter.
 
1067
\item \texttt{F:identifier:(F)}: This matches any one argument function call
 
1068
  where the argument is the name of the function itself.  This example
 
1069
  shows that it is not necessary to repeat the metavariable type name.
 
1070
\item \texttt{F:identifier:(F:identifier:)}: This matches any one argument
 
1071
  function call 
 
1072
  where the argument is the name of the function itself.  This example
 
1073
  shows that it is possible to repeat the metavariable type name.
 
1074
\end{itemize}
 
1075
 
 
1076
\texttt{When} constraints, \textit{e.g.} \texttt{when != e}, are allowed
 
1077
but the expression \texttt{e} must be represented as a single token.
 
1078
 
 
1079
The generated semantic match behaves as though there were a \texttt{*} in front
 
1080
of every token.
967
1081
 
968
1082
%%% Local Variables:
969
1083
%%% mode: LaTeX