37cd939b14fee1009688c993f3e8f81e73d40cc1
[bpt/coccinelle.git] / engine / transformation_c.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 1(* Yoann Padioleau
26 *
27 * Copyright (C) 2006, 2007 Ecole des Mines de Nantes
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License (GPL)
31 * version 2 as published by the Free Software Foundation.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * file license.txt for more details.
37 *
38 * This file was part of Coccinelle.
39 *)
40 open Common
41
42 module F = Control_flow_c
43
44 (*****************************************************************************)
45 (* The functor argument *)
46 (*****************************************************************************)
47
48 (* info passed recursively in monad in addition to binding *)
49 type xinfo = {
50 optional_storage_iso : bool;
51 optional_qualifier_iso : bool;
52 value_format_iso : bool;
53 current_rule_name : string; (* used for errors *)
54 index : int list (* witness tree indices *)
55 }
56
57 module XTRANS = struct
58
59 (* ------------------------------------------------------------------------*)
60 (* Combinators history *)
61 (* ------------------------------------------------------------------------*)
62 (*
63 * version0:
64 * type ('a, 'b) transformer =
65 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b
66 * exception NoMatch
67 *
68 * version1:
69 * type ('a, 'b) transformer =
70 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b option
71 * use an exception monad
72 *
73 * version2:
74 * type tin = Lib_engine.metavars_binding
75 *)
76
77 (* ------------------------------------------------------------------------*)
78 (* Standard type and operators *)
79 (* ------------------------------------------------------------------------*)
80
81 type tin = {
82 extra: xinfo;
83 binding: Lib_engine.metavars_binding;
84 binding0: Lib_engine.metavars_binding; (* inherited variable *)
85 }
86 type 'x tout = 'x option
87
88 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
89
90 let (>>=) m f = fun tin ->
91 match m tin with
92 | None -> None
93 | Some (a,b) -> f a b tin
94
95 let return = fun x -> fun tin ->
96 Some x
97
98 (* can have fail in transform now that the process is deterministic ? *)
99 let fail = fun tin ->
100 None
101
102 let (>||>) m1 m2 = fun tin ->
103 match m1 tin with
104 | None -> m2 tin
105 | Some x -> Some x (* stop as soon as have found something *)
106
107 let (>|+|>) m1 m2 = m1 >||> m2
108
109 let (>&&>) f m = fun tin ->
110 if f tin then m tin else fail tin
111
112 let optional_storage_flag f = fun tin ->
113 f (tin.extra.optional_storage_iso) tin
114
115 let optional_qualifier_flag f = fun tin ->
116 f (tin.extra.optional_qualifier_iso) tin
117
118 let value_format_flag f = fun tin ->
119 f (tin.extra.value_format_iso) tin
120
121 let mode = Cocci_vs_c.TransformMode
122
123 (* ------------------------------------------------------------------------*)
124 (* Exp *)
125 (* ------------------------------------------------------------------------*)
126 let cocciExp = fun expf expa node -> fun tin ->
127
128 let bigf = {
129 Visitor_c.default_visitor_c_s with
130 Visitor_c.kexpr_s = (fun (k, bigf) expb ->
131 match expf expa expb tin with
132 | None -> (* failed *) k expb
133 | Some (x, expb) -> expb);
134 }
135 in
136 Some (expa, Visitor_c.vk_node_s bigf node)
137
138
139 (* same as cocciExp, but for expressions in an expression, not expressions
140 in a node *)
141 let cocciExpExp = fun expf expa expb -> fun tin ->
142
143 let bigf = {
144 Visitor_c.default_visitor_c_s with
145 Visitor_c.kexpr_s = (fun (k, bigf) expb ->
146 match expf expa expb tin with
147 | None -> (* failed *) k expb
148 | Some (x, expb) -> expb);
149 }
150 in
151 Some (expa, Visitor_c.vk_expr_s bigf expb)
152
153
154 let cocciTy = fun expf expa node -> fun tin ->
155
156 let bigf = {
157 Visitor_c.default_visitor_c_s with
158 Visitor_c.ktype_s = (fun (k, bigf) expb ->
159 match expf expa expb tin with
160 | None -> (* failed *) k expb
161 | Some (x, expb) -> expb);
162 }
163 in
164 Some (expa, Visitor_c.vk_node_s bigf node)
165
166 let cocciInit = fun expf expa node -> fun tin ->
167
168 let bigf = {
169 Visitor_c.default_visitor_c_s with
170 Visitor_c.kini_s = (fun (k, bigf) expb ->
171 match expf expa expb tin with
172 | None -> (* failed *) k expb
173 | Some (x, expb) -> expb);
174 }
175 in
176 Some (expa, Visitor_c.vk_node_s bigf node)
177
178
179 (* ------------------------------------------------------------------------*)
180 (* Tokens *)
181 (* ------------------------------------------------------------------------*)
182 let check_pos info mck pos =
183 match mck with
184 | Ast_cocci.PLUS _ -> raise Impossible
185 | Ast_cocci.CONTEXT (Ast_cocci.FixPos (i1,i2),_)
186 | Ast_cocci.MINUS (Ast_cocci.FixPos (i1,i2),_,_,_) ->
187 pos <= i2 && pos >= i1
188 | Ast_cocci.CONTEXT (Ast_cocci.DontCarePos,_)
189 | Ast_cocci.MINUS (Ast_cocci.DontCarePos,_,_,_) ->
190 true
191 | _ ->
192 match info with
193 Some info ->
194 failwith
195 (Printf.sprintf
196 "weird: dont have position info for the mcodekind in line %d column %d"
197 info.Ast_cocci.line info.Ast_cocci.column)
198 | None ->
199 failwith "weird: dont have position info for the mcodekind"
200
201
202 let tag_with_mck mck ib = fun tin ->
203
204 let cocciinforef = ib.Ast_c.cocci_tag in
205 let (oldmcode, oldenvs) = Ast_c.mcode_and_env_of_cocciref cocciinforef in
206
207 let mck =
208 (* coccionly:
209 if !Flag_parsing_cocci.sgrep_mode
210 then Sgrep.process_sgrep ib mck
211 else
212 *)
213 mck
214 in
215 (match mck, Ast_c.pinfo_of_info ib with
216 | _, Ast_c.AbstractLineTok _ -> raise Impossible
217 | Ast_cocci.MINUS(_), Ast_c.ExpandedTok _ ->
218 failwith ("try to delete an expanded token: " ^ (Ast_c.str_of_info ib))
219 | _ -> ()
220 );
221
222 let many_count = function
223 Ast_cocci.BEFORE(_,Ast_cocci.MANY) | Ast_cocci.AFTER(_,Ast_cocci.MANY)
224 | Ast_cocci.BEFOREAFTER(_,_,Ast_cocci.MANY) -> true
225 | _ -> false in
226
227 (match (oldmcode,mck) with
228 | (Ast_cocci.CONTEXT(_,Ast_cocci.NOTHING), _) ->
229 (* nothing there, so take the new stuff *)
230 let update_inst inst = function
231 Ast_cocci.MINUS (pos,_,adj,any_xxs) ->
232 Ast_cocci.MINUS (pos,inst,adj,any_xxs)
233 | mck -> mck in
234 cocciinforef := Some (update_inst tin.extra.index mck, [tin.binding])
235 | (_, Ast_cocci.CONTEXT(_,Ast_cocci.NOTHING)) ->
236 (* can this case occur? stay with the old stuff *)
237 ()
238 | (Ast_cocci.MINUS(old_pos,old_inst,old_adj,[]),
239 Ast_cocci.MINUS(new_pos,new_inst,new_adj,[]))
240 when old_pos = new_pos &&
241 (List.mem tin.binding oldenvs or !Flag.sgrep_mode2)
242 (* no way to combine adjacency information, just drop one *)
243 ->
244 cocciinforef := Some
245 (Ast_cocci.MINUS
246 (old_pos,Common.union_set old_inst new_inst,old_adj,[]),
247 [tin.binding]);
248 (if !Flag_matcher.show_misc
249 then pr2 "already tagged but only removed, so safe")
250
251 | (Ast_cocci.CONTEXT(old_pos,old_modif),
252 Ast_cocci.CONTEXT(new_pos,new_modif))
253 when old_pos = new_pos &&
254 old_modif = new_modif && many_count old_modif ->
255 (* iteration only allowed on context; no way to replace something
256 more than once; now no need for iterable; just check a flag *)
257
258 cocciinforef :=
259 Some(Ast_cocci.CONTEXT(old_pos,old_modif),tin.binding::oldenvs)
260
261 | _ ->
262 (* coccionly:
263 if !Flag.sgrep_mode2
264 then ib (* safe *)
265 else
266 *)
267 begin
268 (* coccionly:
269 pad: if dont want cocci write:
270 failwith
271 (match Ast_c.pinfo_of_info ib with
272 Ast_c.FakeTok _ -> "already tagged fake token"
273 *)
274 let pm str mcode env =
275 Printf.sprintf
276 "%s modification:\n%s\nAccording to environment %d:\n%s\n"
277 str
278 (Common.format_to_string
279 (function _ ->
280 Pretty_print_cocci.print_mcodekind mcode))
281 (List.length env)
282 (String.concat "\n"
283 (List.map
284 (function ((r,vr),vl) ->
285 Printf.sprintf " %s.%s -> %s" r vr
286 (Common.format_to_string
287 (function _ ->
288 Pretty_print_engine.pp_binding_kind vl)))
289 env)) in
290 flush stdout; flush stderr;
291 Common.pr2
292 ("\n"^ (String.concat "\n"
293 (List.map (pm "previous" oldmcode) oldenvs)) ^ "\n"
294 ^ (pm "current" mck tin.binding));
295 failwith
296 (match Ast_c.pinfo_of_info ib with
297 Ast_c.FakeTok _ ->
298 Common.sprintf "%s: already tagged fake token\n"
299 tin.extra.current_rule_name
300 | _ ->
301 Printf.sprintf
302 "%s: already tagged token:\nC code context\n%s"
303 tin.extra.current_rule_name
304 (Common.error_message (Ast_c.file_of_info ib)
305 (Ast_c.str_of_info ib, Ast_c.opos_of_info ib)))
306 end);
307 ib
308
309 let tokenf ia ib = fun tin ->
310 let (_,i,mck,_) = ia in
311 let pos = Ast_c.info_to_fixpos ib in
312 if check_pos (Some i) mck pos
313 then return (ia, tag_with_mck mck ib tin) tin
314 else fail tin
315
316 let tokenf_mck mck ib = fun tin ->
317 let pos = Ast_c.info_to_fixpos ib in
318 if check_pos None mck pos
319 then return (mck, tag_with_mck mck ib tin) tin
320 else fail tin
321
322
323 (* ------------------------------------------------------------------------*)
324 (* Distribute mcode *)
325 (* ------------------------------------------------------------------------*)
326
327 (* When in the SP we attach something to a metavariable, or delete it, as in
328 * - S
329 * + foo();
330 * we have to minusize all the token that compose S in the C code, and
331 * attach the 'foo();' to the right token, the one at the very right.
332 *)
333
334 type 'a distributer =
335 (Ast_c.info -> Ast_c.info) * (* what to do on left *)
336 (Ast_c.info -> Ast_c.info) * (* what to do on middle *)
337 (Ast_c.info -> Ast_c.info) * (* what to do on right *)
338 (Ast_c.info -> Ast_c.info) -> (* what to do on both *)
339 'a -> 'a
340
341 let distribute_mck mcodekind distributef expr tin =
342 match mcodekind with
343 | Ast_cocci.MINUS (pos,_,adj,any_xxs) ->
344 let inst = tin.extra.index in
345 distributef (
346 (fun ib ->
347 tag_with_mck (Ast_cocci.MINUS (pos,inst,adj,any_xxs)) ib tin),
348 (fun ib ->
349 tag_with_mck (Ast_cocci.MINUS (pos,inst,adj,[])) ib tin),
350 (fun ib ->
351 tag_with_mck (Ast_cocci.MINUS (pos,inst,adj,[])) ib tin),
352 (fun ib ->
353 tag_with_mck (Ast_cocci.MINUS (pos,inst,adj,any_xxs)) ib tin)
354 ) expr
355 | Ast_cocci.CONTEXT (pos,any_befaft) ->
356 (match any_befaft with
357 | Ast_cocci.NOTHING -> expr
358
359 | Ast_cocci.BEFORE (xxs,c) ->
360 distributef (
361 (fun ib -> tag_with_mck
362 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE (xxs,c))) ib tin),
363 (fun x -> x),
364 (fun x -> x),
365 (fun ib -> tag_with_mck
366 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE (xxs,c))) ib tin)
367 ) expr
368 | Ast_cocci.AFTER (xxs,c) ->
369 distributef (
370 (fun x -> x),
371 (fun x -> x),
372 (fun ib -> tag_with_mck
373 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER (xxs,c))) ib tin),
374 (fun ib -> tag_with_mck
375 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER (xxs,c))) ib tin)
376 ) expr
377
378 | Ast_cocci.BEFOREAFTER (xxs, yys, c) ->
379 distributef (
380 (fun ib -> tag_with_mck
381 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE (xxs,c))) ib tin),
382 (fun x -> x),
383 (fun ib -> tag_with_mck
384 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER (yys,c))) ib tin),
385 (fun ib -> tag_with_mck
386 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFOREAFTER (xxs,yys,c)))
387 ib tin)
388 ) expr
389
390 )
391 | Ast_cocci.PLUS _ -> raise Impossible
392
393
394 (* use new strategy, collect ii, sort, recollect and tag *)
395
396 let mk_bigf (maxpos, minpos) (lop,mop,rop,bop) =
397 let bigf = {
398 Visitor_c.default_visitor_c_s with
399 Visitor_c.kinfo_s = (fun (k,bigf) i ->
400 let pos = Ast_c.info_to_fixpos i in
401 match () with
402 | _ when Ast_cocci.equal_pos pos maxpos &&
403 Ast_cocci.equal_pos pos minpos -> bop i
404 | _ when Ast_cocci.equal_pos pos maxpos -> rop i
405 | _ when Ast_cocci.equal_pos pos minpos -> lop i
406 | _ -> mop i
407 )
408 } in
409 bigf
410
411 let distribute_mck_expr (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
412 Visitor_c.vk_expr_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
413
414 let distribute_mck_args (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
415 Visitor_c.vk_args_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
416
417 let distribute_mck_type (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
418 Visitor_c.vk_type_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
419
420 let distribute_mck_decl (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
421 Visitor_c.vk_decl_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
422
423 let distribute_mck_field (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
424 Visitor_c.vk_struct_field_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
425
426 let distribute_mck_ini (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
427 Visitor_c.vk_ini_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
428
429 let distribute_mck_inis (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
430 Visitor_c.vk_inis_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
431
432 let distribute_mck_param (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
433 Visitor_c.vk_param_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
434
435 let distribute_mck_params (maxpos, minpos) = fun (lop,mop,rop,bop) ->fun x ->
436 Visitor_c.vk_params_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
437 x
438
439 let distribute_mck_node (maxpos, minpos) = fun (lop,mop,rop,bop) ->fun x ->
440 Visitor_c.vk_node_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
441 x
442
443 let distribute_mck_enum_fields (maxpos, minpos) =
444 fun (lop,mop,rop,bop) ->fun x ->
445 Visitor_c.vk_enum_fields_splitted_s
446 (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
447 x
448
449 let distribute_mck_struct_fields (maxpos, minpos) =
450 fun (lop,mop,rop,bop) ->fun x ->
451 Visitor_c.vk_struct_fields_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
452 x
453
454 let distribute_mck_cst (maxpos, minpos) =
455 fun (lop,mop,rop,bop) ->fun x ->
456 Visitor_c.vk_cst_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
457 x
458
459
460 let distribute_mck_define_params (maxpos, minpos) = fun (lop,mop,rop,bop) ->
461 fun x ->
462 Visitor_c.vk_define_params_splitted_s
463 (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
464 x
465
466 let get_pos mck =
467 match mck with
468 | Ast_cocci.PLUS _ -> raise Impossible
469 | Ast_cocci.CONTEXT (Ast_cocci.FixPos (i1,i2),_)
470 | Ast_cocci.MINUS (Ast_cocci.FixPos (i1,i2),_,_,_) ->
471 Ast_cocci.FixPos (i1,i2)
472 | Ast_cocci.CONTEXT (Ast_cocci.DontCarePos,_)
473 | Ast_cocci.MINUS (Ast_cocci.DontCarePos,_,_,_) ->
474 Ast_cocci.DontCarePos
475 | _ -> failwith "weird: dont have position info for the mcodekind 2"
476
477 let distrf (ii_of_x_f, distribute_mck_x_f) =
478 fun ia x -> fun tin ->
479 let mck = Ast_cocci.get_mcodekind ia in
480 let (max, min) = Lib_parsing_c.max_min_by_pos (ii_of_x_f x)
481 in
482 if
483 (* bug: check_pos mck max && check_pos mck min
484 *
485 * if do that then if have - f(...); and in C f(1,2); then we
486 * would get a "already tagged" because the '...' would sucess in
487 * transformaing both '1' and '1,2'. So being in the range is not
488 * enough. We must be equal exactly to the range!
489 *)
490 (match get_pos mck with
491 | Ast_cocci.DontCarePos -> true
492 | Ast_cocci.FixPos (i1, i2) ->
493 i1 =*= min && i2 =*= max
494 | _ -> raise Impossible
495 )
496
497 then
498 return (
499 ia,
500 distribute_mck mck (distribute_mck_x_f (max,min)) x tin
501 ) tin
502 else fail tin
503
504
505 let distrf_e = distrf (Lib_parsing_c.ii_of_expr, distribute_mck_expr)
506 let distrf_args = distrf (Lib_parsing_c.ii_of_args, distribute_mck_args)
507 let distrf_type = distrf (Lib_parsing_c.ii_of_type, distribute_mck_type)
508 let distrf_param = distrf (Lib_parsing_c.ii_of_param, distribute_mck_param)
509 let distrf_params = distrf (Lib_parsing_c.ii_of_params,distribute_mck_params)
510 let distrf_ini = distrf (Lib_parsing_c.ii_of_ini,distribute_mck_ini)
511 let distrf_inis = distrf (Lib_parsing_c.ii_of_inis,distribute_mck_inis)
512 let distrf_decl = distrf (Lib_parsing_c.ii_of_decl,distribute_mck_decl)
513 let distrf_field = distrf (Lib_parsing_c.ii_of_field,distribute_mck_field)
514 let distrf_node = distrf (Lib_parsing_c.ii_of_node,distribute_mck_node)
515 let distrf_enum_fields =
516 distrf (Lib_parsing_c.ii_of_enum_fields, distribute_mck_enum_fields)
517 let distrf_struct_fields =
518 distrf (Lib_parsing_c.ii_of_struct_fields, distribute_mck_struct_fields)
519 let distrf_cst =
520 distrf (Lib_parsing_c.ii_of_cst, distribute_mck_cst)
521 let distrf_define_params =
522 distrf (Lib_parsing_c.ii_of_define_params,distribute_mck_define_params)
523
524
525 (* ------------------------------------------------------------------------*)
526 (* Environment *)
527 (* ------------------------------------------------------------------------*)
528 let meta_name_to_str (s1, s2) = s1 ^ "." ^ s2
529
530 let envf keep inherited = fun (s, value, _) f tin ->
531 let s = Ast_cocci.unwrap_mcode s in
532 let v =
533 if keep =*= Type_cocci.Saved
534 then (
535 try Some (List.assoc s tin.binding)
536 with Not_found ->
537 pr2(sprintf
538 "Don't find value for metavariable %s in the environment"
539 (meta_name_to_str s));
540 None)
541 else
542 (* not raise Impossible! *)
543 Some (value)
544 in
545 match v with
546 | None -> fail tin
547 | Some (value') ->
548
549 (* Ex: in cocci_vs_c someone wants to add a binding. Here in
550 * transformation3 the value for this var may be already in the
551 * env, because for instance its value were fixed in a previous
552 * SmPL rule. So here we want to check that this is the same value.
553 * If forget to do the check, what can happen ? Because of Exp
554 * and other disjunctive feature of cocci_vs_c (>||>), we
555 * may accept a match at a wrong position. Maybe later this
556 * will be detected via the pos system on tokens, but maybe
557 * not. So safer to keep the check.
558 *)
559
560 (*f () tin*)
561 let equal =
562 if inherited
563 then Cocci_vs_c.equal_inh_metavarval
564 else Cocci_vs_c.equal_metavarval in
565 if equal value value'
566 then f () tin
567 else fail tin
568
569
570 let check_idconstraint matcher c id = fun f tin -> f () tin
571 let check_constraints_ne matcher constraints exp = fun f tin -> f () tin
572
573 (* ------------------------------------------------------------------------*)
574 (* Environment, allbounds *)
575 (* ------------------------------------------------------------------------*)
576 let (all_bound : Ast_cocci.meta_name list -> tin -> bool) = fun l tin ->
577 true (* in transform we don't care ? *)
578
579 end
580
581 (*****************************************************************************)
582 (* Entry point *)
583 (*****************************************************************************)
584 module TRANS = Cocci_vs_c.COCCI_VS_C (XTRANS)
585
586
587 let transform_re_node a b tin =
588 match TRANS.rule_elem_node a b tin with
589 | None -> raise Impossible
590 | Some (_sp, b') -> b'
591
592 let (transform2: string (* rule name *) -> string list (* dropped_isos *) ->
593 Lib_engine.metavars_binding (* inherited bindings *) ->
594 Lib_engine.numbered_transformation_info -> F.cflow -> F.cflow) =
595 fun rule_name dropped_isos binding0 xs cflow ->
596
597 let extra = {
598 optional_storage_iso = not(List.mem "optional_storage" dropped_isos);
599 optional_qualifier_iso = not(List.mem "optional_qualifier" dropped_isos);
600 value_format_iso = not(List.mem "value_format" dropped_isos);
601 current_rule_name = rule_name;
602 index = [];
603 } in
604
605 (* find the node, transform, update the node, and iter for all elements *)
606
607 xs +> List.fold_left (fun acc (index, (nodei, binding, rule_elem)) ->
608 (* subtil: not cflow#nodes but acc#nodes *)
609 let node = acc#nodes#assoc nodei in
610
611 if !Flag.show_transinfo
612 then pr2 "transform one node";
613
614 let tin = {
615 XTRANS.extra = {extra with index = index};
616 XTRANS.binding = binding0@binding;
617 XTRANS.binding0 = []; (* not used - everything constant for trans *)
618 } in
619
620 let node' = transform_re_node rule_elem node tin in
621
622 (* assert that have done something. But with metaruleElem sometimes
623 dont modify fake nodes. So special case before on Fake nodes. *)
624 (match F.unwrap node with
625 | F.Enter | F.Exit | F.ErrorExit
626 | F.EndStatement _ | F.CaseNode _
627 | F.Fake
628 | F.TrueNode | F.FalseNode | F.AfterNode | F.FallThroughNode
629 -> ()
630 | _ -> () (* assert (not (node =*= node')); *)
631 );
632
633 (* useless, we dont go back from flow to ast now *)
634 (* let node' = lastfix_comma_struct node' in *)
635
636 acc#replace_node (nodei, node');
637 acc
638 ) cflow
639
640
641
642 let transform a b c d e =
643 Common.profile_code "Transformation3.transform"
644 (fun () -> transform2 a b c d e)