Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_cocci / comm_assoc.ml
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
23 (*
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.
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
45 (* searches for E op ..., for any commutative and associative binary
46 operator. When this satisfies the isomorphism conditions (ie all minus, or
47 context for the op and ...), then this is converted to Nested(E,op).
48 Nested is not used before this phase. *)
49
50 module Ast = Ast_cocci
51 module Ast0 = Ast0_cocci
52 module V0 = Visitor_ast0
53 module VT0 = Visitor_ast0_types
54
55 let comm_assoc =
56 [Ast.Arith(Ast.Plus);Ast.Arith(Ast.Mul);Ast.Arith(Ast.And);Ast.Arith(Ast.Or);
57 Ast.Logical(Ast.AndLog);Ast.Logical(Ast.OrLog)]
58
59 let is_minus e =
60 match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false
61
62 let is_context e =
63 !Flag.sgrep_mode2 or (* everything is context for sgrep *)
64 (match Ast0.get_mcodekind e with
65 Ast0.CONTEXT(cell) -> true
66 | _ -> false)
67
68 let nopos mc =
69 match Ast0.get_pos mc with Ast0.MetaPos _ -> false | Ast0.NoMetaPos -> true
70
71 let process_binops rule_name =
72 let expr r k e1 =
73 let e = k e1 in
74 match Ast0.unwrap e with
75 Ast0.Binary(left,op,right)
76 when List.mem (Ast0.unwrap_mcode op) comm_assoc ->
77 (match Ast0.unwrap right with
78 Ast0.Edots(d,None) ->
79 if (is_minus e || (is_context e && is_context right))
80 && nopos op && nopos d
81 (* keep dots to record required modif *)
82 then Ast0.rewrap e (Ast0.Nested(left,op,right))
83 else
84 (Printf.printf
85 "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name;
86 Unparse_ast0.expression e1;
87 Format.print_newline();
88 e)
89 | Ast0.Edots(d,_) ->
90 (Printf.printf
91 "%s: whencode interferes with comm_assoc iso" rule_name;
92 Unparse_ast0.expression e1;
93 Format.print_newline();
94 e)
95 | _ -> e)
96 | _ -> e in
97 V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr}
98
99 let comm_assoc rule rule_name dropped_isos =
100 if List.mem "comm_assoc" dropped_isos
101 then rule
102 else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule