Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / parsing_cocci / comm_assoc.ml
1 (*
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
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
27 (* searches for E op ..., for any commutative and associative binary
28 operator. When this satisfies the isomorphism conditions (ie all minus, or
29 context for the op and ...), then this is converted to Nested(E,op).
30 Nested is not used before this phase. *)
31
32 module Ast = Ast_cocci
33 module Ast0 = Ast0_cocci
34 module V0 = Visitor_ast0
35 module VT0 = Visitor_ast0_types
36
37 let comm_assoc =
38 [Ast.Arith(Ast.Plus);Ast.Arith(Ast.Mul);Ast.Arith(Ast.And);Ast.Arith(Ast.Or);
39 Ast.Logical(Ast.AndLog);Ast.Logical(Ast.OrLog)]
40
41 let is_minus e =
42 match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false
43
44 let is_context e =
45 !Flag.sgrep_mode2 or (* everything is context for sgrep *)
46 (match Ast0.get_mcodekind e with
47 Ast0.CONTEXT(cell) -> true
48 | _ -> false)
49
50 let nopos mc = (Ast0.get_pos mc) = []
51
52 let process_binops rule_name =
53 let expr r k e1 =
54 let e = k e1 in
55 match Ast0.unwrap e with
56 Ast0.Binary(left,op,right)
57 when List.mem (Ast0.unwrap_mcode op) comm_assoc ->
58 (match Ast0.unwrap right with
59 Ast0.Edots(d,None) ->
60 if (is_minus e || (is_context e && is_context right))
61 && nopos op && nopos d
62 (* keep dots to record required modif *)
63 then Ast0.rewrap e (Ast0.Nested(left,op,right))
64 else
65 (Printf.printf
66 "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name;
67 Unparse_ast0.expression e1;
68 Format.print_newline();
69 e)
70 | Ast0.Edots(d,_) ->
71 (Printf.printf
72 "%s: whencode interferes with comm_assoc iso" rule_name;
73 Unparse_ast0.expression e1;
74 Format.print_newline();
75 e)
76 | _ -> e)
77 | _ -> e in
78 V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr}
79
80 let comm_assoc rule rule_name dropped_isos =
81 if List.mem "comm_assoc" dropped_isos
82 then rule
83 else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule