Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / engine / sgrep.ml
CommitLineData
34e49164
C
1type marker =
2 NoMark | BefMark of string | AftMark of string
3 | BefAftMark of string * string
4
5let extract_sgrep_marker l =
6 let rec inner_loop acc = function
7 [] -> (acc,[])
8 | Ast_cocci.SgrepStartTag(s)::rest ->
9 (match acc with
10 NoMark -> inner_loop (BefMark(s)) rest
11 | _ -> failwith "unexpected mark")
12 | Ast_cocci.SgrepEndTag(s)::rest ->
13 (match acc with
14 NoMark -> inner_loop (AftMark(s)) rest
15 | BefMark(m) -> inner_loop (BefAftMark(m,s)) rest
16 | _ -> failwith "unexpected mark")
17 | x::rest ->
18 let (acc,rest) = inner_loop acc rest in
19 (acc,x::rest) in
20 let (acc,l) =
21 List.fold_left
22 (function (acc,prev) ->
23 function cur ->
24 let (acc,cur) = inner_loop acc cur in
25 (acc,cur::prev))
26 (NoMark,[]) l in
27 (acc,List.rev l)
28
29let process_sgrep ii mck =
30 let file = Ast_c.file_of_info ii in
31 let line = Ast_c.line_of_info ii in
32 let col = Ast_c.col_of_info ii in
33 let str = Ast_c.str_of_info ii in
34 match mck with
708f4980 35 Ast_cocci.MINUS(pos,inst,adj,repl) ->
34e49164
C
36 (match extract_sgrep_marker repl with
37 (NoMark,_) -> mck
38 | (BefMark(marker),repl) ->
39 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
40 marker file line col;
708f4980 41 Ast_cocci.MINUS(pos,inst,adj,repl)
34e49164
C
42 | (AftMark(marker),repl) ->
43 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
44 marker file line (col + String.length str);
708f4980 45 Ast_cocci.MINUS(pos,inst,adj,repl)
34e49164
C
46 | (BefAftMark(bmarker,amarker),repl) ->
47 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
48 bmarker file line col;
49 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
50 amarker file line (col + String.length str);
708f4980 51 Ast_cocci.MINUS(pos,inst,adj,repl))
34e49164 52 | Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING) -> mck
951c7801 53 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c)) ->
34e49164
C
54 (match extract_sgrep_marker bef with
55 (NoMark,_) -> mck
56 | (BefMark(marker),[]) ->
57 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
58 marker file line col;
59 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
60 | (BefMark(marker),bef) ->
61 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
62 marker file line col;
951c7801 63 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
34e49164 64 | _ -> failwith "after not possible")
951c7801 65 | Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c)) ->
34e49164
C
66 (match extract_sgrep_marker aft with
67 (NoMark,_) -> mck
68 | (AftMark(marker),[]) ->
69 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
70 marker file line (col + String.length str);
71 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
72 | (AftMark(marker),aft) ->
73 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
74 marker file line (col + String.length str);
951c7801 75 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
34e49164 76 | _ -> failwith "before not possible")
951c7801 77 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c)) ->
34e49164
C
78 (match extract_sgrep_marker bef with
79 (NoMark,_) ->
80 (match extract_sgrep_marker aft with
81 (NoMark,_) -> mck
82 | (AftMark(marker),[]) ->
83 Printf.printf
84 "Match on line %s ending at %s: line %d offset %d\n"
85 marker file line (col + String.length str);
951c7801 86 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
34e49164
C
87 | (AftMark(marker),aft) ->
88 Printf.printf
89 "Match on line %s ending at %s: line %d offset %d\n"
90 marker file line (col + String.length str);
951c7801 91 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
34e49164
C
92 | _ -> failwith "before not possible")
93 | (BefMark(marker),[]) ->
94 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
95 marker file line col;
96 (match extract_sgrep_marker aft with
951c7801 97 (NoMark,_) -> Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
34e49164
C
98 | (AftMark(marker),[]) ->
99 Printf.printf
100 "Match on line %s ending at %s: line %d offset %d\n"
101 marker file line (col + String.length str);
102 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
103 | (AftMark(marker),aft) ->
104 Printf.printf
105 "Match on line %s ending at %s: line %d offset %d\n"
106 marker file line (col + String.length str);
951c7801 107 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
34e49164
C
108 | _ -> failwith "before not possible")
109 | (BefMark(marker),bef) ->
110 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
111 marker file line col;
112 (match extract_sgrep_marker aft with
708f4980 113 (NoMark,_) ->
951c7801 114 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
34e49164
C
115 | (AftMark(marker),[]) ->
116 Printf.printf
117 "Match on line %s ending at %s: line %d offset %d\n"
118 marker file line (col + String.length str);
951c7801 119 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
34e49164
C
120 | (AftMark(marker),aft) ->
121 Printf.printf
122 "Match on line %s ending at %s: line %d offset %d\n"
123 marker file line (col + String.length str);
951c7801 124 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
34e49164
C
125 | _ -> failwith "before not possible")
126 | _ -> failwith "after not possible")
127 | _ -> failwith "unexpected plus code"