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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
27 #
0 "./simple_assignments.ml"
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.
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.
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.
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/>.
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
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
60 (* find assignments that can match an initialization *)
62 let pure_mcodekind = function
65 (Ast.NOTHING
,_
,_
) -> true
69 let is_simple_assign left op
=
70 (match Ast0.unwrap left
with
71 Ast0.Ident
(_
) | Ast0.MetaExpr
(_
,_
,_
,_
,_
) -> true
74 ((Ast0.unwrap_mcode op
) = Ast.SimpleAssign
)
76 let is_simple_ast_assign left op minus_left
=
77 (match Ast.unwrap left
with
79 | Ast.MetaExpr
(name
,_
,_
,_
,_
,_
) ->
80 (match Ast0.unwrap minus_left
with
81 Ast0.MetaExpr
(name1
,_
,_
,_
,_
) ->
82 Ast.unwrap_mcode name
= Ast0.unwrap_mcode name1
86 ((Ast.unwrap_mcode op
) = Ast.SimpleAssign
)
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");
96 let rebuild e1 left right op simple
=
97 Ast0.rewrap e1
(Ast0.Assignment
(left
,op
,right
,simple
))
100 match Ast0.unwrap e1
with
101 Ast0.Assignment
(left
,op
,right
,_
) ->
102 if is_simple_assign left op
104 (if !Flag.sgrep_mode2
105 then rebuild e1 left right op
true
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")
120 (pure_mcodekind m
) &&
121 (pure_mcodekind (Ast0.get_mcodekind left
)) &&
122 (pure_mcodekind (Ast0.get_mcode_mcodekind op
)) in
124 then warning e1
"not pure"
125 else rebuild e1 left right op
pure)
127 | Ast0.DisjExpr
(lp
,exps
,mids
,rp
) ->
130 (lp
,List.map
(function x
-> exp (Ast0.get_mcodekind x
) x
) exps
,
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
))
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
))
145 {V0.rebuilder_functions
with VT0.rebuilder_stmtfn
= statement} in
146 List.map
fn.VT0.rebuilder_rec_top_level l