Release coccinelle-0.1.8
[bpt/coccinelle.git] / engine / sgrep.ml
CommitLineData
34e49164 1(*
faf9a90c 2* Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
34e49164
C
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
23type marker =
24 NoMark | BefMark of string | AftMark of string
25 | BefAftMark of string * string
26
27let 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
51let 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
708f4980 57 Ast_cocci.MINUS(pos,inst,adj,repl) ->
34e49164
C
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;
708f4980 63 Ast_cocci.MINUS(pos,inst,adj,repl)
34e49164
C
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);
708f4980 67 Ast_cocci.MINUS(pos,inst,adj,repl)
34e49164
C
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);
708f4980 73 Ast_cocci.MINUS(pos,inst,adj,repl))
34e49164
C
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
708f4980
C
135 (NoMark,_) ->
136 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft))
34e49164
C
137 | (AftMark(marker),[]) ->
138 Printf.printf
139 "Match on line %s ending at %s: line %d offset %d\n"
140 marker file line (col + String.length str);
141 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFORE(bef))
142 | (AftMark(marker),aft) ->
143 Printf.printf
144 "Match on line %s ending at %s: line %d offset %d\n"
145 marker file line (col + String.length str);
146 Ast_cocci.CONTEXT(pos,Ast_cocci.BEFOREAFTER(bef,aft))
147 | _ -> failwith "before not possible")
148 | _ -> failwith "after not possible")
149 | _ -> failwith "unexpected plus code"