Release coccinelle-0.1.1
[bpt/coccinelle.git] / engine / transformation3.ml
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 module F = Control_flow_c
26
27 (*****************************************************************************)
28 (* The functor argument *)
29 (*****************************************************************************)
30
31 (* info passed recursively in monad in addition to binding *)
32 type xinfo = {
33 optional_storage_iso : bool;
34 optional_qualifier_iso : bool;
35 value_format_iso : bool;
36 current_rule_name : string; (* used for errors *)
37 }
38
39 module XTRANS = struct
40
41 (* ------------------------------------------------------------------------*)
42 (* Combinators history *)
43 (* ------------------------------------------------------------------------*)
44 (*
45 * version0:
46 * type ('a, 'b) transformer =
47 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b
48 * exception NoMatch
49 *
50 * version1:
51 * type ('a, 'b) transformer =
52 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b option
53 * use an exception monad
54 *
55 * version2:
56 * type tin = Lib_engine.metavars_binding
57 *)
58
59 (* ------------------------------------------------------------------------*)
60 (* Standard type and operators *)
61 (* ------------------------------------------------------------------------*)
62
63 type tin = {
64 extra: xinfo;
65 binding: Lib_engine.metavars_binding;
66 binding0: Lib_engine.metavars_binding; (* inherited variable *)
67 }
68 type 'x tout = 'x option
69
70 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
71
72 let (>>=) m f = fun tin ->
73 match m tin with
74 | None -> None
75 | Some (a,b) -> f a b tin
76
77 let return = fun x -> fun tin ->
78 Some x
79
80 (* can have fail in transform now that the process is deterministic ? *)
81 let fail = fun tin ->
82 None
83
84 let (>||>) m1 m2 = fun tin ->
85 match m1 tin with
86 | None -> m2 tin
87 | Some x -> Some x (* stop as soon as have found something *)
88
89 let (>|+|>) m1 m2 = m1 >||> m2
90
91 let (>&&>) f m = fun tin ->
92 if f tin then m tin else fail tin
93
94 let optional_storage_flag f = fun tin ->
95 f (tin.extra.optional_storage_iso) tin
96
97 let optional_qualifier_flag f = fun tin ->
98 f (tin.extra.optional_qualifier_iso) tin
99
100 let value_format_flag f = fun tin ->
101 f (tin.extra.value_format_iso) tin
102
103 let mode = Cocci_vs_c_3.TransformMode
104
105 (* ------------------------------------------------------------------------*)
106 (* Exp *)
107 (* ------------------------------------------------------------------------*)
108 let cocciExp = fun expf expa node -> fun tin ->
109
110 let bigf = {
111 Visitor_c.default_visitor_c_s with
112 Visitor_c.kexpr_s = (fun (k, bigf) expb ->
113 match expf expa expb tin with
114 | None -> (* failed *) k expb
115 | Some (x, expb) -> expb);
116 }
117 in
118 Some (expa, Visitor_c.vk_node_s bigf node)
119
120
121 (* same as cocciExp, but for expressions in an expression, not expressions
122 in a node *)
123 let cocciExpExp = fun expf expa expb -> fun tin ->
124
125 let bigf = {
126 Visitor_c.default_visitor_c_s with
127 Visitor_c.kexpr_s = (fun (k, bigf) expb ->
128 match expf expa expb tin with
129 | None -> (* failed *) k expb
130 | Some (x, expb) -> expb);
131 }
132 in
133 Some (expa, Visitor_c.vk_expr_s bigf expb)
134
135
136 let cocciTy = fun expf expa node -> fun tin ->
137
138 let bigf = {
139 Visitor_c.default_visitor_c_s with
140 Visitor_c.ktype_s = (fun (k, bigf) expb ->
141 match expf expa expb tin with
142 | None -> (* failed *) k expb
143 | Some (x, expb) -> expb);
144 }
145 in
146 Some (expa, Visitor_c.vk_node_s bigf node)
147
148 let cocciInit = fun expf expa node -> fun tin ->
149
150 let bigf = {
151 Visitor_c.default_visitor_c_s with
152 Visitor_c.kini_s = (fun (k, bigf) expb ->
153 match expf expa expb tin with
154 | None -> (* failed *) k expb
155 | Some (x, expb) -> expb);
156 }
157 in
158 Some (expa, Visitor_c.vk_node_s bigf node)
159
160
161 (* ------------------------------------------------------------------------*)
162 (* Tokens *)
163 (* ------------------------------------------------------------------------*)
164 let check_pos info mck pos =
165 match mck with
166 | Ast_cocci.PLUS -> raise Impossible
167 | Ast_cocci.CONTEXT (Ast_cocci.FixPos (i1,i2),_)
168 | Ast_cocci.MINUS (Ast_cocci.FixPos (i1,i2),_) ->
169 pos <= i2 && pos >= i1
170 | Ast_cocci.CONTEXT (Ast_cocci.DontCarePos,_)
171 | Ast_cocci.MINUS (Ast_cocci.DontCarePos,_) ->
172 true
173 | _ ->
174 match info with
175 Some info ->
176 failwith
177 (Printf.sprintf
178 "wierd: dont have position info for the mcodekind in line %d column %d"
179 info.Ast_cocci.line info.Ast_cocci.column)
180 | None ->
181 failwith "wierd: dont have position info for the mcodekind"
182
183
184 let tag_with_mck mck ib = fun tin ->
185
186 let cocciinforef = ib.Ast_c.cocci_tag in
187 let (oldmcode, oldenv) = !cocciinforef in
188
189 let mck =
190 if !Flag_parsing_cocci.sgrep_mode
191 then Sgrep.process_sgrep ib mck
192 else mck
193 in
194 (match mck, Ast_c.pinfo_of_info ib with
195 | _, Ast_c.AbstractLineTok _ -> raise Impossible
196 | Ast_cocci.MINUS(_), Ast_c.ExpandedTok _ ->
197 failwith ("try to delete an expanded token: " ^ (Ast_c.str_of_info ib))
198 | _ -> ()
199 );
200
201 match (oldmcode,mck) with
202 | (Ast_cocci.CONTEXT(_,Ast_cocci.NOTHING), _)
203 | (_, Ast_cocci.CONTEXT(_,Ast_cocci.NOTHING))
204 ->
205 cocciinforef := (mck, tin.binding);
206 ib
207
208 | _ ->
209 if (oldmcode, oldenv) = (mck, tin.binding)
210 then begin
211 if !Flag.show_misc
212 then pr2 "already tagged but with same mcode, so safe";
213 ib
214 end
215 else
216 if !Flag.sgrep_mode2
217 then ib (* safe *)
218 else
219 begin
220 Format.set_formatter_out_channel stderr;
221 Common.pr2 "SP mcode ";
222 Pretty_print_cocci.print_mcodekind oldmcode;
223 Format.print_newline();
224 Common.pr2 "C code mcode ";
225 Pretty_print_cocci.print_mcodekind mck;
226 Format.print_newline();
227 Format.print_flush();
228 failwith
229 (Common.sprintf "%s: already tagged token:\n%s"
230 tin.extra.current_rule_name
231 (Common.error_message (Ast_c.file_of_info ib)
232 (Ast_c.str_of_info ib, Ast_c.opos_of_info ib)))
233 end
234
235 let tokenf ia ib = fun tin ->
236 let (_,i,mck,_) = ia in
237 let pos = Ast_c.info_to_fixpos ib in
238 if check_pos (Some i) mck pos
239 then return (ia, tag_with_mck mck ib tin) tin
240 else fail tin
241
242 let tokenf_mck mck ib = fun tin ->
243 let pos = Ast_c.info_to_fixpos ib in
244 if check_pos None mck pos
245 then return (mck, tag_with_mck mck ib tin) tin
246 else fail tin
247
248
249 (* ------------------------------------------------------------------------*)
250 (* Distribute mcode *)
251 (* ------------------------------------------------------------------------*)
252
253 (* When in the SP we attach something to a metavariable, or delete it, as in
254 * - S
255 * + foo();
256 * we have to minusize all the token that compose S in the C code, and
257 * attach the 'foo();' to the right token, the one at the very right.
258 *)
259
260 type 'a distributer =
261 (Ast_c.info -> Ast_c.info) * (* what to do on left *)
262 (Ast_c.info -> Ast_c.info) * (* what to do on middle *)
263 (Ast_c.info -> Ast_c.info) * (* what to do on right *)
264 (Ast_c.info -> Ast_c.info) -> (* what to do on both *)
265 'a -> 'a
266
267 let distribute_mck mcodekind distributef expr tin =
268 match mcodekind with
269 | Ast_cocci.MINUS (pos,any_xxs) ->
270 distributef (
271 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,any_xxs)) ib tin),
272 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,[])) ib tin),
273 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,[])) ib tin),
274 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,any_xxs)) ib tin)
275 ) expr
276 | Ast_cocci.CONTEXT (pos,any_befaft) ->
277 (match any_befaft with
278 | Ast_cocci.NOTHING -> expr
279
280 | Ast_cocci.BEFORE xxs ->
281 distributef (
282 (fun ib -> tag_with_mck
283 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE xxs)) ib tin),
284 (fun x -> x),
285 (fun x -> x),
286 (fun ib -> tag_with_mck
287 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE xxs)) ib tin)
288 ) expr
289 | Ast_cocci.AFTER xxs ->
290 distributef (
291 (fun x -> x),
292 (fun x -> x),
293 (fun ib -> tag_with_mck
294 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER xxs)) ib tin),
295 (fun ib -> tag_with_mck
296 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER xxs)) ib tin)
297 ) expr
298
299 | Ast_cocci.BEFOREAFTER (xxs, yys) ->
300 distributef (
301 (fun ib -> tag_with_mck
302 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE xxs)) ib tin),
303 (fun x -> x),
304 (fun ib -> tag_with_mck
305 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER yys)) ib tin),
306 (fun ib -> tag_with_mck
307 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFOREAFTER (xxs,yys)))
308 ib tin)
309 ) expr
310
311 )
312 | Ast_cocci.PLUS -> raise Impossible
313
314
315 (* use new strategy, collect ii, sort, recollect and tag *)
316
317 let mk_bigf (maxpos, minpos) (lop,mop,rop,bop) =
318 let bigf = {
319 Visitor_c.default_visitor_c_s with
320 Visitor_c.kinfo_s = (fun (k,bigf) i ->
321 let pos = Ast_c.info_to_fixpos i in
322 match () with
323 | _ when Ast_cocci.equal_pos pos maxpos &&
324 Ast_cocci.equal_pos pos minpos -> bop i
325 | _ when Ast_cocci.equal_pos pos maxpos -> rop i
326 | _ when Ast_cocci.equal_pos pos minpos -> lop i
327 | _ -> mop i
328 )
329 } in
330 bigf
331
332 let distribute_mck_expr (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
333 Visitor_c.vk_expr_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
334
335 let distribute_mck_args (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
336 Visitor_c.vk_args_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
337
338 let distribute_mck_type (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
339 Visitor_c.vk_type_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
340
341 let distribute_mck_ini (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
342 Visitor_c.vk_ini_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
343
344 let distribute_mck_param (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
345 Visitor_c.vk_param_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
346
347 let distribute_mck_params (maxpos, minpos) = fun (lop,mop,rop,bop) ->fun x ->
348 Visitor_c.vk_params_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
349 x
350
351 let distribute_mck_node (maxpos, minpos) = fun (lop,mop,rop,bop) ->fun x ->
352 Visitor_c.vk_node_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
353 x
354
355 let distribute_mck_struct_fields (maxpos, minpos) =
356 fun (lop,mop,rop,bop) ->fun x ->
357 Visitor_c.vk_struct_fields_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
358 x
359
360 let distribute_mck_cst (maxpos, minpos) =
361 fun (lop,mop,rop,bop) ->fun x ->
362 Visitor_c.vk_cst_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
363 x
364
365
366 let distribute_mck_define_params (maxpos, minpos) = fun (lop,mop,rop,bop) ->
367 fun x ->
368 Visitor_c.vk_define_params_splitted_s
369 (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
370 x
371
372 let get_pos mck =
373 match mck with
374 | Ast_cocci.PLUS -> raise Impossible
375 | Ast_cocci.CONTEXT (Ast_cocci.FixPos (i1,i2),_)
376 | Ast_cocci.MINUS (Ast_cocci.FixPos (i1,i2),_) ->
377 Ast_cocci.FixPos (i1,i2)
378 | Ast_cocci.CONTEXT (Ast_cocci.DontCarePos,_)
379 | Ast_cocci.MINUS (Ast_cocci.DontCarePos,_) ->
380 Ast_cocci.DontCarePos
381 | _ -> failwith "wierd: dont have position info for the mcodekind"
382
383 let distrf (ii_of_x_f, distribute_mck_x_f) =
384 fun ia x -> fun tin ->
385 let mck = Ast_cocci.get_mcodekind ia in
386 let (max, min) = Lib_parsing_c.max_min_by_pos (ii_of_x_f x)
387 in
388 if
389 (* bug: check_pos mck max && check_pos mck min
390 *
391 * if do that then if have - f(...); and in C f(1,2); then we
392 * would get a "already tagged" because the '...' would sucess in
393 * transformaing both '1' and '1,2'. So being in the range is not
394 * enough. We must be equal exactly to the range!
395 *)
396 (match get_pos mck with
397 | Ast_cocci.DontCarePos -> true
398 | Ast_cocci.FixPos (i1, i2) ->
399 i1 = min && i2 = max
400 | _ -> raise Impossible
401 )
402
403 then
404 return (
405 ia,
406 distribute_mck mck (distribute_mck_x_f (max,min)) x tin
407 ) tin
408 else fail tin
409
410
411 let distrf_e = distrf (Lib_parsing_c.ii_of_expr, distribute_mck_expr)
412 let distrf_args = distrf (Lib_parsing_c.ii_of_args, distribute_mck_args)
413 let distrf_type = distrf (Lib_parsing_c.ii_of_type, distribute_mck_type)
414 let distrf_param = distrf (Lib_parsing_c.ii_of_param, distribute_mck_param)
415 let distrf_params = distrf (Lib_parsing_c.ii_of_params,distribute_mck_params)
416 let distrf_ini = distrf (Lib_parsing_c.ii_of_ini,distribute_mck_ini)
417 let distrf_node = distrf (Lib_parsing_c.ii_of_node,distribute_mck_node)
418 let distrf_struct_fields =
419 distrf (Lib_parsing_c.ii_of_struct_fields, distribute_mck_struct_fields)
420 let distrf_cst =
421 distrf (Lib_parsing_c.ii_of_cst, distribute_mck_cst)
422 let distrf_define_params =
423 distrf (Lib_parsing_c.ii_of_define_params,distribute_mck_define_params)
424
425
426 (* ------------------------------------------------------------------------*)
427 (* Environment *)
428 (* ------------------------------------------------------------------------*)
429 let meta_name_to_str (s1, s2) =
430 s1 ^ "." ^ s2
431
432 let envf keep _inherited = fun (s, value, _) f tin ->
433 let s = Ast_cocci.unwrap_mcode s in
434 let v =
435 if keep = Type_cocci.Saved
436 then (
437 try Some (List.assoc s tin.binding)
438 with Not_found ->
439 pr2(sprintf
440 "Don't find value for metavariable %s in the environment"
441 (meta_name_to_str s));
442 None)
443 else
444 (* not raise Impossible! *)
445 Some (value)
446 in
447 match v with
448 | None -> fail tin
449 | Some (value') ->
450
451 (* Ex: in cocci_vs_c someone wants to add a binding. Here in
452 * transformation3 the value for this var may be already in the
453 * env, because for instance its value were fixed in a previous
454 * SmPL rule. So here we want to check that this is the same value.
455 * If forget to do the check, what can happen ? Because of Exp
456 * and other disjunctive feature of cocci_vs_c (>||>), we
457 * may accept a match at a wrong position. Maybe later this
458 * will be detected via the pos system on tokens, but maybe
459 * not. So safer to keep the check.
460 *)
461
462 (*f () tin*)
463 if Cocci_vs_c_3.equal_metavarval value value'
464 then f () tin
465 else fail tin
466
467
468 let check_constraints matcher constraints exp = fun f tin -> f () tin
469
470 (* ------------------------------------------------------------------------*)
471 (* Environment, allbounds *)
472 (* ------------------------------------------------------------------------*)
473 let (all_bound : Ast_cocci.meta_name list -> tin -> bool) = fun l tin ->
474 true (* in transform we don't care ? *)
475
476 end
477
478 (*****************************************************************************)
479 (* Entry point *)
480 (*****************************************************************************)
481 module TRANS = Cocci_vs_c_3.COCCI_VS_C (XTRANS)
482
483
484 let transform_re_node a b tin =
485 match TRANS.rule_elem_node a b tin with
486 | None -> raise Impossible
487 | Some (_sp, b') -> b'
488
489 let (transform2: string (* rule name *) -> string list (* dropped_isos *) ->
490 Lib_engine.metavars_binding (* inherited bindings *) ->
491 Lib_engine.transformation_info -> F.cflow -> F.cflow) =
492 fun rule_name dropped_isos binding0 xs cflow ->
493
494 let extra = {
495 optional_storage_iso = not(List.mem "optional_storage" dropped_isos);
496 optional_qualifier_iso = not(List.mem "optional_qualifier" dropped_isos);
497 value_format_iso = not(List.mem "value_format" dropped_isos);
498 current_rule_name = rule_name;
499 } in
500
501 (* find the node, transform, update the node, and iter for all elements *)
502
503 xs +> List.fold_left (fun acc (nodei, binding, rule_elem) ->
504 (* subtil: not cflow#nodes but acc#nodes *)
505 let node = acc#nodes#assoc nodei in
506
507 if !Flag.show_misc
508 then pr2 "transform one node";
509
510 let tin = {
511 XTRANS.extra = extra;
512 XTRANS.binding = binding0@binding;
513 XTRANS.binding0 = []; (* not used - everything constant for trans *)
514 } in
515
516 let node' = transform_re_node rule_elem node tin in
517
518 (* assert that have done something. But with metaruleElem sometimes
519 dont modify fake nodes. So special case before on Fake nodes. *)
520 (match F.unwrap node with
521 | F.Enter | F.Exit | F.ErrorExit
522 | F.EndStatement _ | F.CaseNode _
523 | F.Fake
524 | F.TrueNode | F.FalseNode | F.AfterNode | F.FallThroughNode
525 -> ()
526 | _ -> () (* assert (not (node =*= node')); *)
527 );
528
529 (* useless, we dont go back from flow to ast now *)
530 (* let node' = lastfix_comma_struct node' in *)
531
532 acc#replace_node (nodei, node');
533 acc
534 ) cflow
535
536
537
538 let transform a b c d e =
539 Common.profile_code "Transformation3.transform"
540 (fun () -> transform2 a b c d e)