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.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
25 (* For each rule return the list of variables that are used after it.
26 Also augment various parts of each rule with unitary, inherited, and freshness
29 (* metavar decls should be better integrated into computations of free
30 variables in plus code *)
32 module Ast
= Ast_cocci
33 module V
= Visitor_ast
34 module TC
= Type_cocci
36 let rec nub = function
38 | (x
::xs
) when (List.mem x xs
) -> nub xs
39 | (x
::xs
) -> x
::(nub xs
)
41 (* Collect all variable references in a minirule. For a disj, we collect
42 the maximum number (2 is enough) of references in any branch. *)
44 let collect_unitary_nonunitary free_usage
=
45 let free_usage = List.sort compare
free_usage in
46 let rec loop1 todrop
= function (* skips multiple occurrences *)
48 | (x
::xs
) as all
-> if x
= todrop
then loop1 todrop xs
else all
in
49 let rec loop2 = function
53 if x
= y
(* occurs more than once in free_usage *)
55 let (unitary
,non_unitary
) = loop2(loop1 x xs
) in
56 (unitary
,x
::non_unitary
)
57 else (* occurs only once in free_usage *)
58 let (unitary
,non_unitary
) = loop2 (y
::xs
) in
59 (x
::unitary
,non_unitary
) in
62 let collect_refs include_constraints
=
63 let bind x y
= x
@ y
in
64 let option_default = [] in
66 let donothing recursor k e
= k e
in (* just combine in the normal way *)
68 let donothing_a recursor k e
= (* anything is not wrapped *)
69 k e
in (* just combine in the normal way *)
71 (* the following considers that anything that occurs non-unitarily in one
72 branch occurs nonunitarily in all branches. This is not optimal, but
73 doing better seems to require a breadth-first traversal, which is
74 perhaps better to avoid. Also, unitarily is represented as occuring once,
75 while nonunitarily is represented as twice - more is irrelevant *)
76 (* cases for disjs and metavars *)
77 let bind_disj refs_branches
=
78 let (unitary
,nonunitary
) =
79 List.split
(List.map
collect_unitary_nonunitary refs_branches
) in
80 let unitary = nub (List.concat
unitary) in
81 let nonunitary = nub (List.concat
nonunitary) in
83 List.filter
(function x
-> not
(List.mem x
nonunitary)) unitary in
84 unitary@nonunitary@nonunitary in
86 let metaid (x
,_
,_
,_
) = x
in
88 let astfvident recursor k i
=
90 (match Ast.unwrap i
with
91 Ast.MetaId
(name
,idconstraint
,_
,_
) | Ast.MetaFunc
(name
,idconstraint
,_
,_
)
92 | Ast.MetaLocalFunc
(name
,idconstraint
,_
,_
) ->
94 if include_constraints
96 match idconstraint
with
97 Ast.IdNegIdSet
(_
,metas) -> metas
100 bind (List.rev
metas) [metaid name
]
101 | Ast.DisjId
(ids
) -> bind_disj (List.map k ids
)
102 | _
-> option_default) in
104 let rec type_collect res
= function
105 TC.ConstVol
(_
,ty
) | TC.Pointer
(ty
) | TC.FunctionPointer
(ty
)
106 | TC.Array
(ty
) -> type_collect res ty
107 | TC.EnumName
(TC.MV
(tyname
,_
,_
)) ->
109 | TC.StructUnionName
(_
,TC.MV
(tyname
,_
,_
)) ->
111 | TC.MetaType
(tyname
,_
,_
) ->
113 | TC.SignedT
(_
,Some ty
) -> type_collect res ty
116 let astfvexpr recursor k e
=
118 (match Ast.unwrap e
with
119 Ast.MetaExpr
(name
,constraints
,_
,Some type_list
,_
,_
) ->
120 let types = List.fold_left
type_collect option_default type_list
in
122 if include_constraints
124 match constraints
with
125 Ast.SubExpCstrt l
-> l
128 bind extra (bind [metaid name
] types)
129 | Ast.MetaErr
(name
,constraints
,_
,_
)
130 | Ast.MetaExpr
(name
,constraints
,_
,_
,_
,_
) ->
132 if include_constraints
134 match constraints
with
135 Ast.SubExpCstrt l
-> l
138 bind extra [metaid name
]
139 | Ast.MetaExprList
(name
,Ast.MetaListLen
(lenname
,_
,_
),_
,_
) ->
140 [metaid name
;metaid lenname
]
141 | Ast.MetaExprList
(name
,_
,_
,_
) -> [metaid name
]
142 | Ast.DisjExpr
(exps
) -> bind_disj (List.map k exps
)
143 | _
-> option_default) in
145 let astfvdecls recursor k d
=
147 (match Ast.unwrap d
with
148 Ast.MetaDecl
(name
,_
,_
) | Ast.MetaField
(name
,_
,_
) -> [metaid name
]
149 | Ast.MetaFieldList
(name
,Ast.MetaListLen
(lenname
,_
,_
),_
,_
) ->
150 [metaid name
;metaid lenname
]
151 | Ast.MetaFieldList
(name
,_
,_
,_
) ->
153 | Ast.DisjDecl
(decls
) -> bind_disj (List.map k decls
)
154 | _
-> option_default) in
156 let astfvfullType recursor k ty
=
158 (match Ast.unwrap ty
with
159 Ast.DisjType
(types) -> bind_disj (List.map k
types)
160 | _
-> option_default) in
162 let astfvtypeC recursor k ty
=
164 (match Ast.unwrap ty
with
165 Ast.MetaType
(name
,_
,_
) -> [metaid name
]
166 | _
-> option_default) in
168 let astfvinit recursor k ty
=
170 (match Ast.unwrap ty
with
171 Ast.MetaInit
(name
,_
,_
) -> [metaid name
]
172 | _
-> option_default) in
174 let astfvparam recursor k p
=
176 (match Ast.unwrap p
with
177 Ast.MetaParam
(name
,_
,_
) -> [metaid name
]
178 | Ast.MetaParamList
(name
,Ast.MetaListLen
(lenname
,_
,_
),_
,_
) ->
179 [metaid name
;metaid lenname
]
180 | Ast.MetaParamList
(name
,_
,_
,_
) -> [metaid name
]
181 | _
-> option_default) in
183 let astfvrule_elem recursor k re
=
184 (*within a rule_elem, pattern3 manages the coherence of the bindings*)
187 (match Ast.unwrap re
with
188 Ast.MetaRuleElem
(name
,_
,_
) | Ast.MetaStmt
(name
,_
,_
,_
)
189 | Ast.MetaStmtList
(name
,_
,_
) -> [metaid name
]
190 | _
-> option_default)) in
192 let astfvstatement recursor k s
=
194 (match Ast.unwrap s
with
196 bind_disj (List.map recursor
.V.combiner_statement_dots stms
)
197 | _
-> option_default) in
200 if include_constraints
202 match Ast.get_pos_var mc
with
203 Ast.MetaPos
(name
,constraints
,_
,_
,_
) -> (metaid name
)::constraints
204 | _
-> option_default
205 else option_default in
207 V.combiner
bind option_default
208 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
209 donothing donothing donothing donothing donothing
210 astfvident astfvexpr astfvfullType astfvtypeC astfvinit astfvparam
211 astfvdecls astfvrule_elem astfvstatement donothing donothing donothing_a
213 let collect_all_refs = collect_refs true
214 let collect_non_constraint_refs = collect_refs false
216 let collect_all_rule_refs minirules
=
217 List.fold_left
(@) []
218 (List.map
collect_all_refs.V.combiner_top_level minirules
)
220 let collect_all_minirule_refs = collect_all_refs.V.combiner_top_level
222 (* ---------------------------------------------------------------- *)
225 let bind = Common.union_set
in
226 let option_default = [] in
228 let donothing recursor k e
= k e
in (* just combine in the normal way *)
230 let metaid (x
,_
,_
,_
) = x
in
232 (* cases for metavariables *)
233 let astfvident recursor k i
=
235 (match Ast.unwrap i
with
236 Ast.MetaId
(name
,_
,TC.Saved
,_
)
237 | Ast.MetaFunc
(name
,_
,TC.Saved
,_
)
238 | Ast.MetaLocalFunc
(name
,_
,TC.Saved
,_
) -> [metaid name
]
239 | _
-> option_default) in
241 let rec type_collect res
= function
242 TC.ConstVol
(_
,ty
) | TC.Pointer
(ty
) | TC.FunctionPointer
(ty
)
243 | TC.Array
(ty
) -> type_collect res ty
244 | TC.EnumName
(TC.MV
(tyname
,TC.Saved
,_
)) ->
246 | TC.StructUnionName
(_
,TC.MV
(tyname
,TC.Saved
,_
)) ->
248 | TC.MetaType
(tyname
,TC.Saved
,_
) ->
250 | TC.SignedT
(_
,Some ty
) -> type_collect res ty
253 let astfvexpr recursor k e
=
255 match Ast.unwrap e
with
256 Ast.MetaExpr
(name
,_
,_
,Some type_list
,_
,_
) ->
257 List.fold_left
type_collect option_default type_list
261 (match Ast.unwrap e
with
262 Ast.MetaErr
(name
,_
,TC.Saved
,_
) | Ast.MetaExpr
(name
,_
,TC.Saved
,_
,_
,_
)
264 | Ast.MetaExprList
(name
,Ast.MetaListLen
(lenname
,ls
,_
),ns
,_
) ->
266 match ns
with TC.Saved
-> [metaid name
] | _
-> [] in
268 match ls
with TC.Saved
-> [metaid lenname
] | _
-> [] in
270 | Ast.MetaExprList
(name
,_
,TC.Saved
,_
) -> [metaid name
]
271 | _
-> option_default) in
274 let astfvtypeC recursor k ty
=
276 (match Ast.unwrap ty
with
277 Ast.MetaType
(name
,TC.Saved
,_
) -> [metaid name
]
278 | _
-> option_default) in
280 let astfvinit recursor k ty
=
282 (match Ast.unwrap ty
with
283 Ast.MetaInit
(name
,TC.Saved
,_
) -> [metaid name
]
284 | _
-> option_default) in
286 let astfvparam recursor k p
=
288 (match Ast.unwrap p
with
289 Ast.MetaParam
(name
,TC.Saved
,_
) -> [metaid name
]
290 | Ast.MetaParamList
(name
,Ast.MetaListLen
(lenname
,ls
,_
),ns
,_
) ->
292 match ns
with TC.Saved
-> [metaid name
] | _
-> [] in
294 match ls
with TC.Saved
-> [metaid lenname
] | _
-> [] in
296 | Ast.MetaParamList
(name
,_
,TC.Saved
,_
) -> [metaid name
]
297 | _
-> option_default) in
299 let astfvdecls recursor k d
=
301 (match Ast.unwrap d
with
302 Ast.MetaDecl
(name
,TC.Saved
,_
) | Ast.MetaField
(name
,TC.Saved
,_
) ->
304 | Ast.MetaFieldList
(name
,Ast.MetaListLen
(lenname
,ls
,_
),ns
,_
) ->
306 match ns
with TC.Saved
-> [metaid name
] | _
-> [] in
308 match ls
with TC.Saved
-> [metaid lenname
] | _
-> [] in
310 | Ast.MetaFieldList
(name
,_
,TC.Saved
,_
) -> [metaid name
]
311 | _
-> option_default) in
313 let astfvrule_elem recursor k re
=
314 (*within a rule_elem, pattern3 manages the coherence of the bindings*)
317 (match Ast.unwrap re
with
318 Ast.MetaRuleElem
(name
,TC.Saved
,_
) | Ast.MetaStmt
(name
,TC.Saved
,_
,_
)
319 | Ast.MetaStmtList
(name
,TC.Saved
,_
) -> [metaid name
]
320 | _
-> option_default)) in
323 match Ast.get_pos_var e
with
324 Ast.MetaPos
(name
,_
,_
,TC.Saved
,_
) -> [metaid name
]
325 | _
-> option_default in
327 V.combiner
bind option_default
328 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
329 donothing donothing donothing donothing donothing
330 astfvident astfvexpr donothing astfvtypeC astfvinit astfvparam
331 astfvdecls astfvrule_elem donothing donothing donothing donothing
333 (* ---------------------------------------------------------------- *)
335 (* For the rules under a given metavariable declaration, collect all of the
336 variables that occur in the plus code *)
338 let cip_mcodekind r mck
=
339 let process_anything_list_list anythings
=
340 let astfvs = collect_all_refs.V.combiner_anything
in
341 List.fold_left
(@) []
342 (List.map
(function l
-> List.fold_left
(@) [] (List.map
astfvs l
))
345 Ast.MINUS
(_
,_
,_
,anythings
) -> process_anything_list_list anythings
346 | Ast.CONTEXT
(_
,befaft
) ->
348 Ast.BEFORE
(ll
,_
) -> process_anything_list_list ll
349 | Ast.AFTER
(ll
,_
) -> process_anything_list_list ll
350 | Ast.BEFOREAFTER
(llb
,lla
,_
) ->
351 (process_anything_list_list lla
) @
352 (process_anything_list_list llb
)
357 let collect_fresh_seed_env metavars l
=
362 Ast.MetaFreshIdDecl
(_
,seed
) as x
->
363 ((Ast.get_meta_name x
),seed
)::prev
366 let (seed_env
,seeds
) =
368 (function (seed_env
,seeds
) as prev
->
371 (let v = List.assoc x
fresh in
378 Ast.SeedId
(id
) -> id
::prev
381 ((x
,ids)::seed_env
,Common.union_set
ids seeds
)
382 | _
-> ((x
,[])::seed_env
,seeds
))
383 with Not_found
-> prev
)
385 (List.rev seed_env
,List.rev seeds
)
387 let collect_fresh_seed metavars l
=
388 let (_
,seeds
) = collect_fresh_seed_env metavars l
in seeds
390 let collect_in_plus_term =
392 let bind x y
= x
@ y
in
393 let option_default = [] in
394 let donothing r k e
= k e
in
396 (* no positions in the + code *)
397 let mcode r
(_
,_
,mck
,_
) = cip_mcodekind r mck
in
399 (* case for things with bef/aft mcode *)
401 let astfvrule_elem recursor k re
=
402 match Ast.unwrap re
with
403 Ast.FunHeader
(bef
,_
,fi
,nm
,_
,params
,_
) ->
408 Ast.FType
(ty
) -> collect_all_refs.V.combiner_fullType ty
411 let nm_metas = collect_all_refs.V.combiner_ident nm
in
413 match Ast.unwrap params
with
414 Ast.DOTS
(params
) | Ast.CIRCLES
(params
) ->
418 match Ast.unwrap p
with
419 Ast.VoidParam
(t
) | Ast.Param
(t
,_
) ->
420 collect_all_refs.V.combiner_fullType t
423 | _
-> failwith
"not allowed for params" in
427 (bind (cip_mcodekind recursor bef
) (k re
))))
428 | Ast.Decl
(bef
,_
,_
) ->
429 bind (cip_mcodekind recursor bef
) (k re
)
432 let astfvstatement recursor k s
=
433 match Ast.unwrap s
with
434 Ast.IfThen
(_
,_
,(_
,_
,_
,aft
)) | Ast.IfThenElse
(_
,_
,_
,_
,(_
,_
,_
,aft
))
435 | Ast.While
(_
,_
,(_
,_
,_
,aft
)) | Ast.For
(_
,_
,(_
,_
,_
,aft
))
436 | Ast.Iterator
(_
,_
,(_
,_
,_
,aft
)) ->
437 bind (k s
) (cip_mcodekind recursor aft
)
440 V.combiner
bind option_default
441 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
442 donothing donothing donothing donothing donothing
443 donothing donothing donothing donothing donothing donothing
444 donothing astfvrule_elem astfvstatement donothing donothing donothing
446 let collect_in_plus metavars minirules
=
448 (collect_fresh_seed metavars
450 (List.map
collect_in_plus_term.V.combiner_top_level minirules
)))
452 (* ---------------------------------------------------------------- *)
454 (* For the rules under a given metavariable declaration, collect all of the
455 variables that occur only once and more than once in the minus code *)
457 let collect_all_multirefs minirules
=
458 let refs = List.map
collect_all_refs.V.combiner_top_level minirules
in
459 collect_unitary_nonunitary (List.concat
refs)
461 (* ---------------------------------------------------------------- *)
463 (* classify as unitary (no binding) or nonunitary (env binding) or saved
466 let classify_variables metavar_decls minirules used_after
=
467 let metavars = List.map
Ast.get_meta_name metavar_decls
in
468 let (unitary,nonunitary) = collect_all_multirefs minirules
in
469 let inplus = collect_in_plus metavar_decls minirules
in
471 let donothing r k e
= k e
in
472 let check_unitary name inherited
=
473 if List.mem name
inplus or List.mem name used_after
475 else if not inherited
&& List.mem name
unitary
477 else TC.Nonunitary
in
479 let get_option f
= function Some x
-> Some
(f x
) | None
-> None
in
481 let classify (name
,_
,_
,_
) =
482 let inherited = not
(List.mem name
metavars) in
483 (check_unitary name
inherited,inherited) in
486 match Ast.get_pos_var mc
with
487 Ast.MetaPos
(name
,constraints
,per
,unitary,inherited) ->
488 let (unitary,inherited) = classify name
in
489 Ast.set_pos_var
(Ast.MetaPos
(name
,constraints
,per
,unitary,inherited))
495 match Ast.unwrap
e with
496 Ast.MetaId
(name
,constraints
,_
,_
) ->
497 let (unitary,inherited) = classify name
in
499 (Ast.MetaId
(name
,constraints
,unitary,inherited))
500 | Ast.MetaFunc
(name
,constraints
,_
,_
) ->
501 let (unitary,inherited) = classify name
in
502 Ast.rewrap
e (Ast.MetaFunc
(name
,constraints
,unitary,inherited))
503 | Ast.MetaLocalFunc
(name
,constraints
,_
,_
) ->
504 let (unitary,inherited) = classify name
in
505 Ast.rewrap
e (Ast.MetaLocalFunc
(name
,constraints
,unitary,inherited))
508 let rec type_infos = function
509 TC.ConstVol
(cv
,ty
) -> TC.ConstVol
(cv
,type_infos ty
)
510 | TC.Pointer
(ty
) -> TC.Pointer
(type_infos ty
)
511 | TC.FunctionPointer
(ty
) -> TC.FunctionPointer
(type_infos ty
)
512 | TC.Array
(ty
) -> TC.Array
(type_infos ty
)
513 | TC.EnumName
(TC.MV
(name
,_
,_
)) ->
514 let (unitary,inherited) = classify (name
,(),(),Ast.NoMetaPos
) in
515 TC.EnumName
(TC.MV
(name
,unitary,inherited))
516 | TC.StructUnionName
(su
,TC.MV
(name
,_
,_
)) ->
517 let (unitary,inherited) = classify (name
,(),(),Ast.NoMetaPos
) in
518 TC.StructUnionName
(su
,TC.MV
(name
,unitary,inherited))
519 | TC.MetaType
(name
,_
,_
) ->
520 let (unitary,inherited) = classify (name
,(),(),Ast.NoMetaPos
) in
521 Type_cocci.MetaType
(name
,unitary,inherited)
522 | TC.SignedT
(sgn
,Some ty
) -> TC.SignedT
(sgn
,Some
(type_infos ty
))
525 let expression r k
e =
527 match Ast.unwrap
e with
528 Ast.MetaErr
(name
,constraints
,_
,_
) ->
529 let (unitary,inherited) = classify name
in
530 Ast.rewrap
e (Ast.MetaErr
(name
,constraints
,unitary,inherited))
531 | Ast.MetaExpr
(name
,constraints
,_
,ty
,form
,_
) ->
532 let (unitary,inherited) = classify name
in
533 let ty = get_option (List.map
type_infos) ty in
534 Ast.rewrap
e (Ast.MetaExpr
(name
,constraints
,unitary,ty,form
,inherited))
535 | Ast.MetaExprList
(name
,Ast.MetaListLen
(lenname
,_
,_
),_
,_
) ->
536 (* lenname should have the same properties of being unitary or
538 let (unitary,inherited) = classify name
in
539 let (lenunitary
,leninherited
) = classify lenname
in
543 Ast.MetaListLen
(lenname
,lenunitary
,leninherited
),
545 | Ast.MetaExprList
(name
,lenname
,_
,_
) ->
546 (* lenname should have the same properties of being unitary or
548 let (unitary,inherited) = classify name
in
549 Ast.rewrap
e (Ast.MetaExprList
(name
,lenname
,unitary,inherited))
554 match Ast.unwrap
e with
555 Ast.MetaType
(name
,_
,_
) ->
556 let (unitary,inherited) = classify name
in
557 Ast.rewrap
e (Ast.MetaType
(name
,unitary,inherited))
562 match Ast.unwrap
e with
563 Ast.MetaInit
(name
,_
,_
) ->
564 let (unitary,inherited) = classify name
in
565 Ast.rewrap
e (Ast.MetaInit
(name
,unitary,inherited))
570 match Ast.unwrap
e with
571 Ast.MetaParam
(name
,_
,_
) ->
572 let (unitary,inherited) = classify name
in
573 Ast.rewrap
e (Ast.MetaParam
(name
,unitary,inherited))
574 | Ast.MetaParamList
(name
,Ast.MetaListLen
(lenname
,_
,_
),_
,_
) ->
575 let (unitary,inherited) = classify name
in
576 let (lenunitary
,leninherited
) = classify lenname
in
579 (name
,Ast.MetaListLen
(lenname
,lenunitary
,leninherited
),
581 | Ast.MetaParamList
(name
,lenname
,_
,_
) ->
582 let (unitary,inherited) = classify name
in
583 Ast.rewrap
e (Ast.MetaParamList
(name
,lenname
,unitary,inherited))
588 match Ast.unwrap
e with
589 Ast.MetaDecl
(name
,_
,_
) ->
590 let (unitary,inherited) = classify name
in
591 Ast.rewrap
e (Ast.MetaDecl
(name
,unitary,inherited))
592 | Ast.MetaField
(name
,_
,_
) ->
593 let (unitary,inherited) = classify name
in
594 Ast.rewrap
e (Ast.MetaField
(name
,unitary,inherited))
595 | Ast.MetaFieldList
(name
,Ast.MetaListLen
(lenname
,_
,_
),_
,_
) ->
596 let (unitary,inherited) = classify name
in
597 let (lenunitary
,leninherited
) = classify lenname
in
600 (name
,Ast.MetaListLen
(lenname
,lenunitary
,leninherited
),
602 | Ast.MetaFieldList
(name
,lenname
,_
,_
) ->
603 let (unitary,inherited) = classify name
in
604 Ast.rewrap
e (Ast.MetaFieldList
(name
,lenname
,unitary,inherited))
607 let rule_elem r k
e =
609 match Ast.unwrap
e with
610 Ast.MetaStmt
(name
,_
,msi
,_
) ->
611 let (unitary,inherited) = classify name
in
612 Ast.rewrap
e (Ast.MetaStmt
(name
,unitary,msi
,inherited))
613 | Ast.MetaStmtList
(name
,_
,_
) ->
614 let (unitary,inherited) = classify name
in
615 Ast.rewrap
e (Ast.MetaStmtList
(name
,unitary,inherited))
619 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
620 donothing donothing donothing donothing donothing
621 ident expression donothing typeC init param decl rule_elem
622 donothing donothing donothing donothing in
624 List.map
fn.V.rebuilder_top_level minirules
626 (* ---------------------------------------------------------------- *)
628 (* For a minirule, collect the set of non-local (not in "bound") variables that
629 are referenced. Store them in a hash table. *)
631 (* bound means the metavariable was declared previously, not locally *)
633 (* Highly inefficient, because we call collect_all_refs on nested code
634 multiple times. But we get the advantage of not having too many variants
635 of the same functions. *)
637 (* Inherited doesn't include position constraints. If they are not bound
638 then there is no constraint. *)
640 let astfvs metavars bound
=
645 Ast.MetaFreshIdDecl
(_
,seed
) as x
->
646 ((Ast.get_meta_name x
),seed
)::prev
650 let collect_fresh l
=
651 let (matched
,freshvars
) =
653 (function (matched
,freshvars
) ->
655 try let v = List.assoc x
fresh in (matched
,(x
,v)::freshvars
)
656 with Not_found
-> (x
::matched
,freshvars
))
658 (List.rev matched
, List.rev freshvars
) in
660 (* cases for the elements of anything *)
661 let simple_setup getter k re
=
662 let minus_free = nub (getter
collect_all_refs re
) in
664 nub (getter
collect_non_constraint_refs re
) in
666 collect_fresh_seed metavars (getter
collect_in_plus_term re
) in
667 let free = Common.union_set
minus_free plus_free in
668 let nc_free = Common.union_set
minus_nc_free plus_free in
670 List.filter
(function x
-> not
(List.mem x bound
)) free in
672 List.filter
(function x
-> List.mem x bound
) nc_free in
674 List.filter
(function x
-> not
(List.mem x bound
)) minus_free in
675 let (matched
,fresh) = collect_fresh unbound in
677 Ast.free_vars
= matched
;
678 Ast.minus_free_vars
= munbound;
679 Ast.fresh_vars
= fresh;
680 Ast.inherited = inherited;
681 Ast.saved_witness
= []} in
683 let astfvrule_elem recursor k re
=
684 simple_setup (function x
-> x
.V.combiner_rule_elem
) k re
in
686 let astfvstatement recursor k s
=
687 let minus_free = nub (collect_all_refs.V.combiner_statement s
) in
689 nub (collect_non_constraint_refs.V.combiner_statement s
) in
691 collect_fresh_seed metavars
692 (collect_in_plus_term.V.combiner_statement s
) in
693 let free = Common.union_set
minus_free plus_free in
694 let nc_free = Common.union_set
minus_nc_free plus_free in
695 let classify free minus_free =
696 let (unbound,inherited) =
697 List.partition
(function x
-> not
(List.mem x bound
)) free in
699 List.filter
(function x
-> not
(List.mem x bound
)) minus_free in
700 let (matched
,fresh) = collect_fresh unbound in
701 (matched
,munbound,fresh,inherited) in
705 collect_fresh_seed metavars
706 (cip_mcodekind collect_in_plus_term aft
) in
707 match Ast.unwrap
res with
708 Ast.IfThen
(header
,branch
,(_
,_
,_
,aft
)) ->
709 let (unbound,_
,fresh,inherited) = classify (cip_plus aft
) [] in
710 Ast.IfThen
(header
,branch
,(unbound,fresh,inherited,aft
))
711 | Ast.IfThenElse
(header
,branch1
,els
,branch2
,(_
,_
,_
,aft
)) ->
712 let (unbound,_
,fresh,inherited) = classify (cip_plus aft
) [] in
713 Ast.IfThenElse
(header
,branch1
,els
,branch2
,
714 (unbound,fresh,inherited,aft
))
715 | Ast.While
(header
,body
,(_
,_
,_
,aft
)) ->
716 let (unbound,_
,fresh,inherited) = classify (cip_plus aft
) [] in
717 Ast.While
(header
,body
,(unbound,fresh,inherited,aft
))
718 | Ast.For
(header
,body
,(_
,_
,_
,aft
)) ->
719 let (unbound,_
,fresh,inherited) = classify (cip_plus aft
) [] in
720 Ast.For
(header
,body
,(unbound,fresh,inherited,aft
))
721 | Ast.Iterator
(header
,body
,(_
,_
,_
,aft
)) ->
722 let (unbound,_
,fresh,inherited) = classify (cip_plus aft
) [] in
723 Ast.Iterator
(header
,body
,(unbound,fresh,inherited,aft
))
726 let (matched
,munbound,fresh,_
) = classify free minus_free in
728 List.filter
(function x
-> List.mem x bound
) nc_free in
731 Ast.free_vars
= matched
;
732 Ast.minus_free_vars
= munbound;
733 Ast.fresh_vars
= fresh;
734 Ast.inherited = inherited;
735 Ast.saved_witness
= []} in
737 let astfvstatement_dots recursor k sd
=
738 simple_setup (function x
-> x
.V.combiner_statement_dots
) k sd
in
740 let astfvcase_line recursor k cl
=
741 simple_setup (function x
-> x
.V.combiner_case_line
) k cl
in
743 let astfvtoplevel recursor k tl
=
744 let saved = collect_saved.V.combiner_top_level tl
in
745 {(k tl
) with Ast.saved_witness
= saved} in
748 let donothing r k
e = k
e in
751 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
752 donothing donothing astfvstatement_dots donothing donothing
753 donothing donothing donothing donothing donothing donothing donothing
754 astfvrule_elem astfvstatement astfvcase_line astfvtoplevel donothing
757 let collect_astfvs rules =
758 let rec loop bound = function
760 | (metavars,(nm,rule_info,minirules))::rules ->
762 Common.minus_set bound (List.map Ast.get_meta_name metavars) in
764 (List.map (astfvs metavars bound).V.rebuilder_top_level minirules))::
765 (loop ((List.map Ast.get_meta_name metavars)@bound) rules) in
769 let collect_astfvs rules
=
770 let rec loop bound = function
772 | (metavars, rule
)::rules
->
774 Ast.ScriptRule
(_
,_
,_
,_
,script_vars
,_
) ->
775 (* why are metavars in rule, but outside for cocci rule??? *)
776 let bound = script_vars
@ bound in
777 rule
::(loop bound rules
)
778 | Ast.InitialScriptRule
(_
,_
,_
,_
) | Ast.FinalScriptRule
(_
,_
,_
,_
) ->
779 (* bound stays as is because script rules have no names, so no
780 inheritance is possible *)
781 rule
::(loop bound rules
)
782 | Ast.CocciRule
(nm
, rule_info
, minirules
, isexp
, ruletype
) ->
784 Common.minus_set
bound (List.map
Ast.get_meta_name
metavars) in
787 (List.map
(astfvs metavars bound).V.rebuilder_top_level
790 (loop ((List.map
Ast.get_meta_name
metavars)@bound) rules
) in
793 (* ---------------------------------------------------------------- *)
794 (* position variables that appear as a constraint on another position variable.
795 a position variable also cannot appear both positively and negatively in a
798 let get_neg_pos_list (_
,rule
) used_after_list
=
799 let donothing r k
e = k
e in
800 let bind (p1
,np1
) (p2
,np2
) =
801 (Common.union_set p1 p2
, Common.union_set np1 np2
) in
802 let option_default = ([],[]) in
803 let metaid (x
,_
,_
,_
) = x
in
805 match Ast.get_pos_var mc
with
806 Ast.MetaPos
(name
,constraints
,Ast.PER
,_
,_
) ->
807 ([metaid name
],constraints
)
808 | Ast.MetaPos
(name
,constraints
,Ast.ALL
,_
,_
) ->
809 ([],(metaid name
)::constraints
)
810 | _
-> option_default in
812 V.combiner
bind option_default
813 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
814 donothing donothing donothing donothing donothing
815 donothing donothing donothing donothing donothing donothing
816 donothing donothing donothing donothing donothing donothing in
818 Ast.CocciRule
(_
,_
,minirules
,_
,_
) ->
820 (function toplevel
->
821 let (positions
,neg_positions
) = v.V.combiner_top_level toplevel
in
822 (if List.exists
(function p
-> List.mem p neg_positions
) positions
825 "a variable cannot be used both as a position and a constraint");
828 | Ast.ScriptRule _
| Ast.InitialScriptRule _
| Ast.FinalScriptRule _
->
829 (*no negated positions*) []
831 (* ---------------------------------------------------------------- *)
833 (* collect used after lists, per minirule *)
835 (* defined is a list of variables that were declared in a previous metavar
838 (* Top-level used after: For each rule collect the set of variables that
839 are inherited, ie used but not defined. These are accumulated back to
840 their point of definition. *)
843 let collect_top_level_used_after metavar_rule_list
=
844 let drop_virt = List.filter
(function ("virtual",_
) -> false | _
-> true) in
845 let (used_after
,used_after_lists
) =
847 (function (metavar_list
,r
) ->
848 function (used_after
,used_after_lists
) ->
849 let locally_defined =
851 Ast.ScriptRule
(_
,_
,_
,_
,free_vars
,_
) -> free_vars
852 | _
-> List.map
Ast.get_meta_name metavar_list
in
853 let continue_propagation =
854 List.filter
(function x
-> not
(List.mem x
locally_defined))
858 Ast.ScriptRule
(_
,_
,_
,mv
,_
,_
) ->
859 drop_virt(List.map
(function (_
,(r
,v),_
) -> (r
,v)) mv
)
860 | Ast.InitialScriptRule
(_
,_
,_
,_
)
861 | Ast.FinalScriptRule
(_
,_
,_
,_
) -> []
862 | Ast.CocciRule
(_
,_
,rule
,_
,_
) ->
864 (Common.union_set
(nub (collect_all_rule_refs rule
))
865 (collect_in_plus metavar_list rule
)) in
867 List.filter
(function x
-> not
(List.mem x
locally_defined))
869 (Common.union_set
inherited continue_propagation,
870 used_after
::used_after_lists
))
871 metavar_rule_list
([],[]) in
872 match used_after
with
873 [] -> used_after_lists
876 (Printf.sprintf
"collect_top_level_used_after: unbound variables %s"
877 (String.concat
" " (List.map
(function (_
,x
) -> x
) used_after
)))
879 let collect_local_used_after metavars minirules used_after
=
880 let locally_defined = List.map
Ast.get_meta_name
metavars in
881 let rec loop = function
882 [] -> (used_after
,[],[],[],[])
884 (* In a rule there are three kinds of local variables:
885 1. Variables referenced in the minus or context code.
886 These get a value by matching. This value can be used in
888 2. Fresh variables referenced in the plus code.
889 3. Variables referenced in the seeds of the fresh variables.
890 There are also non-local variables. These may either be variables
891 referenced in the minus, context, or plus code, or they may be
892 variables referenced in the seeds of the fresh variables. *)
893 (* Step 1: collect all references in minus/context, plus, seed
895 let variables_referenced_in_minus_context_code =
896 nub (collect_all_minirule_refs minirule
) in
897 let variables_referenced_in_plus_code =
898 collect_in_plus_term.V.combiner_top_level minirule
in
899 let (env_of_fresh_seeds
,seeds_and_plus
) =
900 collect_fresh_seed_env
901 metavars variables_referenced_in_plus_code in
903 Common.union_set
variables_referenced_in_minus_context_code
905 (* Step 2: identify locally defined ones *)
906 let local_fresh = List.map fst env_of_fresh_seeds
in
908 List.partition
(function x
-> List.mem x
locally_defined) in
909 let local_env_of_fresh_seeds =
910 (* these have to be restricted to only one value if the associated
911 fresh variable is used after *)
912 List.map
(function (f
,ss
) -> (f
,is_local ss
)) env_of_fresh_seeds
in
913 let (local_all_free_vars
,nonlocal_all_free_vars
) =
914 is_local all_free_vars in
915 (* Step 3, recurse on the rest of the rules, making available whatever
916 has been defined in this one *)
917 let (mini_used_after
,fvs_lists
,mini_used_after_lists
,
918 mini_fresh_used_after_lists
,mini_fresh_used_after_seeds
) =
920 (* Step 4: collect the results. These are:
921 1. All of the variables used non-locally in the rules starting
923 2. All of the free variables to the end of the semantic patch
924 3. The variables that are used afterwards and defined here by
925 matching (minus or context code)
926 4. The variables that are used afterwards and are defined here as
928 5. The variables that are used as seeds in computing the bindings
929 of the variables collected in part 4. *)
930 let (local_used_after
, nonlocal_used_after
) =
931 is_local mini_used_after
in
932 let (fresh_local_used_after
(*4*),matched_local_used_after
) =
933 List.partition
(function x
-> List.mem x
local_fresh)
935 let matched_local_used_after(*3*) =
936 Common.union_set
matched_local_used_after nonlocal_used_after
in
937 let new_used_after = (*1*)
938 Common.union_set nonlocal_all_free_vars nonlocal_used_after
in
939 let fresh_local_used_after_seeds =
941 (* no point to keep variables that already are gtd to have only
943 (function x
-> not
(List.mem x
matched_local_used_after))
944 (List.fold_left
(function p
-> function c
-> Common.union_set c p
)
948 fst
(List.assoc fua
local_env_of_fresh_seeds))
949 fresh_local_used_after
)) in
950 (new_used_after,all_free_vars::fvs_lists
(*2*),
951 matched_local_used_after::mini_used_after_lists
,
952 fresh_local_used_after
::mini_fresh_used_after_lists
,
953 fresh_local_used_after_seeds::mini_fresh_used_after_seeds
) in
954 let (_
,fvs_lists
,used_after_lists
(*ua*),
955 fresh_used_after_lists
(*fua*),fresh_used_after_lists_seeds
(*fuas*)) =
957 (fvs_lists
,used_after_lists
,
958 fresh_used_after_lists
,fresh_used_after_lists_seeds
)
962 let collect_used_after metavar_rule_list
=
963 let used_after_lists = collect_top_level_used_after metavar_rule_list
in
965 (function (metavars,r
) ->
966 function used_after
->
968 Ast.ScriptRule
(_
,_
,_
,_
,_
,_
) (* no minirules, so nothing to do? *)
969 | Ast.InitialScriptRule
(_
,_
,_
,_
) | Ast.FinalScriptRule
(_
,_
,_
,_
) ->
970 ([], [used_after
], [[]], [])
971 | Ast.CocciRule
(name
, rule_info
, minirules
, _
,_
) ->
972 collect_local_used_after metavars minirules used_after
974 metavar_rule_list
used_after_lists
976 let rec split4 = function
978 | (a
,b
,c
,d
)::l
-> let (a1
,b1
,c1
,d1
) = split4 l
in (a
::a1
,b
::b1
,c
::c1
,d
::d1
)
980 (* ---------------------------------------------------------------- *)
983 let free_vars rules
=
984 let metavars = List.map
(function (mv
,rule
) -> mv
) rules
in
985 let (fvs_lists
,used_after_matched_lists
,
986 fresh_used_after_lists
,fresh_used_after_lists_seeds
) =
987 split4 (collect_used_after rules
) in
989 List.map2
get_neg_pos_list rules used_after_matched_lists
in
990 let positions_list = (* for all rules, assume all positions are used after *)
994 Ast.ScriptRule _
(* doesn't declare position variables *)
995 | Ast.InitialScriptRule _
| Ast.FinalScriptRule _
-> []
996 | Ast.CocciRule
(_
,_
,rule
,_
,_
) ->
1000 function Ast.MetaPosDecl
(_
,nm
) -> nm
::prev
| _
-> prev
)
1002 List.map
(function _
-> positions) rule
)
1007 function (ua
,fua
) ->
1010 | Ast.InitialScriptRule _
| Ast.FinalScriptRule _
-> r
1011 | Ast.CocciRule
(nm
, rule_info
, r
, is_exp
,ruletype
) ->
1014 classify_variables mv r
1015 ((List.concat ua
) @ (List.concat fua
)),
1017 rules
(List.combine used_after_matched_lists fresh_used_after_lists
) in
1018 let new_rules = collect_astfvs (List.combine
metavars new_rules) in
1019 (metavars,new_rules,
1020 fvs_lists
,neg_pos_lists,
1021 (used_after_matched_lists
,
1022 fresh_used_after_lists
,fresh_used_after_lists_seeds
),