Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / parsing_cocci / simple_assignments.ml
CommitLineData
34e49164
C
1module Ast0 = Ast0_cocci
2module Ast = Ast_cocci
3module V0 = Visitor_ast0
b1b2de81 4module VT0 = Visitor_ast0_types
34e49164
C
5
6(* find assignments that can match an initialization *)
7
8let pure_mcodekind = function
9 Ast0.CONTEXT(mc) ->
10 (match !mc with
11 (Ast.NOTHING,_,_) -> true
12 | _ -> false)
13 | _ -> false
14
15let is_simple_assign left op =
16 (match Ast0.unwrap left with
17 Ast0.Ident(_) | Ast0.MetaExpr(_,_,_,_,_) -> true
18 | _ -> false)
19 &&
20 ((Ast0.unwrap_mcode op) = Ast.SimpleAssign)
21
22let is_simple_ast_assign left op minus_left =
23 (match Ast.unwrap left with
24 Ast.Ident(_) -> true
25 | Ast.MetaExpr(name,_,_,_,_,_) ->
26 (match Ast0.unwrap minus_left with
27 Ast0.MetaExpr(name1,_,_,_,_) ->
28 Ast.unwrap_mcode name = Ast0.unwrap_mcode name1
29 | _ -> false)
30 | _ -> false)
31 &&
32 ((Ast.unwrap_mcode op) = Ast.SimpleAssign)
33
34let warning e msg =
35 Common.pr2
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");
40 e
41
42let rebuild e1 left right op simple =
43 Ast0.rewrap e1 (Ast0.Assignment(left,op,right,simple))
44
45let rec exp mc e1 =
46 match Ast0.unwrap e1 with
47 Ast0.Assignment(left,op,right,_) ->
48 if is_simple_assign left op
49 then
50 (if !Flag.sgrep_mode2
51 then rebuild e1 left right op true
52 else
53 match mc with
54 Ast0.MINUS(mc) ->
55 (match !mc with
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")
64 | m ->
65 let pure =
66 (pure_mcodekind m) &&
67 (pure_mcodekind (Ast0.get_mcodekind left)) &&
68 (pure_mcodekind (Ast0.get_mcode_mcodekind op)) in
69 if not pure
70 then warning e1 "not pure"
71 else rebuild e1 left right op pure)
72 else e1
73 | Ast0.DisjExpr(lp,exps,mids,rp) ->
74 Ast0.rewrap e1
75 (Ast0.DisjExpr
76 (lp,List.map (function x -> exp (Ast0.get_mcodekind x) x) exps,
77 mids,rp))
78 | Ast0.OptExp(e) ->
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))
82 | _ -> e1
83
84let simple_assignments l =
34e49164
C
85 let statement r k e =
86 match Ast0.unwrap e with
87 Ast0.Exp(e1) -> Ast0.rewrap e (Ast0.Exp(exp (Ast0.get_mcodekind e) e1))
88 | _ -> k e in
89 let fn =
90 V0.rebuilder
b1b2de81
C
91 {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in
92 List.map fn.VT0.rebuilder_rec_top_level l