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