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.
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.
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.
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/>.
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
26 * Copyright 2010, INRIA, University of Copenhagen
27 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
28 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
29 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
30 * This file is part of Coccinelle.
32 * Coccinelle is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, according to version 2 of the License.
36 * Coccinelle is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
44 * The authors reserve the right to distribute this or future versions of
45 * Coccinelle under other licenses.
52 (*****************************************************************************)
54 (*****************************************************************************)
56 (* There can be multiple .c for the same cocci file. The convention
57 * is to have one base.cocci and a base.c and some optional
60 * If want to test without iso, use -iso_file empty.iso option.
62 let testone prefix x compare_with_expected_flag
=
63 let x = if x =~
"\\(.*\\)_ver0$" then matched1
x else x in
64 let base = if x =~
"\\(.*\\)_ver[0-9]+$" then matched1
x else x in
66 let cfile = prefix ^
x ^
".c" in
67 let cocci_file = prefix ^
base ^
".cocci" in
69 let expected_res = prefix ^
x ^
".res" in
71 let cocci_infos = Cocci.pre_engine
(cocci_file, !Config.std_iso
) in
72 let res = Cocci.full_engine
cocci_infos [cfile] in
73 Cocci.post_engine
cocci_infos;
75 match Common.optionise
(fun () -> List.assoc
cfile res) with
76 | Some
(Some outfile
) ->
77 if List.length
res > 1
78 then pr2
("note that not just " ^
cfile ^
" was involved");
80 let tmpfile = "/tmp/"^
Common.basename
cfile in
81 pr2
(sprintf
"One file modified. Result is here: %s" tmpfile);
82 Common.command2
("mv "^outfile^
" "^
tmpfile);
85 pr2
"no modification on the input file";
87 | None
-> raise Impossible
89 if compare_with_expected_flag
91 Compare_c.compare_default
generated expected_res
92 +> Compare_c.compare_result_to_string
97 (* ------------------------------------------------------------------------ *)
98 (* note: if you get some weird results in -testall, and not in -test,
99 * it is possible that a test file work in -test but may not
100 * work while used inside a -testall. If we have some bugs in our
101 * parser that modify some global state and that those states
102 * are not reseted between each test file, then having run previous
103 * test files may have an influence on another test file which mean
104 * than a test may work in isolation (via -test) but not otherwise
105 * (via -testall). Fortunately such bugs are rare.
108 let testall ?
(expected_score_file
="tests/SCORE_expected.sexp") () =
110 let score = empty_score
() in
112 let expected_result_files =
113 Common.glob
"tests/*.res"
114 +> List.filter
(fun f
-> Common.filesize f
> 0)
115 +> List.map
Filename.basename
120 expected_result_files +> List.iter
(fun res ->
122 if res =~
"\\(.*\\).res" then matched1
res else raise Impossible
in
123 let base = if x =~
"\\(.*\\)_ver[0-9]+" then matched1
x else x in
124 let cfile = "tests/" ^
x ^
".c" in
125 let cocci_file = "tests/" ^
base ^
".cocci" in
126 let expected = "tests/" ^
res in
128 let timeout_testall = 30 in
131 Common.timeout_function
timeout_testall (fun () ->
135 let cocci_infos = Cocci.pre_engine
(cocci_file, !Config.std_iso
) in
136 let xs = Cocci.full_engine
cocci_infos [cfile] in
137 Cocci.post_engine
cocci_infos;
140 match List.assoc
cfile xs with
141 | Some
generated -> generated
145 let (correct
, diffxs
) = Compare_c.compare_default
generated expected
148 (* I don't use Compare_c.compare_result_to_string because
149 * I want to indent a little more the messages.
152 | Compare_c.Correct
-> Hashtbl.add
score res Common.Ok
;
154 let s = Str.global_replace
155 (Str.regexp
"\"/tmp/cocci-output.*\"") "<COCCIOUTPUTFILE>" s
157 (* on macos the temporary files are stored elsewhere *)
158 let s = Str.global_replace
159 (Str.regexp
"\"/var/folders/.*/cocci-output.*\"") "<COCCIOUTPUTFILE>" s
162 "INCORRECT:" ^
s ^
"\n" ^
163 " diff (result(<) vs expected_result(>)) = \n" ^
164 (diffxs
+> List.map
(fun s -> " "^
s^
"\n") +> Common.join
"")
166 Hashtbl.add
score res (Common.Pb
s)
167 | Compare_c.PbOnlyInNotParsedCorrectly
s ->
169 "seems incorrect, but only because of code that " ^
170 "was not parsable" ^
s
172 Hashtbl.add
score res (Common.Pb
s)
177 Common.reset_pr_indent
();
178 let s = "PROBLEM\n" ^
(" exn = " ^
Printexc.to_string exn ^
"\n") in
179 Hashtbl.add
score res (Common.Pb
s)
183 pr2
"--------------------------------";
185 pr2
"--------------------------------";
187 Common.hash_to_list
score +> List.iter
(fun (s, v
) ->
188 pr_no_nl
(Printf.sprintf
"%-30s: " s);
191 | Common.Ok
-> "CORRECT\n"
195 flush stdout
; flush stderr
;
197 pr2
"--------------------------------";
198 pr2
"regression testing information";
199 pr2
"--------------------------------";
201 (* now default argument of testall:
202 let expected_score_file = "tests/SCORE_expected.sexp" in
204 let expected_score_file_orig = "tests/SCORE_expected_orig.sexp" in
205 let best_of_both_file = "tests/SCORE_best_of_both.sexp" in
206 let actual_score_file = "tests/SCORE_actual.sexp" in
208 pr2
("regression file: "^
expected_score_file);
209 let (expected_score
: score) =
210 if Sys.file_exists
expected_score_file
212 let sexp = Sexp.load_sexp
expected_score_file in
213 Sexp_common.score_of_sexp
sexp
215 if Sys.file_exists
expected_score_file_orig
217 pr2
(spf
"use expected orig file (%s)" expected_score_file_orig);
218 Common.command2
(spf
"cp %s %s" expected_score_file_orig
219 expected_score_file);
220 let sexp = Sexp.load_sexp
expected_score_file in
221 Sexp_common.score_of_sexp
sexp
227 let new_bestscore = Common.regression_testing_vs
score expected_score
in
230 let xs = Common.hash_to_list
score in
231 let sexp = Sexp_common.sexp_of_score_list
xs in
232 let s_score = Sexp.to_string_hum
sexp in
233 Common.write_file ~file
:(actual_score_file) s_score;
235 let xs2 = Common.hash_to_list
new_bestscore in
236 let sexp2 = Sexp_common.sexp_of_score_list
xs2 in
237 let s_score2 = Sexp.to_string_hum
sexp2 in
238 Common.write_file ~file
:(best_of_both_file) s_score2;
240 Common.print_total_score
score;
242 let (good
, total
) = Common.total_scores
score in
243 let (expected_good
, expected_total
) = Common.total_scores expected_score
in
245 if good
= expected_good
247 pr2
"Current score is equal to expected score; everything is fine";
251 if good
< expected_good
253 pr2
"Current score is lower than expected :(";
254 pr2
(spf
"(was expecting %d but got %d)" expected_good good
);
256 pr2
"If you think it's normal, then maybe you need to update the";
257 pr2
(spf
"score file %s, copying info from %s."
258 expected_score_file actual_score_file);
262 pr2
"Current score is greater than expected :)";
263 pr2
(spf
"(was expecting %d but got %d)" expected_good good
);
264 pr2
"Generating new expected score file and saving old one";
265 Common.command2_y_or_no_exit_if_no
266 (spf
"mv %s %s" expected_score_file (expected_score_file ^
".save"));
267 Common.command2_y_or_no_exit_if_no
268 (spf
"mv %s %s" best_of_both_file expected_score_file);
274 (* ------------------------------------------------------------------------ *)
276 type okfailed
= Ok
| SpatchOK
| Failed
279 let t_to_s = function
281 | SpatchOK
-> ".spatch_ok"
282 | Failed
-> ".failed"
284 let delete_previous_result_files infile
=
285 [Ok
;SpatchOK
;Failed
] +> List.iter
(fun kind
->
286 Common.command2
("rm -f " ^ infile ^
t_to_s kind
)
289 (* quite similar to compare_with_expected below *)
290 let test_okfailed cocci_file cfiles
=
291 cfiles
+> List.iter
delete_previous_result_files;
293 (* final_files contain the name of an output file (a .ok or .failed
294 * or .spatch_ok), and also some additionnal strings to be printed in
295 * this output file in addition to the general error message of
297 let final_files = ref [] in
301 Common.new_temp_file
"cocci" ".stdout"
304 let t = Unix.gettimeofday
() in
305 let time_per_file_str () =
306 let t'
= Unix.gettimeofday
() in
307 let tdiff = t'
-. t in
308 let tperfile = tdiff /. (float_of_int
(List.length cfiles
)) in
309 spf
"time: %f" tperfile
312 Common.redirect_stdout_stderr
newout (fun () ->
314 Common.timeout_function_opt
!Flag_cocci.timeout
(fun () ->
316 let cocci_infos = Cocci.pre_engine
(cocci_file, !Config.std_iso
) in
317 let outfiles = Cocci.full_engine
cocci_infos cfiles
in
318 Cocci.post_engine
cocci_infos;
320 let time_str = time_per_file_str () in
322 outfiles +> List.iter
(fun (infile
, outopt
) ->
323 let (dir
, base, ext
) = Common.dbe_of_filename infile
in
324 let expected_suffix =
328 | s -> pr2
("WEIRD: not a .c or .h :" ^
base ^
"." ^
s);
329 "" (* no extension, will compare to same file *)
332 Common.filename_of_dbe
(dir
, base, expected_suffix) in
334 Common.filename_of_dbe
(dir
,"corrected_"^
base,expected_suffix)
337 (* can delete more than the first delete_previous_result_files
338 * because here we can have more files than in cfiles, for instance
341 delete_previous_result_files infile
;
343 match outopt
, Common.lfile_exists
expected_res with
346 | Some outfile
, false ->
347 let s =("PB: input file " ^ infile ^
" modified but no .res") in
348 push2
(infile^
t_to_s Failed
, [s;time_str]) final_files
353 | Some
outfile -> outfile
357 let diff = Compare_c.compare_default
outfile expected_res in
358 let s1 = (Compare_c.compare_result_to_string
diff) in
359 if fst
diff =*= Compare_c.Correct
360 then push2
(infile ^
(t_to_s Ok
), [s1;time_str]) final_files
362 if Common.lfile_exists
expected_res2
364 let diff = Compare_c.compare_default
outfile expected_res2 in
365 let s2 = Compare_c.compare_result_to_string
diff in
366 if fst
diff =*= Compare_c.Correct
367 then push2
(infile ^
(t_to_s SpatchOK
),[s2;s1;time_str])
369 else push2
(infile ^
(t_to_s Failed
), [s2;s1;time_str])
372 else push2
(infile ^
(t_to_s Failed
), [s1;time_str]) final_files
378 Str.global_replace
(Str.regexp
"\\\\n") "\n"
379 (Str.global_replace
(Str.regexp
("\\\\\"")) "\""
380 (Str.global_replace
(Str.regexp
"\\\\t") "\t" s)) in
381 let s = "PROBLEM\n"^
(" exn = " ^
clean(Printexc.to_string exn
) ^
"\n")
383 let time_str = time_per_file_str ()
385 (* we may miss some file because cfiles is shorter than outfiles.
386 * For instance the detected local headers are not in cfiles, so
387 * may have less failed. But at least have some failed.
389 cfiles
+> List.iter
(fun infile
->
390 push2
(infile ^
(t_to_s Failed
), [s;time_str]) final_files;
393 !final_files +> List.iter
(fun (file
, additional_strs
) ->
394 Common.command2
("cp " ^
newout ^
" " ^ file
);
395 with_open_outfile file
(fun (pr
, chan
) ->
396 additional_strs
+> List.iter
(fun s -> pr
(s ^
"\n"))
402 let test_regression_okfailed () =
405 let chop_ext f
= f
+> Filename.chop_extension
in
407 let newscore = Common.empty_score
() in
409 Common.cmd_to_list
("find -name \"*.ok\"")
411 Common.cmd_to_list
("find -name \"*.spatch_ok\"")
413 let failed = Common.cmd_to_list
("find -name \"*.failed\"") in
415 if null
(oks ++ failed)
416 then failwith
"no ok/failed file, you certainly did a make clean"
418 oks +> List.iter
(fun s ->
419 Hashtbl.add
newscore (chop_ext s) Common.Ok
421 failed +> List.iter
(fun s ->
422 Hashtbl.add
newscore (chop_ext s) (Common.Pb
"fail")
424 pr2
"--------------------------------";
425 pr2
"regression testing information";
426 pr2
"--------------------------------";
427 Common.regression_testing
newscore ("score_failed.marshalled")
431 (* ------------------------------------------------------------------------ *)
432 (* quite similar to test_ok_failed. Maybe could factorize code *)
433 let compare_with_expected outfiles =
435 outfiles +> List.iter
(fun (infile
, outopt
) ->
436 let (dir
, base, ext
) = Common.dbe_of_filename infile
in
437 let expected_suffix =
441 | s -> failwith
("weird C file, not a .c or .h :" ^
s)
444 Common.filename_of_dbe
(dir
, base, expected_suffix) in
446 Common.filename_of_dbe
(dir
,"corrected_"^
base,expected_suffix)
449 match outopt
, Common.lfile_exists
expected_res with
451 | Some
outfile, false ->
452 let s =("PB: input file " ^ infile ^
" modified but no .res") in
457 | Some
outfile -> outfile
460 let diff = Compare_c.compare_default
outfile expected_res in
461 let s1 = (Compare_c.compare_result_to_string
diff) in
462 if fst
diff =*= Compare_c.Correct
463 then pr2_no_nl
(infile ^
" " ^
s1)
465 if Common.lfile_exists
expected_res2
467 let diff = Compare_c.compare_default
outfile expected_res2 in
468 let s2 = Compare_c.compare_result_to_string
diff in
469 if fst
diff =*= Compare_c.Correct
470 then pr2
(infile ^
" is spatchOK " ^
s2)
471 else pr2
(infile ^
" is failed " ^
s2)
473 else pr2
(infile ^
" is failed " ^
s1)
476 (*****************************************************************************)
477 (* Subsystem testing *)
478 (*****************************************************************************)
480 let test_parse_cocci file
=
481 if not
(file
=~
".*\\.cocci")
482 then pr2
"warning: seems not a .cocci file";
484 let (_
,xs,_
,_
,_
,_
,(grep_tokens
,query
,_
)) =
485 Parse_cocci.process file
(Some
!Config.std_iso
) false in
486 xs +> List.iter
Pretty_print_cocci.unparse
;
487 Format.print_newline
();
488 (* compile ocaml script code *)
489 (match Prepare_ocamlcocci.prepare file
xs with
491 | Some ocaml_script_file
->
493 Prepare_ocamlcocci.load_file ocaml_script_file
;
495 (if not
!Common.save_tmp_files
496 then Prepare_ocamlcocci.clean_file ocaml_script_file
);
497 (* Print the list of registered functions *)
498 Prepare_ocamlcocci.test
());
499 Printf.printf
"grep tokens\n";
500 (match grep_tokens
with
501 None
-> pr
"No query"
502 | Some
x -> pr
(String.concat
" || " x));
503 match !Flag.scanner
with
504 Flag.NoScanner
| Flag.Grep
-> ()
505 | Flag.Glimpse
| Flag.IdUtils
| Flag.Google _
->
506 Printf.printf
"%s tokens\n"
507 (if !Flag.scanner
= Flag.Glimpse
then "glimpse" else "google");
509 None
-> pr
"No query"
510 | Some
x -> pr
(String.concat
" ||\n" x))
514 (*****************************************************************************)
515 (* to be called by ocaml toplevel, to test. *)
516 (*****************************************************************************)
518 (* no point to memoize this one *)
519 let sp_of_file file iso
= Parse_cocci.process file iso
false
525 let flows_of_ast astc =
526 astc +> Common.map_filter (fun e -> ast_to_flow_with_error_messages e)
531 let one_ctl ctls = List.hd (List.hd ctls)