Commit | Line | Data |
---|---|---|
9bc82bae C |
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 | ||
c491d8ee C |
25 | (* |
26 | * Copyright 2010, INRIA, University of Copenhagen | |
27 | * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix | |
28 | * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen | |
29 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix | |
30 | * This file is part of Coccinelle. | |
31 | * | |
32 | * Coccinelle is free software: you can redistribute it and/or modify | |
33 | * it under the terms of the GNU General Public License as published by | |
34 | * the Free Software Foundation, according to version 2 of the License. | |
35 | * | |
36 | * Coccinelle is distributed in the hope that it will be useful, | |
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
39 | * GNU General Public License for more details. | |
40 | * | |
41 | * You should have received a copy of the GNU General Public License | |
42 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
43 | * | |
44 | * The authors reserve the right to distribute this or future versions of | |
45 | * Coccinelle under other licenses. | |
46 | *) | |
47 | ||
48 | ||
951c7801 C |
49 | module Past = Ast_popl |
50 | module Ast = Ast_cocci | |
51 | module V = Visitor_ast | |
52 | module CTL = Ast_ctl | |
53 | ||
54 | (* --------------------------------------------------------------------- *) | |
55 | (* result type *) | |
56 | ||
57 | type cocci_predicate = Lib_engine.predicate * Ast.meta_name Ast_ctl.modif | |
58 | type formula = | |
59 | (cocci_predicate,Ast_cocci.meta_name, Wrapper_ctl.info) Ast_ctl.generic_ctl | |
60 | ||
61 | (* --------------------------------------------------------------------- *) | |
62 | ||
63 | let contains_modif = | |
64 | let bind x y = x or y in | |
65 | let option_default = false in | |
66 | let mcode r (_,_,kind,_) = | |
67 | match kind with | |
68 | Ast.MINUS(_,_,_,_) -> true | |
69 | | Ast.PLUS _ -> failwith "not possible" | |
70 | | Ast.CONTEXT(_,info) -> not (info = Ast.NOTHING) in | |
71 | let do_nothing r k e = k e in | |
72 | let rule_elem r k re = | |
73 | let res = k re in | |
74 | match Ast.unwrap re with | |
75 | Ast.FunHeader(bef,_,fninfo,name,lp,params,rp) -> | |
76 | bind (mcode r ((),(),bef,Ast.NoMetaPos)) res | |
77 | | Ast.Decl(bef,_,decl) -> bind (mcode r ((),(),bef,Ast.NoMetaPos)) res | |
78 | | _ -> res in | |
79 | let recursor = | |
80 | V.combiner bind option_default | |
81 | mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode | |
c491d8ee | 82 | do_nothing do_nothing do_nothing do_nothing do_nothing |
951c7801 C |
83 | do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing |
84 | do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in | |
85 | recursor.V.combiner_rule_elem | |
86 | ||
87 | let ctl_exists keep_wit v x = | |
88 | CTL.Exists(!Flag_popl.keep_all_wits or keep_wit,v,x) | |
89 | ||
90 | let predmaker keep_wit term = | |
91 | if (!Flag_popl.keep_all_wits or keep_wit) && | |
92 | (!Flag_popl.mark_all or contains_modif term) | |
93 | then | |
94 | let v = ("","_v") in | |
95 | ctl_exists true v | |
96 | (CTL.Pred (Lib_engine.Match(term),CTL.Modif v)) | |
97 | else CTL.Pred (Lib_engine.Match(term),CTL.Control) | |
98 | ||
99 | (* --------------------------------------------------------------------- *) | |
100 | ||
101 | let is_true = function CTL.True -> true | _ -> false | |
102 | ||
103 | let is_false = function CTL.False -> true | _ -> false | |
104 | ||
105 | let ctl_true = CTL.True | |
106 | ||
107 | let ctl_false = CTL.False | |
108 | ||
109 | let ctl_and x y = | |
110 | if is_true x then y | |
111 | else if is_true y then x else CTL.And(CTL.STRICT,x,y) | |
112 | ||
113 | let ctl_or x y = | |
114 | if is_false x then y | |
115 | else if is_false y then x else CTL.Or(x,y) | |
116 | ||
117 | let ctl_seqor x y = CTL.SeqOr(x,y) | |
118 | ||
119 | let ctl_not x = CTL.Not(x) | |
120 | ||
121 | let ctl_ax x = | |
122 | if is_true x then CTL.True | |
123 | else CTL.AX(CTL.FORWARD,CTL.STRICT,x) | |
124 | ||
125 | let ctl_ex x = | |
126 | if is_true x then CTL.True | |
127 | else CTL.EX(CTL.FORWARD,x) | |
128 | ||
129 | let ctl_back_ex x = | |
130 | if is_true x then CTL.True | |
131 | else CTL.EX(CTL.BACKWARD,x) | |
132 | ||
133 | let after = CTL.Pred(Lib_engine.After, CTL.Control) | |
134 | let fall = CTL.Pred(Lib_engine.FallThrough, CTL.Control) | |
135 | let exit = CTL.Pred(Lib_engine.Exit, CTL.Control) | |
136 | let truepred = CTL.Pred(Lib_engine.TrueBranch, CTL.Control) | |
137 | let falsepred = CTL.Pred(Lib_engine.FalseBranch, CTL.Control) | |
138 | let retpred = CTL.Pred(Lib_engine.Return, CTL.Control) | |
139 | ||
140 | let string2var x = ("",x) | |
141 | ||
142 | let labelctr = ref 0 | |
143 | let get_label_ctr _ = | |
144 | let cur = !labelctr in | |
145 | labelctr := cur + 1; | |
146 | string2var (Printf.sprintf "l%d" cur) | |
147 | ||
148 | let ctl_au x y = CTL.AU(CTL.FORWARD,CTL.STRICT,x,y) | |
149 | ||
150 | let ctl_uncheck x = CTL.Uncheck(x) | |
151 | ||
152 | let make_meta_rule_elem d = | |
153 | let nm = "_S" in | |
154 | Ast.make_meta_rule_elem nm d ([],[],[]) | |
155 | ||
156 | (* --------------------------------------------------------------------- *) | |
157 | ||
158 | let rec ctl_seq keep_wit a = function | |
159 | Past.Seq(elem,seq) -> | |
160 | ctl_element keep_wit (ctl_seq keep_wit a seq) elem | |
161 | | Past.Empty -> a | |
162 | | Past.SExists(var,seq) -> ctl_exists keep_wit var (ctl_seq keep_wit a seq) | |
163 | ||
164 | and ctl_term keep_wit a = function | |
165 | Past.Atomic(term) -> ctl_and (predmaker keep_wit term) (ctl_ax a) | |
166 | | Past.IfThen(test,thn,(_,_,_,aft)) -> ifthen keep_wit (Some a) test thn aft | |
167 | | Past.TExists(var,term) -> | |
168 | ctl_exists keep_wit var (ctl_term keep_wit a term) | |
169 | ||
170 | and ctl_element keep_wit a = function | |
171 | Past.Term(term,ba) -> | |
172 | do_between_dots keep_wit ba (ctl_term keep_wit a term) a | |
173 | | Past.Or(seq1,seq2) -> | |
174 | ctl_seqor (ctl_seq keep_wit a seq1) (ctl_seq keep_wit a seq2) | |
175 | | Past.DInfo(dots) -> ctl_au (guard_ctl_dots keep_wit a dots) a | |
176 | | Past.EExists(var,elem) -> | |
177 | ctl_exists keep_wit var (ctl_element keep_wit a elem) | |
178 | ||
179 | (* --------------------------------------------------------------------- *) | |
180 | ||
181 | and guard_ctl_seq keep_wit a = function | |
182 | Past.Seq(elem,Past.Empty) -> guard_ctl_element keep_wit a elem | |
183 | | Past.Seq(elem,seq) -> | |
184 | ctl_element keep_wit (guard_ctl_seq keep_wit a seq) elem | |
185 | | Past.Empty -> ctl_true | |
186 | | Past.SExists(var,seq) -> | |
187 | ctl_exists keep_wit var (guard_ctl_seq keep_wit a seq) | |
188 | ||
189 | and guard_ctl_term keep_wit = function | |
190 | Past.Atomic(term) -> predmaker keep_wit term | |
191 | | Past.IfThen(test,thn,(_,_,_,aft)) -> ifthen keep_wit None test thn aft | |
192 | | Past.TExists(var,term) -> | |
193 | ctl_exists keep_wit var (guard_ctl_term keep_wit term) | |
194 | ||
195 | and guard_ctl_element keep_wit a = function | |
196 | Past.Term(term,_) -> guard_ctl_term keep_wit term | |
197 | | Past.Or(seq1,seq2) -> | |
198 | ctl_seqor | |
199 | (guard_ctl_seq keep_wit a seq1) (guard_ctl_seq keep_wit a seq2) | |
200 | | Past.DInfo(dots) -> ctl_au (guard_ctl_dots keep_wit a dots) a | |
201 | | Past.EExists(var,elem) -> | |
202 | ctl_exists keep_wit var (guard_ctl_element keep_wit a elem) | |
203 | ||
204 | and guard_ctl_dots keep_wit a = function | |
205 | Past.Dots -> ctl_true | |
206 | (* | Past.Nest(_) when not keep_wit -> ctl_true | |
207 | a possible optimization, but irrelevant to popl example *) | |
208 | | Past.Nest(seq) -> | |
209 | ctl_or | |
210 | (guard_ctl_seq true a seq) | |
211 | (ctl_not (guard_ctl_seq false a seq)) | |
212 | | Past.When(dots,seq) -> | |
213 | ctl_and | |
214 | (guard_ctl_dots keep_wit a dots) | |
215 | (ctl_not (guard_ctl_seq false a seq)) | |
216 | ||
217 | (* --------------------------------------------------------------------- *) | |
218 | ||
219 | and ifthen keep_wit a test thn aft = | |
220 | (* "if (test) thn; after" becomes: | |
221 | if(test) & AX((TrueBranch & AX thn) v FallThrough v (After & AXAX after)) | |
222 | & EX After | |
223 | (* doesn't work for C code if (x) return 1; else return 2; *) | |
224 | *) | |
225 | let end_code = | |
226 | match (aft,a) with | |
227 | (Ast.CONTEXT(_,Ast.NOTHING),None) -> ctl_true | |
228 | | (Ast.CONTEXT(_,Ast.NOTHING),Some a) -> ctl_ax (ctl_ax a) | |
229 | | (_,None) -> failwith "not possible" | |
230 | | (_,Some a) -> | |
231 | ctl_ax | |
232 | (ctl_and | |
233 | (predmaker keep_wit (make_meta_rule_elem aft)) | |
234 | (ctl_ax a)) in | |
235 | let body = | |
236 | ctl_or | |
237 | (ctl_and truepred | |
238 | (ctl_ax | |
239 | (guard_ctl_term keep_wit thn))) | |
240 | (ctl_or fall | |
241 | (ctl_and after end_code)) in | |
242 | ctl_and (ctl_term keep_wit body test) | |
243 | (match a with Some CTL.True | None -> ctl_true | Some _ -> ctl_ex after) | |
244 | ||
245 | and do_between_dots keep_wit ba term after = | |
246 | match ba with | |
247 | Past.AddingBetweenDots (brace_term,n) | |
248 | | Past.DroppingBetweenDots (brace_term,n) -> | |
249 | (* not sure at all what to do here for after... *) | |
250 | let match_brace = ctl_term keep_wit after brace_term in | |
251 | let v = Printf.sprintf "_r_%d" n in | |
252 | let case1 = ctl_and (CTL.Ref v) match_brace in | |
253 | let case2 = ctl_and (ctl_not (CTL.Ref v)) term in | |
254 | CTL.Let | |
255 | (v,ctl_or | |
256 | (ctl_back_ex truepred) | |
257 | (ctl_back_ex (ctl_back_ex falsepred)), | |
ae4735db | 258 | ctl_or case1 case2) |
951c7801 C |
259 | | Past.NoDots -> term |
260 | ||
261 | (* --------------------------------------------------------------------- *) | |
262 | ||
263 | let toctl sl = ctl_seq true ctl_true sl |