367f5f104ec2be357b6921bb1faf5579b0369dbc
[bpt/coccinelle.git] / parsing_cocci / simple_assignments.ml
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
25 module Ast0 = Ast0_cocci
26 module Ast = Ast_cocci
27 module V0 = Visitor_ast0
28 module VT0 = Visitor_ast0_types
29
30 (* find assignments that can match an initialization *)
31
32 let pure_mcodekind = function
33 Ast0.CONTEXT(mc) ->
34 (match !mc with
35 (Ast.NOTHING,_,_) -> true
36 | _ -> false)
37 | _ -> false
38
39 let is_simple_assign left op =
40 (match Ast0.unwrap left with
41 Ast0.Ident(_) | Ast0.MetaExpr(_,_,_,_,_) -> true
42 | _ -> false)
43 &&
44 ((Ast0.unwrap_mcode op) = Ast.SimpleAssign)
45
46 let is_simple_ast_assign left op minus_left =
47 (match Ast.unwrap left with
48 Ast.Ident(_) -> true
49 | Ast.MetaExpr(name,_,_,_,_,_) ->
50 (match Ast0.unwrap minus_left with
51 Ast0.MetaExpr(name1,_,_,_,_) ->
52 Ast.unwrap_mcode name = Ast0.unwrap_mcode name1
53 | _ -> false)
54 | _ -> false)
55 &&
56 ((Ast.unwrap_mcode op) = Ast.SimpleAssign)
57
58 let warning e msg =
59 Common.pr2
60 ("the simple assignment expression on line "^
61 (string_of_int (Ast0.get_line e))^
62 " contains transformations\n"^
63 "that prevent it from matching a declaration ("^msg^")\n");
64 e
65
66 let rebuild e1 left right op simple =
67 Ast0.rewrap e1 (Ast0.Assignment(left,op,right,simple))
68
69 let rec exp mc e1 =
70 match Ast0.unwrap e1 with
71 Ast0.Assignment(left,op,right,_) ->
72 if is_simple_assign left op
73 then
74 (if !Flag.sgrep_mode2
75 then rebuild e1 left right op true
76 else
77 match mc with
78 Ast0.MINUS(mc) ->
79 (match !mc with
80 ([[Ast.ExpressionTag(e2)]],_) ->
81 (match Ast.unwrap e2 with
82 Ast.Assignment(left',op',_,_) ->
83 if is_simple_ast_assign left' op' left
84 then rebuild e1 left right op true
85 else warning e1 "replacement is not simple"
86 | _ -> warning e1 "replacement is not an assignment")
87 | _ -> warning e1 "multiple replacements")
88 | m ->
89 let pure =
90 (pure_mcodekind m) &&
91 (pure_mcodekind (Ast0.get_mcodekind left)) &&
92 (pure_mcodekind (Ast0.get_mcode_mcodekind op)) in
93 if not pure
94 then warning e1 "not pure"
95 else rebuild e1 left right op pure)
96 else e1
97 | Ast0.DisjExpr(lp,exps,mids,rp) ->
98 Ast0.rewrap e1
99 (Ast0.DisjExpr
100 (lp,List.map (function x -> exp (Ast0.get_mcodekind x) x) exps,
101 mids,rp))
102 | Ast0.OptExp(e) ->
103 Ast0.rewrap e1 (Ast0.OptExp(exp (Ast0.get_mcodekind e) e))
104 | Ast0.UniqueExp(e) ->
105 Ast0.rewrap e1 (Ast0.UniqueExp(exp (Ast0.get_mcodekind e) e))
106 | _ -> e1
107
108 let simple_assignments l =
109 let statement r k e =
110 match Ast0.unwrap e with
111 Ast0.Exp(e1) -> Ast0.rewrap e (Ast0.Exp(exp (Ast0.get_mcodekind e) e1))
112 | _ -> k e in
113 let fn =
114 V0.rebuilder
115 {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in
116 List.map fn.VT0.rebuilder_rec_top_level l