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