f1089b282ecd2ffc4ac1442d3eaf28debc0f5b96
[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 type marker =
26 NoMark | BefMark of string | AftMark of string
27 | BefAftMark of string * string
28
29 let extract_sgrep_marker l =
30 let rec inner_loop acc = function
31 [] -> (acc,[])
32 | Ast_cocci.SgrepStartTag(s)::rest ->
33 (match acc with
34 NoMark -> inner_loop (BefMark(s)) rest
35 | _ -> failwith "unexpected mark")
36 | Ast_cocci.SgrepEndTag(s)::rest ->
37 (match acc with
38 NoMark -> inner_loop (AftMark(s)) rest
39 | BefMark(m) -> inner_loop (BefAftMark(m,s)) rest
40 | _ -> failwith "unexpected mark")
41 | x::rest ->
42 let (acc,rest) = inner_loop acc rest in
43 (acc,x::rest) in
44 let (acc,l) =
45 List.fold_left
46 (function (acc,prev) ->
47 function cur ->
48 let (acc,cur) = inner_loop acc cur in
49 (acc,cur::prev))
50 (NoMark,[]) l in
51 (acc,List.rev l)
52
53 let process_sgrep ii mck =
54 let file = Ast_c.file_of_info ii in
55 let line = Ast_c.line_of_info ii in
56 let col = Ast_c.col_of_info ii in
57 let str = Ast_c.str_of_info ii in
58 match mck with
59 Ast_cocci.MINUS(pos,inst,adj,repl) ->
60 (match extract_sgrep_marker repl with
61 (NoMark,_) -> mck
62 | (BefMark(marker),repl) ->
63 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
64 marker file line col;
65 Ast_cocci.MINUS(pos,inst,adj,repl)
66 | (AftMark(marker),repl) ->
67 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
68 marker file line (col + String.length str);
69 Ast_cocci.MINUS(pos,inst,adj,repl)
70 | (BefAftMark(bmarker,amarker),repl) ->
71 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
72 bmarker file line col;
73 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
74 amarker file line (col + String.length str);
75 Ast_cocci.MINUS(pos,inst,adj,repl))
76 | Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING) -> mck
77 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c)) ->
78 (match extract_sgrep_marker bef with
79 (NoMark,_) -> mck
80 | (BefMark(marker),[]) ->
81 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
82 marker file line col;
83 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
84 | (BefMark(marker),bef) ->
85 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
86 marker file line col;
87 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
88 | _ -> failwith "after not possible")
89 | Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c)) ->
90 (match extract_sgrep_marker aft with
91 (NoMark,_) -> mck
92 | (AftMark(marker),[]) ->
93 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
94 marker file line (col + String.length str);
95 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
96 | (AftMark(marker),aft) ->
97 Printf.printf "Match on line %s ending at %s: line %d offset %d\n"
98 marker file line (col + String.length str);
99 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
100 | _ -> failwith "before not possible")
101 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c)) ->
102 (match extract_sgrep_marker bef with
103 (NoMark,_) ->
104 (match extract_sgrep_marker aft with
105 (NoMark,_) -> mck
106 | (AftMark(marker),[]) ->
107 Printf.printf
108 "Match on line %s ending at %s: line %d offset %d\n"
109 marker file line (col + String.length str);
110 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
111 | (AftMark(marker),aft) ->
112 Printf.printf
113 "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.BEFOREAFTER(bef,aft,c))
116 | _ -> failwith "before not possible")
117 | (BefMark(marker),[]) ->
118 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
119 marker file line col;
120 (match extract_sgrep_marker aft with
121 (NoMark,_) -> Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
122 | (AftMark(marker),[]) ->
123 Printf.printf
124 "Match on line %s ending at %s: line %d offset %d\n"
125 marker file line (col + String.length str);
126 Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING)
127 | (AftMark(marker),aft) ->
128 Printf.printf
129 "Match on line %s ending at %s: line %d offset %d\n"
130 marker file line (col + String.length str);
131 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
132 | _ -> failwith "before not possible")
133 | (BefMark(marker),bef) ->
134 Printf.printf "Match on line %s starting at %s: line %d offset %d\n"
135 marker file line col;
136 (match extract_sgrep_marker aft with
137 (NoMark,_) ->
138 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
139 | (AftMark(marker),[]) ->
140 Printf.printf
141 "Match on line %s ending at %s: line %d offset %d\n"
142 marker file line (col + String.length str);
143 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
144 | (AftMark(marker),aft) ->
145 Printf.printf
146 "Match on line %s ending at %s: line %d offset %d\n"
147 marker file line (col + String.length str);
148 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
149 | _ -> failwith "before not possible")
150 | _ -> failwith "after not possible")
151 | _ -> failwith "unexpected plus code"