2 * Copyright 2005-2010, 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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
45 module Ast
= Ast_cocci
46 module Ast0
= Ast0_cocci
47 module V0
= Visitor_ast0
48 module VT0
= Visitor_ast0_types
50 (* call set_test_pos on test expressions *)
52 (* The goal of this is to identify test expressions in the SmPL file, so that
53 isomorphisms like X != NULL => X are only applied in a test expression context.
55 There is a related check in cocci_vs_c3.ml that in x || ..., a match
56 without the || is only accepted in a test expression context. This uses
57 the annotations in the C file. *)
59 let rec process_exp e
=
60 let e = Ast0.set_test_pos
e in(* allow test isos *)
61 let e = Ast0.set_test_exp
e in(* require that a test expression is matched *)
62 match Ast0.unwrap
e with
63 Ast0.Paren
(lp
,e1
,rp
) ->
64 Ast0.rewrap
e (Ast0.Paren
(lp
,process_exp e1
,rp
))
68 let expression r k
e =
70 match Ast0.unwrap
e with
71 Ast0.CondExpr
(e1
,q
,e2
,c
,e3
) ->
72 Ast0.rewrap
e (Ast0.CondExpr
(process_exp e1
,q
,e2
,c
,e3
))
73 | Ast0.Binary
(e1
,op
,e2
) ->
74 (match Ast0.unwrap_mcode op
with
75 Ast.Logical
(Ast.AndLog
) | Ast.Logical
(Ast.OrLog
) ->
76 Ast0.rewrap
e (Ast0.Binary
(process_exp e1
,op
,process_exp e2
))
78 | Ast0.Unary
(e1
,op
) ->
79 (match Ast0.unwrap_mcode op
with
80 Ast.Not
-> Ast0.rewrap
e (Ast0.Unary
(process_exp e1
,op
))
84 let process_wc = function
85 Ast0.WhenNotTrue
(e) -> Ast0.WhenNotTrue
(process_exp e)
86 | Ast0.WhenNotFalse
(e) -> Ast0.WhenNotFalse
(process_exp e)
91 match Ast0.unwrap
s with
92 Ast0.IfThen
(i
,lp
,e,rp
,s1
,aft
) ->
93 Ast0.rewrap
s (Ast0.IfThen
(i
,lp
,process_exp e,rp
,s1
,aft
))
94 | Ast0.IfThenElse
(i
,lp
,e,rp
,s1
,e1
,s2
,aft
) ->
95 Ast0.rewrap
s (Ast0.IfThenElse
(i
,lp
,process_exp e,rp
,s1
,e1
,s2
,aft
))
96 | Ast0.While
(i
,lp
,e,rp
,s1
,aft
) ->
97 Ast0.rewrap
s (Ast0.While
(i
,lp
,process_exp e,rp
,s1
,aft
))
98 | Ast0.Do
(d
,s1
,w
,lp
,e,rp
,sc
) ->
99 Ast0.rewrap
s (Ast0.Do
(d
,s1
,w
,lp
,process_exp e,rp
,sc
))
100 | Ast0.For
(f
,lp
,e1
,sc1
,Some e2
,sc2
,e3
,rp
,s1
,aft
) ->
102 (Ast0.For
(f
,lp
,e1
,sc1
,Some
(process_exp e2
),sc2
,e3
,rp
,s1
,aft
))
104 Ast0.rewrap
s (Ast0.Dots
(d
,List.map
process_wc wc
))
105 | Ast0.Nest
(l
,s1
,r
,wc
,m
) ->
106 Ast0.rewrap
s (Ast0.Nest
(l
,s1
,r
,List.map
process_wc wc
,m
))
110 {V0.rebuilder_functions
with
111 VT0.rebuilder_exprfn
= expression; VT0.rebuilder_stmtfn
= statement}
113 let process = List.map
set_test_poss.VT0.rebuilder_rec_top_level
115 let process_anything = set_test_poss.VT0.rebuilder_rec_anything