2 * Copyright 2005-2009, 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.
23 (* The following finds out for each file, how it does deallocation for each
29 let l = input_line i
in
30 (if String.length
l > 2 && String.get
l 0 = '
+'
31 then info := (String.sub
l 1 (String.length
l - 1))::!info);
34 with End_of_file
-> List.rev
!info
37 let rec loop acc
= function
40 if String.get x
0 = '
+'
(* the start of a new file *)
42 (match Str.split (Str.regexp
" ") x
with
43 _
::x
::_
-> loop ((x
,[])::acc
) xs
44 | _
-> failwith
("no file: "^x
))
48 (file
,instances
)::rest
-> (file
,x
::instances
)::rest
49 | _
-> failwith
"not possible" in
51 let res = List.rev
(loop [] l) in
52 List.map
(function (x
,l) -> (x
,List.rev
l)) res
54 let detect_alloc_free str
l =
56 let (same
,diff
) = List.partition
(function (a1
,f1
) -> a
= a1
) l in
58 [(a1
,f1
)] -> if List.mem f f1
then l else (a1
,f
::f1
) :: diff
60 let rec loop acc = function
63 match Str.split (Str.regexp
(str^
"\", ")) x
with
69 (match Str.split (Str.regexp
"[, )]+") rest
with
70 alloc
::free
::_
-> try_add alloc free
acc
76 (List.map
(function (a
,f
) -> (a
,List.sort compare f
)) (loop [] l))
78 let rec iterate str
= function
85 List.partition
(function (x1
,l1
) -> l1
= info) rest
in
87 [(files
,info)] -> (x
::files
,info)::diff
88 | _
-> ([x
],info)::diff
)
89 (iterate str xs
) (detect_alloc_free str
l)
91 (* ------------------------------------------------------------------------ *)
92 (* The following prints that information *)
96 (function (files
,(a
,fs
)) ->
97 List.iter
(function x
-> Printf.printf
"%s\n" x
) files
;
98 Printf.printf
" alloc: %s, free: %s\n" a
(String.concat
", " fs
);
102 (* ------------------------------------------------------------------------ *)
103 (* The following makes a semantic patch for that information *)
105 let sedify o generic_file dir
l =
107 (function (files
,(a
,fs
)) ->
113 "sed s/ALLOC/%s/ %s | sed s/FREE/%s/ > %s/%s-%s.cocci\n"
114 a generic_file f dir a f
) in ()
118 (function (files
,(a
,fs
)) ->
120 [f
] -> Printf.fprintf o
"mono_spatch_linux %s-%s.cocci &\n" a f
124 let collect_allocs l =
127 (function rest
-> function x
->
128 if List.mem x rest
then rest
else x
::rest
) in
131 function (files
,(a
,fs
)) ->
133 List.partition
(function (a1
,fs1
) -> a
= a1
) rest
in
135 [(a1
,fs1
)] -> (a
,union fs fs1
)::diff
137 | _ -> failwith
"not possible")
140 let sedify_ors o generic_file dir
l =
141 let l = collect_allocs l in
148 Printf.sprintf
"\"\\\\\\(%s\\\\\\)\""
149 (String.concat
"\\\\\\|" fs
) in
153 "sed s/ALLOC/%s/ %s | sed s/FREE/%s/ > %s/%s-%s_et_al.cocci\n"
154 a generic_file
sfs dir a f
) in ())
161 Printf.fprintf o
"mono_spatch_linux %s-%s_et_al.cocci &\n" a f
)
164 (* ------------------------------------------------------------------------ *)
167 let gen = ref "generic2.cocci"
170 let str = ref "detected allocator"
172 let options = [ "-sed", Arg.Set
sed, "sed output";
173 "-sp", Arg.String
(function x
-> gen := x
),
175 "-str", Arg.String
(function x
-> str := x
),
176 "cocci file for use with sed";
177 "-dir", Arg.String
(function x
-> dir := x
),
178 "dir for sed output"; ]
182 Arg.parse
(Arg.align
options) (fun x
-> file := x
) usage;
183 let i = open_in
!file in
187 let l = iterate !str l in
191 let o = open_out
(Printf.sprintf
"%s/files" !dir) in
192 Printf.fprintf
o "#!/bin/sh\n\n";
193 sedify o !gen !dir l;
194 sedify_ors o !gen !dir l;
195 Printf.fprintf
o "\nwait\n/bin/rm tmp*out\n";
198 if not
!sed then print_output l