Coccinelle release 1.0.0-rc13
[bpt/coccinelle.git] / parsing_cocci / safe_for_multi_decls.ml
CommitLineData
f537ebc4 1(*
17ba0788
C
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
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.
9 *
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.
d6ce1786
C
13 *
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.
18 *
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/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27# 0 "./safe_for_multi_decls.ml"
28(*
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.
36 *
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.
f537ebc4
C
40 *
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.
45 *
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/>.
48 *
49 * The authors reserve the right to distribute this or future versions of
50 * Coccinelle under other licenses.
51 *)
52
53
feec80c3 54# 0 "./safe_for_multi_decls.ml"
690d68d1
C
55(* This phase sets the safe_for_multi_decls field, which is normally false,
56to true for transformations on declarations where the only change is on the
57declared variable. This is the only kind of change on such a declaration
58that can safely be done without splitting the declaration. *)
59
60module Ast = Ast_cocci
61module V = Visitor_ast
62
63let mcode _ (_,_,kind,_) =
64 match kind with
65 Ast.MINUS(_,_,_,_) -> true
66 | Ast.PLUS _ -> failwith "not possible"
67 | Ast.CONTEXT(_,info) -> not (info = Ast.NOTHING)
68
69let contains_modif =
70 let bind x y = x or y in
71 let option_default = false in
72 let do_nothing r k e = k e in
73 let rule_elem r k re =
74 let res = k re in
75 match Ast.unwrap re with
76 Ast.FunHeader(bef,_,fninfo,name,lp,params,rp) ->
8f657093
C
77 bind (mcode r ((),(),bef,[])) res
78 | Ast.Decl(bef,_,decl) -> bind (mcode r ((),(),bef,[])) res
690d68d1
C
79 | _ -> res in
80 let init r k i =
81 let res = k i in
82 match Ast.unwrap i with
83 Ast.StrInitList(allminus,_,_,_,_) -> allminus or res
84 | _ -> res in
85 let recursor =
86 V.combiner bind option_default
87 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
88 do_nothing do_nothing do_nothing do_nothing do_nothing
89 do_nothing do_nothing do_nothing do_nothing init do_nothing
90 do_nothing rule_elem do_nothing do_nothing do_nothing do_nothing in
91 recursor.V.combiner_fullType
92
93let decl r k e =
94 let e = k e in
95 match Ast.unwrap e with
96 Ast.Init(stg,ty,_,_,_,sem)
97 | Ast.UnInit(stg,ty,_,sem) ->
98 let stg_modif =
99 match stg with
100 Some stg -> mcode () stg
101 | None -> false in
102 let ft_modif = contains_modif ty in
103 let sem_modif = mcode () sem in
104 if not(stg_modif or ft_modif or sem_modif)
105 then {e with Ast.safe_for_multi_decls = true}
106 else e
107 | _ -> e
108let mcode e = e
109let donothing r k e = k e
110
111let process =
112 let fn = V.rebuilder
113 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
114 donothing donothing donothing donothing donothing
115 donothing donothing donothing donothing
116 donothing donothing decl donothing
117 donothing donothing donothing donothing in
118 List.map fn.V.rebuilder_top_level
119
120let safe_for_multi_decls rules =
121 List.map
122 (function (mv,r) ->
123 (mv,
124 match r with
125 Ast.ScriptRule _
126 | Ast.InitialScriptRule _ | Ast.FinalScriptRule _ -> r
127 | Ast.CocciRule (nm, rule_info, r, is_exp,ruletype) ->
128 Ast.CocciRule(nm, rule_info,process r,is_exp,ruletype)))
129 rules