Commit | Line | Data |
---|---|---|
5636bb2c C |
1 | (* |
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. | |
5 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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/>. | |
17 | * | |
18 | * The authors reserve the right to distribute this or future versions of | |
19 | * Coccinelle under other licenses. | |
20 | *) | |
21 | ||
22 | ||
9f8e26f4 | 23 | (* |
ae4735db | 24 | * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen |
9f8e26f4 C |
25 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix |
26 | * This file is part of Coccinelle. | |
27 | * | |
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. | |
31 | * | |
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. | |
36 | * | |
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/>. | |
39 | * | |
40 | * The authors reserve the right to distribute this or future versions of | |
41 | * Coccinelle under other licenses. | |
42 | *) | |
43 | ||
44 | ||
34e49164 C |
45 | module Ast0 = Ast0_cocci |
46 | module Ast = Ast_cocci | |
47 | module V0 = Visitor_ast0 | |
b1b2de81 | 48 | module VT0 = Visitor_ast0_types |
34e49164 C |
49 | |
50 | (* find assignments that can match an initialization *) | |
51 | ||
52 | let pure_mcodekind = function | |
53 | Ast0.CONTEXT(mc) -> | |
54 | (match !mc with | |
55 | (Ast.NOTHING,_,_) -> true | |
56 | | _ -> false) | |
57 | | _ -> false | |
58 | ||
59 | let is_simple_assign left op = | |
60 | (match Ast0.unwrap left with | |
61 | Ast0.Ident(_) | Ast0.MetaExpr(_,_,_,_,_) -> true | |
62 | | _ -> false) | |
63 | && | |
64 | ((Ast0.unwrap_mcode op) = Ast.SimpleAssign) | |
65 | ||
66 | let is_simple_ast_assign left op minus_left = | |
67 | (match Ast.unwrap left with | |
68 | Ast.Ident(_) -> true | |
69 | | Ast.MetaExpr(name,_,_,_,_,_) -> | |
70 | (match Ast0.unwrap minus_left with | |
71 | Ast0.MetaExpr(name1,_,_,_,_) -> | |
72 | Ast.unwrap_mcode name = Ast0.unwrap_mcode name1 | |
73 | | _ -> false) | |
74 | | _ -> false) | |
75 | && | |
76 | ((Ast.unwrap_mcode op) = Ast.SimpleAssign) | |
77 | ||
78 | let warning e msg = | |
79 | Common.pr2 | |
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"); | |
84 | e | |
85 | ||
86 | let rebuild e1 left right op simple = | |
87 | Ast0.rewrap e1 (Ast0.Assignment(left,op,right,simple)) | |
88 | ||
89 | let rec exp mc e1 = | |
90 | match Ast0.unwrap e1 with | |
91 | Ast0.Assignment(left,op,right,_) -> | |
92 | if is_simple_assign left op | |
93 | then | |
94 | (if !Flag.sgrep_mode2 | |
95 | then rebuild e1 left right op true | |
96 | else | |
97 | match mc with | |
98 | Ast0.MINUS(mc) -> | |
99 | (match !mc with | |
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") | |
108 | | m -> | |
109 | let pure = | |
110 | (pure_mcodekind m) && | |
111 | (pure_mcodekind (Ast0.get_mcodekind left)) && | |
112 | (pure_mcodekind (Ast0.get_mcode_mcodekind op)) in | |
113 | if not pure | |
114 | then warning e1 "not pure" | |
115 | else rebuild e1 left right op pure) | |
116 | else e1 | |
117 | | Ast0.DisjExpr(lp,exps,mids,rp) -> | |
118 | Ast0.rewrap e1 | |
119 | (Ast0.DisjExpr | |
120 | (lp,List.map (function x -> exp (Ast0.get_mcodekind x) x) exps, | |
121 | mids,rp)) | |
122 | | Ast0.OptExp(e) -> | |
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)) | |
126 | | _ -> e1 | |
127 | ||
128 | let simple_assignments l = | |
34e49164 C |
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)) | |
132 | | _ -> k e in | |
133 | let fn = | |
134 | V0.rebuilder | |
b1b2de81 C |
135 | {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in |
136 | List.map fn.VT0.rebuilder_rec_top_level l |