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