Release coccinelle-0.2.4rc3
[bpt/coccinelle.git] / engine / sgrep.ml
CommitLineData
c491d8ee
C
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
34e49164
C
25type marker =
26 NoMark | BefMark of string | AftMark of string
27 | BefAftMark of string * string
28
29let 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
53let 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
708f4980 59 Ast_cocci.MINUS(pos,inst,adj,repl) ->
34e49164
C
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;
708f4980 65 Ast_cocci.MINUS(pos,inst,adj,repl)
34e49164
C
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);
708f4980 69 Ast_cocci.MINUS(pos,inst,adj,repl)
34e49164
C
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);
708f4980 75 Ast_cocci.MINUS(pos,inst,adj,repl))
34e49164 76 | Ast_cocci.CONTEXT(pos,Ast_cocci.NOTHING) -> mck
951c7801 77 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c)) ->
34e49164
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;
951c7801 87 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
34e49164 88 | _ -> failwith "after not possible")
951c7801 89 | Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c)) ->
34e49164
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);
951c7801 99 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
34e49164 100 | _ -> failwith "before not possible")
951c7801 101 | Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c)) ->
34e49164
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);
951c7801 110 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
34e49164
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);
951c7801 115 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
34e49164
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
951c7801 121 (NoMark,_) -> Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
34e49164
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);
951c7801 131 Ast_cocci.CONTEXT(pos,Ast_cocci.AFTER(aft,c))
34e49164
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
708f4980 137 (NoMark,_) ->
951c7801 138 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
34e49164
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);
951c7801 143 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef,c))
34e49164
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);
951c7801 148 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft,c))
34e49164
C
149 | _ -> failwith "before not possible")
150 | _ -> failwith "after not possible")
151 | _ -> failwith "unexpected plus code"