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.
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
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.
212
It is possible to specify some information about the definition of a fresh
213
identifier. See the wiki.
209
218
\CASE{\NT{COMMA\_LIST}\mth{(}\NT{pmid}\mth{)}}
218
227
\RULE{\rt{mid}} \CASE{\T{rulename\_id}.\T{id}}
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}}
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}.
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:
300
[...] // some semantic patch code
311
[...] // some semantic patch code
316
But the following is not correct:
325
[...] // some semantic patch code
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
279
337
\section{Metavariables for scripts}
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}
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}
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}}
778
842
\RULE{\rt{designator}}
785
849
\CASE{\mth{\T{metaid}^{\ssf{Declarer}}}}
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}.
788
856
\section{Statements}
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{)}})}
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{[\^{}"]}"}
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.
1043
\section{Command-line semantic match}
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
1053
Some examples of semantic matches that can be given as an argument to {\tt
1054
-sp} are as follows:
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
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
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.
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.
1079
The generated semantic match behaves as though there were a \texttt{*} in front
968
1082
%%% Local Variables: