Release coccinelle-0.2.4
[bpt/coccinelle.git] / parsing_cocci / test_exps.ml
CommitLineData
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
49module Ast = Ast_cocci
50module Ast0 = Ast0_cocci
51module V0 = Visitor_ast0
b1b2de81 52module VT0 = Visitor_ast0_types
34e49164
C
53
54(* call set_test_pos on test expressions *)
55
56(* The goal of this is to identify test expressions in the SmPL file, so that
57isomorphisms like X != NULL => X are only applied in a test expression context.
58
59There is a related check in cocci_vs_c3.ml that in x || ..., a match
60without the || is only accepted in a test expression context. This uses
61the annotations in the C file. *)
62
63let rec process_exp e =
faf9a90c
C
64 let e = Ast0.set_test_pos e in(* allow test isos *)
65 let e = Ast0.set_test_exp e in(* require that a test expression is matched *)
34e49164 66 match Ast0.unwrap e with
faf9a90c
C
67 Ast0.Paren(lp,e1,rp) ->
68 Ast0.rewrap e (Ast0.Paren(lp,process_exp e1,rp))
34e49164
C
69 | _ -> e
70
71let set_test_poss =
34e49164
C
72 let expression r k e =
73 let e = k e in
74 match Ast0.unwrap e with
75 Ast0.CondExpr(e1,q,e2,c,e3) ->
76 Ast0.rewrap e (Ast0.CondExpr(process_exp e1,q,e2,c,e3))
77 | Ast0.Binary(e1,op,e2) ->
78 (match Ast0.unwrap_mcode op with
79 Ast.Logical(Ast.AndLog) | Ast.Logical(Ast.OrLog) ->
80 Ast0.rewrap e (Ast0.Binary(process_exp e1,op,process_exp e2))
81 | _ -> e)
82 | Ast0.Unary(e1,op) ->
83 (match Ast0.unwrap_mcode op with
84 Ast.Not -> Ast0.rewrap e (Ast0.Unary(process_exp e1,op))
85 | _ -> e)
86 | _ -> e in
87
1be43e12
C
88 let process_wc = function
89 Ast0.WhenNotTrue(e) -> Ast0.WhenNotTrue(process_exp e)
90 | Ast0.WhenNotFalse(e) -> Ast0.WhenNotFalse(process_exp e)
91 | wc -> wc in
92
34e49164
C
93 let statement r k s =
94 let s = k s in
95 match Ast0.unwrap s with
96 Ast0.IfThen(i,lp,e,rp,s1,aft) ->
97 Ast0.rewrap s (Ast0.IfThen(i,lp,process_exp e,rp,s1,aft))
98 | Ast0.IfThenElse(i,lp,e,rp,s1,e1,s2,aft) ->
99 Ast0.rewrap s (Ast0.IfThenElse(i,lp,process_exp e,rp,s1,e1,s2,aft))
100 | Ast0.While(i,lp,e,rp,s1,aft) ->
101 Ast0.rewrap s (Ast0.While(i,lp,process_exp e,rp,s1,aft))
102 | Ast0.Do(d,s1,w,lp,e,rp,sc) ->
103 Ast0.rewrap s (Ast0.Do(d,s1,w,lp,process_exp e,rp,sc))
104 | Ast0.For(f,lp,e1,sc1,Some e2,sc2,e3,rp,s1,aft) ->
105 Ast0.rewrap s
106 (Ast0.For(f,lp,e1,sc1,Some (process_exp e2),sc2,e3,rp,s1,aft))
1be43e12
C
107 | Ast0.Dots(d,wc) ->
108 Ast0.rewrap s (Ast0.Dots(d,List.map process_wc wc))
109 | Ast0.Nest(l,s1,r,wc,m) ->
110 Ast0.rewrap s (Ast0.Nest(l,s1,r,List.map process_wc wc,m))
34e49164
C
111 | _ -> s in
112
113 V0.rebuilder
b1b2de81
C
114 {V0.rebuilder_functions with
115 VT0.rebuilder_exprfn = expression; VT0.rebuilder_stmtfn = statement}
34e49164 116
b1b2de81 117let process = List.map set_test_poss.VT0.rebuilder_rec_top_level
34e49164 118
b1b2de81 119let process_anything = set_test_poss.VT0.rebuilder_rec_anything
34e49164 120