Release coccinelle-0.2.3rc2
[bpt/coccinelle.git] / parsing_cocci / comm_assoc.ml
CommitLineData
5636bb2c 1(*
90aeb998
C
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
9f8e26f4
C
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
34e49164
C
25(* searches for E op ..., for any commutative and associative binary
26operator. When this satisfies the isomorphism conditions (ie all minus, or
27context for the op and ...), then this is converted to Nested(E,op).
28Nested is not used before this phase. *)
29
30module Ast = Ast_cocci
31module Ast0 = Ast0_cocci
32module V0 = Visitor_ast0
b1b2de81 33module VT0 = Visitor_ast0_types
34e49164
C
34
35let 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
39let is_minus e =
40 match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false
41
42let 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
48let nopos mc =
49 match Ast0.get_pos mc with Ast0.MetaPos _ -> false | Ast0.NoMetaPos -> true
50
51let process_binops rule_name =
34e49164
C
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)
faf9a90c 69 | Ast0.Edots(d,_) ->
34e49164
C
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
b1b2de81 77 V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr}
34e49164
C
78
79let comm_assoc rule rule_name dropped_isos =
80 if List.mem "comm_assoc" dropped_isos
81 then rule
b1b2de81 82 else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule