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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
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. *)
28 module Ast
= Ast_cocci
29 module Ast0
= Ast0_cocci
30 module V0
= Visitor_ast0
31 module VT0
= Visitor_ast0_types
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
)]
38 match Ast0.get_mcodekind e
with Ast0.MINUS
(cell
) -> true | _
-> false
41 !Flag.sgrep_mode2
or (* everything is context for sgrep *)
42 (match Ast0.get_mcodekind e
with
43 Ast0.CONTEXT
(cell
) -> true
47 match Ast0.get_pos mc
with Ast0.MetaPos _
-> false | Ast0.NoMetaPos
-> true
49 let process_binops rule_name
=
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
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
))
63 "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name
;
64 Unparse_ast0.expression e1
;
65 Format.print_newline
();
69 "%s: whencode interferes with comm_assoc iso" rule_name
;
70 Unparse_ast0.expression e1
;
71 Format.print_newline
();
75 V0.rebuilder
{V0.rebuilder_functions
with VT0.rebuilder_exprfn
= expr}
77 let comm_assoc rule rule_name dropped_isos
=
78 if List.mem
"comm_assoc" dropped_isos
80 else List.map
(process_binops rule_name
).VT0.rebuilder_rec_top_level rule