Commit | Line | Data |
---|---|---|
1be43e12 C |
1 | (* |
2 | * Copyright 2005-2008, Ecole des Mines de Nantes, University of Copenhagen | |
3 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller | |
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 | open Common | |
24 | ||
25 | (*****************************************************************************) | |
26 | (* The functor argument *) | |
27 | (*****************************************************************************) | |
28 | ||
29 | (* info passed recursively in monad in addition to binding *) | |
30 | type xinfo = { | |
31 | optional_storage_iso : bool; | |
32 | optional_qualifier_iso : bool; | |
33 | value_format_iso : bool; | |
34 | } | |
35 | ||
36 | module XMATCH = struct | |
37 | ||
38 | (* ------------------------------------------------------------------------*) | |
39 | (* Combinators history *) | |
40 | (* ------------------------------------------------------------------------*) | |
41 | (* | |
42 | * version0: | |
43 | * type ('a, 'b) matcher = 'a -> 'b -> bool | |
44 | * | |
45 | * version1: same but with a global variable holding the current binding | |
46 | * BUT bug | |
47 | * - can have multiple possibilities | |
48 | * - globals sux | |
49 | * - sometimes have to undo, cos if start match, then it binds, | |
50 | * and if later it does not match, then must undo the first binds. | |
51 | * ex: when match parameters, can try to match, but then we found far | |
52 | * later that the last argument of a function does not match | |
53 | * => have to uando the binding !!! | |
54 | * (can handle that too with a global, by saving the | |
55 | * global, ... but sux) | |
56 | * => better not use global | |
57 | * | |
58 | * version2: | |
59 | * type ('a, 'b) matcher = binding -> 'a -> 'b -> binding list | |
60 | * | |
61 | * Empty list mean failure (let matchfailure = []). | |
62 | * To be able to have pretty code, have to use partial application | |
63 | * powa, and so the type is in fact | |
64 | * | |
65 | * version3: | |
66 | * type ('a, 'b) matcher = 'a -> 'b -> binding -> binding list | |
67 | * | |
68 | * Then by defining the correct combinators, can have quite pretty code (that | |
69 | * looks like the clean code of version0). | |
70 | * | |
71 | * opti: return a lazy list of possible matchs ? | |
72 | * | |
73 | * version4: type tin = Lib_engine.metavars_binding | |
74 | *) | |
75 | ||
76 | (* ------------------------------------------------------------------------*) | |
77 | (* Standard type and operators *) | |
78 | (* ------------------------------------------------------------------------*) | |
79 | ||
80 | type tin = { | |
81 | extra: xinfo; | |
82 | binding: Lib_engine.metavars_binding; | |
83 | } | |
84 | (* 'x is a ('a * 'b) but in fact dont care about 'b, we just tag the SP *) | |
85 | (* opti? use set instead of list *) | |
86 | type 'x tout = ('x * Lib_engine.metavars_binding) list | |
87 | ||
88 | type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout | |
89 | ||
90 | (* was >&&> *) | |
91 | let (>>=) m1 m2 = fun tin -> | |
92 | let xs = m1 tin in | |
93 | let xxs = xs +> List.map (fun ((a,b), binding) -> | |
94 | m2 a b {extra = tin.extra; binding = binding} | |
95 | ) in | |
96 | List.flatten xxs | |
97 | ||
98 | (* Je compare les bindings retournés par les differentes branches. | |
99 | * Si la deuxieme branche amene a des bindings qui sont deja presents | |
100 | * dans la premiere branche, alors je ne les accepte pas. | |
101 | * | |
102 | * update: still useful now that julia better handle Exp directly via | |
103 | * ctl tricks using positions ? | |
104 | *) | |
105 | let (>|+|>) m1 m2 = fun tin -> | |
106 | (* CHOICE | |
107 | let xs = m1 tin in | |
108 | if null xs | |
109 | then m2 tin | |
110 | else xs | |
111 | *) | |
112 | let res1 = m1 tin in | |
113 | let res2 = m2 tin in | |
114 | let list_bindings_already = List.map snd res1 in | |
115 | res1 ++ | |
116 | (res2 +> List.filter (fun (x, binding) -> | |
117 | not | |
118 | (list_bindings_already +> List.exists (fun already -> | |
119 | Lib_engine.equal_binding binding already)) | |
120 | )) | |
121 | ||
122 | ||
123 | ||
124 | ||
125 | let (>||>) m1 m2 = fun tin -> | |
126 | (* CHOICE | |
127 | let xs = m1 tin in | |
128 | if null xs | |
129 | then m2 tin | |
130 | else xs | |
131 | *) | |
132 | (* opti? use set instead of list *) | |
133 | m1 tin ++ m2 tin | |
134 | ||
135 | ||
136 | let return res = fun tin -> | |
137 | [res, tin.binding] | |
138 | ||
139 | let fail = fun tin -> | |
140 | [] | |
141 | ||
142 | let (>&&>) f m = fun tin -> | |
143 | if f tin | |
144 | then m tin | |
145 | else fail tin | |
146 | ||
147 | ||
148 | let mode = Cocci_vs_c_3.PatternMode | |
149 | ||
150 | (* ------------------------------------------------------------------------*) | |
151 | (* Exp *) | |
152 | (* ------------------------------------------------------------------------*) | |
153 | let cocciExp = fun expf expa node -> fun tin -> | |
154 | ||
155 | let globals = ref [] in | |
156 | let bigf = { | |
157 | (* julia's style *) | |
158 | Visitor_c.default_visitor_c with | |
159 | Visitor_c.kexpr = (fun (k, bigf) expb -> | |
160 | match expf expa expb tin with | |
161 | | [] -> (* failed *) k expb | |
162 | | xs -> | |
163 | globals := xs @ !globals; | |
164 | if not !Flag_engine.disallow_nested_exps then k expb (* CHOICE *) | |
165 | ); | |
166 | (* pad's style. | |
167 | * push2 expr globals; k expr | |
168 | * ... | |
169 | * !globals +> List.fold_left (fun acc e -> acc >||> match_e_e expr e) | |
170 | * (return false) | |
171 | * | |
172 | *) | |
173 | } | |
174 | in | |
175 | Visitor_c.vk_node bigf node; | |
176 | !globals +> List.map (fun ((a, _exp), binding) -> | |
177 | (a, node), binding | |
178 | ) | |
179 | ||
180 | (* same as cocciExp, but for expressions in an expression, not expressions | |
181 | in a node *) | |
182 | let cocciExpExp = fun expf expa expb -> fun tin -> | |
183 | ||
184 | let globals = ref [] in | |
185 | let bigf = { | |
186 | (* julia's style *) | |
187 | Visitor_c.default_visitor_c with | |
188 | Visitor_c.kexpr = (fun (k, bigf) expb -> | |
189 | match expf expa expb tin with | |
190 | | [] -> (* failed *) k expb | |
191 | | xs -> | |
192 | globals := xs @ !globals; | |
193 | if not !Flag_engine.disallow_nested_exps then k expb (* CHOICE *) | |
194 | ); | |
195 | (* pad's style. | |
196 | * push2 expr globals; k expr | |
197 | * ... | |
198 | * !globals +> List.fold_left (fun acc e -> acc >||> match_e_e expr e) | |
199 | * (return false) | |
200 | * | |
201 | *) | |
202 | } | |
203 | in | |
204 | Visitor_c.vk_expr bigf expb; | |
205 | !globals +> List.map (fun ((a, _exp), binding) -> | |
206 | (a, expb), binding | |
207 | ) | |
208 | ||
209 | let cocciTy = fun expf expa node -> fun tin -> | |
210 | ||
211 | let globals = ref [] in | |
212 | let bigf = { | |
213 | Visitor_c.default_visitor_c with | |
214 | Visitor_c.ktype = (fun (k, bigf) expb -> | |
215 | match expf expa expb tin with | |
216 | | [] -> (* failed *) k expb | |
217 | | xs -> globals := xs @ !globals); | |
218 | ||
219 | } | |
220 | in | |
221 | Visitor_c.vk_node bigf node; | |
222 | !globals +> List.map (fun ((a, _exp), binding) -> | |
223 | (a, node), binding | |
224 | ) | |
225 | ||
226 | ||
227 | (* ------------------------------------------------------------------------*) | |
228 | (* Distribute mcode *) | |
229 | (* ------------------------------------------------------------------------*) | |
230 | let tag_mck_pos mck posmck = | |
231 | match mck with | |
232 | | Ast_cocci.PLUS -> Ast_cocci.PLUS | |
233 | | Ast_cocci.CONTEXT (pos, xs) -> | |
234 | assert (pos = Ast_cocci.NoPos || pos = Ast_cocci.DontCarePos); | |
235 | Ast_cocci.CONTEXT (posmck, xs) | |
236 | | Ast_cocci.MINUS (pos, xs) -> | |
237 | assert (pos = Ast_cocci.NoPos || pos = Ast_cocci.DontCarePos); | |
238 | Ast_cocci.MINUS (posmck, xs) | |
239 | ||
240 | ||
241 | let tag_mck_pos_mcode (x,info,mck,pos) posmck stuff = fun tin -> | |
242 | [((x, info, tag_mck_pos mck posmck, pos),stuff), tin.binding] | |
243 | ||
244 | ||
245 | let distrf (ii_of_x_f) = | |
246 | fun mcode x -> fun tin -> | |
247 | let (max, min) = Lib_parsing_c.max_min_by_pos (ii_of_x_f x) | |
248 | in | |
249 | let posmck = Ast_cocci.FixPos (min, max) (* subtil: and not max, min !!*) | |
250 | in | |
251 | tag_mck_pos_mcode mcode posmck x tin | |
252 | ||
253 | let distrf_e = distrf (Lib_parsing_c.ii_of_expr) | |
254 | let distrf_args = distrf (Lib_parsing_c.ii_of_args) | |
255 | let distrf_type = distrf (Lib_parsing_c.ii_of_type) | |
256 | let distrf_param = distrf (Lib_parsing_c.ii_of_param) | |
257 | let distrf_params = distrf (Lib_parsing_c.ii_of_params) | |
258 | let distrf_ini = distrf (Lib_parsing_c.ii_of_ini) | |
259 | let distrf_node = distrf (Lib_parsing_c.ii_of_node) | |
260 | let distrf_struct_fields = distrf (Lib_parsing_c.ii_of_struct_fields) | |
261 | let distrf_cst = distrf (Lib_parsing_c.ii_of_cst) | |
262 | let distrf_define_params = distrf (Lib_parsing_c.ii_of_define_params) | |
263 | ||
264 | ||
265 | (* ------------------------------------------------------------------------*) | |
266 | (* Constraints on metavariable values *) | |
267 | (* ------------------------------------------------------------------------*) | |
268 | let check_constraints matcher constraints exp = fun f tin -> | |
269 | let rec loop = function | |
270 | [] -> f () tin (* success *) | |
271 | | c::cs -> | |
272 | match matcher c exp tin with | |
273 | [] (* failure *) -> loop cs | |
274 | | _ (* success *) -> fail tin in | |
275 | loop constraints | |
276 | ||
277 | let check_pos_constraints constraints pvalu f tin = | |
278 | check_constraints | |
279 | (fun c exp tin -> | |
280 | let success = [[]] in | |
281 | let failure = [] in | |
282 | (match Common.optionise (fun () -> tin.binding +> List.assoc c) with | |
283 | Some valu' -> | |
284 | if Cocci_vs_c_3.equal_metavarval exp valu' | |
285 | then success else failure | |
286 | | None -> | |
287 | (* if the variable is not there, it puts no constraints *) | |
288 | (* not sure this is still useful *) | |
289 | failure)) | |
290 | constraints pvalu f tin | |
291 | ||
292 | (* ------------------------------------------------------------------------*) | |
293 | (* Environment *) | |
294 | (* ------------------------------------------------------------------------*) | |
295 | (* pre: if have declared a new metavar that hide another one, then | |
296 | * must be passed with a binding that deleted this metavar | |
297 | * | |
298 | * Here we dont use the keep argument of julia. cf f(X,X), J'ai | |
299 | * besoin de garder le X en interne, meme si julia s'en fout elle du | |
300 | * X et qu'elle a mis X a DontSaved. | |
301 | *) | |
302 | let check_add_metavars_binding strip _keep inherited = fun (k, valu) tin -> | |
303 | (match Common.optionise (fun () -> tin.binding +> List.assoc k) with | |
304 | | Some (valu') -> | |
305 | if Cocci_vs_c_3.equal_metavarval valu valu' | |
306 | then Some tin.binding | |
307 | else None | |
308 | ||
309 | | None -> | |
310 | if inherited | |
311 | then None | |
312 | else | |
313 | let valu' = | |
314 | match valu with | |
315 | Ast_c.MetaIdVal a -> Ast_c.MetaIdVal a | |
316 | | Ast_c.MetaFuncVal a -> Ast_c.MetaFuncVal a | |
317 | | Ast_c.MetaLocalFuncVal a -> Ast_c.MetaLocalFuncVal a (*more?*) | |
318 | | Ast_c.MetaExprVal a -> | |
319 | Ast_c.MetaExprVal | |
320 | (if strip | |
321 | then Lib_parsing_c.al_expr a | |
322 | else Lib_parsing_c.semi_al_expr a) | |
323 | | Ast_c.MetaExprListVal a -> | |
324 | Ast_c.MetaExprListVal | |
325 | (if strip | |
326 | then Lib_parsing_c.al_arguments a | |
327 | else Lib_parsing_c.semi_al_arguments a) | |
328 | ||
329 | | Ast_c.MetaStmtVal a -> | |
330 | Ast_c.MetaStmtVal | |
331 | (if strip | |
332 | then Lib_parsing_c.al_statement a | |
333 | else Lib_parsing_c.semi_al_statement a) | |
334 | | Ast_c.MetaTypeVal a -> | |
335 | Ast_c.MetaTypeVal | |
336 | (if strip | |
337 | then Lib_parsing_c.al_type a | |
338 | else Lib_parsing_c.semi_al_type a) | |
339 | ||
340 | | Ast_c.MetaListlenVal a -> Ast_c.MetaListlenVal a | |
341 | ||
342 | | Ast_c.MetaParamVal a -> failwith "not handling MetaParamVal" | |
343 | | Ast_c.MetaParamListVal a -> | |
344 | Ast_c.MetaParamListVal | |
345 | (if strip | |
346 | then Lib_parsing_c.al_params a | |
347 | else Lib_parsing_c.semi_al_params a) | |
348 | ||
349 | | Ast_c.MetaPosVal (pos1,pos2) -> Ast_c.MetaPosVal (pos1,pos2) | |
350 | | Ast_c.MetaPosValList l -> Ast_c.MetaPosValList l | |
351 | in Some (tin.binding +> Common.insert_assoc (k, valu')) | |
352 | ) | |
353 | ||
354 | let envf keep inherited = fun (k, valu, get_max_min) f tin -> | |
355 | let x = Ast_cocci.unwrap_mcode k in | |
356 | match check_add_metavars_binding true keep inherited (x, valu) tin with | |
357 | | Some binding -> | |
358 | let new_tin = {extra = tin.extra; binding = binding} in | |
359 | (match Ast_cocci.get_pos_var k with | |
360 | Ast_cocci.MetaPos(name,constraints,per,keep,inherited) -> | |
361 | let pvalu = | |
362 | let (file,min,max) = get_max_min() in | |
363 | Ast_c.MetaPosValList[(file,min,max)] in | |
364 | (* check constraints. success means that there is a match with | |
365 | one of the constraints, which will ultimately result in | |
366 | failure. *) | |
367 | check_pos_constraints constraints pvalu | |
368 | (function () -> | |
369 | (* constraints are satisfied, now see if we are compatible | |
370 | with existing bindings *) | |
371 | function new_tin -> | |
372 | let x = Ast_cocci.unwrap_mcode name in | |
373 | (match | |
374 | check_add_metavars_binding false keep inherited (x, pvalu) | |
375 | new_tin with | |
376 | | Some binding -> | |
377 | f () {extra = new_tin.extra; binding = binding} | |
378 | | None -> fail tin)) | |
379 | new_tin | |
380 | | Ast_cocci.NoMetaPos -> f () new_tin) | |
381 | | None -> fail tin | |
382 | ||
383 | (* ------------------------------------------------------------------------*) | |
384 | (* Environment, allbounds *) | |
385 | (* ------------------------------------------------------------------------*) | |
386 | (* all referenced inherited variables have to be bound. This would | |
387 | * be naturally checked for the minus or context ones in the | |
388 | * matching process, but have to check the plus ones as well. The | |
389 | * result of get_inherited contains all of these, but the potential | |
390 | * redundant checking for the minus and context ones is probably not | |
391 | * a big deal. If it's a problem, could fix free_vars to distinguish | |
392 | * between + variables and the other ones. *) | |
393 | ||
394 | let (all_bound : Ast_cocci.meta_name list -> tin -> bool) = fun l tin -> | |
395 | l +> List.for_all (fun inhvar -> | |
396 | match Common.optionise (fun () -> tin.binding +> List.assoc inhvar) with | |
397 | | Some _ -> true | |
398 | | None -> false | |
399 | ) | |
400 | ||
401 | let optional_storage_flag f = fun tin -> | |
402 | f (tin.extra.optional_storage_iso) tin | |
403 | ||
404 | let optional_qualifier_flag f = fun tin -> | |
405 | f (tin.extra.optional_qualifier_iso) tin | |
406 | ||
407 | let value_format_flag f = fun tin -> | |
408 | f (tin.extra.value_format_iso) tin | |
409 | ||
410 | ||
411 | (* ------------------------------------------------------------------------*) | |
412 | (* Tokens *) | |
413 | (* ------------------------------------------------------------------------*) | |
414 | let tokenf ia ib = fun tin -> | |
415 | let pos = Ast_c.info_to_fixpos ib in | |
416 | let posmck = Ast_cocci.FixPos (pos, pos) in | |
417 | let finish tin = tag_mck_pos_mcode ia posmck ib tin in | |
418 | match Ast_cocci.get_pos_var ia with | |
419 | Ast_cocci.MetaPos(name,constraints,per,keep,inherited) -> | |
420 | let mpos = Lib_parsing_c.lin_col_by_pos [ib] in | |
421 | let pvalu = Ast_c.MetaPosValList [mpos] in | |
422 | check_pos_constraints constraints pvalu | |
423 | (function () -> | |
424 | (* constraints are satisfied, now see if we are compatible | |
425 | with existing bindings *) | |
426 | function new_tin -> | |
427 | let x = Ast_cocci.unwrap_mcode name in | |
428 | (match | |
429 | check_add_metavars_binding false keep inherited (x, pvalu) tin | |
430 | with | |
431 | Some binding -> finish {extra = tin.extra; binding = binding} | |
432 | | None -> fail tin)) | |
433 | tin | |
434 | | _ -> finish tin | |
435 | ||
436 | let tokenf_mck mck ib = fun tin -> | |
437 | let pos = Ast_c.info_to_fixpos ib in | |
438 | let posmck = Ast_cocci.FixPos (pos, pos) in | |
439 | [(tag_mck_pos mck posmck, ib), tin.binding] | |
440 | ||
441 | end | |
442 | ||
443 | (*****************************************************************************) | |
444 | (* Entry point *) | |
445 | (*****************************************************************************) | |
446 | module MATCH = Cocci_vs_c_3.COCCI_VS_C (XMATCH) | |
447 | ||
448 | ||
449 | let match_re_node2 dropped_isos a b binding = | |
450 | ||
451 | let tin = { | |
452 | XMATCH.extra = { | |
453 | optional_storage_iso = not(List.mem "optional_storage" dropped_isos); | |
454 | optional_qualifier_iso = not(List.mem "optional_qualifier" dropped_isos); | |
455 | value_format_iso = not(List.mem "value_format" dropped_isos); | |
456 | }; | |
457 | XMATCH.binding = binding; | |
458 | } in | |
459 | ||
460 | MATCH.rule_elem_node a b tin | |
461 | (* take only the tagged-SP, the 'a' *) | |
462 | +> List.map (fun ((a,_b), binding) -> a, binding) | |
463 | ||
464 | ||
465 | let match_re_node a b c d = | |
466 | Common.profile_code "Pattern3.match_re_node" | |
467 | (fun () -> match_re_node2 a b c d) |