Commit | Line | Data |
---|---|---|
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. | |
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 | ||
34e49164 C |
27 | module Ast0 = Ast0_cocci |
28 | module Ast = Ast_cocci | |
29 | module V0 = Visitor_ast0 | |
b1b2de81 | 30 | module VT0 = Visitor_ast0_types |
34e49164 C |
31 | |
32 | (* find assignments that can match an initialization *) | |
33 | ||
34 | let pure_mcodekind = function | |
35 | Ast0.CONTEXT(mc) -> | |
36 | (match !mc with | |
37 | (Ast.NOTHING,_,_) -> true | |
38 | | _ -> false) | |
39 | | _ -> false | |
40 | ||
41 | let is_simple_assign left op = | |
42 | (match Ast0.unwrap left with | |
43 | Ast0.Ident(_) | Ast0.MetaExpr(_,_,_,_,_) -> true | |
44 | | _ -> false) | |
45 | && | |
46 | ((Ast0.unwrap_mcode op) = Ast.SimpleAssign) | |
47 | ||
48 | let is_simple_ast_assign left op minus_left = | |
49 | (match Ast.unwrap left with | |
50 | Ast.Ident(_) -> true | |
51 | | Ast.MetaExpr(name,_,_,_,_,_) -> | |
52 | (match Ast0.unwrap minus_left with | |
53 | Ast0.MetaExpr(name1,_,_,_,_) -> | |
54 | Ast.unwrap_mcode name = Ast0.unwrap_mcode name1 | |
55 | | _ -> false) | |
56 | | _ -> false) | |
57 | && | |
58 | ((Ast.unwrap_mcode op) = Ast.SimpleAssign) | |
59 | ||
60 | let warning e msg = | |
61 | Common.pr2 | |
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"); | |
66 | e | |
67 | ||
68 | let rebuild e1 left right op simple = | |
69 | Ast0.rewrap e1 (Ast0.Assignment(left,op,right,simple)) | |
70 | ||
71 | let rec exp mc e1 = | |
72 | match Ast0.unwrap e1 with | |
73 | Ast0.Assignment(left,op,right,_) -> | |
74 | if is_simple_assign left op | |
75 | then | |
76 | (if !Flag.sgrep_mode2 | |
77 | then rebuild e1 left right op true | |
78 | else | |
79 | match mc with | |
80 | Ast0.MINUS(mc) -> | |
81 | (match !mc with | |
8babbc8f | 82 | (Ast.REPLACEMENT([[Ast.ExpressionTag(e2)]],_),_) -> |
34e49164 C |
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") | |
90 | | m -> | |
91 | let pure = | |
92 | (pure_mcodekind m) && | |
93 | (pure_mcodekind (Ast0.get_mcodekind left)) && | |
94 | (pure_mcodekind (Ast0.get_mcode_mcodekind op)) in | |
95 | if not pure | |
96 | then warning e1 "not pure" | |
97 | else rebuild e1 left right op pure) | |
98 | else e1 | |
99 | | Ast0.DisjExpr(lp,exps,mids,rp) -> | |
100 | Ast0.rewrap e1 | |
101 | (Ast0.DisjExpr | |
102 | (lp,List.map (function x -> exp (Ast0.get_mcodekind x) x) exps, | |
103 | mids,rp)) | |
104 | | Ast0.OptExp(e) -> | |
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)) | |
108 | | _ -> e1 | |
109 | ||
110 | let simple_assignments l = | |
34e49164 C |
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)) | |
114 | | _ -> k e in | |
115 | let fn = | |
116 | V0.rebuilder | |
b1b2de81 C |
117 | {V0.rebuilder_functions with VT0.rebuilder_stmtfn = statement} in |
118 | List.map fn.VT0.rebuilder_rec_top_level l |