Coccinelle release 1.0.0-rc13
[bpt/coccinelle.git] / parsing_cocci / simple_assignments.ml
1 (*
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.
9 *
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.
13 *
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.
18 *
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/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27 # 0 "./simple_assignments.ml"
28 (*
29 * Copyright 2012, INRIA
30 * Julia Lawall, Gilles Muller
31 * Copyright 2010-2011, INRIA, University of Copenhagen
32 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
33 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
34 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
35 * This file is part of Coccinelle.
36 *
37 * Coccinelle is free software: you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation, according to version 2 of the License.
40 *
41 * Coccinelle is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License
47 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
48 *
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
51 *)
52
53
54 # 0 "./simple_assignments.ml"
55 module Ast0 = Ast0_cocci
56 module Ast = Ast_cocci
57 module V0 = Visitor_ast0
58 module VT0 = Visitor_ast0_types
59
60 (* find assignments that can match an initialization *)
61
62 let pure_mcodekind = function
63 Ast0.CONTEXT(mc) ->
64 (match !mc with
65 (Ast.NOTHING,_,_) -> true
66 | _ -> false)
67 | _ -> false
68
69 let is_simple_assign left op =
70 (match Ast0.unwrap left with
71 Ast0.Ident(_) | Ast0.MetaExpr(_,_,_,_,_) -> true
72 | _ -> false)
73 &&
74 ((Ast0.unwrap_mcode op) = Ast.SimpleAssign)
75
76 let is_simple_ast_assign left op minus_left =
77 (match Ast.unwrap left with
78 Ast.Ident(_) -> true
79 | Ast.MetaExpr(name,_,_,_,_,_) ->
80 (match Ast0.unwrap minus_left with
81 Ast0.MetaExpr(name1,_,_,_,_) ->
82 Ast.unwrap_mcode name = Ast0.unwrap_mcode name1
83 | _ -> false)
84 | _ -> false)
85 &&
86 ((Ast.unwrap_mcode op) = Ast.SimpleAssign)
87
88 let warning e msg =
89 Common.pr2
90 ("the simple assignment expression on line "^
91 (string_of_int (Ast0.get_line e))^
92 " contains transformations\n"^
93 "that prevent it from matching a declaration ("^msg^")\n");
94 e
95
96 let rebuild e1 left right op simple =
97 Ast0.rewrap e1 (Ast0.Assignment(left,op,right,simple))
98
99 let rec exp mc e1 =
100 match Ast0.unwrap e1 with
101 Ast0.Assignment(left,op,right,_) ->
102 if is_simple_assign left op
103 then
104 (if !Flag.sgrep_mode2
105 then rebuild e1 left right op true
106 else
107 match mc with
108 Ast0.MINUS(mc) ->
109 (match !mc with
110 (Ast.REPLACEMENT([[Ast.ExpressionTag(e2)]],_),_) ->
111 (match Ast.unwrap e2 with
112 Ast.Assignment(left',op',_,_) ->
113 if is_simple_ast_assign left' op' left
114 then rebuild e1 left right op true
115 else warning e1 "replacement is not simple"
116 | _ -> warning e1 "replacement is not an assignment")
117 | _ -> warning e1 "multiple replacements")
118 | m ->
119 let pure =
120 (pure_mcodekind m) &&
121 (pure_mcodekind (Ast0.get_mcodekind left)) &&
122 (pure_mcodekind (Ast0.get_mcode_mcodekind op)) in
123 if not pure
124 then warning e1 "not pure"
125 else rebuild e1 left right op pure)
126 else e1
127 | Ast0.DisjExpr(lp,exps,mids,rp) ->
128 Ast0.rewrap e1
129 (Ast0.DisjExpr
130 (lp,List.map (function x -> exp (Ast0.get_mcodekind x) x) exps,
131 mids,rp))
132 | Ast0.OptExp(e) ->
133 Ast0.rewrap e1 (Ast0.OptExp(exp (Ast0.get_mcodekind e) e))
134 | Ast0.UniqueExp(e) ->
135 Ast0.rewrap e1 (Ast0.UniqueExp(exp (Ast0.get_mcodekind e) e))
136 | _ -> e1
137
138 let simple_assignments l =
139 let statement r k e =
140 match Ast0.unwrap e with
141 Ast0.Exp(e1) -> Ast0.rewrap e (Ast0.Exp(exp (Ast0.get_mcodekind e) e1))
142 | _ -> k e in
143 let fn =
144 V0.rebuilder
145 {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in
146 List.map fn.VT0.rebuilder_rec_top_level l