Release coccinelle-0.2.4
[bpt/coccinelle.git] / parsing_cocci / simple_assignments.ml
CommitLineData
9bc82bae
C
1(*
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.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
c491d8ee
C
25(*
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.
31 *
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.
35 *
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.
40 *
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/>.
43 *
44 * The authors reserve the right to distribute this or future versions of
45 * Coccinelle under other licenses.
46 *)
47
48
34e49164
C
49module Ast0 = Ast0_cocci
50module Ast = Ast_cocci
51module V0 = Visitor_ast0
b1b2de81 52module VT0 = Visitor_ast0_types
34e49164
C
53
54(* find assignments that can match an initialization *)
55
56let pure_mcodekind = function
57 Ast0.CONTEXT(mc) ->
58 (match !mc with
59 (Ast.NOTHING,_,_) -> true
60 | _ -> false)
61 | _ -> false
62
63let is_simple_assign left op =
64 (match Ast0.unwrap left with
65 Ast0.Ident(_) | Ast0.MetaExpr(_,_,_,_,_) -> true
66 | _ -> false)
67 &&
68 ((Ast0.unwrap_mcode op) = Ast.SimpleAssign)
69
70let is_simple_ast_assign left op minus_left =
71 (match Ast.unwrap left with
72 Ast.Ident(_) -> true
73 | Ast.MetaExpr(name,_,_,_,_,_) ->
74 (match Ast0.unwrap minus_left with
75 Ast0.MetaExpr(name1,_,_,_,_) ->
76 Ast.unwrap_mcode name = Ast0.unwrap_mcode name1
77 | _ -> false)
78 | _ -> false)
79 &&
80 ((Ast.unwrap_mcode op) = Ast.SimpleAssign)
81
82let warning e msg =
83 Common.pr2
84 ("the simple assignment expression on line "^
85 (string_of_int (Ast0.get_line e))^
86 " contains transformations\n"^
87 "that prevent it from matching a declaration ("^msg^")\n");
88 e
89
90let rebuild e1 left right op simple =
91 Ast0.rewrap e1 (Ast0.Assignment(left,op,right,simple))
92
93let rec exp mc e1 =
94 match Ast0.unwrap e1 with
95 Ast0.Assignment(left,op,right,_) ->
96 if is_simple_assign left op
97 then
98 (if !Flag.sgrep_mode2
99 then rebuild e1 left right op true
100 else
101 match mc with
102 Ast0.MINUS(mc) ->
103 (match !mc with
104 ([[Ast.ExpressionTag(e2)]],_) ->
105 (match Ast.unwrap e2 with
106 Ast.Assignment(left',op',_,_) ->
107 if is_simple_ast_assign left' op' left
108 then rebuild e1 left right op true
109 else warning e1 "replacement is not simple"
110 | _ -> warning e1 "replacement is not an assignment")
111 | _ -> warning e1 "multiple replacements")
112 | m ->
113 let pure =
114 (pure_mcodekind m) &&
115 (pure_mcodekind (Ast0.get_mcodekind left)) &&
116 (pure_mcodekind (Ast0.get_mcode_mcodekind op)) in
117 if not pure
118 then warning e1 "not pure"
119 else rebuild e1 left right op pure)
120 else e1
121 | Ast0.DisjExpr(lp,exps,mids,rp) ->
122 Ast0.rewrap e1
123 (Ast0.DisjExpr
124 (lp,List.map (function x -> exp (Ast0.get_mcodekind x) x) exps,
125 mids,rp))
126 | Ast0.OptExp(e) ->
127 Ast0.rewrap e1 (Ast0.OptExp(exp (Ast0.get_mcodekind e) e))
128 | Ast0.UniqueExp(e) ->
129 Ast0.rewrap e1 (Ast0.UniqueExp(exp (Ast0.get_mcodekind e) e))
130 | _ -> e1
131
132let simple_assignments l =
34e49164
C
133 let statement r k e =
134 match Ast0.unwrap e with
135 Ast0.Exp(e1) -> Ast0.rewrap e (Ast0.Exp(exp (Ast0.get_mcodekind e) e1))
136 | _ -> k e in
137 let fn =
138 V0.rebuilder
b1b2de81
C
139 {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in
140 List.map fn.VT0.rebuilder_rec_top_level l