Release coccinelle-0.2.0
[bpt/coccinelle.git] / parsing_cocci / test_exps.ml
1 (*
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.
5 *
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.
9 *
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.
14 *
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/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
23 module Ast = Ast_cocci
24 module Ast0 = Ast0_cocci
25 module V0 = Visitor_ast0
26 module VT0 = Visitor_ast0_types
27
28 (* call set_test_pos on test expressions *)
29
30 (* The goal of this is to identify test expressions in the SmPL file, so that
31 isomorphisms like X != NULL => X are only applied in a test expression context.
32
33 There is a related check in cocci_vs_c3.ml that in x || ..., a match
34 without the || is only accepted in a test expression context. This uses
35 the annotations in the C file. *)
36
37 let rec process_exp e =
38 let e = Ast0.set_test_pos e in(* allow test isos *)
39 let e = Ast0.set_test_exp e in(* require that a test expression is matched *)
40 match Ast0.unwrap e with
41 Ast0.Paren(lp,e1,rp) ->
42 Ast0.rewrap e (Ast0.Paren(lp,process_exp e1,rp))
43 | _ -> e
44
45 let set_test_poss =
46 let expression r k e =
47 let e = k e in
48 match Ast0.unwrap e with
49 Ast0.CondExpr(e1,q,e2,c,e3) ->
50 Ast0.rewrap e (Ast0.CondExpr(process_exp e1,q,e2,c,e3))
51 | Ast0.Binary(e1,op,e2) ->
52 (match Ast0.unwrap_mcode op with
53 Ast.Logical(Ast.AndLog) | Ast.Logical(Ast.OrLog) ->
54 Ast0.rewrap e (Ast0.Binary(process_exp e1,op,process_exp e2))
55 | _ -> e)
56 | Ast0.Unary(e1,op) ->
57 (match Ast0.unwrap_mcode op with
58 Ast.Not -> Ast0.rewrap e (Ast0.Unary(process_exp e1,op))
59 | _ -> e)
60 | _ -> e in
61
62 let process_wc = function
63 Ast0.WhenNotTrue(e) -> Ast0.WhenNotTrue(process_exp e)
64 | Ast0.WhenNotFalse(e) -> Ast0.WhenNotFalse(process_exp e)
65 | wc -> wc in
66
67 let statement r k s =
68 let s = k s in
69 match Ast0.unwrap s with
70 Ast0.IfThen(i,lp,e,rp,s1,aft) ->
71 Ast0.rewrap s (Ast0.IfThen(i,lp,process_exp e,rp,s1,aft))
72 | Ast0.IfThenElse(i,lp,e,rp,s1,e1,s2,aft) ->
73 Ast0.rewrap s (Ast0.IfThenElse(i,lp,process_exp e,rp,s1,e1,s2,aft))
74 | Ast0.While(i,lp,e,rp,s1,aft) ->
75 Ast0.rewrap s (Ast0.While(i,lp,process_exp e,rp,s1,aft))
76 | Ast0.Do(d,s1,w,lp,e,rp,sc) ->
77 Ast0.rewrap s (Ast0.Do(d,s1,w,lp,process_exp e,rp,sc))
78 | Ast0.For(f,lp,e1,sc1,Some e2,sc2,e3,rp,s1,aft) ->
79 Ast0.rewrap s
80 (Ast0.For(f,lp,e1,sc1,Some (process_exp e2),sc2,e3,rp,s1,aft))
81 | Ast0.Dots(d,wc) ->
82 Ast0.rewrap s (Ast0.Dots(d,List.map process_wc wc))
83 | Ast0.Nest(l,s1,r,wc,m) ->
84 Ast0.rewrap s (Ast0.Nest(l,s1,r,List.map process_wc wc,m))
85 | _ -> s in
86
87 V0.rebuilder
88 {V0.rebuilder_functions with
89 VT0.rebuilder_exprfn = expression; VT0.rebuilder_stmtfn = statement}
90
91 let process = List.map set_test_poss.VT0.rebuilder_rec_top_level
92
93 let process_anything = set_test_poss.VT0.rebuilder_rec_anything
94