2 * Copyright 2005-2010, 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.
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.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
47 * Copyright (C) 2006, 2007 Ecole des Mines de Nantes
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License (GPL)
51 * version 2 as published by the Free Software Foundation.
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * file license.txt for more details.
58 * This file was part of Coccinelle.
62 module F
= Control_flow_c
64 (*****************************************************************************)
65 (* The functor argument *)
66 (*****************************************************************************)
68 (* info passed recursively in monad in addition to binding *)
70 optional_storage_iso
: bool;
71 optional_qualifier_iso
: bool;
72 value_format_iso
: bool;
73 current_rule_name
: string; (* used for errors *)
74 index
: int list
(* witness tree indices *)
77 module XTRANS
= struct
79 (* ------------------------------------------------------------------------*)
80 (* Combinators history *)
81 (* ------------------------------------------------------------------------*)
84 * type ('a, 'b) transformer =
85 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b
89 * type ('a, 'b) transformer =
90 * 'a -> 'b -> Lib_engine.metavars_binding -> 'b option
91 * use an exception monad
94 * type tin = Lib_engine.metavars_binding
97 (* ------------------------------------------------------------------------*)
98 (* Standard type and operators *)
99 (* ------------------------------------------------------------------------*)
103 binding
: Lib_engine.metavars_binding
;
104 binding0
: Lib_engine.metavars_binding
; (* inherited variable *)
106 type 'x tout
= 'x
option
108 type ('a
, 'b
) matcher
= 'a
-> 'b
-> tin
-> ('a
* 'b
) tout
110 let (>>=) m f
= fun tin
->
113 | Some
(a
,b
) -> f a b tin
115 let return = fun x
-> fun tin
->
118 (* can have fail in transform now that the process is deterministic ? *)
119 let fail = fun tin
->
122 let (>||>) m1 m2
= fun tin
->
125 | Some x
-> Some x
(* stop as soon as have found something *)
127 let (>|+|>) m1 m2
= m1
>||> m2
129 let (>&&>) f m
= fun tin
->
130 if f tin
then m tin
else fail tin
132 let optional_storage_flag f
= fun tin
->
133 f
(tin
.extra
.optional_storage_iso
) tin
135 let optional_qualifier_flag f
= fun tin
->
136 f
(tin
.extra
.optional_qualifier_iso
) tin
138 let value_format_flag f
= fun tin
->
139 f
(tin
.extra
.value_format_iso
) tin
141 let mode = Cocci_vs_c.TransformMode
143 (* ------------------------------------------------------------------------*)
145 (* ------------------------------------------------------------------------*)
146 let cocciExp = fun expf expa node
-> fun tin
->
149 Visitor_c.default_visitor_c_s
with
150 Visitor_c.kexpr_s
= (fun (k
, bigf) expb
->
151 match expf expa expb tin
with
152 | None
-> (* failed *) k expb
153 | Some
(x
, expb
) -> expb
);
156 Some
(expa
, Visitor_c.vk_node_s
bigf node
)
159 (* same as cocciExp, but for expressions in an expression, not expressions
161 let cocciExpExp = fun expf expa expb
-> fun tin
->
164 Visitor_c.default_visitor_c_s
with
165 Visitor_c.kexpr_s
= (fun (k
, bigf) expb
->
166 match expf expa expb tin
with
167 | None
-> (* failed *) k expb
168 | Some
(x
, expb
) -> expb
);
171 Some
(expa
, Visitor_c.vk_expr_s
bigf expb
)
174 let cocciTy = fun expf expa node
-> fun tin
->
177 Visitor_c.default_visitor_c_s
with
178 Visitor_c.ktype_s
= (fun (k
, bigf) expb
->
179 match expf expa expb tin
with
180 | None
-> (* failed *) k expb
181 | Some
(x
, expb
) -> expb
);
184 Some
(expa
, Visitor_c.vk_node_s
bigf node
)
186 let cocciInit = fun expf expa node
-> fun tin
->
189 Visitor_c.default_visitor_c_s
with
190 Visitor_c.kini_s
= (fun (k
, bigf) expb
->
191 match expf expa expb tin
with
192 | None
-> (* failed *) k expb
193 | Some
(x
, expb
) -> expb
);
196 Some
(expa
, Visitor_c.vk_node_s
bigf node
)
199 (* ------------------------------------------------------------------------*)
201 (* ------------------------------------------------------------------------*)
202 let check_pos info mck pos
=
204 | Ast_cocci.PLUS _
-> raise Impossible
205 | Ast_cocci.CONTEXT
(Ast_cocci.FixPos
(i1
,i2
),_
)
206 | Ast_cocci.MINUS
(Ast_cocci.FixPos
(i1
,i2
),_
,_
,_
) ->
207 pos
<= i2
&& pos
>= i1
208 | Ast_cocci.CONTEXT
(Ast_cocci.DontCarePos
,_
)
209 | Ast_cocci.MINUS
(Ast_cocci.DontCarePos
,_
,_
,_
) ->
216 "weird: dont have position info for the mcodekind in line %d column %d"
217 info
.Ast_cocci.line info
.Ast_cocci.column
)
219 failwith
"weird: dont have position info for the mcodekind"
222 let tag_with_mck mck ib
= fun tin
->
224 let cocciinforef = ib
.Ast_c.cocci_tag
in
225 let (oldmcode
, oldenvs
) = Ast_c.mcode_and_env_of_cocciref
cocciinforef in
229 if !Flag_parsing_cocci.sgrep_mode
230 then Sgrep.process_sgrep ib mck
235 (match mck, Ast_c.pinfo_of_info ib
with
236 | _
, Ast_c.AbstractLineTok _
-> raise Impossible
237 | Ast_cocci.MINUS
(_
), Ast_c.ExpandedTok _
->
238 failwith
("try to delete an expanded token: " ^
(Ast_c.str_of_info ib
))
242 let many_count = function
243 Ast_cocci.BEFORE
(_
,Ast_cocci.MANY
) | Ast_cocci.AFTER
(_
,Ast_cocci.MANY
)
244 | Ast_cocci.BEFOREAFTER
(_
,_
,Ast_cocci.MANY
) -> true
247 (match (oldmcode
,mck) with
248 | (Ast_cocci.CONTEXT
(_
,Ast_cocci.NOTHING
), _
) ->
249 (* nothing there, so take the new stuff *)
250 let update_inst inst
= function
251 Ast_cocci.MINUS
(pos
,_
,adj
,any_xxs
) ->
252 Ast_cocci.MINUS
(pos
,inst
,adj
,any_xxs
)
254 cocciinforef := Some
(update_inst tin
.extra
.index
mck, [tin
.binding
])
255 | (_
, Ast_cocci.CONTEXT
(_
,Ast_cocci.NOTHING
)) ->
256 (* can this case occur? stay with the old stuff *)
258 | (Ast_cocci.MINUS
(old_pos
,old_inst
,old_adj
,[]),
259 Ast_cocci.MINUS
(new_pos
,new_inst
,new_adj
,[]))
260 when old_pos
= new_pos
&&
261 (List.mem tin
.binding oldenvs
or !Flag.sgrep_mode2
)
262 (* no way to combine adjacency information, just drop one *)
266 (old_pos
,Common.union_set old_inst new_inst
,old_adj
,[]),
268 (if !Flag_matcher.show_misc
269 then pr2
"already tagged but only removed, so safe")
271 | (Ast_cocci.CONTEXT
(old_pos
,old_modif
),
272 Ast_cocci.CONTEXT
(new_pos
,new_modif
))
273 when old_pos
= new_pos
&&
274 old_modif
= new_modif
&& many_count old_modif
->
275 (* iteration only allowed on context; no way to replace something
276 more than once; now no need for iterable; just check a flag *)
279 Some
(Ast_cocci.CONTEXT
(old_pos
,old_modif
),tin
.binding
::oldenvs
)
289 pad: if dont want cocci write:
291 (match Ast_c.pinfo_of_info ib with
292 Ast_c.FakeTok _ -> "already tagged fake token"
294 let pm str mcode env
=
296 "%s modification:\n%s\nAccording to environment %d:\n%s\n"
298 (Common.format_to_string
300 Pretty_print_cocci.print_mcodekind mcode
))
304 (function ((r
,vr
),vl
) ->
305 Printf.sprintf
" %s.%s -> %s" r vr
306 (Common.format_to_string
308 Pretty_print_engine.pp_binding_kind vl
)))
310 flush stdout
; flush stderr
;
312 ("\n"^
(String.concat
"\n"
313 (List.map
(pm "previous" oldmcode
) oldenvs
)) ^
"\n"
314 ^
(pm "current" mck tin
.binding
));
316 (match Ast_c.pinfo_of_info ib
with
318 Common.sprintf
"%s: already tagged fake token\n"
319 tin
.extra
.current_rule_name
322 "%s: already tagged token:\nC code context\n%s"
323 tin
.extra
.current_rule_name
324 (Common.error_message
(Ast_c.file_of_info ib
)
325 (Ast_c.str_of_info ib
, Ast_c.opos_of_info ib
)))
329 let tokenf ia ib
= fun tin
->
330 let (_
,i
,mck,_
) = ia
in
331 let pos = Ast_c.info_to_fixpos ib
in
332 if check_pos (Some i
) mck pos
333 then return (ia
, tag_with_mck mck ib tin
) tin
336 let tokenf_mck mck ib
= fun tin
->
337 let pos = Ast_c.info_to_fixpos ib
in
338 if check_pos None
mck pos
339 then return (mck, tag_with_mck mck ib tin
) tin
343 (* ------------------------------------------------------------------------*)
344 (* Distribute mcode *)
345 (* ------------------------------------------------------------------------*)
347 (* When in the SP we attach something to a metavariable, or delete it, as in
350 * we have to minusize all the token that compose S in the C code, and
351 * attach the 'foo();' to the right token, the one at the very right.
354 type 'a distributer
=
355 (Ast_c.info
-> Ast_c.info
) * (* what to do on left *)
356 (Ast_c.info
-> Ast_c.info
) * (* what to do on middle *)
357 (Ast_c.info
-> Ast_c.info
) * (* what to do on right *)
358 (Ast_c.info
-> Ast_c.info
) -> (* what to do on both *)
361 let distribute_mck mcodekind distributef expr tin
=
363 | Ast_cocci.MINUS
(pos,_
,adj
,any_xxs
) ->
364 let inst = tin
.extra
.index
in
367 tag_with_mck (Ast_cocci.MINUS
(pos,inst,adj
,any_xxs
)) ib tin
),
369 tag_with_mck (Ast_cocci.MINUS
(pos,inst,adj
,[])) ib tin
),
371 tag_with_mck (Ast_cocci.MINUS
(pos,inst,adj
,[])) ib tin
),
373 tag_with_mck (Ast_cocci.MINUS
(pos,inst,adj
,any_xxs
)) ib tin
)
375 | Ast_cocci.CONTEXT
(pos,any_befaft
) ->
376 (match any_befaft
with
377 | Ast_cocci.NOTHING
-> expr
379 | Ast_cocci.BEFORE
(xxs
,c
) ->
381 (fun ib
-> tag_with_mck
382 (Ast_cocci.CONTEXT
(pos,Ast_cocci.BEFORE
(xxs
,c
))) ib tin
),
385 (fun ib
-> tag_with_mck
386 (Ast_cocci.CONTEXT
(pos,Ast_cocci.BEFORE
(xxs
,c
))) ib tin
)
388 | Ast_cocci.AFTER
(xxs
,c
) ->
392 (fun ib
-> tag_with_mck
393 (Ast_cocci.CONTEXT
(pos,Ast_cocci.AFTER
(xxs
,c
))) ib tin
),
394 (fun ib
-> tag_with_mck
395 (Ast_cocci.CONTEXT
(pos,Ast_cocci.AFTER
(xxs
,c
))) ib tin
)
398 | Ast_cocci.BEFOREAFTER
(xxs
, yys
, c
) ->
400 (fun ib
-> tag_with_mck
401 (Ast_cocci.CONTEXT
(pos,Ast_cocci.BEFORE
(xxs
,c
))) ib tin
),
403 (fun ib
-> tag_with_mck
404 (Ast_cocci.CONTEXT
(pos,Ast_cocci.AFTER
(yys
,c
))) ib tin
),
405 (fun ib
-> tag_with_mck
406 (Ast_cocci.CONTEXT
(pos,Ast_cocci.BEFOREAFTER
(xxs
,yys
,c
)))
411 | Ast_cocci.PLUS _
-> raise Impossible
414 (* use new strategy, collect ii, sort, recollect and tag *)
416 let mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
) =
418 Visitor_c.default_visitor_c_s
with
419 Visitor_c.kinfo_s
= (fun (k
,bigf) i
->
420 let pos = Ast_c.info_to_fixpos i
in
422 | _
when Ast_cocci.equal_pos
pos maxpos
&&
423 Ast_cocci.equal_pos
pos minpos
-> bop i
424 | _
when Ast_cocci.equal_pos
pos maxpos
-> rop i
425 | _
when Ast_cocci.equal_pos
pos minpos
-> lop i
431 let distribute_mck_expr (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) -> fun x
->
432 Visitor_c.vk_expr_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
)) x
434 let distribute_mck_args (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) -> fun x
->
435 Visitor_c.vk_args_splitted_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
)) x
437 let distribute_mck_type (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) -> fun x
->
438 Visitor_c.vk_type_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
)) x
440 let distribute_mck_ini (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) -> fun x
->
441 Visitor_c.vk_ini_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
)) x
443 let distribute_mck_param (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) -> fun x
->
444 Visitor_c.vk_param_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
)) x
446 let distribute_mck_params (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) ->fun x
->
447 Visitor_c.vk_params_splitted_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
))
450 let distribute_mck_node (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) ->fun x
->
451 Visitor_c.vk_node_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
))
454 let distribute_mck_struct_fields (maxpos
, minpos
) =
455 fun (lop
,mop
,rop
,bop
) ->fun x
->
456 Visitor_c.vk_struct_fields_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
))
459 let distribute_mck_cst (maxpos
, minpos
) =
460 fun (lop
,mop
,rop
,bop
) ->fun x
->
461 Visitor_c.vk_cst_s
(mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
))
465 let distribute_mck_define_params (maxpos
, minpos
) = fun (lop
,mop
,rop
,bop
) ->
467 Visitor_c.vk_define_params_splitted_s
468 (mk_bigf (maxpos
, minpos
) (lop
,mop
,rop
,bop
))
473 | Ast_cocci.PLUS _
-> raise Impossible
474 | Ast_cocci.CONTEXT
(Ast_cocci.FixPos
(i1
,i2
),_
)
475 | Ast_cocci.MINUS
(Ast_cocci.FixPos
(i1
,i2
),_
,_
,_
) ->
476 Ast_cocci.FixPos
(i1
,i2
)
477 | Ast_cocci.CONTEXT
(Ast_cocci.DontCarePos
,_
)
478 | Ast_cocci.MINUS
(Ast_cocci.DontCarePos
,_
,_
,_
) ->
479 Ast_cocci.DontCarePos
480 | _
-> failwith
"weird: dont have position info for the mcodekind"
482 let distrf (ii_of_x_f
, distribute_mck_x_f
) =
483 fun ia x
-> fun tin
->
484 let mck = Ast_cocci.get_mcodekind ia
in
485 let (max
, min
) = Lib_parsing_c.max_min_by_pos
(ii_of_x_f x
)
488 (* bug: check_pos mck max && check_pos mck min
490 * if do that then if have - f(...); and in C f(1,2); then we
491 * would get a "already tagged" because the '...' would sucess in
492 * transformaing both '1' and '1,2'. So being in the range is not
493 * enough. We must be equal exactly to the range!
495 (match get_pos mck with
496 | Ast_cocci.DontCarePos
-> true
497 | Ast_cocci.FixPos
(i1
, i2
) ->
498 i1
=*= min
&& i2
=*= max
499 | _
-> raise Impossible
505 distribute_mck mck (distribute_mck_x_f
(max
,min
)) x tin
510 let distrf_e = distrf (Lib_parsing_c.ii_of_expr
, distribute_mck_expr)
511 let distrf_args = distrf (Lib_parsing_c.ii_of_args
, distribute_mck_args)
512 let distrf_type = distrf (Lib_parsing_c.ii_of_type
, distribute_mck_type)
513 let distrf_param = distrf (Lib_parsing_c.ii_of_param
, distribute_mck_param)
514 let distrf_params = distrf (Lib_parsing_c.ii_of_params
,distribute_mck_params)
515 let distrf_ini = distrf (Lib_parsing_c.ii_of_ini
,distribute_mck_ini)
516 let distrf_node = distrf (Lib_parsing_c.ii_of_node
,distribute_mck_node)
517 let distrf_struct_fields =
518 distrf (Lib_parsing_c.ii_of_struct_fields
, distribute_mck_struct_fields)
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)
525 (* ------------------------------------------------------------------------*)
527 (* ------------------------------------------------------------------------*)
528 let meta_name_to_str (s1
, s2
) = s1 ^
"." ^ s2
530 let envf keep inherited
= fun (s
, value, _
) f tin
->
531 let s = Ast_cocci.unwrap_mcode
s in
533 if keep
=*= Type_cocci.Saved
535 try Some
(List.assoc
s tin
.binding
)
538 "Don't find value for metavariable %s in the environment"
539 (meta_name_to_str s));
542 (* not raise Impossible! *)
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.
563 then Cocci_vs_c.equal_inh_metavarval
564 else Cocci_vs_c.equal_metavarval
in
565 if equal value value'
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
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 ? *)
581 (*****************************************************************************)
583 (*****************************************************************************)
584 module TRANS
= Cocci_vs_c.COCCI_VS_C
(XTRANS
)
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'
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
->
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
;
605 (* find the node, transform, update the node, and iter for all elements *)
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
611 if !Flag.show_transinfo
612 then pr2
"transform one node";
615 XTRANS.extra = {extra with index
= index
};
616 XTRANS.binding
= binding0
@binding
;
617 XTRANS.binding0
= []; (* not used - everything constant for trans *)
620 let node'
= transform_re_node rule_elem
node tin in
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 _
628 | F.TrueNode
| F.FalseNode
| F.AfterNode
| F.FallThroughNode
630 | _
-> () (* assert (not (node =*= node')); *)
633 (* useless, we dont go back from flow to ast now *)
634 (* let node' = lastfix_comma_struct node' in *)
636 acc#replace_node
(nodei
, node'
);
642 let transform a b c d e
=
643 Common.profile_code
"Transformation3.transform"
644 (fun () -> transform2 a b c d e
)