Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / parsing_cocci / test_exps.ml
1 (*
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
27 module Ast = Ast_cocci
28 module Ast0 = Ast0_cocci
29 module V0 = Visitor_ast0
30 module VT0 = Visitor_ast0_types
31
32 (* call set_test_pos on test expressions *)
33
34 (* The goal of this is to identify test expressions in the SmPL file, so that
35 isomorphisms like X != NULL => X are only applied in a test expression context.
36
37 There is a related check in cocci_vs_c3.ml that in x || ..., a match
38 without the || is only accepted in a test expression context. This uses
39 the annotations in the C file. *)
40
41 let rec process_exp e =
42 let e = Ast0.set_test_pos e in(* allow test isos *)
43 let e = Ast0.set_test_exp e in(* require that a test expression is matched *)
44 match Ast0.unwrap e with
45 Ast0.Paren(lp,e1,rp) ->
46 Ast0.rewrap e (Ast0.Paren(lp,process_exp e1,rp))
47 | _ -> e
48
49 let set_test_poss =
50 let expression r k e =
51 let e = k e in
52 match Ast0.unwrap e with
53 Ast0.CondExpr(e1,q,e2,c,e3) ->
54 Ast0.rewrap e (Ast0.CondExpr(process_exp e1,q,e2,c,e3))
55 | Ast0.Binary(e1,op,e2) ->
56 (match Ast0.unwrap_mcode op with
57 Ast.Logical(Ast.AndLog) | Ast.Logical(Ast.OrLog) ->
58 Ast0.rewrap e (Ast0.Binary(process_exp e1,op,process_exp e2))
59 | _ -> e)
60 | Ast0.Unary(e1,op) ->
61 (match Ast0.unwrap_mcode op with
62 Ast.Not -> Ast0.rewrap e (Ast0.Unary(process_exp e1,op))
63 | _ -> e)
64 | _ -> e in
65
66 let process_wc = function
67 Ast0.WhenNotTrue(e) -> Ast0.WhenNotTrue(process_exp e)
68 | Ast0.WhenNotFalse(e) -> Ast0.WhenNotFalse(process_exp e)
69 | wc -> wc in
70
71 let statement r k s =
72 let s = k s in
73 match Ast0.unwrap s with
74 Ast0.IfThen(i,lp,e,rp,s1,aft) ->
75 Ast0.rewrap s (Ast0.IfThen(i,lp,process_exp e,rp,s1,aft))
76 | Ast0.IfThenElse(i,lp,e,rp,s1,e1,s2,aft) ->
77 Ast0.rewrap s (Ast0.IfThenElse(i,lp,process_exp e,rp,s1,e1,s2,aft))
78 | Ast0.While(i,lp,e,rp,s1,aft) ->
79 Ast0.rewrap s (Ast0.While(i,lp,process_exp e,rp,s1,aft))
80 | Ast0.Do(d,s1,w,lp,e,rp,sc) ->
81 Ast0.rewrap s (Ast0.Do(d,s1,w,lp,process_exp e,rp,sc))
82 | Ast0.For(f,lp,e1,sc1,Some e2,sc2,e3,rp,s1,aft) ->
83 Ast0.rewrap s
84 (Ast0.For(f,lp,e1,sc1,Some (process_exp e2),sc2,e3,rp,s1,aft))
85 | Ast0.Dots(d,wc) ->
86 Ast0.rewrap s (Ast0.Dots(d,List.map process_wc wc))
87 | Ast0.Nest(l,s1,r,wc,m) ->
88 Ast0.rewrap s (Ast0.Nest(l,s1,r,List.map process_wc wc,m))
89 | _ -> s in
90
91 V0.rebuilder
92 {V0.rebuilder_functions with
93 VT0.rebuilder_exprfn = expression; VT0.rebuilder_stmtfn = statement}
94
95 let process = List.map set_test_poss.VT0.rebuilder_rec_top_level
96
97 let process_anything = set_test_poss.VT0.rebuilder_rec_anything
98