Commit | Line | Data |
---|---|---|
9bc82bae C |
1 | (* |
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. | |
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 | ||
c491d8ee C |
25 | (* |
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. | |
31 | * | |
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. | |
35 | * | |
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. | |
40 | * | |
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/>. | |
43 | * | |
44 | * The authors reserve the right to distribute this or future versions of | |
45 | * Coccinelle under other licenses. | |
46 | *) | |
47 | ||
48 | ||
34e49164 C |
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. *) | |
53 | ||
54 | module Ast = Ast_cocci | |
55 | module Ast0 = Ast0_cocci | |
56 | module V0 = Visitor_ast0 | |
b1b2de81 | 57 | module VT0 = Visitor_ast0_types |
34e49164 C |
58 | |
59 | let comm_assoc = | |
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)] | |
62 | ||
63 | let is_minus e = | |
64 | match Ast0.get_mcodekind e with Ast0.MINUS(cell) -> true | _ -> false | |
65 | ||
66 | let is_context e = | |
67 | !Flag.sgrep_mode2 or (* everything is context for sgrep *) | |
68 | (match Ast0.get_mcodekind e with | |
69 | Ast0.CONTEXT(cell) -> true | |
70 | | _ -> false) | |
71 | ||
72 | let nopos mc = | |
73 | match Ast0.get_pos mc with Ast0.MetaPos _ -> false | Ast0.NoMetaPos -> true | |
74 | ||
75 | let process_binops rule_name = | |
34e49164 C |
76 | let expr r k e1 = |
77 | let e = k e1 in | |
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 | |
82 | Ast0.Edots(d,None) -> | |
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)) | |
87 | else | |
88 | (Printf.printf | |
89 | "%s: position variables or mixed modifs interfere with comm_assoc iso" rule_name; | |
90 | Unparse_ast0.expression e1; | |
91 | Format.print_newline(); | |
92 | e) | |
faf9a90c | 93 | | Ast0.Edots(d,_) -> |
34e49164 C |
94 | (Printf.printf |
95 | "%s: whencode interferes with comm_assoc iso" rule_name; | |
96 | Unparse_ast0.expression e1; | |
97 | Format.print_newline(); | |
98 | e) | |
99 | | _ -> e) | |
100 | | _ -> e in | |
b1b2de81 | 101 | V0.rebuilder {V0.rebuilder_functions with VT0.rebuilder_exprfn = expr} |
34e49164 C |
102 | |
103 | let comm_assoc rule rule_name dropped_isos = | |
104 | if List.mem "comm_assoc" dropped_isos | |
105 | then rule | |
b1b2de81 | 106 | else List.map (process_binops rule_name).VT0.rebuilder_rec_top_level rule |