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 module Ast0
= Ast0_cocci
28 module Ast
= Ast_cocci
29 module V0
= Visitor_ast0
30 module VT0
= Visitor_ast0_types
32 (* find assignments that can match an initialization *)
34 let pure_mcodekind = function
37 (Ast.NOTHING
,_
,_
) -> true
41 let is_simple_assign left op
=
42 (match Ast0.unwrap left
with
43 Ast0.Ident
(_
) | Ast0.MetaExpr
(_
,_
,_
,_
,_
) -> true
46 ((Ast0.unwrap_mcode op
) = Ast.SimpleAssign
)
48 let is_simple_ast_assign left op minus_left
=
49 (match Ast.unwrap left
with
51 | Ast.MetaExpr
(name
,_
,_
,_
,_
,_
) ->
52 (match Ast0.unwrap minus_left
with
53 Ast0.MetaExpr
(name1
,_
,_
,_
,_
) ->
54 Ast.unwrap_mcode name
= Ast0.unwrap_mcode name1
58 ((Ast.unwrap_mcode op
) = Ast.SimpleAssign
)
62 ("the simple assignment expression on line "^
63 (string_of_int
(Ast0.get_line e
))^
64 " contains transformations\n"^
65 "that prevent it from matching a declaration ("^msg^
")\n");
68 let rebuild e1 left right op simple
=
69 Ast0.rewrap e1
(Ast0.Assignment
(left
,op
,right
,simple
))
72 match Ast0.unwrap e1
with
73 Ast0.Assignment
(left
,op
,right
,_
) ->
74 if is_simple_assign left op
77 then rebuild e1 left right op
true
82 (Ast.REPLACEMENT
([[Ast.ExpressionTag
(e2
)]],_
),_
) ->
83 (match Ast.unwrap e2
with
84 Ast.Assignment
(left'
,op'
,_
,_
) ->
85 if is_simple_ast_assign left' op' left
86 then rebuild e1 left right op
true
87 else warning e1
"replacement is not simple"
88 | _
-> warning e1
"replacement is not an assignment")
89 | _
-> warning e1
"multiple replacements")
93 (pure_mcodekind (Ast0.get_mcodekind left
)) &&
94 (pure_mcodekind (Ast0.get_mcode_mcodekind op
)) in
96 then warning e1
"not pure"
97 else rebuild e1 left right op
pure)
99 | Ast0.DisjExpr
(lp
,exps
,mids
,rp
) ->
102 (lp
,List.map
(function x
-> exp (Ast0.get_mcodekind x
) x
) exps
,
105 Ast0.rewrap e1
(Ast0.OptExp
(exp (Ast0.get_mcodekind e
) e
))
106 | Ast0.UniqueExp
(e
) ->
107 Ast0.rewrap e1
(Ast0.UniqueExp
(exp (Ast0.get_mcodekind e
) e
))
110 let simple_assignments l
=
111 let statement r k e
=
112 match Ast0.unwrap e
with
113 Ast0.Exp
(e1
) -> Ast0.rewrap e
(Ast0.Exp
(exp (Ast0.get_mcodekind e
) e1
))
117 {V0.rebuilder_functions
with VT0.rebuilder_stmtfn
= statement} in
118 List.map
fn.VT0.rebuilder_rec_top_level l