Release coccinelle-0.2.3rc1
[bpt/coccinelle.git] / engine / sgrep.ml
1 (*
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.
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 (*
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.
27 *
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.
31 *
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.
36 *
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/>.
39 *
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
42 *)
43
44
45 type marker =
46 NoMark | BefMark of string | AftMark of string
47 | BefAftMark of string * string
48
49 let extract_sgrep_marker l =
50 let rec inner_loop acc = function
51 [] -> (acc,[])
52 | Ast_cocci.SgrepStartTag(s)::rest ->
53 (match acc with
54 NoMark -> inner_loop (BefMark(s)) rest
55 | _ -> failwith "unexpected mark")
56 | Ast_cocci.SgrepEndTag(s)::rest ->
57 (match acc with
58 NoMark -> inner_loop (AftMark(s)) rest
59 | BefMark(m) -> inner_loop (BefAftMark(m,s)) rest
60 | _ -> failwith "unexpected mark")
61 | x::rest ->
62 let (acc,rest) = inner_loop acc rest in
63 (acc,x::rest) in
64 let (acc,l) =
65 List.fold_left
66 (function (acc,prev) ->
67 function cur ->
68 let (acc,cur) = inner_loop acc cur in
69 (acc,cur::prev))
70 (NoMark,[]) l in
71 (acc,List.rev l)
72
73 let process_sgrep ii mck =
74 let file = Ast_c.file_of_info ii in
75 let line = Ast_c.line_of_info ii in
76 let col = Ast_c.col_of_info ii in
77 let str = Ast_c.str_of_info ii in
78 match mck with
79 Ast_cocci.MINUS(pos,inst,adj,repl) ->
80 (match extract_sgrep_marker repl with
81 (NoMark,_) -> mck
82 | (BefMark(marker),repl) ->
83 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
84 marker file line col;
85 Ast_cocci.MINUS(pos,inst,adj,repl)
86 | (AftMark(marker),repl) ->
87 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
88 marker file line (col + String.length str);
89 Ast_cocci.MINUS(pos,inst,adj,repl)
90 | (BefAftMark(bmarker,amarker),repl) ->
91 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
92 bmarker file line col;
93 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
94 amarker file line (col + String.length str);
95 Ast_cocci.MINUS(pos,inst,adj,repl))
96 | Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING) -> mck
97 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c)) ->
98 (match extract_sgrep_marker bef with
99 (NoMark,_) -> mck
100 | (BefMark(marker),[]) ->
101 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
102 marker file line col;
103 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
104 | (BefMark(marker),bef) ->
105 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
106 marker file line col;
107 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
108 | _ -> failwith "after not possible")
109 | Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c)) ->
110 (match extract_sgrep_marker aft with
111 (NoMark,_) -> mck
112 | (AftMark(marker),[]) ->
113 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
114 marker file line (col + String.length str);
115 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
116 | (AftMark(marker),aft) ->
117 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
118 marker file line (col + String.length str);
119 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
120 | _ -> failwith "before not possible")
121 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c)) ->
122 (match extract_sgrep_marker bef with
123 (NoMark,_) ->
124 (match extract_sgrep_marker aft with
125 (NoMark,_) -> mck
126 | (AftMark(marker),[]) ->
127 Printf.printf
128 "Match on line %s ending at %s: line %d offset %d\n"
129 marker file line (col + String.length str);
130 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
131 | (AftMark(marker),aft) ->
132 Printf.printf
133 "Match on line %s ending at %s: line %d offset %d\n"
134 marker file line (col + String.length str);
135 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
136 | _ -> failwith "before not possible")
137 | (BefMark(marker),[]) ->
138 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
139 marker file line col;
140 (match extract_sgrep_marker aft with
141 (NoMark,_) -> Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
142 | (AftMark(marker),[]) ->
143 Printf.printf
144 "Match on line %s ending at %s: line %d offset %d\n"
145 marker file line (col + String.length str);
146 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
147 | (AftMark(marker),aft) ->
148 Printf.printf
149 "Match on line %s ending at %s: line %d offset %d\n"
150 marker file line (col + String.length str);
151 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
152 | _ -> failwith "before not possible")
153 | (BefMark(marker),bef) ->
154 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
155 marker file line col;
156 (match extract_sgrep_marker aft with
157 (NoMark,_) ->
158 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
159 | (AftMark(marker),[]) ->
160 Printf.printf
161 "Match on line %s ending at %s: line %d offset %d\n"
162 marker file line (col + String.length str);
163 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
164 | (AftMark(marker),aft) ->
165 Printf.printf
166 "Match on line %s ending at %s: line %d offset %d\n"
167 marker file line (col + String.length str);
168 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
169 | _ -> failwith "before not possible")
170 | _ -> failwith "after not possible")
171 | _ -> failwith "unexpected plus code"