1 (* searches for E op ..., for any commutative and associative binary
2 operator. When this satisfies the isomorphism conditions (ie all minus, or
3 context for the op and ...), then this is converted to Nested(E,op).
4 Nested is not used before this phase. *)
7 module Ast0
= Ast0_cocci
8 module V0
= Visitor_ast0
9 module VT0
= Visitor_ast0_types
12 [Ast.Arith
(Ast.Plus
);Ast.Arith
(Ast.Mul
);Ast.Arith
(Ast.And
);Ast.Arith
(Ast.Or
);
13 Ast.Logical
(Ast.AndLog
);Ast.Logical
(Ast.OrLog
)]
16 match Ast0.get_mcodekind e
with Ast0.MINUS
(cell
) -> true | _
-> false
19 !Flag.sgrep_mode2
or (* everything is context for sgrep *)
20 (match Ast0.get_mcodekind e
with
21 Ast0.CONTEXT
(cell
) -> true
25 match Ast0.get_pos mc
with Ast0.MetaPos _
-> false | Ast0.NoMetaPos
-> true
27 let process_binops rule_name
=
30 match Ast0.unwrap
e with
31 Ast0.Binary
(left
,op
,right
)
32 when List.mem
(Ast0.unwrap_mcode op
) comm_assoc ->
33 (match Ast0.unwrap right
with
35 if (is_minus e || (is_context e && is_context right
))
36 && nopos op
&& nopos d
37 (* keep dots to record required modif *)
38 then Ast0.rewrap
e (Ast0.Nested
(left
,op
,right
))
41 "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name
;
42 Unparse_ast0.expression e1
;
43 Format.print_newline
();
47 "%s: whencode interferes with comm_assoc iso" rule_name
;
48 Unparse_ast0.expression e1
;
49 Format.print_newline
();
53 V0.rebuilder
{V0.rebuilder_functions
with VT0.rebuilder_exprfn
= expr}
55 let comm_assoc rule rule_name dropped_isos
=
56 if List.mem
"comm_assoc" dropped_isos
58 else List.map
(process_binops rule_name
).VT0.rebuilder_rec_top_level rule