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.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
26 * Copyright 2010, INRIA, University of Copenhagen
27 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
28 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
29 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
30 * This file is part of Coccinelle.
32 * Coccinelle is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, according to version 2 of the License.
36 * Coccinelle is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
44 * The authors reserve the right to distribute this or future versions of
45 * Coccinelle under other licenses.
49 (* searches for E op ..., for any commutative and associative binary
50 operator. When this satisfies the isomorphism conditions (ie all minus, or
51 context for the op and ...), then this is converted to Nested(E,op).
52 Nested is not used before this phase. *)
54 module Ast
= Ast_cocci
55 module Ast0
= Ast0_cocci
56 module V0
= Visitor_ast0
57 module VT0
= Visitor_ast0_types
60 [Ast.Arith
(Ast.Plus
);Ast.Arith
(Ast.Mul
);Ast.Arith
(Ast.And
);Ast.Arith
(Ast.Or
);
61 Ast.Logical
(Ast.AndLog
);Ast.Logical
(Ast.OrLog
)]
64 match Ast0.get_mcodekind e
with Ast0.MINUS
(cell
) -> true | _
-> false
67 !Flag.sgrep_mode2
or (* everything is context for sgrep *)
68 (match Ast0.get_mcodekind e
with
69 Ast0.CONTEXT
(cell
) -> true
73 match Ast0.get_pos mc
with Ast0.MetaPos _
-> false | Ast0.NoMetaPos
-> true
75 let process_binops rule_name
=
78 match Ast0.unwrap
e with
79 Ast0.Binary
(left
,op
,right
)
80 when List.mem
(Ast0.unwrap_mcode op
) comm_assoc ->
81 (match Ast0.unwrap right
with
83 if (is_minus e || (is_context e && is_context right
))
84 && nopos op
&& nopos d
85 (* keep dots to record required modif *)
86 then Ast0.rewrap
e (Ast0.Nested
(left
,op
,right
))
89 "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name
;
90 Unparse_ast0.expression e1
;
91 Format.print_newline
();
95 "%s: whencode interferes with comm_assoc iso" rule_name
;
96 Unparse_ast0.expression e1
;
97 Format.print_newline
();
101 V0.rebuilder
{V0.rebuilder_functions
with VT0.rebuilder_exprfn
= expr}
103 let comm_assoc rule rule_name dropped_isos
=
104 if List.mem
"comm_assoc" dropped_isos
106 else List.map
(process_binops rule_name
).VT0.rebuilder_rec_top_level rule