2 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
45 module Ast0
= Ast0_cocci
46 module Ast
= Ast_cocci
47 module V0
= Visitor_ast0
48 module VT0
= Visitor_ast0_types
50 (* find assignments that can match an initialization *)
52 let pure_mcodekind = function
55 (Ast.NOTHING
,_
,_
) -> true
59 let is_simple_assign left op
=
60 (match Ast0.unwrap left
with
61 Ast0.Ident
(_
) | Ast0.MetaExpr
(_
,_
,_
,_
,_
) -> true
64 ((Ast0.unwrap_mcode op
) = Ast.SimpleAssign
)
66 let is_simple_ast_assign left op minus_left
=
67 (match Ast.unwrap left
with
69 | Ast.MetaExpr
(name
,_
,_
,_
,_
,_
) ->
70 (match Ast0.unwrap minus_left
with
71 Ast0.MetaExpr
(name1
,_
,_
,_
,_
) ->
72 Ast.unwrap_mcode name
= Ast0.unwrap_mcode name1
76 ((Ast.unwrap_mcode op
) = Ast.SimpleAssign
)
80 ("the simple assignment expression on line "^
81 (string_of_int
(Ast0.get_line e
))^
82 " contains transformations\n"^
83 "that prevent it from matching a declaration ("^msg^
")\n");
86 let rebuild e1 left right op simple
=
87 Ast0.rewrap e1
(Ast0.Assignment
(left
,op
,right
,simple
))
90 match Ast0.unwrap e1
with
91 Ast0.Assignment
(left
,op
,right
,_
) ->
92 if is_simple_assign left op
95 then rebuild e1 left right op
true
100 ([[Ast.ExpressionTag
(e2
)]],_
) ->
101 (match Ast.unwrap e2
with
102 Ast.Assignment
(left'
,op'
,_
,_
) ->
103 if is_simple_ast_assign left' op' left
104 then rebuild e1 left right op
true
105 else warning e1
"replacement is not simple"
106 | _
-> warning e1
"replacement is not an assignment")
107 | _
-> warning e1
"multiple replacements")
110 (pure_mcodekind m
) &&
111 (pure_mcodekind (Ast0.get_mcodekind left
)) &&
112 (pure_mcodekind (Ast0.get_mcode_mcodekind op
)) in
114 then warning e1
"not pure"
115 else rebuild e1 left right op
pure)
117 | Ast0.DisjExpr
(lp
,exps
,mids
,rp
) ->
120 (lp
,List.map
(function x
-> exp (Ast0.get_mcodekind x
) x
) exps
,
123 Ast0.rewrap e1
(Ast0.OptExp
(exp (Ast0.get_mcodekind e
) e
))
124 | Ast0.UniqueExp
(e
) ->
125 Ast0.rewrap e1
(Ast0.UniqueExp
(exp (Ast0.get_mcodekind e
) e
))
128 let simple_assignments l
=
129 let statement r k e
=
130 match Ast0.unwrap e
with
131 Ast0.Exp
(e1
) -> Ast0.rewrap e
(Ast0.Exp
(exp (Ast0.get_mcodekind e
) e1
))
135 {V0.rebuilder_functions
with VT0.rebuilder_stmtfn
= statement} in
136 List.map
fn.VT0.rebuilder_rec_top_level l