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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
25 module F = Control_flow_c
27 (*****************************************************************************)
28 (* The functor argument *)
29 (*****************************************************************************)
31 (* info passed recursively in monad in addition to binding *)
33 optional_storage_iso : bool;
34 optional_qualifier_iso : bool;
35 value_format_iso : bool;
36 current_rule_name : string; (* used for errors *)
39 module XTRANS = struct
41 (* ------------------------------------------------------------------------*)
42 (* Combinators history *)
43 (* ------------------------------------------------------------------------*)
46 * type ('a, 'b) transformer =
47 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b
51 * type ('a, 'b) transformer =
52 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b option
53 * use an exception monad
56 * type tin = Lib_engine.metavars_binding
59 (* ------------------------------------------------------------------------*)
60 (* Standard type and operators *)
61 (* ------------------------------------------------------------------------*)
65 binding: Lib_engine.metavars_binding;
66 binding0: Lib_engine.metavars_binding; (* inherited variable *)
68 type 'x tout = 'x option
70 type ('a, 'b) matcher = 'a -> 'b -> tin -> ('a * 'b) tout
72 let (>>=) m f = fun tin ->
75 | Some (a,b) -> f a b tin
77 let return = fun x -> fun tin ->
80 (* can have fail in transform now that the process is deterministic ? *)
84 let (>||>) m1 m2 = fun tin ->
87 | Some x -> Some x (* stop as soon as have found something *)
89 let (>|+|>) m1 m2 = m1 >||> m2
91 let (>&&>) f m = fun tin ->
92 if f tin then m tin else fail tin
94 let optional_storage_flag f = fun tin ->
95 f (tin.extra.optional_storage_iso) tin
97 let optional_qualifier_flag f = fun tin ->
98 f (tin.extra.optional_qualifier_iso) tin
100 let value_format_flag f = fun tin ->
101 f (tin.extra.value_format_iso) tin
103 let mode = Cocci_vs_c_3.TransformMode
105 (* ------------------------------------------------------------------------*)
107 (* ------------------------------------------------------------------------*)
108 let cocciExp = fun expf expa node -> fun tin ->
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);
118 Some (expa, Visitor_c.vk_node_s bigf node)
121 (* same as cocciExp, but for expressions in an expression, not expressions
123 let cocciExpExp = fun expf expa expb -> fun tin ->
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);
133 Some (expa, Visitor_c.vk_expr_s bigf expb)
136 let cocciTy = fun expf expa node -> fun tin ->
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);
146 Some (expa, Visitor_c.vk_node_s bigf node)
149 (* ------------------------------------------------------------------------*)
151 (* ------------------------------------------------------------------------*)
152 let check_pos info mck pos =
154 | Ast_cocci.PLUS -> raise Impossible
155 | Ast_cocci.CONTEXT (Ast_cocci.FixPos (i1,i2),_)
156 | Ast_cocci.MINUS (Ast_cocci.FixPos (i1,i2),_) ->
157 pos <= i2 && pos >= i1
158 | Ast_cocci.CONTEXT (Ast_cocci.DontCarePos,_)
159 | Ast_cocci.MINUS (Ast_cocci.DontCarePos,_) ->
166 "wierd: dont have position info for the mcodekind in line %d column %d"
167 info.Ast_cocci.line info.Ast_cocci.column)
169 failwith "wierd: dont have position info for the mcodekind"
172 let tag_with_mck mck ib = fun tin ->
174 let cocciinforef = ib.Ast_c.cocci_tag in
175 let (oldmcode, oldenv) = !cocciinforef in
178 if !Flag_parsing_cocci.sgrep_mode
179 then Sgrep.process_sgrep ib mck
182 (match mck, Ast_c.pinfo_of_info ib with
183 | _, Ast_c.AbstractLineTok _ -> raise Impossible
184 | Ast_cocci.MINUS(_), Ast_c.ExpandedTok _ ->
185 failwith ("try to delete an expanded token: " ^ (Ast_c.str_of_info ib))
189 match (oldmcode,mck) with
190 | (Ast_cocci.CONTEXT(_,Ast_cocci.NOTHING), _)
191 | (_, Ast_cocci.CONTEXT(_,Ast_cocci.NOTHING))
193 cocciinforef := (mck, tin.binding);
197 if (oldmcode, oldenv) = (mck, tin.binding)
200 then pr2 "already tagged but with same mcode, so safe";
208 Format.set_formatter_out_channel stderr;
209 Common.pr2 "SP mcode ";
210 Pretty_print_cocci.print_mcodekind oldmcode;
211 Format.print_newline();
212 Common.pr2 "C code mcode ";
213 Pretty_print_cocci.print_mcodekind mck;
214 Format.print_newline();
215 Format.print_flush();
217 (Common.sprintf "%s: already tagged token:\n%s"
218 tin.extra.current_rule_name
219 (Common.error_message (Ast_c.file_of_info ib)
220 (Ast_c.str_of_info ib, Ast_c.opos_of_info ib)))
223 let tokenf ia ib = fun tin ->
224 let (_,i,mck,_) = ia in
225 let pos = Ast_c.info_to_fixpos ib in
226 if check_pos (Some i) mck pos
227 then return (ia, tag_with_mck mck ib tin) tin
230 let tokenf_mck mck ib = fun tin ->
231 let pos = Ast_c.info_to_fixpos ib in
232 if check_pos None mck pos
233 then return (mck, tag_with_mck mck ib tin) tin
237 (* ------------------------------------------------------------------------*)
238 (* Distribute mcode *)
239 (* ------------------------------------------------------------------------*)
241 (* When in the SP we attach something to a metavariable, or delete it, as in
244 * we have to minusize all the token that compose S in the C code, and
245 * attach the 'foo();' to the right token, the one at the very right.
248 type 'a distributer =
249 (Ast_c.info -> Ast_c.info) * (* what to do on left *)
250 (Ast_c.info -> Ast_c.info) * (* what to do on middle *)
251 (Ast_c.info -> Ast_c.info) * (* what to do on right *)
252 (Ast_c.info -> Ast_c.info) -> (* what to do on both *)
255 let distribute_mck mcodekind distributef expr tin =
257 | Ast_cocci.MINUS (pos,any_xxs) ->
259 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,any_xxs)) ib tin),
260 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,[])) ib tin),
261 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,[])) ib tin),
262 (fun ib -> tag_with_mck (Ast_cocci.MINUS (pos,any_xxs)) ib tin)
264 | Ast_cocci.CONTEXT (pos,any_befaft) ->
265 (match any_befaft with
266 | Ast_cocci.NOTHING -> expr
268 | Ast_cocci.BEFORE xxs ->
270 (fun ib -> tag_with_mck
271 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE xxs)) ib tin),
274 (fun ib -> tag_with_mck
275 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE xxs)) ib tin)
277 | Ast_cocci.AFTER xxs ->
281 (fun ib -> tag_with_mck
282 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER xxs)) ib tin),
283 (fun ib -> tag_with_mck
284 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER xxs)) ib tin)
287 | Ast_cocci.BEFOREAFTER (xxs, yys) ->
289 (fun ib -> tag_with_mck
290 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFORE xxs)) ib tin),
292 (fun ib -> tag_with_mck
293 (Ast_cocci.CONTEXT (pos,Ast_cocci.AFTER yys)) ib tin),
294 (fun ib -> tag_with_mck
295 (Ast_cocci.CONTEXT (pos,Ast_cocci.BEFOREAFTER (xxs,yys)))
300 | Ast_cocci.PLUS -> raise Impossible
303 (* use new strategy, collect ii, sort, recollect and tag *)
305 let mk_bigf (maxpos, minpos) (lop,mop,rop,bop) =
307 Visitor_c.default_visitor_c_s with
308 Visitor_c.kinfo_s = (fun (k,bigf) i ->
309 let pos = Ast_c.info_to_fixpos i in
311 | _ when Ast_cocci.equal_pos pos maxpos &&
312 Ast_cocci.equal_pos pos minpos -> bop i
313 | _ when Ast_cocci.equal_pos pos maxpos -> rop i
314 | _ when Ast_cocci.equal_pos pos minpos -> lop i
320 let distribute_mck_expr (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
321 Visitor_c.vk_expr_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
323 let distribute_mck_args (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
324 Visitor_c.vk_args_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
326 let distribute_mck_type (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
327 Visitor_c.vk_type_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
329 let distribute_mck_ini (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
330 Visitor_c.vk_ini_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
332 let distribute_mck_param (maxpos, minpos) = fun (lop,mop,rop,bop) -> fun x ->
333 Visitor_c.vk_param_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop)) x
335 let distribute_mck_params (maxpos, minpos) = fun (lop,mop,rop,bop) ->fun x ->
336 Visitor_c.vk_params_splitted_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
339 let distribute_mck_node (maxpos, minpos) = fun (lop,mop,rop,bop) ->fun x ->
340 Visitor_c.vk_node_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
343 let distribute_mck_struct_fields (maxpos, minpos) =
344 fun (lop,mop,rop,bop) ->fun x ->
345 Visitor_c.vk_struct_fields_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
348 let distribute_mck_cst (maxpos, minpos) =
349 fun (lop,mop,rop,bop) ->fun x ->
350 Visitor_c.vk_cst_s (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
354 let distribute_mck_define_params (maxpos, minpos) = fun (lop,mop,rop,bop) ->
356 Visitor_c.vk_define_params_splitted_s
357 (mk_bigf (maxpos, minpos) (lop,mop,rop,bop))
362 | Ast_cocci.PLUS -> raise Impossible
363 | Ast_cocci.CONTEXT (Ast_cocci.FixPos (i1,i2),_)
364 | Ast_cocci.MINUS (Ast_cocci.FixPos (i1,i2),_) ->
365 Ast_cocci.FixPos (i1,i2)
366 | Ast_cocci.CONTEXT (Ast_cocci.DontCarePos,_)
367 | Ast_cocci.MINUS (Ast_cocci.DontCarePos,_) ->
368 Ast_cocci.DontCarePos
369 | _ -> failwith "wierd: dont have position info for the mcodekind"
371 let distrf (ii_of_x_f, distribute_mck_x_f) =
372 fun ia x -> fun tin ->
373 let mck = Ast_cocci.get_mcodekind ia in
374 let (max, min) = Lib_parsing_c.max_min_by_pos (ii_of_x_f x)
377 (* bug: check_pos mck max && check_pos mck min
379 * if do that then if have - f(...); and in C f(1,2); then we
380 * would get a "already tagged" because the '...' would sucess in
381 * transformaing both '1' and '1,2'. So being in the range is not
382 * enough. We must be equal exactly to the range!
384 (match get_pos mck with
385 | Ast_cocci.DontCarePos -> true
386 | Ast_cocci.FixPos (i1, i2) ->
388 | _ -> raise Impossible
394 distribute_mck mck (distribute_mck_x_f (max,min)) x tin
399 let distrf_e = distrf (Lib_parsing_c.ii_of_expr, distribute_mck_expr)
400 let distrf_args = distrf (Lib_parsing_c.ii_of_args, distribute_mck_args)
401 let distrf_type = distrf (Lib_parsing_c.ii_of_type, distribute_mck_type)
402 let distrf_param = distrf (Lib_parsing_c.ii_of_param, distribute_mck_param)
403 let distrf_params = distrf (Lib_parsing_c.ii_of_params,distribute_mck_params)
404 let distrf_ini = distrf (Lib_parsing_c.ii_of_ini,distribute_mck_ini)
405 let distrf_node = distrf (Lib_parsing_c.ii_of_node,distribute_mck_node)
406 let distrf_struct_fields =
407 distrf (Lib_parsing_c.ii_of_struct_fields, distribute_mck_struct_fields)
409 distrf (Lib_parsing_c.ii_of_cst, distribute_mck_cst)
410 let distrf_define_params =
411 distrf (Lib_parsing_c.ii_of_define_params,distribute_mck_define_params)
414 (* ------------------------------------------------------------------------*)
416 (* ------------------------------------------------------------------------*)
417 let meta_name_to_str (s1, s2) =
420 let envf keep _inherited = fun (s, value, _) f tin ->
421 let s = Ast_cocci.unwrap_mcode s in
423 if keep = Type_cocci.Saved
425 try Some (List.assoc s tin.binding)
428 "Don't find value for metavariable %s in the environment"
429 (meta_name_to_str s));
432 (* not raise Impossible! *)
439 (* Ex: in cocci_vs_c someone wants to add a binding. Here in
440 * transformation3 the value for this var may be already in the
441 * env, because for instance its value were fixed in a previous
442 * SmPL rule. So here we want to check that this is the same value.
443 * If forget to do the check, what can happen ? Because of Exp
444 * and other disjunctive feature of cocci_vs_c (>||>), we
445 * may accept a match at a wrong position. Maybe later this
446 * will be detected via the pos system on tokens, but maybe
447 * not. So safer to keep the check.
451 if Cocci_vs_c_3.equal_metavarval value value'
456 let check_constraints matcher constraints exp = fun f tin -> f () tin
458 (* ------------------------------------------------------------------------*)
459 (* Environment, allbounds *)
460 (* ------------------------------------------------------------------------*)
461 let (all_bound : Ast_cocci.meta_name list -> tin -> bool) = fun l tin ->
462 true (* in transform we don't care ? *)
466 (*****************************************************************************)
468 (*****************************************************************************)
469 module TRANS = Cocci_vs_c_3.COCCI_VS_C (XTRANS)
472 let transform_re_node a b tin =
473 match TRANS.rule_elem_node a b tin with
474 | None -> raise Impossible
475 | Some (_sp, b') -> b'
477 let (transform2: string (* rule name *) -> string list (* dropped_isos *) ->
478 Lib_engine.metavars_binding (* inherited bindings *) ->
479 Lib_engine.transformation_info -> F.cflow -> F.cflow) =
480 fun rule_name dropped_isos binding0 xs cflow ->
483 optional_storage_iso = not(List.mem "optional_storage" dropped_isos);
484 optional_qualifier_iso = not(List.mem "optional_qualifier" dropped_isos);
485 value_format_iso = not(List.mem "value_format" dropped_isos);
486 current_rule_name = rule_name;
489 (* find the node, transform, update the node, and iter for all elements *)
491 xs +> List.fold_left (fun acc (nodei, binding, rule_elem) ->
492 (* subtil: not cflow#nodes but acc#nodes *)
493 let node = acc#nodes#assoc nodei in
496 then pr2 "transform one node";
499 XTRANS.extra = extra;
500 XTRANS.binding = binding0@binding;
501 XTRANS.binding0 = []; (* not used - everything constant for trans *)
504 let node' = transform_re_node rule_elem node tin in
506 (* assert that have done something. But with metaruleElem sometimes
507 dont modify fake nodes. So special case before on Fake nodes. *)
508 (match F.unwrap node with
509 | F.Enter | F.Exit | F.ErrorExit
510 | F.EndStatement _ | F.CaseNode _
512 | F.TrueNode | F.FalseNode | F.AfterNode | F.FallThroughNode
514 | _ -> () (* assert (not (node =*= node')); *)
517 (* useless, we dont go back from flow to ast now *)
518 (* let node' = lastfix_comma_struct node' in *)
520 acc#replace_node (nodei, node');
526 let transform a b c d e =
527 Common.profile_code "Transformation3.transform"
528 (fun () -> transform2 a b c d e)