2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
23 module Past
= Ast_popl
24 module Ast
= Ast_cocci
25 module V
= Visitor_ast
28 (* --------------------------------------------------------------------- *)
31 type cocci_predicate
= Lib_engine.predicate
* Ast.meta_name
Ast_ctl.modif
33 (cocci_predicate
,Ast_cocci.meta_name
, Wrapper_ctl.info
) Ast_ctl.generic_ctl
35 (* --------------------------------------------------------------------- *)
38 let bind x y
= x
or y
in
39 let option_default = false in
40 let mcode r
(_
,_
,kind
,_
) =
42 Ast.MINUS
(_
,_
) -> true
43 | Ast.PLUS
-> failwith
"not possible"
44 | Ast.CONTEXT
(_
,info
) -> not
(info
= Ast.NOTHING
) in
45 let do_nothing r k e
= k e
in
46 let rule_elem r k re
=
48 match Ast.unwrap re
with
49 Ast.FunHeader
(bef
,_
,fninfo
,name
,lp
,params
,rp
) ->
50 bind (mcode r
((),(),bef
,Ast.NoMetaPos
)) res
51 | Ast.Decl
(bef
,_
,decl
) -> bind (mcode r
((),(),bef
,Ast.NoMetaPos
)) res
54 V.combiner
bind option_default
55 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
57 do_nothing do_nothing do_nothing do_nothing
58 do_nothing do_nothing do_nothing do_nothing do_nothing do_nothing
59 do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in
60 recursor.V.combiner_rule_elem
62 let ctl_exists v x keep_wit
= CTL.Exists
(v
,x
,keep_wit
)
64 let predmaker guard term
=
65 let pos = ("","_p") in
67 (if guard
&& contains_modif term
71 (CTL.Pred
(Lib_engine.Match
(term
),CTL.Modif
v))
72 else CTL.Pred
(Lib_engine.Match
(term
),CTL.Control
))
74 (* --------------------------------------------------------------------- *)
76 let is_true = function CTL.True
-> true | _
-> false
78 let is_false = function CTL.False
-> true | _
-> false
80 let ctl_true = CTL.True
82 let ctl_false = CTL.False
86 else if is_true y
then x
else CTL.And
(CTL.STRICT
,x
,y
)
90 else if is_false y
then x
else CTL.Or
(x
,y
)
92 let ctl_seqor x y
= CTL.SeqOr
(x
,y
)
94 let ctl_not x
= CTL.Not
(x
)
97 if is_true x
then CTL.True
98 else CTL.AX
(CTL.FORWARD
,CTL.STRICT
,x
)
100 let after = CTL.Pred
(Lib_engine.After
, CTL.Control
)
101 let exit = CTL.Pred
(Lib_engine.Exit
, CTL.Control
)
102 let truepred = CTL.Pred
(Lib_engine.TrueBranch
, CTL.Control
)
103 let retpred = CTL.Pred
(Lib_engine.Return
, CTL.Control
)
105 let string2var x
= ("",x
)
108 let get_label_ctr _
=
109 let cur = !labelctr in
111 string2var (Printf.sprintf
"l%d" cur)
113 let ctl_au x seq_after y
=
114 let lv = get_label_ctr() in
115 let labelpred = CTL.Pred
(Lib_engine.Label
lv,CTL.Control
) in
116 let preflabelpred = CTL.Pred
(Lib_engine.PrefixLabel
lv,CTL.Control
) in
117 let matchgoto = CTL.Pred
(Lib_engine.Goto
,CTL.Control
) in
121 (Ast.Break
(Ast.make_mcode
"break",Ast.make_mcode
";"))) in
125 (Ast.Continue
(Ast.make_mcode
"continue",Ast.make_mcode
";"))) in
128 (ctl_and (ctl_and truepred labelpred)
130 (CTL.FORWARD
,CTL.STRICT
,preflabelpred,
131 ctl_and preflabelpred
133 (ctl_and (ctl_or (ctl_or matchgoto matchbreak) matchcontinue)
135 (CTL.FORWARD
,CTL.STRICT
,
136 ctl_not seq_after
))))))) in
137 CTL.AU
(CTL.FORWARD
,CTL.STRICT
,x
,ctl_or y
stop_early)
139 let ctl_uncheck x
= CTL.Uncheck
(x
)
141 (* --------------------------------------------------------------------- *)
143 let rec ctl_seq keep_wit a
= function
144 Past.Seq
(elem
,seq
) ->
145 ctl_element keep_wit
(ctl_seq keep_wit a seq
) elem
147 | Past.SExists
(var
,seq
) -> ctl_exists keep_wit var
(ctl_seq keep_wit a seq
)
149 and ctl_element keep_wit a
= function
150 Past.Term
(term
) -> ctl_and (predmaker keep_wit term
) (ctl_ax a
)
151 | Past.Or
(seq1
,seq2
) ->
152 ctl_seqor (ctl_seq keep_wit a seq1
) (ctl_seq keep_wit a seq2
)
153 | Past.DInfo
(dots
,seq_bef
,seq_aft
) ->
155 List.fold_left
ctl_or ctl_false
156 (List.map
(ctl_element
false ctl_true) l
) in
157 let s = shortest (Common.union_set seq_bef seq_aft
) in
158 ctl_au (ctl_and (guard_ctl_dots keep_wit dots
) (ctl_not s))
160 | Past.EExists
(var
,elem
) ->
161 ctl_exists keep_wit var
(ctl_element keep_wit a elem
)
163 (* --------------------------------------------------------------------- *)
165 and guard_ctl_seq keep_wit
= function
166 Past.Seq
(elem
,Past.Empty
) -> guard_ctl_element keep_wit elem
167 | Past.Seq
(elem
,seq
) ->
168 ctl_element keep_wit
(guard_ctl_seq keep_wit seq
) elem
169 | Past.Empty
-> ctl_true
170 | Past.SExists
(var
,seq
) ->
171 ctl_exists keep_wit var
(guard_ctl_seq keep_wit seq
)
173 and guard_ctl_element keep_wit
= function
174 Past.Term
(term
) -> predmaker keep_wit term
175 | Past.Or
(seq1
,seq2
) ->
176 ctl_seqor (guard_ctl_seq keep_wit seq1
) (guard_ctl_seq keep_wit seq2
)
177 | Past.DInfo
(dots
,seq_bef
,seq_aft
) ->
179 List.fold_left
ctl_or ctl_false
180 (List.map
(ctl_element
false ctl_true) l
) in
181 let s = shortest (Common.union_set seq_bef seq_aft
) in
182 let aft = ctl_or s exit in
183 ctl_au (ctl_and (guard_ctl_dots keep_wit dots
) (ctl_not s))
184 (shortest seq_aft
) aft
185 | Past.EExists
(var
,elem
) ->
186 ctl_exists keep_wit var
(guard_ctl_element keep_wit elem
)
188 and guard_ctl_dots keep_wit
= function
189 Past.Dots
-> ctl_true
190 | Past.Nest
(_
) when not keep_wit
-> ctl_true
192 ctl_or (guard_ctl_seq
true seq
) (ctl_not (guard_ctl_seq
false seq
))
193 | Past.When
(dots
,seq
) ->
195 (guard_ctl_dots keep_wit dots
)
196 (ctl_not (ctl_seq false ctl_true seq
))
197 | Past.DExists
(var
,dots
) ->
198 ctl_exists keep_wit var
(guard_ctl_dots keep_wit dots
)
200 (* --------------------------------------------------------------------- *)
202 let toctl sl
= ctl_seq true ctl_true sl