Release coccinelle-0.2.3rc3
[bpt/coccinelle.git] / parsing_cocci / comm_assoc.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 (* searches for E op ..., for any commutative and associative binary
26 operator. When this satisfies the isomorphism conditions (ie all minus, or
27 context for the op and ...), then this is converted to Nested(E,op).
28 Nested is not used before this phase. *)
29
30 module Ast = Ast_cocci
31 module Ast0 = Ast0_cocci
32 module V0 = Visitor_ast0
33 module VT0 = Visitor_ast0_types
34
35 let comm_assoc =
36 [Ast.Arith(Ast.Plus);Ast.Arith(Ast.Mul);Ast.Arith(Ast.And);Ast.Arith(Ast.Or);
37 Ast.Logical(Ast.AndLog);Ast.Logical(Ast.OrLog)]
38
39 let is_minus e =
40 match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false
41
42 let is_context e =
43 !Flag.sgrep_mode2 or (* everything is context for sgrep *)
44 (match Ast0.get_mcodekind e with
45 Ast0.CONTEXT(cell) -> true
46 | _ -> false)
47
48 let nopos mc =
49 match Ast0.get_pos mc with Ast0.MetaPos _ -> false | Ast0.NoMetaPos -> true
50
51 let process_binops rule_name =
52 let expr r k e1 =
53 let e = k e1 in
54 match Ast0.unwrap e with
55 Ast0.Binary(left,op,right)
56 when List.mem (Ast0.unwrap_mcode op) comm_assoc ->
57 (match Ast0.unwrap right with
58 Ast0.Edots(d,None) ->
59 if (is_minus e || (is_context e && is_context right))
60 && nopos op && nopos d
61 (* keep dots to record required modif *)
62 then Ast0.rewrap e (Ast0.Nested(left,op,right))
63 else
64 (Printf.printf
65 "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name;
66 Unparse_ast0.expression e1;
67 Format.print_newline();
68 e)
69 | Ast0.Edots(d,_) ->
70 (Printf.printf
71 "%s: whencode interferes with comm_assoc iso" rule_name;
72 Unparse_ast0.expression e1;
73 Format.print_newline();
74 e)
75 | _ -> e)
76 | _ -> e in
77 V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr}
78
79 let comm_assoc rule rule_name dropped_isos =
80 if List.mem "comm_assoc" dropped_isos
81 then rule
82 else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule