Debian Codegraph @

parsing_cocci/re_constraints.ml

raw symbols SCIP
1 (*
2 * This file is part of Coccinelle, licensed under the terms of the GPL v2.
3 * See copyright.txt in the Coccinelle source code for more information.
4 * The Coccinelle source code can be obtained at http://coccinelle.lip6.fr
5 *)
6
7 module Ast = Ast_cocci
8 module V = Visitor_ast
9
10 (* if a variable affected by a script constraint is always referenced only
11 with all the other local variables that are mentioned by that script
12 constraint, then we can push the script constraint down to the rule_element
13 level *)
14
15 (* Could be more optimal by checking that the params of the constraint
16 are not under a disj. Memoize to avoid rechecking for each constraint. *)
17
18 let disj_free_table = Hashtbl.create 101
19
20 let disj_free re =
21 let bind = (&&) in
22 let option_default = true in
23 let mcode _ _ = true in
24 let donothing r k e = k e in
25 (* case for anything with a disj *)
26 let ident r k e =
27 match Ast.unwrap e with Ast.DisjId _ -> false | _ -> k e in
28 let expr r k e =
29 match Ast.unwrap e with Ast.DisjExpr _ -> false | _ -> k e in
30 let ty r k e =
31 match Ast.unwrap e with Ast.DisjType _ -> false | _ -> k e in
32 let decl r k e =
33 match Ast.unwrap e with Ast.DisjDecl _ -> false | _ -> k e in
34 let ann_field r k e =
35 match Ast.unwrap e with Ast.DisjField _ -> false | _ -> k e in
36 let rule_elem r k e =
37 match Ast.unwrap e with Ast.DisjRuleElem _ -> false | _ -> k e in
38 let statement r k e =
39 match Ast.unwrap e with Ast.Disj _ -> false | _ -> k e in
40 let v =
41 V.combiner bind option_default {V.cmcode=mcode} {V.cdonothing=donothing}
42 ~ident:ident ~expr:expr ~ft:ty ~decl:decl ~annotated_field:ann_field
43 ~rule:rule_elem ~stmt:statement donothing in
44 try Hashtbl.find disj_free_table re
45 with Not_found ->
46 let res = v.V.combiner_rule_elem re in
47 Hashtbl.add disj_free_table re res;
48 res
49
50 let ok_for_all_rule_elems cstr minirules =
51 let bind = (&&) in
52 let option_default = true in
53 let mcode _ _ = true in
54 let donothing r k e = k e in
55
56 let (self,(_key,_lang,params,_pos,_code)) = cstr in
57 let rule_elem r k re =
58 let available = Ast.get_minus_nc_fvs re in
59 if List.mem self available
60 then
61 let params_available =
62 List.for_all (fun x -> List.mem x available) (List.map fst params) in
63 if params_available
64 then
65 let disj_free = disj_free re in
66 if disj_free
67 then true
68 else
69 failwith
70 (Printf.sprintf
71 "%s: constraint on variable %s cannot be evaluated in line %d due to disjunction\n%s"
72 (fst self) (snd self) (Ast.get_line re)
73 (Pretty_print_cocci.rule_elem_to_string re))
74 else
75 failwith
76 (Printf.sprintf
77 "%s: constraint on variable %s cannot be evaluated in line %d. available: %s\nwanted: %s"
78 (fst self) (snd self) (Ast.get_line re) (Dumper.dump available)
79 (Dumper.dump (List.map fst params)))
80 else true (* not relevant to this rule_elem *) in
81
82 let v =
83 V.combiner bind option_default {V.cmcode=mcode} {V.cdonothing=donothing}
84 ~rule:rule_elem donothing in
85 List.for_all v.V.combiner_top_level minirules
86
87 let update_for_all_rule_elems cstr minirules =
88 let mcode mc = mc in
89 let donothing r k e = k e in
90
91 let (self,((_key,_lang,params,_pos,_code) as sc)) = cstr in
92 let rule_elem r k re =
93 let re = k re in
94 let available = Ast.get_minus_nc_fvs re in
95 if List.mem self available
96 then Ast.add_constraint re (self,Ast.CstrScript(true,sc))
97 else re in
98
99 let v =
100 V.rebuilder {V.rmcode=mcode} {V.rdonothing=donothing} ~rule:rule_elem donothing in
101 List.map v.V.rebuilder_top_level minirules
102
103 let remove rule_name ((nm,_) as x) =
104 let cell = Hashtbl.find Data.non_local_script_constraints (rule_name,nm) in
105 let rest = List.filter (function y -> not(x = y)) !cell in
106 cell := rest
107
108 let re_constraints rules =
109 let constraints_by_rules = Hashtbl.create 101 in
110 Hashtbl.iter
111 (fun (rl,_) cstr ->
112 List.iter (Common.hashadd constraints_by_rules rl) !cstr)
113 Data.non_local_script_constraints;
114 let rules =
115 List.map
116 (function rule ->
117 match rule with
118 Ast.CocciRule(rule_name,info,minirules,b,ty) ->
119 let constraints =
120 try !(Hashtbl.find constraints_by_rules rule_name)
121 with Not_found -> [] in
122 let minirules =
123 List.fold_left
124 (fun minirules cstr ->
125 if ok_for_all_rule_elems cstr minirules
126 then
127 begin
128 remove rule_name cstr;
129 update_for_all_rule_elems cstr minirules
130 end
131 else minirules)
132 minirules constraints in
133 Ast.CocciRule(rule_name,info,minirules,b,ty)
134 | Ast.ScriptRule _ | Ast.InitialScriptRule _ | Ast.FinalScriptRule _ ->
135 rule)
136 rules in
137 Hashtbl.clear disj_free_table;
138 rules
139