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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 module Past
= Ast_popl
28 module Ast
= Ast_cocci
29 module V
= Visitor_ast
32 (* --------------------------------------------------------------------- *)
35 type cocci_predicate
= Lib_engine.predicate
* Ast.meta_name
Ast_ctl.modif
37 (cocci_predicate
,Ast_cocci.meta_name
, Wrapper_ctl.info
) Ast_ctl.generic_ctl
39 (* --------------------------------------------------------------------- *)
42 let bind x y
= x
or y
in
43 let option_default = false in
44 let mcode r
(_
,_
,kind
,_
) =
46 Ast.MINUS
(_
,_
) -> true
47 | Ast.PLUS
-> failwith
"not possible"
48 | Ast.CONTEXT
(_
,info
) -> not
(info
= Ast.NOTHING
) in
49 let do_nothing r k e
= k e
in
50 let rule_elem r k re
=
52 match Ast.unwrap re
with
53 Ast.FunHeader
(bef
,_
,fninfo
,name
,lp
,params
,rp
) ->
54 bind (mcode r
((),(),bef
,[])) res
55 | Ast.Decl
(bef
,_
,decl
) -> bind (mcode r
((),(),bef
,[])) res
58 V.combiner
bind option_default
59 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
61 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
66 let ctl_exists v x keep_wit
= CTL.Exists
(v
,x
,keep_wit
)
68 let predmaker guard term
=
69 let pos = ("","_p") in
71 (if guard
&& contains_modif term
75 (CTL.Pred
(Lib_engine.Match
(term
),CTL.Modif
v))
76 else CTL.Pred
(Lib_engine.Match
(term
),CTL.Control
))
78 (* --------------------------------------------------------------------- *)
80 let is_true = function CTL.True
-> true | _
-> false
82 let is_false = function CTL.False
-> true | _
-> false
84 let ctl_true = CTL.True
86 let ctl_false = CTL.False
90 else if is_true y
then x
else CTL.And
(CTL.STRICT
,x
,y
)
94 else if is_false y
then x
else CTL.Or
(x
,y
)
96 let ctl_seqor x y
= CTL.SeqOr
(x
,y
)
98 let ctl_not x
= CTL.Not
(x
)
101 if is_true x
then CTL.True
102 else CTL.AX
(CTL.FORWARD
,CTL.STRICT
,x
)
104 let after = CTL.Pred
(Lib_engine.After
, CTL.Control
)
105 let exit = CTL.Pred
(Lib_engine.Exit
, CTL.Control
)
106 let truepred = CTL.Pred
(Lib_engine.TrueBranch
, CTL.Control
)
107 let retpred = CTL.Pred
(Lib_engine.Return
, CTL.Control
)
109 let string2var x
= ("",x
)
112 let get_label_ctr _
=
113 let cur = !labelctr in
115 string2var (Printf.sprintf
"l%d" cur)
117 let ctl_au x seq_after y
=
118 let lv = get_label_ctr() in
119 let labelpred = CTL.Pred
(Lib_engine.Label
lv,CTL.Control
) in
120 let preflabelpred = CTL.Pred
(Lib_engine.PrefixLabel
lv,CTL.Control
) in
121 let matchgoto = CTL.Pred
(Lib_engine.Goto
,CTL.Control
) in
125 (Ast.Break
(Ast.make_mcode
"break",Ast.make_mcode
";"))) in
129 (Ast.Continue
(Ast.make_mcode
"continue",Ast.make_mcode
";"))) in
132 (ctl_and (ctl_and truepred labelpred)
134 (CTL.FORWARD
,CTL.STRICT
,preflabelpred,
135 ctl_and preflabelpred
137 (ctl_and (ctl_or (ctl_or matchgoto matchbreak) matchcontinue)
139 (CTL.FORWARD
,CTL.STRICT
,
140 ctl_not seq_after
))))))) in
141 CTL.AU
(CTL.FORWARD
,CTL.STRICT
,x
,ctl_or y
stop_early)
143 let ctl_uncheck x
= CTL.Uncheck
(x
)
145 (* --------------------------------------------------------------------- *)
147 let rec ctl_seq keep_wit a
= function
148 Past.Seq
(elem
,seq
) ->
149 ctl_element keep_wit
(ctl_seq keep_wit a seq
) elem
151 | Past.SExists
(var
,seq
) -> ctl_exists keep_wit var
(ctl_seq keep_wit a seq
)
153 and ctl_element keep_wit a
= function
154 Past.Term
(term
) -> ctl_and (predmaker keep_wit term
) (ctl_ax a
)
155 | Past.Or
(seq1
,seq2
) ->
156 ctl_seqor (ctl_seq keep_wit a seq1
) (ctl_seq keep_wit a seq2
)
157 | Past.DInfo
(dots
,seq_bef
,seq_aft
) ->
159 List.fold_left
ctl_or ctl_false
160 (List.map
(ctl_element
false ctl_true) l
) in
161 let s = shortest (Common.union_set seq_bef seq_aft
) in
162 ctl_au (ctl_and (guard_ctl_dots keep_wit dots
) (ctl_not s))
164 | Past.EExists
(var
,elem
) ->
165 ctl_exists keep_wit var
(ctl_element keep_wit a elem
)
167 (* --------------------------------------------------------------------- *)
169 and guard_ctl_seq keep_wit
= function
170 Past.Seq
(elem
,Past.Empty
) -> guard_ctl_element keep_wit elem
171 | Past.Seq
(elem
,seq
) ->
172 ctl_element keep_wit
(guard_ctl_seq keep_wit seq
) elem
173 | Past.Empty
-> ctl_true
174 | Past.SExists
(var
,seq
) ->
175 ctl_exists keep_wit var
(guard_ctl_seq keep_wit seq
)
177 and guard_ctl_element keep_wit
= function
178 Past.Term
(term
) -> predmaker keep_wit term
179 | Past.Or
(seq1
,seq2
) ->
180 ctl_seqor (guard_ctl_seq keep_wit seq1
) (guard_ctl_seq keep_wit seq2
)
181 | Past.DInfo
(dots
,seq_bef
,seq_aft
) ->
183 List.fold_left
ctl_or ctl_false
184 (List.map
(ctl_element
false ctl_true) l
) in
185 let s = shortest (Common.union_set seq_bef seq_aft
) in
186 let aft = ctl_or s exit in
187 ctl_au (ctl_and (guard_ctl_dots keep_wit dots
) (ctl_not s))
188 (shortest seq_aft
) aft
189 | Past.EExists
(var
,elem
) ->
190 ctl_exists keep_wit var
(guard_ctl_element keep_wit elem
)
192 and guard_ctl_dots keep_wit
= function
193 Past.Dots
-> ctl_true
194 | Past.Nest
(_
) when not keep_wit
-> ctl_true
196 ctl_or (guard_ctl_seq
true seq
) (ctl_not (guard_ctl_seq
false seq
))
197 | Past.When
(dots
,seq
) ->
199 (guard_ctl_dots keep_wit dots
)
200 (ctl_not (ctl_seq false ctl_true seq
))
201 | Past.DExists
(var
,dots
) ->
202 ctl_exists keep_wit var
(guard_ctl_dots keep_wit dots
)
204 (* --------------------------------------------------------------------- *)
206 let toctl sl
= Asttoctl2.CODE
(ctl_seq true ctl_true sl
)