713e02099906cd9fbdc2d2b2da30d84959e4836e
[bpt/coccinelle.git] / parsing_cocci / parse_cocci.ml
1 (*
2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
23 (* splits the entire file into minus and plus fragments, and parses each
24 separately (thus duplicating work for the parsing of the context elements) *)
25
26 module D = Data
27 module PC = Parser_cocci_menhir
28 module V0 = Visitor_ast0
29 module VT0 = Visitor_ast0_types
30 module Ast = Ast_cocci
31 module Ast0 = Ast0_cocci
32 let pr = Printf.sprintf
33 (*let pr2 s = prerr_string s; prerr_string "\n"; flush stderr*)
34 let pr2 s = Printf.printf "%s\n" s
35
36 (* for isomorphisms. all should be at the front!!! *)
37 let reserved_names =
38 ["all";"optional_storage";"optional_qualifier";"value_format";"comm_assoc"]
39
40 (* ----------------------------------------------------------------------- *)
41 (* Debugging... *)
42
43 let line_type (d,_,_,_,_,_,_,_) = d
44
45 let line_type2c tok =
46 match line_type tok with
47 D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> ":-"
48 | D.PLUS -> ":+"
49 | D.PLUSPLUS -> ":++"
50 | D.CONTEXT | D.UNIQUE | D.OPT -> ""
51
52 let token2c (tok,_) =
53 match tok with
54 PC.TIdentifier -> "identifier"
55 | PC.TType -> "type"
56 | PC.TParameter -> "parameter"
57 | PC.TConstant -> "constant"
58 | PC.TExpression -> "expression"
59 | PC.TIdExpression -> "idexpression"
60 | PC.TInitialiser -> "initialiser"
61 | PC.TStatement -> "statement"
62 | PC.TPosition -> "position"
63 | PC.TPosAny -> "any"
64 | PC.TFunction -> "function"
65 | PC.TLocal -> "local"
66 | PC.Tlist -> "list"
67 | PC.TFresh -> "fresh"
68 | PC.TCppConcatOp -> "##"
69 | PC.TPure -> "pure"
70 | PC.TContext -> "context"
71 | PC.TTypedef -> "typedef"
72 | PC.TDeclarer -> "declarer"
73 | PC.TIterator -> "iterator"
74 | PC.TName -> "name"
75 | PC.TRuleName str -> "rule_name-"^str
76 | PC.TUsing -> "using"
77 | PC.TVirtual -> "virtual"
78 | PC.TPathIsoFile str -> "path_iso_file-"^str
79 | PC.TDisable -> "disable"
80 | PC.TExtends -> "extends"
81 | PC.TDepends -> "depends"
82 | PC.TOn -> "on"
83 | PC.TEver -> "ever"
84 | PC.TNever -> "never"
85 | PC.TExists -> "exists"
86 | PC.TForall -> "forall"
87 | PC.TError -> "error"
88 | PC.TWords -> "words"
89 | PC.TGenerated -> "generated"
90
91 | PC.TNothing -> "nothing"
92
93 | PC.Tchar(clt) -> "char"^(line_type2c clt)
94 | PC.Tshort(clt) -> "short"^(line_type2c clt)
95 | PC.Tint(clt) -> "int"^(line_type2c clt)
96 | PC.Tdouble(clt) -> "double"^(line_type2c clt)
97 | PC.Tfloat(clt) -> "float"^(line_type2c clt)
98 | PC.Tlong(clt) -> "long"^(line_type2c clt)
99 | PC.Tvoid(clt) -> "void"^(line_type2c clt)
100 | PC.Tstruct(clt) -> "struct"^(line_type2c clt)
101 | PC.Tunion(clt) -> "union"^(line_type2c clt)
102 | PC.Tenum(clt) -> "enum"^(line_type2c clt)
103 | PC.Tunsigned(clt) -> "unsigned"^(line_type2c clt)
104 | PC.Tsigned(clt) -> "signed"^(line_type2c clt)
105 | PC.Tstatic(clt) -> "static"^(line_type2c clt)
106 | PC.Tinline(clt) -> "inline"^(line_type2c clt)
107 | PC.Ttypedef(clt) -> "typedef"^(line_type2c clt)
108 | PC.Tattr(s,clt) -> s^(line_type2c clt)
109 | PC.Tauto(clt) -> "auto"^(line_type2c clt)
110 | PC.Tregister(clt) -> "register"^(line_type2c clt)
111 | PC.Textern(clt) -> "extern"^(line_type2c clt)
112 | PC.Tconst(clt) -> "const"^(line_type2c clt)
113 | PC.Tvolatile(clt) -> "volatile"^(line_type2c clt)
114
115 | PC.TPragma(s,_) -> s
116 | PC.TIncludeL(s,clt) -> (pr "#include \"%s\"" s)^(line_type2c clt)
117 | PC.TIncludeNL(s,clt) -> (pr "#include <%s>" s)^(line_type2c clt)
118 | PC.TDefine(clt,_) -> "#define"^(line_type2c clt)
119 | PC.TDefineParam(clt,_,_,_) -> "#define_param"^(line_type2c clt)
120 | PC.TMinusFile(s,clt) -> (pr "--- %s" s)^(line_type2c clt)
121 | PC.TPlusFile(s,clt) -> (pr "+++ %s" s)^(line_type2c clt)
122
123 | PC.TInc(clt) -> "++"^(line_type2c clt)
124 | PC.TDec(clt) -> "--"^(line_type2c clt)
125
126 | PC.TIf(clt) -> "if"^(line_type2c clt)
127 | PC.TElse(clt) -> "else"^(line_type2c clt)
128 | PC.TWhile(clt) -> "while"^(line_type2c clt)
129 | PC.TFor(clt) -> "for"^(line_type2c clt)
130 | PC.TDo(clt) -> "do"^(line_type2c clt)
131 | PC.TSwitch(clt) -> "switch"^(line_type2c clt)
132 | PC.TCase(clt) -> "case"^(line_type2c clt)
133 | PC.TDefault(clt) -> "default"^(line_type2c clt)
134 | PC.TReturn(clt) -> "return"^(line_type2c clt)
135 | PC.TBreak(clt) -> "break"^(line_type2c clt)
136 | PC.TContinue(clt) -> "continue"^(line_type2c clt)
137 | PC.TGoto(clt) -> "goto"^(line_type2c clt)
138 | PC.TIdent(s,clt) -> (pr "ident-%s" s)^(line_type2c clt)
139 | PC.TTypeId(s,clt) -> (pr "typename-%s" s)^(line_type2c clt)
140 | PC.TDeclarerId(s,clt) -> (pr "declarername-%s" s)^(line_type2c clt)
141 | PC.TIteratorId(s,clt) -> (pr "iteratorname-%s" s)^(line_type2c clt)
142 | PC.TMetaDeclarer(_,_,_,clt) -> "declmeta"^(line_type2c clt)
143 | PC.TMetaIterator(_,_,_,clt) -> "itermeta"^(line_type2c clt)
144
145 | PC.TSizeof(clt) -> "sizeof"^(line_type2c clt)
146
147 | PC.TString(x,clt) -> x^(line_type2c clt)
148 | PC.TChar(x,clt) -> x^(line_type2c clt)
149 | PC.TFloat(x,clt) -> x^(line_type2c clt)
150 | PC.TInt(x,clt) -> x^(line_type2c clt)
151
152 | PC.TOrLog(clt) -> "||"^(line_type2c clt)
153 | PC.TAndLog(clt) -> "&&"^(line_type2c clt)
154 | PC.TOr(clt) -> "|"^(line_type2c clt)
155 | PC.TXor(clt) -> "^"^(line_type2c clt)
156 | PC.TAnd (clt) -> "&"^(line_type2c clt)
157 | PC.TEqEq(clt) -> "=="^(line_type2c clt)
158 | PC.TNotEq(clt) -> "!="^(line_type2c clt)
159 | PC.TTildeEq(clt) -> "~="^(line_type2c clt)
160 | PC.TTildeExclEq(clt) -> "~!="^(line_type2c clt)
161 | PC.TLogOp(op,clt) ->
162 (match op with
163 Ast.Inf -> "<"
164 | Ast.InfEq -> "<="
165 | Ast.Sup -> ">"
166 | Ast.SupEq -> ">="
167 | _ -> failwith "not possible")
168 ^(line_type2c clt)
169 | PC.TShOp(op,clt) ->
170 (match op with
171 Ast.DecLeft -> "<<"
172 | Ast.DecRight -> ">>"
173 | _ -> failwith "not possible")
174 ^(line_type2c clt)
175 | PC.TPlus(clt) -> "+"^(line_type2c clt)
176 | PC.TMinus(clt) -> "-"^(line_type2c clt)
177 | PC.TMul(clt) -> "*"^(line_type2c clt)
178 | PC.TDmOp(op,clt) ->
179 (match op with
180 Ast.Div -> "/"
181 | Ast.Mod -> "%"
182 | _ -> failwith "not possible")
183 ^(line_type2c clt)
184 | PC.TTilde (clt) -> "~"^(line_type2c clt)
185
186 | PC.TMetaParam(_,_,clt) -> "parammeta"^(line_type2c clt)
187 | PC.TMetaParamList(_,_,_,clt) -> "paramlistmeta"^(line_type2c clt)
188 | PC.TMetaConst(_,_,_,_,clt) -> "constmeta"^(line_type2c clt)
189 | PC.TMetaErr(_,_,_,clt) -> "errmeta"^(line_type2c clt)
190 | PC.TMetaExp(_,_,_,_,clt) -> "expmeta"^(line_type2c clt)
191 | PC.TMetaIdExp(_,_,_,_,clt) -> "idexpmeta"^(line_type2c clt)
192 | PC.TMetaLocalIdExp(_,_,_,_,clt) -> "localidexpmeta"^(line_type2c clt)
193 | PC.TMetaExpList(_,_,_,clt) -> "explistmeta"^(line_type2c clt)
194 | PC.TMetaId(_,_,_,clt) -> "idmeta"^(line_type2c clt)
195 | PC.TMetaType(_,_,clt) -> "typemeta"^(line_type2c clt)
196 | PC.TMetaInit(_,_,clt) -> "initmeta"^(line_type2c clt)
197 | PC.TMetaStm(_,_,clt) -> "stmmeta"^(line_type2c clt)
198 | PC.TMetaStmList(_,_,clt) -> "stmlistmeta"^(line_type2c clt)
199 | PC.TMetaFunc(_,_,_,clt) -> "funcmeta"^(line_type2c clt)
200 | PC.TMetaLocalFunc(_,_,_,clt) -> "funcmeta"^(line_type2c clt)
201 | PC.TMetaPos(_,_,_,clt) -> "posmeta"
202 | PC.TMPtVirg -> ";"
203 | PC.TArobArob -> "@@"
204 | PC.TArob -> "@"
205 | PC.TPArob -> "P@"
206 | PC.TScript -> "script"
207 | PC.TInitialize -> "initialize"
208 | PC.TFinalize -> "finalize"
209
210 | PC.TWhen(clt) -> "WHEN"^(line_type2c clt)
211 | PC.TWhenTrue(clt) -> "WHEN TRUE"^(line_type2c clt)
212 | PC.TWhenFalse(clt) -> "WHEN FALSE"^(line_type2c clt)
213 | PC.TAny(clt) -> "ANY"^(line_type2c clt)
214 | PC.TStrict(clt) -> "STRICT"^(line_type2c clt)
215 | PC.TEllipsis(clt) -> "..."^(line_type2c clt)
216 (*
217 | PC.TCircles(clt) -> "ooo"^(line_type2c clt)
218 | PC.TStars(clt) -> "***"^(line_type2c clt)
219 *)
220
221 | PC.TOEllipsis(clt) -> "<..."^(line_type2c clt)
222 | PC.TCEllipsis(clt) -> "...>"^(line_type2c clt)
223 | PC.TPOEllipsis(clt) -> "<+..."^(line_type2c clt)
224 | PC.TPCEllipsis(clt) -> "...+>"^(line_type2c clt)
225 (*
226 | PC.TOCircles(clt) -> "<ooo"^(line_type2c clt)
227 | PC.TCCircles(clt) -> "ooo>"^(line_type2c clt)
228 | PC.TOStars(clt) -> "<***"^(line_type2c clt)
229 | PC.TCStars(clt) -> "***>"^(line_type2c clt)
230 *)
231 | PC.TBang0 -> "!"
232 | PC.TPlus0 -> "+"
233 | PC.TWhy0 -> "?"
234
235 | PC.TWhy(clt) -> "?"^(line_type2c clt)
236 | PC.TDotDot(clt) -> ":"^(line_type2c clt)
237 | PC.TBang(clt) -> "!"^(line_type2c clt)
238 | PC.TOPar(clt) -> "("^(line_type2c clt)
239 | PC.TOPar0(clt) -> "("^(line_type2c clt)
240 | PC.TMid0(clt) -> "|"^(line_type2c clt)
241 | PC.TCPar(clt) -> ")"^(line_type2c clt)
242 | PC.TCPar0(clt) -> ")"^(line_type2c clt)
243
244 | PC.TOBrace(clt) -> "{"^(line_type2c clt)
245 | PC.TCBrace(clt) -> "}"^(line_type2c clt)
246 | PC.TOCro(clt) -> "["^(line_type2c clt)
247 | PC.TCCro(clt) -> "]"^(line_type2c clt)
248 | PC.TOInit(clt) -> "{"^(line_type2c clt)
249
250 | PC.TPtrOp(clt) -> "->"^(line_type2c clt)
251
252 | PC.TEq(clt) -> "="^(line_type2c clt)
253 | PC.TAssign(_,clt) -> "=op"^(line_type2c clt)
254 | PC.TDot(clt) -> "."^(line_type2c clt)
255 | PC.TComma(clt) -> ","^(line_type2c clt)
256 | PC.TPtVirg(clt) -> ";"^(line_type2c clt)
257
258 | PC.EOF -> "eof"
259 | PC.TLineEnd(clt) -> "line end"
260 | PC.TInvalid -> "invalid"
261 | PC.TFunDecl(clt) -> "fundecl"
262
263 | PC.TIso -> "<=>"
264 | PC.TRightIso -> "=>"
265 | PC.TIsoTopLevel -> "TopLevel"
266 | PC.TIsoExpression -> "Expression"
267 | PC.TIsoArgExpression -> "ArgExpression"
268 | PC.TIsoTestExpression -> "TestExpression"
269 | PC.TIsoStatement -> "Statement"
270 | PC.TIsoDeclaration -> "Declaration"
271 | PC.TIsoType -> "Type"
272 | PC.TScriptData s -> s
273
274 let print_tokens s tokens =
275 Printf.printf "%s\n" s;
276 List.iter (function x -> Printf.printf "%s " (token2c x)) tokens;
277 Printf.printf "\n\n";
278 flush stdout
279
280 type plus = PLUS | NOTPLUS | SKIP
281
282 let plus_attachable only_plus (tok,_) =
283 match tok with
284 PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
285 | PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
286 | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
287 | PC.Tstatic(clt)
288 | PC.Tinline(clt) | PC.Ttypedef(clt) | PC.Tattr(_,clt)
289 | PC.Tauto(clt) | PC.Tregister(clt)
290 | PC.Textern(clt) | PC.Tconst(clt) | PC.Tvolatile(clt)
291
292 | PC.TIncludeL(_,clt) | PC.TIncludeNL(_,clt) | PC.TDefine(clt,_)
293 | PC.TDefineParam(clt,_,_,_) | PC.TMinusFile(_,clt) | PC.TPlusFile(_,clt)
294
295 | PC.TInc(clt) | PC.TDec(clt)
296
297 | PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
298 | PC.TSwitch(clt) | PC.TCase(clt) | PC.TDefault(clt) | PC.TReturn(clt)
299 | PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt) | PC.TIdent(_,clt)
300 | PC.TTypeId(_,clt) | PC.TDeclarerId(_,clt) | PC.TIteratorId(_,clt)
301
302 | PC.TSizeof(clt)
303
304 | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
305
306 | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
307 | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TLogOp(_,clt)
308 | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
309 | PC.TDmOp(_,clt) | PC.TTilde (clt)
310
311 | PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
312 | PC.TMetaConst(_,_,_,_,clt) | PC.TMetaErr(_,_,_,clt)
313 | PC.TMetaExp(_,_,_,_,clt) | PC.TMetaIdExp(_,_,_,_,clt)
314 | PC.TMetaLocalIdExp(_,_,_,_,clt)
315 | PC.TMetaExpList(_,_,_,clt)
316 | PC.TMetaId(_,_,_,clt)
317 | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt) | PC.TMetaStm(_,_,clt)
318 | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
319 | PC.TMetaLocalFunc(_,_,_,clt)
320
321 | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
322 | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
323 (* | PC.TCircles(clt) | PC.TStars(clt) *)
324
325 | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
326 | PC.TCPar(clt)
327
328 | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
329 | PC.TOInit(clt)
330
331 | PC.TPtrOp(clt)
332
333 | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
334 | PC.TPtVirg(clt) ->
335 if List.mem (line_type clt) [D.PLUS;D.PLUSPLUS]
336 then PLUS
337 else if only_plus then NOTPLUS
338 else if line_type clt = D.CONTEXT then PLUS else NOTPLUS
339
340 | PC.TOPar0(clt) | PC.TMid0(clt) | PC.TCPar0(clt)
341 | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
342 | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) (* | PC.TOCircles(clt)
343 | PC.TCCircles(clt) | PC.TOStars(clt) | PC.TCStars(clt) *) -> NOTPLUS
344 | PC.TMetaPos(nm,_,_,_) -> NOTPLUS
345
346 | _ -> SKIP
347
348 let get_clt (tok,_) =
349 match tok with
350 PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
351 | PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
352 | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
353 | PC.Tstatic(clt)
354 | PC.Tinline(clt) | PC.Tattr(_,clt) | PC.Tauto(clt) | PC.Tregister(clt)
355 | PC.Textern(clt) | PC.Tconst(clt) | PC.Tvolatile(clt)
356
357 | PC.TIncludeL(_,clt) | PC.TIncludeNL(_,clt) | PC.TDefine(clt,_)
358 | PC.TDefineParam(clt,_,_,_) | PC.TMinusFile(_,clt) | PC.TPlusFile(_,clt)
359
360 | PC.TInc(clt) | PC.TDec(clt)
361
362 | PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
363 | PC.TSwitch(clt) | PC.TCase(clt) | PC.TDefault(clt) | PC.TReturn(clt)
364 | PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt) | PC.TIdent(_,clt)
365 | PC.TTypeId(_,clt) | PC.TDeclarerId(_,clt) | PC.TIteratorId(_,clt)
366
367 | PC.TSizeof(clt)
368
369 | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
370
371 | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
372 | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TLogOp(_,clt)
373 | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
374 | PC.TDmOp(_,clt) | PC.TTilde (clt)
375
376 | PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
377 | PC.TMetaConst(_,_,_,_,clt) | PC.TMetaErr(_,_,_,clt)
378 | PC.TMetaExp(_,_,_,_,clt) | PC.TMetaIdExp(_,_,_,_,clt)
379 | PC.TMetaLocalIdExp(_,_,_,_,clt)
380 | PC.TMetaExpList(_,_,_,clt)
381 | PC.TMetaId(_,_,_,clt)
382 | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt) | PC.TMetaStm(_,_,clt)
383 | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
384 | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt)
385
386 | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt) |
387 PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
388 (* | PC.TCircles(clt) | PC.TStars(clt) *)
389
390 | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
391 | PC.TCPar(clt)
392
393 | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
394 | PC.TOInit(clt)
395
396 | PC.TPtrOp(clt)
397
398 | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
399 | PC.TPtVirg(clt)
400
401 | PC.TOPar0(clt) | PC.TMid0(clt) | PC.TCPar0(clt)
402 | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
403 | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) (* | PC.TOCircles(clt)
404 | PC.TCCircles(clt) | PC.TOStars(clt) | PC.TCStars(clt) *) -> clt
405
406 | _ -> failwith "no clt"
407
408 let update_clt (tok,x) clt =
409 match tok with
410 PC.Tchar(_) -> (PC.Tchar(clt),x)
411 | PC.Tshort(_) -> (PC.Tshort(clt),x)
412 | PC.Tint(_) -> (PC.Tint(clt),x)
413 | PC.Tdouble(_) -> (PC.Tdouble(clt),x)
414 | PC.Tfloat(_) -> (PC.Tfloat(clt),x)
415 | PC.Tlong(_) -> (PC.Tlong(clt),x)
416 | PC.Tvoid(_) -> (PC.Tvoid(clt),x)
417 | PC.Tstruct(_) -> (PC.Tstruct(clt),x)
418 | PC.Tunion(_) -> (PC.Tunion(clt),x)
419 | PC.Tenum(_) -> (PC.Tenum(clt),x)
420 | PC.Tunsigned(_) -> (PC.Tunsigned(clt),x)
421 | PC.Tsigned(_) -> (PC.Tsigned(clt),x)
422 | PC.Tstatic(_) -> (PC.Tstatic(clt),x)
423 | PC.Tinline(_) -> (PC.Tinline(clt),x)
424 | PC.Ttypedef(_) -> (PC.Ttypedef(clt),x)
425 | PC.Tattr(s,_) -> (PC.Tattr(s,clt),x)
426 | PC.Tauto(_) -> (PC.Tauto(clt),x)
427 | PC.Tregister(_) -> (PC.Tregister(clt),x)
428 | PC.Textern(_) -> (PC.Textern(clt),x)
429 | PC.Tconst(_) -> (PC.Tconst(clt),x)
430 | PC.Tvolatile(_) -> (PC.Tvolatile(clt),x)
431
432 | PC.TIncludeL(s,_) -> (PC.TIncludeL(s,clt),x)
433 | PC.TIncludeNL(s,_) -> (PC.TIncludeNL(s,clt),x)
434 | PC.TDefine(_,a) -> (PC.TDefine(clt,a),x)
435 | PC.TDefineParam(_,a,b,c) -> (PC.TDefineParam(clt,a,b,c),x)
436 | PC.TMinusFile(s,_) -> (PC.TMinusFile(s,clt),x)
437 | PC.TPlusFile(s,_) -> (PC.TPlusFile(s,clt),x)
438
439 | PC.TInc(_) -> (PC.TInc(clt),x)
440 | PC.TDec(_) -> (PC.TDec(clt),x)
441
442 | PC.TIf(_) -> (PC.TIf(clt),x)
443 | PC.TElse(_) -> (PC.TElse(clt),x)
444 | PC.TWhile(_) -> (PC.TWhile(clt),x)
445 | PC.TFor(_) -> (PC.TFor(clt),x)
446 | PC.TDo(_) -> (PC.TDo(clt),x)
447 | PC.TSwitch(_) -> (PC.TSwitch(clt),x)
448 | PC.TCase(_) -> (PC.TCase(clt),x)
449 | PC.TDefault(_) -> (PC.TDefault(clt),x)
450 | PC.TReturn(_) -> (PC.TReturn(clt),x)
451 | PC.TBreak(_) -> (PC.TBreak(clt),x)
452 | PC.TContinue(_) -> (PC.TContinue(clt),x)
453 | PC.TGoto(_) -> (PC.TGoto(clt),x)
454 | PC.TIdent(s,_) -> (PC.TIdent(s,clt),x)
455 | PC.TTypeId(s,_) -> (PC.TTypeId(s,clt),x)
456 | PC.TDeclarerId(s,_) -> (PC.TDeclarerId(s,clt),x)
457 | PC.TIteratorId(s,_) -> (PC.TIteratorId(s,clt),x)
458
459 | PC.TSizeof(_) -> (PC.TSizeof(clt),x)
460
461 | PC.TString(s,_) -> (PC.TString(s,clt),x)
462 | PC.TChar(s,_) -> (PC.TChar(s,clt),x)
463 | PC.TFloat(s,_) -> (PC.TFloat(s,clt),x)
464 | PC.TInt(s,_) -> (PC.TInt(s,clt),x)
465
466 | PC.TOrLog(_) -> (PC.TOrLog(clt),x)
467 | PC.TAndLog(_) -> (PC.TAndLog(clt),x)
468 | PC.TOr(_) -> (PC.TOr(clt),x)
469 | PC.TXor(_) -> (PC.TXor(clt),x)
470 | PC.TAnd (_) -> (PC.TAnd (clt),x)
471 | PC.TEqEq(_) -> (PC.TEqEq(clt),x)
472 | PC.TNotEq(_) -> (PC.TNotEq(clt),x)
473 | PC.TTildeEq(_) -> (PC.TTildeEq(clt),x)
474 | PC.TLogOp(op,_) -> (PC.TLogOp(op,clt),x)
475 | PC.TShOp(op,_) -> (PC.TShOp(op,clt),x)
476 | PC.TPlus(_) -> (PC.TPlus(clt),x)
477 | PC.TMinus(_) -> (PC.TMinus(clt),x)
478 | PC.TMul(_) -> (PC.TMul(clt),x)
479 | PC.TDmOp(op,_) -> (PC.TDmOp(op,clt),x)
480 | PC.TTilde (_) -> (PC.TTilde (clt),x)
481
482 | PC.TMetaParam(a,b,_) -> (PC.TMetaParam(a,b,clt),x)
483 | PC.TMetaParamList(a,b,c,_) -> (PC.TMetaParamList(a,b,c,clt),x)
484 | PC.TMetaConst(a,b,c,d,_) -> (PC.TMetaConst(a,b,c,d,clt),x)
485 | PC.TMetaErr(a,b,c,_) -> (PC.TMetaErr(a,b,c,clt),x)
486 | PC.TMetaExp(a,b,c,d,_) -> (PC.TMetaExp(a,b,c,d,clt),x)
487 | PC.TMetaIdExp(a,b,c,d,_) -> (PC.TMetaIdExp(a,b,c,d,clt),x)
488 | PC.TMetaLocalIdExp(a,b,c,d,_) -> (PC.TMetaLocalIdExp(a,b,c,d,clt),x)
489 | PC.TMetaExpList(a,b,c,_) -> (PC.TMetaExpList(a,b,c,clt),x)
490 | PC.TMetaId(a,b,c,_) -> (PC.TMetaId(a,b,c,clt),x)
491 | PC.TMetaType(a,b,_) -> (PC.TMetaType(a,b,clt),x)
492 | PC.TMetaInit(a,b,_) -> (PC.TMetaInit(a,b,clt),x)
493 | PC.TMetaStm(a,b,_) -> (PC.TMetaStm(a,b,clt),x)
494 | PC.TMetaStmList(a,b,_) -> (PC.TMetaStmList(a,b,clt),x)
495 | PC.TMetaFunc(a,b,c,_) -> (PC.TMetaFunc(a,b,c,clt),x)
496 | PC.TMetaLocalFunc(a,b,c,_) -> (PC.TMetaLocalFunc(a,b,c,clt),x)
497
498 | PC.TWhen(_) -> (PC.TWhen(clt),x)
499 | PC.TWhenTrue(_) -> (PC.TWhenTrue(clt),x)
500 | PC.TWhenFalse(_) -> (PC.TWhenFalse(clt),x)
501 | PC.TAny(_) -> (PC.TAny(clt),x)
502 | PC.TStrict(_) -> (PC.TStrict(clt),x)
503 | PC.TEllipsis(_) -> (PC.TEllipsis(clt),x)
504 (*
505 | PC.TCircles(_) -> (PC.TCircles(clt),x)
506 | PC.TStars(_) -> (PC.TStars(clt),x)
507 *)
508
509 | PC.TOEllipsis(_) -> (PC.TOEllipsis(clt),x)
510 | PC.TCEllipsis(_) -> (PC.TCEllipsis(clt),x)
511 | PC.TPOEllipsis(_) -> (PC.TPOEllipsis(clt),x)
512 | PC.TPCEllipsis(_) -> (PC.TPCEllipsis(clt),x)
513 (*
514 | PC.TOCircles(_) -> (PC.TOCircles(clt),x)
515 | PC.TCCircles(_) -> (PC.TCCircles(clt),x)
516 | PC.TOStars(_) -> (PC.TOStars(clt),x)
517 | PC.TCStars(_) -> (PC.TCStars(clt),x)
518 *)
519
520 | PC.TWhy(_) -> (PC.TWhy(clt),x)
521 | PC.TDotDot(_) -> (PC.TDotDot(clt),x)
522 | PC.TBang(_) -> (PC.TBang(clt),x)
523 | PC.TOPar(_) -> (PC.TOPar(clt),x)
524 | PC.TOPar0(_) -> (PC.TOPar0(clt),x)
525 | PC.TMid0(_) -> (PC.TMid0(clt),x)
526 | PC.TCPar(_) -> (PC.TCPar(clt),x)
527 | PC.TCPar0(_) -> (PC.TCPar0(clt),x)
528
529 | PC.TOBrace(_) -> (PC.TOBrace(clt),x)
530 | PC.TCBrace(_) -> (PC.TCBrace(clt),x)
531 | PC.TOCro(_) -> (PC.TOCro(clt),x)
532 | PC.TCCro(_) -> (PC.TCCro(clt),x)
533 | PC.TOInit(_) -> (PC.TOInit(clt),x)
534
535 | PC.TPtrOp(_) -> (PC.TPtrOp(clt),x)
536
537 | PC.TEq(_) -> (PC.TEq(clt),x)
538 | PC.TAssign(s,_) -> (PC.TAssign(s,clt),x)
539 | PC.TDot(_) -> (PC.TDot(clt),x)
540 | PC.TComma(_) -> (PC.TComma(clt),x)
541 | PC.TPtVirg(_) -> (PC.TPtVirg(clt),x)
542
543 | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
544 | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
545
546 | _ -> failwith "no clt"
547
548
549 (* ----------------------------------------------------------------------- *)
550
551 let make_name prefix ln = Printf.sprintf "%s starting on line %d" prefix ln
552
553 (* ----------------------------------------------------------------------- *)
554 (* Read tokens *)
555
556 let wrap_lexbuf_info lexbuf =
557 (Lexing.lexeme lexbuf, Lexing.lexeme_start lexbuf)
558
559 let tokens_all_full token table file get_ats lexbuf end_markers :
560 (bool * ((PC.token * (string * (int * int) * (int * int))) list)) =
561 try
562 let rec aux () =
563 let result = token lexbuf in
564 let info = (Lexing.lexeme lexbuf,
565 (table.(Lexing.lexeme_start lexbuf)),
566 (Lexing.lexeme_start lexbuf, Lexing.lexeme_end lexbuf)) in
567 if result = PC.EOF
568 then
569 if get_ats
570 then failwith "unexpected end of file in a metavariable declaration"
571 else (false,[(result,info)])
572 else if List.mem result end_markers
573 then (true,[(result,info)])
574 else
575 let (more,rest) = aux() in
576 (more,(result, info)::rest)
577 in aux ()
578 with
579 e -> pr2 (Common.error_message file (wrap_lexbuf_info lexbuf) ); raise e
580
581 let tokens_all table file get_ats lexbuf end_markers :
582 (bool * ((PC.token * (string * (int * int) * (int * int))) list)) =
583 tokens_all_full Lexer_cocci.token table file get_ats lexbuf end_markers
584
585 let tokens_script_all table file get_ats lexbuf end_markers :
586 (bool * ((PC.token * (string * (int * int) * (int * int))) list)) =
587 tokens_all_full Lexer_script.token table file get_ats lexbuf end_markers
588
589 (* ----------------------------------------------------------------------- *)
590 (* Split tokens into minus and plus fragments *)
591
592 let split t clt =
593 let (d,_,_,_,_,_,_,_) = clt in
594 match d with
595 D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> ([t],[])
596 | D.PLUS | D.PLUSPLUS -> ([],[t])
597 | D.CONTEXT | D.UNIQUE | D.OPT -> ([t],[t])
598
599 let split_token ((tok,_) as t) =
600 match tok with
601 PC.TIdentifier | PC.TConstant | PC.TExpression | PC.TIdExpression
602 | PC.TStatement | PC.TPosition | PC.TPosAny | PC.TInitialiser
603 | PC.TFunction | PC.TTypedef | PC.TDeclarer | PC.TIterator | PC.TName
604 | PC.TType | PC.TParameter | PC.TLocal | PC.Tlist | PC.TFresh
605 | PC.TCppConcatOp | PC.TPure
606 | PC.TContext | PC.TRuleName(_) | PC.TUsing | PC.TVirtual | PC.TDisable
607 | PC.TExtends | PC.TPathIsoFile(_)
608 | PC.TDepends | PC.TOn | PC.TEver | PC.TNever | PC.TExists | PC.TForall
609 | PC.TError | PC.TWords | PC.TGenerated | PC.TNothing -> ([t],[t])
610
611 | PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
612 | PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
613 | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
614 | PC.Tstatic(clt) | PC.Tauto(clt) | PC.Tregister(clt) | PC.Textern(clt)
615 | PC.Tinline(clt) | PC.Ttypedef(clt) | PC.Tattr(_,clt)
616 | PC.Tconst(clt) | PC.Tvolatile(clt) -> split t clt
617
618 | PC.TPragma(s,_) -> ([],[t]) (* only allowed in + *)
619 | PC.TPlusFile(s,clt) | PC.TMinusFile(s,clt)
620 | PC.TIncludeL(s,clt) | PC.TIncludeNL(s,clt) ->
621 split t clt
622 | PC.TDefine(clt,_) | PC.TDefineParam(clt,_,_,_) -> split t clt
623
624 | PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
625 | PC.TSwitch(clt) | PC.TCase(clt) | PC.TDefault(clt)
626 | PC.TSizeof(clt)
627 | PC.TReturn(clt) | PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt)
628 | PC.TIdent(_,clt)
629 | PC.TTypeId(_,clt) | PC.TDeclarerId(_,clt) | PC.TIteratorId(_,clt)
630 | PC.TMetaConst(_,_,_,_,clt) | PC.TMetaExp(_,_,_,_,clt)
631 | PC.TMetaIdExp(_,_,_,_,clt) | PC.TMetaLocalIdExp(_,_,_,_,clt)
632 | PC.TMetaExpList(_,_,_,clt)
633 | PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
634 | PC.TMetaId(_,_,_,clt) | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt)
635 | PC.TMetaStm(_,_,clt) | PC.TMetaStmList(_,_,clt) | PC.TMetaErr(_,_,_,clt)
636 | PC.TMetaFunc(_,_,_,clt) | PC.TMetaLocalFunc(_,_,_,clt)
637 | PC.TMetaDeclarer(_,_,_,clt) | PC.TMetaIterator(_,_,_,clt) -> split t clt
638 | PC.TMPtVirg | PC.TArob | PC.TArobArob | PC.TScript
639 | PC.TInitialize | PC.TFinalize -> ([t],[t])
640 | PC.TPArob | PC.TMetaPos(_,_,_,_) -> ([t],[])
641
642 | PC.TFunDecl(clt)
643 | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
644 | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
645 | PC.TEllipsis(clt) (* | PC.TCircles(clt) | PC.TStars(clt) *) -> split t clt
646
647 | PC.TOEllipsis(_) | PC.TCEllipsis(_) (* clt must be context *)
648 | PC.TPOEllipsis(_) | PC.TPCEllipsis(_) (* clt must be context *)
649 (*
650 | PC.TOCircles(_) | PC.TCCircles(_) (* clt must be context *)
651 | PC.TOStars(_) | PC.TCStars(_) (* clt must be context *)
652 *)
653 | PC.TBang0 | PC.TPlus0 | PC.TWhy0 ->
654 ([t],[t])
655
656 | PC.TWhy(clt) | PC.TDotDot(clt)
657 | PC.TBang(clt) | PC.TOPar(clt) | PC.TOPar0(clt)
658 | PC.TMid0(clt) | PC.TCPar(clt) | PC.TCPar0(clt) -> split t clt
659
660 | PC.TInc(clt) | PC.TDec(clt) -> split t clt
661
662 | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt) ->
663 split t clt
664
665 | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
666 | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TTildeExclEq(clt) | PC.TLogOp(_,clt)
667 | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
668 | PC.TDmOp(_,clt) | PC.TTilde (clt) -> split t clt
669
670 | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOInit(clt) -> split t clt
671 | PC.TOCro(clt) | PC.TCCro(clt) -> split t clt
672
673 | PC.TPtrOp(clt) -> split t clt
674
675 | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
676 | PC.TPtVirg(clt) -> split t clt
677
678 | PC.EOF | PC.TInvalid -> ([t],[t])
679
680 | PC.TIso | PC.TRightIso
681 | PC.TIsoExpression | PC.TIsoStatement | PC.TIsoDeclaration | PC.TIsoType
682 | PC.TIsoTopLevel | PC.TIsoArgExpression | PC.TIsoTestExpression ->
683 failwith "unexpected tokens"
684 | PC.TScriptData s -> ([t],[t])
685
686 let split_token_stream tokens =
687 let rec loop = function
688 [] -> ([],[])
689 | token::tokens ->
690 let (minus,plus) = split_token token in
691 let (minus_stream,plus_stream) = loop tokens in
692 (minus@minus_stream,plus@plus_stream) in
693 loop tokens
694
695 (* ----------------------------------------------------------------------- *)
696 (* Find function names *)
697 (* This addresses a shift-reduce problem in the parser, allowing us to
698 distinguish a function declaration from a function call even if the latter
699 has no return type. Undoubtedly, this is not very nice, but it doesn't
700 seem very convenient to refactor the grammar to get around the problem. *)
701
702 let rec find_function_names = function
703 [] -> []
704 | ((PC.TIdent(_,clt),info) as t1) :: ((PC.TOPar(_),_) as t2) :: rest
705 | ((PC.TMetaId(_,_,_,clt),info) as t1) :: ((PC.TOPar(_),_) as t2) :: rest
706 | ((PC.TMetaFunc(_,_,_,clt),info) as t1) :: ((PC.TOPar(_),_) as t2) :: rest
707 | ((PC.TMetaLocalFunc(_,_,_,clt),info) as t1)::((PC.TOPar(_),_) as t2)::rest
708 ->
709 let rec skip level = function
710 [] -> ([],false,[])
711 | ((PC.TCPar(_),_) as t)::rest ->
712 let level = level - 1 in
713 if level = 0
714 then ([t],true,rest)
715 else let (pre,found,post) = skip level rest in (t::pre,found,post)
716 | ((PC.TOPar(_),_) as t)::rest ->
717 let level = level + 1 in
718 let (pre,found,post) = skip level rest in (t::pre,found,post)
719 | ((PC.TArobArob,_) as t)::rest
720 | ((PC.TArob,_) as t)::rest
721 | ((PC.EOF,_) as t)::rest -> ([t],false,rest)
722 | t::rest ->
723 let (pre,found,post) = skip level rest in (t::pre,found,post) in
724 let (pre,found,post) = skip 1 rest in
725 (match (found,post) with
726 (true,((PC.TOBrace(_),_) as t3)::rest) ->
727 (PC.TFunDecl(clt),info) :: t1 :: t2 :: pre @
728 t3 :: (find_function_names rest)
729 | _ -> t1 :: t2 :: pre @ find_function_names post)
730 | t :: rest -> t :: find_function_names rest
731
732 (* ----------------------------------------------------------------------- *)
733 (* an attribute is an identifier that preceeds another identifier and
734 begins with __ *)
735
736 let rec detect_attr l =
737 let is_id = function
738 (PC.TIdent(_,_),_) | (PC.TMetaId(_,_,_,_),_) | (PC.TMetaFunc(_,_,_,_),_)
739 | (PC.TMetaLocalFunc(_,_,_,_),_) -> true
740 | _ -> false in
741 let rec loop = function
742 [] -> []
743 | [x] -> [x]
744 | ((PC.TIdent(nm,clt),info) as t1)::id::rest when is_id id ->
745 if String.length nm > 2 && String.sub nm 0 2 = "__"
746 then (PC.Tattr(nm,clt),info)::(loop (id::rest))
747 else t1::(loop (id::rest))
748 | x::xs -> x::(loop xs) in
749 loop l
750
751 (* ----------------------------------------------------------------------- *)
752 (* Look for variable declarations where the name is a typedef name.
753 We assume that C code does not contain a multiplication as a top-level
754 statement. *)
755
756 (* bug: once a type, always a type, even if the same name is later intended
757 to be used as a real identifier *)
758 let detect_types in_meta_decls l =
759 let is_delim infn = function
760 (PC.TOEllipsis(_),_) (* | (PC.TOCircles(_),_) | (PC.TOStars(_),_) *)
761 | (PC.TPOEllipsis(_),_) (* | (PC.TOCircles(_),_) | (PC.TOStars(_),_) *)
762 | (PC.TEllipsis(_),_) (* | (PC.TCircles(_),_) | (PC.TStars(_),_) *)
763 | (PC.TPtVirg(_),_) | (PC.TOBrace(_),_) | (PC.TOInit(_),_)
764 | (PC.TCBrace(_),_)
765 | (PC.TPure,_) | (PC.TContext,_)
766 | (PC.Tstatic(_),_) | (PC.Textern(_),_)
767 | (PC.Tinline(_),_) | (PC.Ttypedef(_),_) | (PC.Tattr(_),_) -> true
768 | (PC.TComma(_),_) when infn > 0 or in_meta_decls -> true
769 | (PC.TDotDot(_),_) when in_meta_decls -> true
770 | _ -> false in
771 let is_choices_delim = function
772 (PC.TOBrace(_),_) | (PC.TComma(_),_) -> true | _ -> false in
773 let is_id = function
774 (PC.TIdent(_,_),_) | (PC.TMetaId(_,_,_,_),_) | (PC.TMetaFunc(_,_,_,_),_)
775 | (PC.TMetaLocalFunc(_,_,_,_),_) -> true
776 | (PC.TMetaParam(_,_,_),_)
777 | (PC.TMetaParamList(_,_,_,_),_)
778 | (PC.TMetaConst(_,_,_,_,_),_)
779 | (PC.TMetaErr(_,_,_,_),_)
780 | (PC.TMetaExp(_,_,_,_,_),_)
781 | (PC.TMetaIdExp(_,_,_,_,_),_)
782 | (PC.TMetaLocalIdExp(_,_,_,_,_),_)
783 | (PC.TMetaExpList(_,_,_,_),_)
784 | (PC.TMetaType(_,_,_),_)
785 | (PC.TMetaInit(_,_,_),_)
786 | (PC.TMetaStm(_,_,_),_)
787 | (PC.TMetaStmList(_,_,_),_)
788 | (PC.TMetaPos(_,_,_,_),_) -> in_meta_decls
789 | _ -> false in
790 let redo_id ident clt v =
791 !Data.add_type_name ident;
792 (PC.TTypeId(ident,clt),v) in
793 let rec loop start infn type_names = function
794 (* infn: 0 means not in a function header
795 > 0 means in a function header, after infn - 1 unmatched open parens*)
796 [] -> []
797 | ((PC.TOBrace(clt),v)::_) as all when in_meta_decls ->
798 collect_choices type_names all (* never a function header *)
799 | delim::(PC.TIdent(ident,clt),v)::((PC.TMul(_),_) as x)::rest
800 when is_delim infn delim ->
801 let newid = redo_id ident clt v in
802 delim::newid::x::(loop false infn (ident::type_names) rest)
803 | delim::(PC.TIdent(ident,clt),v)::id::rest
804 when is_delim infn delim && is_id id ->
805 let newid = redo_id ident clt v in
806 delim::newid::id::(loop false infn (ident::type_names) rest)
807 | ((PC.TFunDecl(_),_) as fn)::rest ->
808 fn::(loop false 1 type_names rest)
809 | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
810 lp::(loop false (infn + 1) type_names rest)
811 | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
812 if infn - 1 = 1
813 then rp::(loop false 0 type_names rest) (* 0 means not in fn header *)
814 else rp::(loop false (infn - 1) type_names rest)
815 | (PC.TIdent(ident,clt),v)::((PC.TMul(_),_) as x)::rest when start ->
816 let newid = redo_id ident clt v in
817 newid::x::(loop false infn (ident::type_names) rest)
818 | (PC.TIdent(ident,clt),v)::id::rest when start && is_id id ->
819 let newid = redo_id ident clt v in
820 newid::id::(loop false infn (ident::type_names) rest)
821 | (PC.TIdent(ident,clt),v)::rest when List.mem ident type_names ->
822 (PC.TTypeId(ident,clt),v)::(loop false infn type_names rest)
823 | ((PC.TIdent(ident,clt),v) as x)::rest ->
824 x::(loop false infn type_names rest)
825 | x::rest -> x::(loop false infn type_names rest)
826 and collect_choices type_names = function
827 [] -> [] (* should happen, but let the parser detect that *)
828 | (PC.TCBrace(clt),v)::rest ->
829 (PC.TCBrace(clt),v)::(loop false 0 type_names rest)
830 | delim::(PC.TIdent(ident,clt),v)::rest
831 when is_choices_delim delim ->
832 let newid = redo_id ident clt v in
833 delim::newid::(collect_choices (ident::type_names) rest)
834 | x::rest -> x::(collect_choices type_names rest) in
835 loop true 0 [] l
836
837
838 (* ----------------------------------------------------------------------- *)
839 (* Insert TLineEnd tokens at the end of a line that contains a WHEN.
840 WHEN is restricted to a single line, to avoid ambiguity in eg:
841 ... WHEN != x
842 +3 *)
843
844 let token2line (tok,_) =
845 match tok with
846 PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
847 | PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
848 | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
849 | PC.Tstatic(clt) | PC.Tauto(clt) | PC.Tregister(clt) | PC.Textern(clt)
850 | PC.Tinline(clt) | PC.Ttypedef(clt) | PC.Tattr(_,clt) | PC.Tconst(clt)
851 | PC.Tvolatile(clt)
852
853 | PC.TInc(clt) | PC.TDec(clt)
854
855 | PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
856 | PC.TSwitch (clt) | PC.TCase (clt) | PC.TDefault (clt) | PC.TSizeof (clt)
857 | PC.TReturn(clt) | PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt)
858 | PC.TIdent(_,clt)
859 | PC.TTypeId(_,clt) | PC.TDeclarerId(_,clt) | PC.TIteratorId(_,clt)
860 | PC.TMetaDeclarer(_,_,_,clt) | PC.TMetaIterator(_,_,_,clt)
861
862 | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
863
864 | PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
865 | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
866 | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
867 | PC.TDmOp(_,clt) | PC.TTilde (clt)
868
869 | PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
870 | PC.TMetaConst(_,_,_,_,clt) | PC.TMetaExp(_,_,_,_,clt)
871 | PC.TMetaIdExp(_,_,_,_,clt) | PC.TMetaLocalIdExp(_,_,_,_,clt)
872 | PC.TMetaExpList(_,_,_,clt)
873 | PC.TMetaId(_,_,_,clt) | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt)
874 | PC.TMetaStm(_,_,clt) | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
875 | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt)
876
877 | PC.TFunDecl(clt)
878 | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
879 | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
880 (* | PC.TCircles(clt) | PC.TStars(clt) *)
881
882 | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
883 | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) (*| PC.TOCircles(clt)
884 | PC.TCCircles(clt) | PC.TOStars(clt) | PC.TCStars(clt) *)
885
886 | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
887 | PC.TOPar0(clt) | PC.TMid0(clt) | PC.TCPar(clt)
888 | PC.TCPar0(clt)
889
890 | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
891 | PC.TOInit(clt)
892
893 | PC.TPtrOp(clt)
894
895 | PC.TDefine(clt,_) | PC.TDefineParam(clt,_,_,_)
896 | PC.TIncludeL(_,clt) | PC.TIncludeNL(_,clt)
897
898 | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
899 | PC.TPtVirg(clt) ->
900 let (_,line,_,_,_,_,_,_) = clt in Some line
901
902 | _ -> None
903
904 let rec insert_line_end = function
905 [] -> []
906 | (((PC.TWhen(clt),q) as x)::xs) ->
907 x::(find_line_end true (token2line x) clt q xs)
908 | (((PC.TDefine(clt,_),q) as x)::xs)
909 | (((PC.TDefineParam(clt,_,_,_),q) as x)::xs) ->
910 x::(find_line_end false (token2line x) clt q xs)
911 | x::xs -> x::(insert_line_end xs)
912
913 and find_line_end inwhen line clt q = function
914 (* don't know what 2nd component should be so just use the info of
915 the When. Also inherit - of when, if any *)
916 [] -> [(PC.TLineEnd(clt),q)]
917 | ((PC.TIdent("strict",clt),a) as x)::xs when token2line x = line ->
918 (PC.TStrict(clt),a) :: (find_line_end inwhen line clt q xs)
919 | ((PC.TIdent("STRICT",clt),a) as x)::xs when token2line x = line ->
920 (PC.TStrict(clt),a) :: (find_line_end inwhen line clt q xs)
921 | ((PC.TIdent("any",clt),a) as x)::xs when token2line x = line ->
922 (PC.TAny(clt),a) :: (find_line_end inwhen line clt q xs)
923 | ((PC.TIdent("ANY",clt),a) as x)::xs when token2line x = line ->
924 (PC.TAny(clt),a) :: (find_line_end inwhen line clt q xs)
925 | ((PC.TIdent("forall",clt),a) as x)::xs when token2line x = line ->
926 (PC.TForall,a) :: (find_line_end inwhen line clt q xs)
927 | ((PC.TIdent("exists",clt),a) as x)::xs when token2line x = line ->
928 (PC.TExists,a) :: (find_line_end inwhen line clt q xs)
929 | ((PC.TComma(clt),a) as x)::xs when token2line x = line ->
930 (PC.TComma(clt),a) :: (find_line_end inwhen line clt q xs)
931 | ((PC.TPArob,a) as x)::xs -> (* no line #, just assume on the same line *)
932 x :: (find_line_end inwhen line clt q xs)
933 | x::xs when token2line x = line -> x :: (find_line_end inwhen line clt q xs)
934 | xs -> (PC.TLineEnd(clt),q)::(insert_line_end xs)
935
936 let rec translate_when_true_false = function
937 [] -> []
938 | (PC.TWhen(clt),q)::((PC.TNotEq(_),_) as x)::(PC.TIdent("true",_),_)::xs ->
939 (PC.TWhenTrue(clt),q)::x::(translate_when_true_false xs)
940 | (PC.TWhen(clt),q)::((PC.TNotEq(_),_) as x)::(PC.TIdent("false",_),_)::xs ->
941 (PC.TWhenFalse(clt),q)::x::(translate_when_true_false xs)
942 | x::xs -> x :: (translate_when_true_false xs)
943
944 (* ----------------------------------------------------------------------- *)
945
946 let check_parentheses tokens =
947 let clt2line (_,line,_,_,_,_,_,_) = line in
948 let rec loop seen_open = function
949 [] -> tokens
950 | (PC.TOPar(clt),q) :: rest
951 | (PC.TDefineParam(clt,_,_,_),q) :: rest ->
952 loop (Common.Left (clt2line clt) :: seen_open) rest
953 | (PC.TOPar0(clt),q) :: rest ->
954 loop (Common.Right (clt2line clt) :: seen_open) rest
955 | (PC.TCPar(clt),q) :: rest ->
956 (match seen_open with
957 [] ->
958 failwith
959 (Printf.sprintf
960 "unexpected close parenthesis in line %d\n" (clt2line clt))
961 | Common.Left _ :: seen_open -> loop seen_open rest
962 | Common.Right open_line :: _ ->
963 failwith
964 (Printf.sprintf
965 "disjunction parenthesis in line %d column 0 matched to normal parenthesis on line %d\n" open_line (clt2line clt)))
966 | (PC.TCPar0(clt),q) :: rest ->
967 (match seen_open with
968 [] ->
969 failwith
970 (Printf.sprintf
971 "unexpected close parenthesis in line %d\n" (clt2line clt))
972 | Common.Right _ :: seen_open -> loop seen_open rest
973 | Common.Left open_line :: _ ->
974 failwith
975 (Printf.sprintf
976 "normal parenthesis in line %d matched to disjunction parenthesis on line %d column 0\n" open_line (clt2line clt)))
977 | x::rest -> loop seen_open rest in
978 loop [] tokens
979
980 (* ----------------------------------------------------------------------- *)
981 (* top level initializers: a sequence of braces followed by a dot *)
982
983 let find_top_init tokens =
984 match tokens with
985 (PC.TOBrace(clt),q) :: rest ->
986 let rec dot_start acc = function
987 ((PC.TOBrace(_),_) as x) :: rest ->
988 dot_start (x::acc) rest
989 | ((PC.TDot(_),_) :: rest) as x ->
990 Some ((PC.TOInit(clt),q) :: (List.rev acc) @ x)
991 | l -> None in
992 let rec comma_end acc = function
993 ((PC.TCBrace(_),_) as x) :: rest ->
994 comma_end (x::acc) rest
995 | ((PC.TComma(_),_) :: rest) as x ->
996 Some ((PC.TOInit(clt),q) :: (List.rev x) @ acc)
997 | l -> None in
998 (match dot_start [] rest with
999 Some x -> x
1000 | None ->
1001 (match List.rev rest with
1002 (* not super sure what this does, but EOF, @, and @@ should be
1003 the same, markind the end of a rule *)
1004 ((PC.EOF,_) as x)::rest | ((PC.TArob,_) as x)::rest
1005 | ((PC.TArobArob,_) as x)::rest ->
1006 (match comma_end [x] rest with
1007 Some x -> x
1008 | None -> tokens)
1009 | _ ->
1010 failwith "unexpected empty token list"))
1011 | _ -> tokens
1012
1013 (* ----------------------------------------------------------------------- *)
1014 (* Integrate pragmas into some adjacent token. + tokens are preferred. Dots
1015 are not allowed. *)
1016
1017 let rec collect_all_pragmas collected = function
1018 (PC.TPragma(s,(_,line,logical_line,offset,col,_,_,pos)),_)::rest ->
1019 let i =
1020 { Ast0.line_start = line; Ast0.line_end = line;
1021 Ast0.logical_start = logical_line; Ast0.logical_end = logical_line;
1022 Ast0.column = col; Ast0.offset = offset; } in
1023 collect_all_pragmas ((s,i)::collected) rest
1024 | l -> (List.rev collected,l)
1025
1026 let rec collect_pass = function
1027 [] -> ([],[])
1028 | x::xs ->
1029 match plus_attachable false x with
1030 SKIP ->
1031 let (pass,rest) = collect_pass xs in
1032 (x::pass,rest)
1033 | _ -> ([],x::xs)
1034
1035 let plus_attach strict = function
1036 None -> NOTPLUS
1037 | Some x -> plus_attachable strict x
1038
1039 let add_bef = function Some x -> [x] | None -> []
1040
1041 (*skips should be things like line end
1042 skips is things before pragmas that can't be attached to, pass is things
1043 after. pass is used immediately. skips accumulates. *)
1044 let rec process_pragmas bef skips = function
1045 [] -> add_bef bef @ List.rev skips
1046 | ((PC.TPragma(s,i),_)::_) as l ->
1047 let (pragmas,rest) = collect_all_pragmas [] l in
1048 let (pass,rest0) = collect_pass rest in
1049 let (next,rest) =
1050 match rest0 with [] -> (None,[]) | next::rest -> (Some next,rest) in
1051 (match (bef,plus_attach true bef,next,plus_attach true next) with
1052 (Some bef,PLUS,_,_) ->
1053 let (a,b,c,d,e,strbef,straft,pos) = get_clt bef in
1054 (update_clt bef (a,b,c,d,e,strbef,pragmas,pos))::List.rev skips@
1055 pass@process_pragmas None [] rest0
1056 | (_,_,Some next,PLUS) ->
1057 let (a,b,c,d,e,strbef,straft,pos) = get_clt next in
1058 (add_bef bef) @ List.rev skips @ pass @
1059 (process_pragmas
1060 (Some (update_clt next (a,b,c,d,e,pragmas,straft,pos)))
1061 [] rest)
1062 | _ ->
1063 (match (bef,plus_attach false bef,next,plus_attach false next) with
1064 (Some bef,PLUS,_,_) ->
1065 let (a,b,c,d,e,strbef,straft,pos) = get_clt bef in
1066 (update_clt bef (a,b,c,d,e,strbef,pragmas,pos))::List.rev skips@
1067 pass@process_pragmas None [] rest0
1068 | (_,_,Some next,PLUS) ->
1069 let (a,b,c,d,e,strbef,straft,pos) = get_clt next in
1070 (add_bef bef) @ List.rev skips @ pass @
1071 (process_pragmas
1072 (Some (update_clt next (a,b,c,d,e,pragmas,straft,pos)))
1073 [] rest)
1074 | _ -> failwith "nothing to attach pragma to"))
1075 | x::xs ->
1076 (match plus_attachable false x with
1077 SKIP -> process_pragmas bef (x::skips) xs
1078 | _ -> (add_bef bef) @ List.rev skips @ (process_pragmas (Some x) [] xs))
1079
1080 (* ----------------------------------------------------------------------- *)
1081 (* Drop ... ... . This is only allowed in + code, and arises when there is
1082 some - code between the ... *)
1083 (* drop whens as well - they serve no purpose in + code and they cause
1084 problems for drop_double_dots *)
1085
1086 let rec drop_when = function
1087 [] -> []
1088 | (PC.TWhen(clt),info)::xs ->
1089 let rec loop = function
1090 [] -> []
1091 | (PC.TLineEnd(_),info)::xs -> drop_when xs
1092 | x::xs -> loop xs in
1093 loop xs
1094 | x::xs -> x::drop_when xs
1095
1096 (* instead of dropping the double dots, we put TNothing in between them.
1097 these vanish after the parser, but keeping all the ...s in the + code makes
1098 it easier to align the + and - code in context_neg and in preparation for the
1099 isomorphisms. This shouldn't matter because the context code of the +
1100 slice is mostly ignored anyway *)
1101 let minus_to_nothing l =
1102 (* for cases like | <..., which may or may not arise from removing minus
1103 code, depending on whether <... is a statement or expression *)
1104 let is_minus tok =
1105 try
1106 let (d,_,_,_,_,_,_,_) = get_clt tok in
1107 (match d with
1108 D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> true
1109 | D.PLUS | D.PLUSPLUS -> false
1110 | D.CONTEXT | D.UNIQUE | D.OPT -> false)
1111 with _ -> false in
1112 let rec minus_loop = function
1113 [] -> []
1114 | (d::ds) as l -> if is_minus d then minus_loop ds else l in
1115 let rec loop = function
1116 [] -> []
1117 | ((PC.TMid0(clt),i) as x)::t1::ts when is_minus t1 ->
1118 (match minus_loop ts with
1119 ((PC.TOEllipsis(_),_)::_) | ((PC.TPOEllipsis(_),_)::_)
1120 | ((PC.TEllipsis(_),_)::_) as l -> x::(PC.TNothing,i)::(loop l)
1121 | l -> x::(loop l))
1122 | t::ts -> t::(loop ts) in
1123 loop l
1124
1125 let rec drop_double_dots l =
1126 let start = function
1127 (PC.TOEllipsis(_),_) | (PC.TPOEllipsis(_),_)
1128 (* | (PC.TOCircles(_),_) | (PC.TOStars(_),_) *) ->
1129 true
1130 | _ -> false in
1131 let middle = function
1132 (PC.TEllipsis(_),_) (* | (PC.TCircles(_),_) | (PC.TStars(_),_) *) -> true
1133 | _ -> false in
1134 let whenline = function
1135 (PC.TLineEnd(_),_) -> true
1136 (*| (PC.TMid0(_),_) -> true*)
1137 | _ -> false in
1138 let final = function
1139 (PC.TCEllipsis(_),_) | (PC.TPCEllipsis(_),_)
1140 (* | (PC.TCCircles(_),_) | (PC.TCStars(_),_) *) ->
1141 true
1142 | _ -> false in
1143 let any_before x = start x or middle x or final x or whenline x in
1144 let any_after x = start x or middle x or final x in
1145 let rec loop ((_,i) as prev) = function
1146 [] -> []
1147 | x::rest when any_before prev && any_after x ->
1148 (PC.TNothing,i)::x::(loop x rest)
1149 | x::rest -> x :: (loop x rest) in
1150 match l with
1151 [] -> []
1152 | (x::xs) -> x :: loop x xs
1153
1154 let rec fix f l =
1155 let cur = f l in
1156 if l = cur then l else fix f cur
1157
1158 (* ( | ... | ) also causes parsing problems *)
1159
1160 exception Not_empty
1161
1162 let rec drop_empty_thing starter middle ender = function
1163 [] -> []
1164 | hd::rest when starter hd ->
1165 let rec loop = function
1166 x::rest when middle x -> loop rest
1167 | x::rest when ender x -> rest
1168 | _ -> raise Not_empty in
1169 (match try Some(loop rest) with Not_empty -> None with
1170 Some x -> drop_empty_thing starter middle ender x
1171 | None -> hd :: drop_empty_thing starter middle ender rest)
1172 | x::rest -> x :: drop_empty_thing starter middle ender rest
1173
1174 let drop_empty_or =
1175 drop_empty_thing
1176 (function (PC.TOPar0(_),_) -> true | _ -> false)
1177 (function (PC.TMid0(_),_) -> true | _ -> false)
1178 (function (PC.TCPar0(_),_) -> true | _ -> false)
1179
1180 let drop_empty_nest = drop_empty_thing
1181
1182 (* ----------------------------------------------------------------------- *)
1183 (* Read tokens *)
1184
1185 let get_s_starts (_, (s,_,(starts, ends))) =
1186 Printf.printf "%d %d\n" starts ends; (s, starts)
1187
1188 let pop2 l =
1189 let v = List.hd !l in
1190 l := List.tl !l;
1191 v
1192
1193 let reinit _ =
1194 PC.reinit (function _ -> PC.TArobArob (* a handy token *))
1195 (Lexing.from_function
1196 (function buf -> function n -> raise Common.Impossible))
1197
1198 let parse_one str parsefn file toks =
1199 let all_tokens = ref toks in
1200 let cur_tok = ref (List.hd !all_tokens) in
1201
1202 let lexer_function _ =
1203 let (v, info) = pop2 all_tokens in
1204 cur_tok := (v, info);
1205 v in
1206
1207 let lexbuf_fake =
1208 Lexing.from_function
1209 (function buf -> function n -> raise Common.Impossible)
1210 in
1211
1212 reinit();
1213
1214 try parsefn lexer_function lexbuf_fake
1215 with
1216 Lexer_cocci.Lexical s ->
1217 failwith
1218 (Printf.sprintf "%s: lexical error: %s\n =%s\n" str s
1219 (Common.error_message file (get_s_starts !cur_tok) ))
1220 | Parser_cocci_menhir.Error ->
1221 failwith
1222 (Printf.sprintf "%s: parse error: \n = %s\n" str
1223 (Common.error_message file (get_s_starts !cur_tok) ))
1224 | Semantic_cocci.Semantic s ->
1225 failwith
1226 (Printf.sprintf "%s: semantic error: %s\n =%s\n" str s
1227 (Common.error_message file (get_s_starts !cur_tok) ))
1228
1229 | e -> raise e
1230
1231 let prepare_tokens tokens =
1232 find_top_init
1233 (translate_when_true_false (* after insert_line_end *)
1234 (insert_line_end
1235 (detect_types false
1236 (find_function_names (detect_attr (check_parentheses tokens))))))
1237
1238 let prepare_mv_tokens tokens =
1239 detect_types false (detect_attr tokens)
1240
1241 let rec consume_minus_positions = function
1242 [] -> []
1243 | ((PC.TOPar0(_),_) as x)::xs | ((PC.TCPar0(_),_) as x)::xs
1244 | ((PC.TMid0(_),_) as x)::xs -> x::consume_minus_positions xs
1245 | x::(PC.TPArob,_)::(PC.TMetaPos(name,constraints,per,clt),_)::xs ->
1246 let (arity,ln,lln,offset,col,strbef,straft,_) = get_clt x in
1247 let name = Parse_aux.clt2mcode name clt in
1248 let x =
1249 update_clt x
1250 (arity,ln,lln,offset,col,strbef,straft,
1251 Ast0.MetaPos(name,constraints,per)) in
1252 x::(consume_minus_positions xs)
1253 | x::xs -> x::consume_minus_positions xs
1254
1255 let any_modif rule =
1256 let mcode x =
1257 match Ast0.get_mcode_mcodekind x with
1258 Ast0.MINUS _ | Ast0.PLUS _ -> true
1259 | _ -> false in
1260 let donothing r k e = k e in
1261 let bind x y = x or y in
1262 let option_default = false in
1263 let fn =
1264 V0.flat_combiner bind option_default
1265 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
1266 donothing donothing donothing donothing donothing donothing
1267 donothing donothing donothing donothing donothing donothing donothing
1268 donothing donothing in
1269 List.exists fn.VT0.combiner_rec_top_level rule
1270
1271 let eval_virt virt =
1272 List.iter
1273 (function x ->
1274 if not (List.mem x virt)
1275 then
1276 failwith
1277 (Printf.sprintf "unknown virtual rule %s\n" x))
1278 (!Flag_parsing_cocci.defined_virtual_rules @
1279 !Flag_parsing_cocci.undefined_virtual_rules);
1280 List.map
1281 (function x ->
1282 if List.mem x !Flag_parsing_cocci.defined_virtual_rules
1283 then (x,true)
1284 else if List.mem x !Flag_parsing_cocci.undefined_virtual_rules
1285 then (x,false)
1286 else
1287 (*Printf.fprintf stderr
1288 "warning: no value specified for virtual rule %s, assuming unmatched\n" x;*)
1289 (x,false))
1290 virt
1291
1292 let drop_last extra l = List.rev(extra@(List.tl(List.rev l)))
1293
1294 let partition_either l =
1295 let rec part_either left right = function
1296 | [] -> (List.rev left, List.rev right)
1297 | x :: l ->
1298 (match x with
1299 | Common.Left e -> part_either (e :: left) right l
1300 | Common.Right e -> part_either left (e :: right) l) in
1301 part_either [] [] l
1302
1303 let get_metavars parse_fn table file lexbuf =
1304 let rec meta_loop acc (* read one decl at a time *) =
1305 let (_,tokens) =
1306 Data.call_in_meta
1307 (function _ ->
1308 tokens_all table file true lexbuf [PC.TArobArob;PC.TMPtVirg]) in
1309 let tokens = prepare_mv_tokens tokens in
1310 match tokens with
1311 [(PC.TArobArob,_)] -> List.rev acc
1312 | _ ->
1313 let metavars = parse_one "meta" parse_fn file tokens in
1314 meta_loop (metavars@acc) in
1315 partition_either (meta_loop [])
1316
1317 let get_script_metavars parse_fn table file lexbuf =
1318 let rec meta_loop acc =
1319 let (_, tokens) =
1320 tokens_all table file true lexbuf [PC.TArobArob; PC.TMPtVirg] in
1321 let tokens = prepare_tokens tokens in
1322 match tokens with
1323 [(PC.TArobArob, _)] -> List.rev acc
1324 | _ ->
1325 let metavar = parse_one "scriptmeta" parse_fn file tokens in
1326 meta_loop (metavar :: acc)
1327 in
1328 meta_loop []
1329
1330 let get_rule_name parse_fn starts_with_name get_tokens file prefix =
1331 Data.in_rule_name := true;
1332 let mknm _ = make_name prefix (!Lexer_cocci.line) in
1333 let name_res =
1334 if starts_with_name
1335 then
1336 let (_,tokens) = get_tokens [PC.TArob] in
1337 let check_name = function
1338 None -> Some (mknm())
1339 | Some nm ->
1340 (if List.mem nm reserved_names
1341 then failwith (Printf.sprintf "invalid name %s\n" nm));
1342 Some nm in
1343 match parse_one "rule name" parse_fn file tokens with
1344 Ast.CocciRulename (nm,a,b,c,d,e) ->
1345 Ast.CocciRulename (check_name nm,a,b,c,d,e)
1346 | Ast.GeneratedRulename (nm,a,b,c,d,e) ->
1347 Ast.GeneratedRulename (check_name nm,a,b,c,d,e)
1348 | Ast.ScriptRulename(s,deps) -> Ast.ScriptRulename(s,deps)
1349 | Ast.InitialScriptRulename(s) -> Ast.InitialScriptRulename(s)
1350 | Ast.FinalScriptRulename(s) -> Ast.FinalScriptRulename(s)
1351 else
1352 Ast.CocciRulename(Some(mknm()),Ast.NoDep,[],[],Ast.Undetermined,false) in
1353 Data.in_rule_name := false;
1354 name_res
1355
1356 let parse_iso file =
1357 let table = Common.full_charpos_to_pos file in
1358 Common.with_open_infile file (fun channel ->
1359 let lexbuf = Lexing.from_channel channel in
1360 let get_tokens = tokens_all table file false lexbuf in
1361 let res =
1362 match get_tokens [PC.TArobArob;PC.TArob] with
1363 (true,start) ->
1364 let parse_start start =
1365 let rev = List.rev start in
1366 let (arob,_) = List.hd rev in
1367 (arob = PC.TArob,List.rev(List.tl rev)) in
1368 let (starts_with_name,start) = parse_start start in
1369 let rec loop starts_with_name start =
1370 (!Data.init_rule)();
1371 (* get metavariable declarations - have to be read before the
1372 rest *)
1373 let (rule_name,_,_,_,_,_) =
1374 match get_rule_name PC.iso_rule_name starts_with_name get_tokens
1375 file ("iso file "^file) with
1376 Ast.CocciRulename (Some n,a,b,c,d,e) -> (n,a,b,c,d,e)
1377 | _ -> failwith "Script rules cannot appear in isomorphism rules"
1378 in
1379 Ast0.rule_name := rule_name;
1380 let iso_metavars =
1381 match get_metavars PC.iso_meta_main table file lexbuf with
1382 (iso_metavars,[]) -> iso_metavars
1383 | _ -> failwith "unexpected inheritance in iso" in
1384 (* get the rule *)
1385 let (more,tokens) =
1386 get_tokens
1387 [PC.TIsoStatement;PC.TIsoExpression;PC.TIsoArgExpression;
1388 PC.TIsoTestExpression;
1389 PC.TIsoDeclaration;PC.TIsoType;PC.TIsoTopLevel] in
1390 let next_start = List.hd(List.rev tokens) in
1391 let dummy_info = ("",(-1,-1),(-1,-1)) in
1392 let tokens = drop_last [(PC.EOF,dummy_info)] tokens in
1393 let tokens = prepare_tokens (start@tokens) in
1394 (*
1395 print_tokens "iso tokens" tokens;
1396 *)
1397 let entry = parse_one "iso main" PC.iso_main file tokens in
1398 let entry = List.map (List.map Test_exps.process_anything) entry in
1399 if more
1400 then (* The code below allows a header like Statement list,
1401 which is more than one word. We don't have that any more,
1402 but the code is left here in case it is put back. *)
1403 match get_tokens [PC.TArobArob;PC.TArob] with
1404 (true,start) ->
1405 let (starts_with_name,start) = parse_start start in
1406 (iso_metavars,entry,rule_name) ::
1407 (loop starts_with_name (next_start::start))
1408 | _ -> failwith "isomorphism ends early"
1409 else [(iso_metavars,entry,rule_name)] in
1410 loop starts_with_name start
1411 | (false,_) -> [] in
1412 res)
1413
1414 let parse_iso_files existing_isos iso_files extra_path =
1415 let get_names = List.map (function (_,_,nm) -> nm) in
1416 let old_names = get_names existing_isos in
1417 Data.in_iso := true;
1418 let (res,_) =
1419 List.fold_left
1420 (function (prev,names) ->
1421 function file ->
1422 Lexer_cocci.init ();
1423 let file =
1424 match file with
1425 Common.Left(fl) -> Filename.concat extra_path fl
1426 | Common.Right(fl) -> Filename.concat Config.path fl in
1427 let current = parse_iso file in
1428 let new_names = get_names current in
1429 if List.exists (function x -> List.mem x names) new_names
1430 then failwith (Printf.sprintf "repeated iso name found in %s" file);
1431 (current::prev,new_names @ names))
1432 ([],old_names) iso_files in
1433 Data.in_iso := false;
1434 existing_isos@(List.concat (List.rev res))
1435
1436 let rec parse file =
1437 let table = Common.full_charpos_to_pos file in
1438 Common.with_open_infile file (fun channel ->
1439 let lexbuf = Lexing.from_channel channel in
1440 let get_tokens = tokens_all table file false lexbuf in
1441 Data.in_prolog := true;
1442 let initial_tokens = get_tokens [PC.TArobArob;PC.TArob] in
1443 Data.in_prolog := false;
1444 let res =
1445 match initial_tokens with
1446 (true,data) ->
1447 (match List.rev data with
1448 ((PC.TArobArob as x),_)::_ | ((PC.TArob as x),_)::_ ->
1449 let include_and_iso_files =
1450 parse_one "include and iso file names" PC.include_main file data in
1451
1452 let (include_files,iso_files,virt) =
1453 List.fold_left
1454 (function (include_files,iso_files,virt) ->
1455 function
1456 Data.Include s -> (s::include_files,iso_files,virt)
1457 | Data.Iso s -> (include_files,s::iso_files,virt)
1458 | Data.Virt l -> (include_files,iso_files,l@virt))
1459 ([],[],[]) include_and_iso_files in
1460 List.iter (function x -> Hashtbl.add Lexer_cocci.rule_names x ())
1461 virt;
1462
1463 let (extra_iso_files, extra_rules, extra_virt) =
1464 let rec loop = function
1465 [] -> ([],[],[])
1466 | (a,b,c)::rest ->
1467 let (x,y,z) = loop rest in
1468 (a::x,b::y,c::z) in
1469 loop (List.map parse include_files) in
1470
1471 let parse_cocci_rule ruletype old_metas
1472 (rule_name, dependencies, iso, dropiso, exists, is_expression) =
1473 Ast0.rule_name := rule_name;
1474 Data.inheritable_positions :=
1475 rule_name :: !Data.inheritable_positions;
1476
1477 (* get metavariable declarations *)
1478 let (metavars, inherited_metavars) =
1479 get_metavars PC.meta_main table file lexbuf in
1480 Hashtbl.add Data.all_metadecls rule_name metavars;
1481 Hashtbl.add Lexer_cocci.rule_names rule_name ();
1482 Hashtbl.add Lexer_cocci.all_metavariables rule_name
1483 (Hashtbl.fold
1484 (fun key v rest -> (key,v)::rest)
1485 Lexer_cocci.metavariables []);
1486
1487 (* get transformation rules *)
1488 let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
1489 let (minus_tokens, _) = split_token_stream tokens in
1490 let (_, plus_tokens) =
1491 split_token_stream (minus_to_nothing tokens) in
1492
1493 let minus_tokens = consume_minus_positions minus_tokens in
1494 let minus_tokens = prepare_tokens minus_tokens in
1495 let plus_tokens = prepare_tokens plus_tokens in
1496
1497 (*
1498 print_tokens "minus tokens" minus_tokens;
1499 print_tokens "plus tokens" plus_tokens;
1500 *)
1501
1502 let plus_tokens =
1503 process_pragmas None []
1504 (fix (function x -> drop_double_dots (drop_empty_or x))
1505 (drop_when plus_tokens)) in
1506 (*
1507 print_tokens "plus tokens" plus_tokens;
1508 Printf.printf "before minus parse\n";
1509 *)
1510 let minus_res =
1511 if is_expression
1512 then parse_one "minus" PC.minus_exp_main file minus_tokens
1513 else parse_one "minus" PC.minus_main file minus_tokens in
1514 (*
1515 Unparse_ast0.unparse minus_res;
1516 Printf.printf "before plus parse\n";
1517 *)
1518 let plus_res =
1519 if !Flag.sgrep_mode2
1520 then (* not actually used for anything, except context_neg *)
1521 List.map
1522 (Iso_pattern.rebuild_mcode None).VT0.rebuilder_rec_top_level
1523 minus_res
1524 else
1525 if is_expression
1526 then parse_one "plus" PC.plus_exp_main file plus_tokens
1527 else parse_one "plus" PC.plus_main file plus_tokens in
1528 (*
1529 Printf.printf "after plus parse\n";
1530 *)
1531
1532 (if not !Flag.sgrep_mode2 &&
1533 (any_modif minus_res or any_modif plus_res)
1534 then Data.inheritable_positions := []);
1535
1536 Check_meta.check_meta rule_name old_metas inherited_metavars
1537 metavars minus_res plus_res;
1538
1539 (more, Ast0.CocciRule ((minus_res, metavars,
1540 (iso, dropiso, dependencies, rule_name, exists)),
1541 (plus_res, metavars), ruletype), metavars, tokens) in
1542
1543 let rec collect_script_tokens = function
1544 [(PC.EOF,_)] | [(PC.TArobArob,_)] | [(PC.TArob,_)] -> ""
1545 | (PC.TScriptData(s),_)::xs -> s^(collect_script_tokens xs)
1546 | toks ->
1547 List.iter
1548 (function x ->
1549 Printf.printf "%s\n" (token2c x))
1550 toks;
1551 failwith "Malformed script rule" in
1552
1553 let parse_script_rule language old_metas deps =
1554 let get_tokens = tokens_script_all table file false lexbuf in
1555
1556 (* meta-variables *)
1557 let metavars =
1558 Data.call_in_meta
1559 (function _ ->
1560 get_script_metavars PC.script_meta_main table file lexbuf) in
1561
1562 let exists_in old_metas (py,(r,m)) =
1563 let test (rr,mr) x =
1564 let (ro,vo) = Ast.get_meta_name x in
1565 ro = rr && vo = mr in
1566 List.exists (test (r,m)) old_metas in
1567
1568 List.iter
1569 (function x ->
1570 let meta2c (r,n) = Printf.sprintf "%s.%s" r n in
1571 if not (exists_in old_metas x) then
1572 failwith
1573 (Printf.sprintf
1574 "Script references unknown meta-variable: %s"
1575 (meta2c(snd x))))
1576 metavars;
1577
1578 (* script code *)
1579 let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
1580 let data = collect_script_tokens tokens in
1581 (more,Ast0.ScriptRule(language, deps, metavars, data),[],tokens) in
1582
1583 let parse_if_script_rule k language =
1584 let get_tokens = tokens_script_all table file false lexbuf in
1585
1586 (* script code *)
1587 let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
1588 let data = collect_script_tokens tokens in
1589 (more,k (language, data),[],tokens) in
1590
1591 let parse_iscript_rule =
1592 parse_if_script_rule
1593 (function (language,data) ->
1594 Ast0.InitialScriptRule(language,data)) in
1595
1596 let parse_fscript_rule =
1597 parse_if_script_rule
1598 (function (language,data) ->
1599 Ast0.FinalScriptRule(language,data)) in
1600
1601 let parse_rule old_metas starts_with_name =
1602 let rulename =
1603 get_rule_name PC.rule_name starts_with_name get_tokens file
1604 "rule" in
1605 match rulename with
1606 Ast.CocciRulename (Some s, a, b, c, d, e) ->
1607 parse_cocci_rule Ast.Normal old_metas (s, a, b, c, d, e)
1608 | Ast.GeneratedRulename (Some s, a, b, c, d, e) ->
1609 Data.in_generating := true;
1610 let res =
1611 parse_cocci_rule Ast.Generated old_metas (s,a,b,c,d,e) in
1612 Data.in_generating := false;
1613 res
1614 | Ast.ScriptRulename(l,deps) -> parse_script_rule l old_metas deps
1615 | Ast.InitialScriptRulename(l) -> parse_iscript_rule l
1616 | Ast.FinalScriptRulename(l) -> parse_fscript_rule l
1617 | _ -> failwith "Malformed rule name"
1618 in
1619
1620 let rec loop old_metas starts_with_name =
1621 (!Data.init_rule)();
1622
1623 let gen_starts_with_name more tokens =
1624 more &&
1625 (match List.hd (List.rev tokens) with
1626 (PC.TArobArob,_) -> false
1627 | (PC.TArob,_) -> true
1628 | _ -> failwith "unexpected token")
1629 in
1630
1631 let (more, rule, metavars, tokens) =
1632 parse_rule old_metas starts_with_name in
1633 if more then
1634 rule::
1635 (loop (metavars @ old_metas) (gen_starts_with_name more tokens))
1636 else [rule] in
1637
1638 (List.fold_left
1639 (function prev -> function cur -> Common.union_set cur prev)
1640 iso_files extra_iso_files,
1641 (* included rules first *)
1642 List.fold_left (@) (loop [] (x = PC.TArob)) (List.rev extra_rules),
1643 List.fold_left (@) virt extra_virt (*no dups allowed*))
1644 | _ -> failwith "unexpected code before the first rule\n")
1645 | (false,[(PC.TArobArob,_)]) | (false,[(PC.TArob,_)]) ->
1646 ([],([] : Ast0.parsed_rule list),[] (*virtual rules*))
1647 | _ -> failwith "unexpected code before the first rule\n" in
1648 res)
1649
1650 (* parse to ast0 and then convert to ast *)
1651 let process file isofile verbose =
1652 let extra_path = Filename.dirname file in
1653 Lexer_cocci.init();
1654 let (iso_files, rules, virt) = parse file in
1655 let virt = eval_virt virt in
1656 let std_isos =
1657 match isofile with
1658 None -> []
1659 | Some iso_file -> parse_iso_files [] [Common.Left iso_file] "" in
1660 let global_isos = parse_iso_files std_isos iso_files extra_path in
1661 let rules = Unitary_ast0.do_unitary rules in
1662 let parsed =
1663 List.map
1664 (function
1665 Ast0.ScriptRule (a,b,c,d) -> [([],Ast.ScriptRule (a,b,c,d))]
1666 | Ast0.InitialScriptRule (a,b) -> [([],Ast.InitialScriptRule (a,b))]
1667 | Ast0.FinalScriptRule (a,b) -> [([],Ast.FinalScriptRule (a,b))]
1668 | Ast0.CocciRule
1669 ((minus, metavarsm,
1670 (iso, dropiso, dependencies, rule_name, exists)),
1671 (plus, metavars),ruletype) ->
1672 let chosen_isos =
1673 parse_iso_files global_isos
1674 (List.map (function x -> Common.Left x) iso)
1675 extra_path in
1676 let chosen_isos =
1677 (* check that dropped isos are actually available *)
1678 (try
1679 let iso_names =
1680 List.map (function (_,_,nm) -> nm) chosen_isos in
1681 let local_iso_names = reserved_names @ iso_names in
1682 let bad_dropped =
1683 List.find
1684 (function dropped ->
1685 not (List.mem dropped local_iso_names))
1686 dropiso in
1687 failwith
1688 ("invalid iso name " ^ bad_dropped ^ " in " ^ rule_name)
1689 with Not_found -> ());
1690 if List.mem "all" dropiso
1691 then
1692 if List.length dropiso = 1
1693 then []
1694 else failwith "disable all should only be by itself"
1695 else (* drop those isos *)
1696 List.filter
1697 (function (_,_,nm) -> not (List.mem nm dropiso))
1698 chosen_isos in
1699 List.iter Iso_compile.process chosen_isos;
1700 let dropped_isos =
1701 match reserved_names with
1702 "all"::others ->
1703 (match dropiso with
1704 ["all"] -> others
1705 | _ ->
1706 List.filter (function x -> List.mem x dropiso) others)
1707 | _ ->
1708 failwith
1709 "bad list of reserved names - all must be at start" in
1710 let minus = Test_exps.process minus in
1711 let minus = Compute_lines.compute_lines false minus in
1712 let plus = Compute_lines.compute_lines false plus in
1713 let is_exp =
1714 (* only relevant to Flag.make_hrule *)
1715 (* doesn't handle multiple minirules properly, but since
1716 we don't really handle them in lots of other ways, it
1717 doesn't seem very important *)
1718 match plus with
1719 [] -> [false]
1720 | p::_ ->
1721 [match Ast0.unwrap p with
1722 Ast0.CODE c ->
1723 (match List.map Ast0.unwrap (Ast0.undots c) with
1724 [Ast0.Exp e] -> true | _ -> false)
1725 | _ -> false] in
1726 let minus = Arity.minus_arity minus in
1727 let ((metavars,minus),function_prototypes) =
1728 Function_prototypes.process
1729 rule_name metavars dropped_isos minus plus ruletype in
1730 let plus = Adjust_pragmas.process plus in
1731 (* warning! context_neg side-effects its arguments *)
1732 let (m,p) = List.split (Context_neg.context_neg minus plus) in
1733 Type_infer.type_infer p;
1734 (if not !Flag.sgrep_mode2
1735 then Insert_plus.insert_plus m p (chosen_isos = []));
1736 Type_infer.type_infer minus;
1737 let (extra_meta, minus) =
1738 match (chosen_isos,ruletype) with
1739 (* separate case for [] because applying isos puts
1740 some restrictions on the -+ code *)
1741 ([],_) | (_,Ast.Generated) -> ([],minus)
1742 | _ -> Iso_pattern.apply_isos chosen_isos minus rule_name in
1743 (* after iso, because iso can intro ... *)
1744 let minus = Adjacency.compute_adjacency minus in
1745 let minus = Comm_assoc.comm_assoc minus rule_name dropiso in
1746 let minus =
1747 if !Flag.sgrep_mode2 then minus
1748 else Single_statement.single_statement minus in
1749 let minus = Simple_assignments.simple_assignments minus in
1750 let minus_ast =
1751 Ast0toast.ast0toast rule_name dependencies dropped_isos
1752 exists minus is_exp ruletype in
1753
1754 match function_prototypes with
1755 None -> [(extra_meta @ metavars, minus_ast)]
1756 | Some mv_fp -> [(extra_meta @ metavars, minus_ast); mv_fp])
1757 (* Ast0.CocciRule ((minus, metavarsm, (iso, dropiso, dependencies, rule_name, exists)), (plus, metavars))*)
1758 rules in
1759 let parsed = List.concat parsed in
1760 let disjd = Disjdistr.disj parsed in
1761
1762 let (metavars,code,fvs,neg_pos,ua,pos) = Free_vars.free_vars disjd in
1763 if !Flag_parsing_cocci.show_SP
1764 then List.iter Pretty_print_cocci.unparse code;
1765
1766 let grep_tokens =
1767 Common.profile_code "get_constants"
1768 (fun () -> Get_constants.get_constants code) in (* for grep *)
1769 let glimpse_tokens2 =
1770 Common.profile_code "get_glimpse_constants" (* for glimpse *)
1771 (fun () -> Get_constants2.get_constants code neg_pos virt) in
1772
1773 (metavars,code,fvs,neg_pos,ua,pos,grep_tokens,glimpse_tokens2,virt)