Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / parsing_cocci / comm_assoc.ml
CommitLineData
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(* searches for E op ..., for any commutative and associative binary
46operator. When this satisfies the isomorphism conditions (ie all minus, or
47context for the op and ...), then this is converted to Nested(E,op).
48Nested is not used before this phase. *)
49
50module Ast = Ast_cocci
51module Ast0 = Ast0_cocci
52module V0 = Visitor_ast0
b1b2de81 53module VT0 = Visitor_ast0_types
34e49164
C
54
55let 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
59let is_minus e =
60 match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false
61
62let 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
68let nopos mc =
69 match Ast0.get_pos mc with Ast0.MetaPos _ -> false | Ast0.NoMetaPos -> true
70
71let process_binops rule_name =
34e49164
C
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)
faf9a90c 89 | Ast0.Edots(d,_) ->
34e49164
C
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
b1b2de81 97 V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr}
34e49164
C
98
99let comm_assoc rule rule_name dropped_isos =
100 if List.mem "comm_assoc" dropped_isos
101 then rule
b1b2de81 102 else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule