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