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.
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.
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.
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.
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/>.
44 * The authors reserve the right to distribute this or future versions of
45 * Coccinelle under other licenses.
49 module Past
= Ast_popl
50 module Ast
= Ast_cocci
51 module V
= Visitor_ast
54 (* --------------------------------------------------------------------- *)
57 type cocci_predicate
= Lib_engine.predicate
* Ast.meta_name
Ast_ctl.modif
59 (cocci_predicate
,Ast_cocci.meta_name
, Wrapper_ctl.info
) Ast_ctl.generic_ctl
61 (* --------------------------------------------------------------------- *)
64 let bind x y
= x
or y
in
65 let option_default = false in
66 let mcode r
(_
,_
,kind
,_
) =
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
=
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
80 V.combiner
bind option_default
81 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
82 do_nothing do_nothing do_nothing do_nothing do_nothing
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
87 let ctl_exists keep_wit v x
=
88 CTL.Exists
(!Flag_popl.keep_all_wits
or keep_wit
,v
,x
)
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
)
96 (CTL.Pred
(Lib_engine.Match
(term
),CTL.Modif
v))
97 else CTL.Pred
(Lib_engine.Match
(term
),CTL.Control
)
99 (* --------------------------------------------------------------------- *)
101 let is_true = function CTL.True
-> true | _
-> false
103 let is_false = function CTL.False
-> true | _
-> false
105 let ctl_true = CTL.True
107 let ctl_false = CTL.False
111 else if is_true y
then x
else CTL.And
(CTL.STRICT
,x
,y
)
115 else if is_false y
then x
else CTL.Or
(x
,y
)
117 let ctl_seqor x y
= CTL.SeqOr
(x
,y
)
119 let ctl_not x
= CTL.Not
(x
)
122 if is_true x
then CTL.True
123 else CTL.AX
(CTL.FORWARD
,CTL.STRICT
,x
)
126 if is_true x
then CTL.True
127 else CTL.EX
(CTL.FORWARD
,x
)
130 if is_true x
then CTL.True
131 else CTL.EX
(CTL.BACKWARD
,x
)
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
)
140 let string2var x
= ("",x
)
143 let get_label_ctr _
=
144 let cur = !labelctr in
146 string2var (Printf.sprintf
"l%d" cur)
148 let ctl_au x y
= CTL.AU
(CTL.FORWARD
,CTL.STRICT
,x
,y
)
150 let ctl_uncheck x
= CTL.Uncheck
(x
)
152 let make_meta_rule_elem d
=
154 Ast.make_meta_rule_elem nm d
([],[],[])
156 (* --------------------------------------------------------------------- *)
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
162 | Past.SExists
(var
,seq
) -> ctl_exists keep_wit var
(ctl_seq keep_wit a seq
)
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
)
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
)
179 (* --------------------------------------------------------------------- *)
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
)
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
)
195 and guard_ctl_element keep_wit a
= function
196 Past.Term
(term
,_
) -> guard_ctl_term keep_wit term
197 | Past.Or
(seq1
,seq2
) ->
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
)
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 *)
210 (guard_ctl_seq
true a seq
)
211 (ctl_not (guard_ctl_seq
false a seq
))
212 | Past.When
(dots
,seq
) ->
214 (guard_ctl_dots keep_wit a dots
)
215 (ctl_not (guard_ctl_seq
false a seq
))
217 (* --------------------------------------------------------------------- *)
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))
223 (* doesn't work for C code if (x) return 1; else return 2; *)
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"
233 (predmaker keep_wit
(make_meta_rule_elem aft
))
239 (guard_ctl_term keep_wit thn
)))
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)
245 and do_between_dots keep_wit ba term
after =
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
256 (ctl_back_ex truepred)
257 (ctl_back_ex (ctl_back_ex falsepred)),
259 | Past.NoDots
-> term
261 (* --------------------------------------------------------------------- *)
263 let toctl sl
= ctl_seq true ctl_true sl