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.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
25 module Ast0
= Ast0_cocci
26 module Ast
= Ast_cocci
27 module V0
= Visitor_ast0
28 module VT0
= Visitor_ast0_types
30 (* find assignments that can match an initialization *)
32 let pure_mcodekind = function
35 (Ast.NOTHING
,_
,_
) -> true
39 let is_simple_assign left op
=
40 (match Ast0.unwrap left
with
41 Ast0.Ident
(_
) | Ast0.MetaExpr
(_
,_
,_
,_
,_
) -> true
44 ((Ast0.unwrap_mcode op
) = Ast.SimpleAssign
)
46 let is_simple_ast_assign left op minus_left
=
47 (match Ast.unwrap left
with
49 | Ast.MetaExpr
(name
,_
,_
,_
,_
,_
) ->
50 (match Ast0.unwrap minus_left
with
51 Ast0.MetaExpr
(name1
,_
,_
,_
,_
) ->
52 Ast.unwrap_mcode name
= Ast0.unwrap_mcode name1
56 ((Ast.unwrap_mcode op
) = Ast.SimpleAssign
)
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");
66 let rebuild e1 left right op simple
=
67 Ast0.rewrap e1
(Ast0.Assignment
(left
,op
,right
,simple
))
70 match Ast0.unwrap e1
with
71 Ast0.Assignment
(left
,op
,right
,_
) ->
72 if is_simple_assign left op
75 then rebuild e1 left right op
true
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")
91 (pure_mcodekind (Ast0.get_mcodekind left
)) &&
92 (pure_mcodekind (Ast0.get_mcode_mcodekind op
)) in
94 then warning e1
"not pure"
95 else rebuild e1 left right op
pure)
97 | Ast0.DisjExpr
(lp
,exps
,mids
,rp
) ->
100 (lp
,List.map
(function x
-> exp (Ast0.get_mcodekind x
) x
) exps
,
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
))
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
))
115 {V0.rebuilder_functions
with VT0.rebuilder_stmtfn
= statement} in
116 List.map
fn.VT0.rebuilder_rec_top_level l