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