Release coccinelle-0.2.4
[bpt/coccinelle.git] / engine / sgrep.ml
1 (*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
25 (*
26 * Copyright 2010, INRIA, University of Copenhagen
27 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
28 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
29 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
30 * This file is part of Coccinelle.
31 *
32 * Coccinelle is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, according to version 2 of the License.
35 *
36 * Coccinelle is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License
42 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
43 *
44 * The authors reserve the right to distribute this or future versions of
45 * Coccinelle under other licenses.
46 *)
47
48
49 type marker =
50 NoMark | BefMark of string | AftMark of string
51 | BefAftMark of string * string
52
53 let extract_sgrep_marker l =
54 let rec inner_loop acc = function
55 [] -> (acc,[])
56 | Ast_cocci.SgrepStartTag(s)::rest ->
57 (match acc with
58 NoMark -> inner_loop (BefMark(s)) rest
59 | _ -> failwith "unexpected mark")
60 | Ast_cocci.SgrepEndTag(s)::rest ->
61 (match acc with
62 NoMark -> inner_loop (AftMark(s)) rest
63 | BefMark(m) -> inner_loop (BefAftMark(m,s)) rest
64 | _ -> failwith "unexpected mark")
65 | x::rest ->
66 let (acc,rest) = inner_loop acc rest in
67 (acc,x::rest) in
68 let (acc,l) =
69 List.fold_left
70 (function (acc,prev) ->
71 function cur ->
72 let (acc,cur) = inner_loop acc cur in
73 (acc,cur::prev))
74 (NoMark,[]) l in
75 (acc,List.rev l)
76
77 let process_sgrep ii mck =
78 let file = Ast_c.file_of_info ii in
79 let line = Ast_c.line_of_info ii in
80 let col = Ast_c.col_of_info ii in
81 let str = Ast_c.str_of_info ii in
82 match mck with
83 Ast_cocci.MINUS(pos,inst,adj,repl) ->
84 (match extract_sgrep_marker repl with
85 (NoMark,_) -> mck
86 | (BefMark(marker),repl) ->
87 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
88 marker file line col;
89 Ast_cocci.MINUS(pos,inst,adj,repl)
90 | (AftMark(marker),repl) ->
91 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
92 marker file line (col + String.length str);
93 Ast_cocci.MINUS(pos,inst,adj,repl)
94 | (BefAftMark(bmarker,amarker),repl) ->
95 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
96 bmarker file line col;
97 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
98 amarker file line (col + String.length str);
99 Ast_cocci.MINUS(pos,inst,adj,repl))
100 | Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING) -> mck
101 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c)) ->
102 (match extract_sgrep_marker bef with
103 (NoMark,_) -> mck
104 | (BefMark(marker),[]) ->
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.NOTHING)
108 | (BefMark(marker),bef) ->
109 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
110 marker file line col;
111 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
112 | _ -> failwith "after not possible")
113 | Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c)) ->
114 (match extract_sgrep_marker aft with
115 (NoMark,_) -> mck
116 | (AftMark(marker),[]) ->
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.NOTHING)
120 | (AftMark(marker),aft) ->
121 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
122 marker file line (col + String.length str);
123 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
124 | _ -> failwith "before not possible")
125 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c)) ->
126 (match extract_sgrep_marker bef with
127 (NoMark,_) ->
128 (match extract_sgrep_marker aft with
129 (NoMark,_) -> mck
130 | (AftMark(marker),[]) ->
131 Printf.printf
132 "Match on line %s ending at %s: line %d offset %d\n"
133 marker file line (col + String.length str);
134 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
135 | (AftMark(marker),aft) ->
136 Printf.printf
137 "Match on line %s ending at %s: line %d offset %d\n"
138 marker file line (col + String.length str);
139 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
140 | _ -> failwith "before not possible")
141 | (BefMark(marker),[]) ->
142 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
143 marker file line col;
144 (match extract_sgrep_marker aft with
145 (NoMark,_) -> Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
146 | (AftMark(marker),[]) ->
147 Printf.printf
148 "Match on line %s ending at %s: line %d offset %d\n"
149 marker file line (col + String.length str);
150 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
151 | (AftMark(marker),aft) ->
152 Printf.printf
153 "Match on line %s ending at %s: line %d offset %d\n"
154 marker file line (col + String.length str);
155 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
156 | _ -> failwith "before not possible")
157 | (BefMark(marker),bef) ->
158 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
159 marker file line col;
160 (match extract_sgrep_marker aft with
161 (NoMark,_) ->
162 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
163 | (AftMark(marker),[]) ->
164 Printf.printf
165 "Match on line %s ending at %s: line %d offset %d\n"
166 marker file line (col + String.length str);
167 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
168 | (AftMark(marker),aft) ->
169 Printf.printf
170 "Match on line %s ending at %s: line %d offset %d\n"
171 marker file line (col + String.length str);
172 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
173 | _ -> failwith "before not possible")
174 | _ -> failwith "after not possible")
175 | _ -> failwith "unexpected plus code"