2 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
24 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
45 (* The following finds out for each file, how it does deallocation for each
51 let l = input_line i
in
52 (if String.length
l > 2 && String.get
l 0 = '
+'
53 then info := (String.sub
l 1 (String.length
l - 1))::!info);
56 with End_of_file
-> List.rev
!info
59 let rec loop acc
= function
62 if String.get x
0 = '
+'
(* the start of a new file *)
64 (match Str.split (Str.regexp
" ") x
with
65 _
::x
::_
-> loop ((x
,[])::acc
) xs
66 | _
-> failwith
("no file: "^x
))
70 (file
,instances
)::rest
-> (file
,x
::instances
)::rest
71 | _
-> failwith
"not possible" in
73 let res = List.rev
(loop [] l) in
74 List.map
(function (x
,l) -> (x
,List.rev
l)) res
76 let detect_alloc_free str
l =
78 let (same
,diff
) = List.partition
(function (a1
,f1
) -> a
= a1
) l in
80 [(a1
,f1
)] -> if List.mem f f1
then l else (a1
,f
::f1
) :: diff
82 let rec loop acc = function
85 match Str.split (Str.regexp
(str^
"\", ")) x
with
91 (match Str.split (Str.regexp
"[, )]+") rest
with
92 alloc
::free
::_
-> try_add alloc free
acc
98 (List.map
(function (a
,f
) -> (a
,List.sort compare f
)) (loop [] l))
100 let rec iterate str
= function
107 List.partition
(function (x1
,l1
) -> l1
= info) rest
in
109 [(files
,info)] -> (x
::files
,info)::diff
110 | _
-> ([x
],info)::diff
)
111 (iterate str xs
) (detect_alloc_free str
l)
113 (* ------------------------------------------------------------------------ *)
114 (* The following prints that information *)
118 (function (files
,(a
,fs
)) ->
119 List.iter
(function x
-> Printf.printf
"%s\n" x
) files
;
120 Printf.printf
" alloc: %s, free: %s\n" a
(String.concat
", " fs
);
124 (* ------------------------------------------------------------------------ *)
125 (* The following makes a semantic patch for that information *)
127 let sedify o generic_file dir
l =
129 (function (files
,(a
,fs
)) ->
135 "sed s/ALLOC/%s/ %s | sed s/FREE/%s/ > %s/%s-%s.cocci\n"
136 a generic_file f dir a f
) in ()
140 (function (files
,(a
,fs
)) ->
142 [f
] -> Printf.fprintf o
"mono_spatch_linux %s-%s.cocci &\n" a f
146 let collect_allocs l =
149 (function rest
-> function x
->
150 if List.mem x rest
then rest
else x
::rest
) in
153 function (files
,(a
,fs
)) ->
155 List.partition
(function (a1
,fs1
) -> a
= a1
) rest
in
157 [(a1
,fs1
)] -> (a
,union fs fs1
)::diff
159 | _ -> failwith
"not possible")
162 let sedify_ors o generic_file dir
l =
163 let l = collect_allocs l in
170 Printf.sprintf
"\"\\\\\\(%s\\\\\\)\""
171 (String.concat
"\\\\\\|" fs
) in
175 "sed s/ALLOC/%s/ %s | sed s/FREE/%s/ > %s/%s-%s_et_al.cocci\n"
176 a generic_file
sfs dir a f
) in ())
183 Printf.fprintf o
"mono_spatch_linux %s-%s_et_al.cocci &\n" a f
)
186 (* ------------------------------------------------------------------------ *)
189 let gen = ref "generic2.cocci"
192 let str = ref "detected allocator"
194 let options = [ "-sed", Arg.Set
sed, "sed output";
195 "-sp", Arg.String
(function x
-> gen := x
),
197 "-str", Arg.String
(function x
-> str := x
),
198 "cocci file for use with sed";
199 "-dir", Arg.String
(function x
-> dir := x
),
200 "dir for sed output"; ]
204 Arg.parse
(Arg.align
options) (fun x
-> file := x
) usage;
205 let i = open_in
!file in
209 let l = iterate !str l in
213 let o = open_out
(Printf.sprintf
"%s/files" !dir) in
214 Printf.fprintf
o "#!/bin/sh\n\n";
215 sedify o !gen !dir l;
216 sedify_ors o !gen !dir l;
217 Printf.fprintf
o "\nwait\n/bin/rm tmp*out\n";
220 if not
!sed then print_output l