Commit | Line | Data |
---|---|---|
34e49164 C |
1 | module Ast = Ast_cocci |
2 | module Ast0 = Ast0_cocci | |
3 | module V0 = Visitor_ast0 | |
b1b2de81 | 4 | module VT0 = Visitor_ast0_types |
34e49164 C |
5 | |
6 | (* call set_test_pos on test expressions *) | |
7 | ||
8 | (* The goal of this is to identify test expressions in the SmPL file, so that | |
9 | isomorphisms like X != NULL => X are only applied in a test expression context. | |
10 | ||
11 | There is a related check in cocci_vs_c3.ml that in x || ..., a match | |
12 | without the || is only accepted in a test expression context. This uses | |
13 | the annotations in the C file. *) | |
14 | ||
15 | let rec process_exp e = | |
faf9a90c C |
16 | let e = Ast0.set_test_pos e in(* allow test isos *) |
17 | let e = Ast0.set_test_exp e in(* require that a test expression is matched *) | |
34e49164 | 18 | match Ast0.unwrap e with |
faf9a90c C |
19 | Ast0.Paren(lp,e1,rp) -> |
20 | Ast0.rewrap e (Ast0.Paren(lp,process_exp e1,rp)) | |
34e49164 C |
21 | | _ -> e |
22 | ||
23 | let set_test_poss = | |
34e49164 C |
24 | let expression r k e = |
25 | let e = k e in | |
26 | match Ast0.unwrap e with | |
27 | Ast0.CondExpr(e1,q,e2,c,e3) -> | |
28 | Ast0.rewrap e (Ast0.CondExpr(process_exp e1,q,e2,c,e3)) | |
29 | | Ast0.Binary(e1,op,e2) -> | |
30 | (match Ast0.unwrap_mcode op with | |
31 | Ast.Logical(Ast.AndLog) | Ast.Logical(Ast.OrLog) -> | |
32 | Ast0.rewrap e (Ast0.Binary(process_exp e1,op,process_exp e2)) | |
33 | | _ -> e) | |
34 | | Ast0.Unary(e1,op) -> | |
35 | (match Ast0.unwrap_mcode op with | |
36 | Ast.Not -> Ast0.rewrap e (Ast0.Unary(process_exp e1,op)) | |
37 | | _ -> e) | |
38 | | _ -> e in | |
39 | ||
1be43e12 C |
40 | let process_wc = function |
41 | Ast0.WhenNotTrue(e) -> Ast0.WhenNotTrue(process_exp e) | |
42 | | Ast0.WhenNotFalse(e) -> Ast0.WhenNotFalse(process_exp e) | |
43 | | wc -> wc in | |
44 | ||
34e49164 C |
45 | let statement r k s = |
46 | let s = k s in | |
47 | match Ast0.unwrap s with | |
48 | Ast0.IfThen(i,lp,e,rp,s1,aft) -> | |
49 | Ast0.rewrap s (Ast0.IfThen(i,lp,process_exp e,rp,s1,aft)) | |
50 | | Ast0.IfThenElse(i,lp,e,rp,s1,e1,s2,aft) -> | |
51 | Ast0.rewrap s (Ast0.IfThenElse(i,lp,process_exp e,rp,s1,e1,s2,aft)) | |
52 | | Ast0.While(i,lp,e,rp,s1,aft) -> | |
53 | Ast0.rewrap s (Ast0.While(i,lp,process_exp e,rp,s1,aft)) | |
54 | | Ast0.Do(d,s1,w,lp,e,rp,sc) -> | |
55 | Ast0.rewrap s (Ast0.Do(d,s1,w,lp,process_exp e,rp,sc)) | |
56 | | Ast0.For(f,lp,e1,sc1,Some e2,sc2,e3,rp,s1,aft) -> | |
57 | Ast0.rewrap s | |
58 | (Ast0.For(f,lp,e1,sc1,Some (process_exp e2),sc2,e3,rp,s1,aft)) | |
1be43e12 C |
59 | | Ast0.Dots(d,wc) -> |
60 | Ast0.rewrap s (Ast0.Dots(d,List.map process_wc wc)) | |
61 | | Ast0.Nest(l,s1,r,wc,m) -> | |
62 | Ast0.rewrap s (Ast0.Nest(l,s1,r,List.map process_wc wc,m)) | |
34e49164 C |
63 | | _ -> s in |
64 | ||
65 | V0.rebuilder | |
b1b2de81 C |
66 | {V0.rebuilder_functions with |
67 | VT0.rebuilder_exprfn = expression; VT0.rebuilder_stmtfn = statement} | |
34e49164 | 68 | |
b1b2de81 | 69 | let process = List.map set_test_poss.VT0.rebuilder_rec_top_level |
34e49164 | 70 | |
b1b2de81 | 71 | let process_anything = set_test_poss.VT0.rebuilder_rec_anything |
34e49164 | 72 |