1 module Ast0
= Ast0_cocci
3 module V0
= Visitor_ast0
4 module VT0
= Visitor_ast0_types
6 (* find assignments that can match an initialization *)
8 let pure_mcodekind = function
11 (Ast.NOTHING
,_
,_
) -> true
15 let is_simple_assign left op
=
16 (match Ast0.unwrap left
with
17 Ast0.Ident
(_
) | Ast0.MetaExpr
(_
,_
,_
,_
,_
) -> true
20 ((Ast0.unwrap_mcode op
) = Ast.SimpleAssign
)
22 let is_simple_ast_assign left op minus_left
=
23 (match Ast.unwrap left
with
25 | Ast.MetaExpr
(name
,_
,_
,_
,_
,_
) ->
26 (match Ast0.unwrap minus_left
with
27 Ast0.MetaExpr
(name1
,_
,_
,_
,_
) ->
28 Ast.unwrap_mcode name
= Ast0.unwrap_mcode name1
32 ((Ast.unwrap_mcode op
) = Ast.SimpleAssign
)
36 ("the simple assignment expression on line "^
37 (string_of_int
(Ast0.get_line e
))^
38 " contains transformations\n"^
39 "that prevent it from matching a declaration ("^msg^
")\n");
42 let rebuild e1 left right op simple
=
43 Ast0.rewrap e1
(Ast0.Assignment
(left
,op
,right
,simple
))
46 match Ast0.unwrap e1
with
47 Ast0.Assignment
(left
,op
,right
,_
) ->
48 if is_simple_assign left op
51 then rebuild e1 left right op
true
56 ([[Ast.ExpressionTag
(e2
)]],_
) ->
57 (match Ast.unwrap e2
with
58 Ast.Assignment
(left'
,op'
,_
,_
) ->
59 if is_simple_ast_assign left' op' left
60 then rebuild e1 left right op
true
61 else warning e1
"replacement is not simple"
62 | _
-> warning e1
"replacement is not an assignment")
63 | _
-> warning e1
"multiple replacements")
67 (pure_mcodekind (Ast0.get_mcodekind left
)) &&
68 (pure_mcodekind (Ast0.get_mcode_mcodekind op
)) in
70 then warning e1
"not pure"
71 else rebuild e1 left right op
pure)
73 | Ast0.DisjExpr
(lp
,exps
,mids
,rp
) ->
76 (lp
,List.map
(function x
-> exp (Ast0.get_mcodekind x
) x
) exps
,
79 Ast0.rewrap e1
(Ast0.OptExp
(exp (Ast0.get_mcodekind e
) e
))
80 | Ast0.UniqueExp
(e
) ->
81 Ast0.rewrap e1
(Ast0.UniqueExp
(exp (Ast0.get_mcodekind e
) e
))
84 let simple_assignments l
=
86 match Ast0.unwrap e
with
87 Ast0.Exp
(e1
) -> Ast0.rewrap e
(Ast0.Exp
(exp (Ast0.get_mcodekind e
) e1
))
91 {V0.rebuilder_functions
with VT0.rebuilder_stmtfn
= statement} in
92 List.map
fn.VT0.rebuilder_rec_top_level l