2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
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.
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.
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.
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/>.
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
30 (*****************************************************************************)
32 (*****************************************************************************)
34 (* There can be multiple .c for the same cocci file. The convention
35 * is to have one base.cocci and a base.c and some optional
38 * If want to test without iso, use -iso_file empty.iso option.
40 let testone prefix x compare_with_expected_flag
=
41 let x = if x =~
"\\(.*\\)_ver0$" then matched1
x else x in
42 let base = if x =~
"\\(.*\\)_ver[0-9]+$" then matched1
x else x in
44 let cfile = prefix ^
x ^
".c" in
45 let cocci_file = prefix ^
base ^
".cocci" in
47 let expected_res = prefix ^
x ^
".res" in
49 let cocci_infos = Cocci.pre_engine
(cocci_file, !Config.std_iso
) in
50 let res = Cocci.full_engine
cocci_infos [cfile] in
51 Cocci.post_engine
cocci_infos;
53 match Common.optionise
(fun () -> List.assoc
cfile res) with
54 | Some
(Some outfile
) ->
55 if List.length
res > 1
56 then pr2
("note that not just " ^
cfile ^
" was involved");
58 let tmpfile = "/tmp/"^
Common.basename
cfile in
59 pr2
(sprintf
"One file modified. Result is here: %s" tmpfile);
60 Common.command2
("mv "^outfile^
" "^
tmpfile);
63 pr2
"no modification on the input file";
65 | None
-> raise Impossible
67 if compare_with_expected_flag
69 Compare_c.compare_default
generated expected_res
70 +> Compare_c.compare_result_to_string
75 (* ------------------------------------------------------------------------ *)
76 (* note: if you get some weird results in -testall, and not in -test,
77 * it is possible that a test file work in -test but may not
78 * work while used inside a -testall. If we have some bugs in our
79 * parser that modify some global state and that those states
80 * are not reseted between each test file, then having run previous
81 * test files may have an influence on another test file which mean
82 * than a test may work in isolation (via -test) but not otherwise
83 * (via -testall). Fortunately such bugs are rare.
86 let testall ?
(expected_score_file
="tests/SCORE_expected.sexp") () =
88 let score = empty_score
() in
90 let expected_result_files =
91 Common.glob
"tests/*.res"
92 +> List.filter
(fun f
-> Common.filesize f
> 0)
93 +> List.map
Filename.basename
98 expected_result_files +> List.iter
(fun res ->
100 if res =~
"\\(.*\\).res" then matched1
res else raise Impossible
in
101 let base = if x =~
"\\(.*\\)_ver[0-9]+" then matched1
x else x in
102 let cfile = "tests/" ^
x ^
".c" in
103 let cocci_file = "tests/" ^
base ^
".cocci" in
104 let expected = "tests/" ^
res in
106 let timeout_testall = 30 in
109 Common.timeout_function
timeout_testall (fun () ->
113 let cocci_infos = Cocci.pre_engine
(cocci_file, !Config.std_iso
) in
114 let xs = Cocci.full_engine
cocci_infos [cfile] in
115 Cocci.post_engine
cocci_infos;
118 match List.assoc
cfile xs with
119 | Some
generated -> generated
123 let (correct
, diffxs
) = Compare_c.compare_default
generated expected
126 (* I don't use Compare_c.compare_result_to_string because
127 * I want to indent a little more the messages.
130 | Compare_c.Correct
-> Hashtbl.add
score res Common.Ok
;
132 let s = Str.global_replace
133 (Str.regexp
"\"/tmp/cocci-output.*\"") "<COCCIOUTPUTFILE>" s
135 (* on macos the temporary files are stored elsewhere *)
136 let s = Str.global_replace
137 (Str.regexp
"\"/var/folders/.*/cocci-output.*\"") "<COCCIOUTPUTFILE>" s
140 "INCORRECT:" ^
s ^
"\n" ^
141 " diff (result(<) vs expected_result(>)) = \n" ^
142 (diffxs
+> List.map
(fun s -> " "^
s^
"\n") +> Common.join
"")
144 Hashtbl.add
score res (Common.Pb
s)
145 | Compare_c.PbOnlyInNotParsedCorrectly
s ->
147 "seems incorrect, but only because of code that " ^
148 "was not parsable" ^
s
150 Hashtbl.add
score res (Common.Pb
s)
155 Common.reset_pr_indent
();
156 let s = "PROBLEM\n" ^
(" exn = " ^
Printexc.to_string exn ^
"\n") in
157 Hashtbl.add
score res (Common.Pb
s)
161 pr2
"--------------------------------";
163 pr2
"--------------------------------";
165 Common.hash_to_list
score +> List.iter
(fun (s, v
) ->
166 pr_no_nl
(Printf.sprintf
"%-30s: " s);
169 | Common.Ok
-> "CORRECT\n"
173 flush stdout
; flush stderr
;
175 pr2
"--------------------------------";
176 pr2
"regression testing information";
177 pr2
"--------------------------------";
179 (* now default argument of testall:
180 let expected_score_file = "tests/SCORE_expected.sexp" in
182 let expected_score_file_orig = "tests/SCORE_expected_orig.sexp" in
183 let best_of_both_file = "tests/SCORE_best_of_both.sexp" in
184 let actual_score_file = "tests/SCORE_actual.sexp" in
186 pr2
("regression file: "^
expected_score_file);
187 let (expected_score
: score) =
188 if Sys.file_exists
expected_score_file
190 let sexp = Sexp.load_sexp
expected_score_file in
191 Sexp_common.score_of_sexp
sexp
193 if Sys.file_exists
expected_score_file_orig
195 pr2
(spf
"use expected orig file (%s)" expected_score_file_orig);
196 Common.command2
(spf
"cp %s %s" expected_score_file_orig
197 expected_score_file);
198 let sexp = Sexp.load_sexp
expected_score_file in
199 Sexp_common.score_of_sexp
sexp
205 let new_bestscore = Common.regression_testing_vs
score expected_score
in
208 let xs = Common.hash_to_list
score in
209 let sexp = Sexp_common.sexp_of_score_list
xs in
210 let s_score = Sexp.to_string_hum
sexp in
211 Common.write_file ~file
:(actual_score_file) s_score;
213 let xs2 = Common.hash_to_list
new_bestscore in
214 let sexp2 = Sexp_common.sexp_of_score_list
xs2 in
215 let s_score2 = Sexp.to_string_hum
sexp2 in
216 Common.write_file ~file
:(best_of_both_file) s_score2;
218 Common.print_total_score
score;
220 let (good
, total
) = Common.total_scores
score in
221 let (expected_good
, expected_total
) = Common.total_scores expected_score
in
223 if good
= expected_good
225 pr2
"Current score is equal to expected score; everything is fine";
229 if good
< expected_good
231 pr2
"Current score is lower than expected :(";
232 pr2
(spf
"(was expecting %d but got %d)" expected_good good
);
234 pr2
"If you think it's normal, then maybe you need to update the";
235 pr2
(spf
"score file %s, copying info from %s."
236 expected_score_file actual_score_file);
240 pr2
"Current score is greater than expected :)";
241 pr2
(spf
"(was expecting %d but got %d)" expected_good good
);
242 pr2
"Generating new expected score file and saving old one";
243 Common.command2_y_or_no_exit_if_no
244 (spf
"mv %s %s" expected_score_file (expected_score_file ^
".save"));
245 Common.command2_y_or_no_exit_if_no
246 (spf
"mv %s %s" best_of_both_file expected_score_file);
252 (* ------------------------------------------------------------------------ *)
254 type okfailed
= Ok
| SpatchOK
| Failed
257 let t_to_s = function
259 | SpatchOK
-> ".spatch_ok"
260 | Failed
-> ".failed"
262 let delete_previous_result_files infile
=
263 [Ok
;SpatchOK
;Failed
] +> List.iter
(fun kind
->
264 Common.command2
("rm -f " ^ infile ^
t_to_s kind
)
267 (* quite similar to compare_with_expected below *)
268 let test_okfailed cocci_file cfiles
=
269 cfiles
+> List.iter
delete_previous_result_files;
271 (* final_files contain the name of an output file (a .ok or .failed
272 * or .spatch_ok), and also some additionnal strings to be printed in
273 * this output file in addition to the general error message of
275 let final_files = ref [] in
279 Common.new_temp_file
"cocci" ".stdout"
282 let t = Unix.gettimeofday
() in
283 let time_per_file_str () =
284 let t'
= Unix.gettimeofday
() in
285 let tdiff = t'
-. t in
286 let tperfile = tdiff /. (float_of_int
(List.length cfiles
)) in
287 spf
"time: %f" tperfile
290 Common.redirect_stdout_stderr
newout (fun () ->
292 Common.timeout_function_opt
!Flag_cocci.timeout
(fun () ->
294 let cocci_infos = Cocci.pre_engine
(cocci_file, !Config.std_iso
) in
295 let outfiles = Cocci.full_engine
cocci_infos cfiles
in
296 Cocci.post_engine
cocci_infos;
298 let time_str = time_per_file_str () in
300 outfiles +> List.iter
(fun (infile
, outopt
) ->
301 let (dir
, base, ext
) = Common.dbe_of_filename infile
in
302 let expected_suffix =
306 | s -> pr2
("WEIRD: not a .c or .h :" ^
base ^
"." ^
s);
307 "" (* no extension, will compare to same file *)
310 Common.filename_of_dbe
(dir
, base, expected_suffix) in
312 Common.filename_of_dbe
(dir
,"corrected_"^
base,expected_suffix)
315 (* can delete more than the first delete_previous_result_files
316 * because here we can have more files than in cfiles, for instance
319 delete_previous_result_files infile
;
321 match outopt
, Common.lfile_exists
expected_res with
324 | Some outfile
, false ->
325 let s =("PB: input file " ^ infile ^
" modified but no .res") in
326 push2
(infile^
t_to_s Failed
, [s;time_str]) final_files
331 | Some
outfile -> outfile
335 let diff = Compare_c.compare_default
outfile expected_res in
336 let s1 = (Compare_c.compare_result_to_string
diff) in
337 if fst
diff =*= Compare_c.Correct
338 then push2
(infile ^
(t_to_s Ok
), [s1;time_str]) final_files
340 if Common.lfile_exists
expected_res2
342 let diff = Compare_c.compare_default
outfile expected_res2 in
343 let s2 = Compare_c.compare_result_to_string
diff in
344 if fst
diff =*= Compare_c.Correct
345 then push2
(infile ^
(t_to_s SpatchOK
),[s2;s1;time_str])
347 else push2
(infile ^
(t_to_s Failed
), [s2;s1;time_str])
350 else push2
(infile ^
(t_to_s Failed
), [s1;time_str]) final_files
356 Str.global_replace
(Str.regexp
"\\\\n") "\n"
357 (Str.global_replace
(Str.regexp
("\\\\\"")) "\""
358 (Str.global_replace
(Str.regexp
"\\\\t") "\t" s)) in
359 let s = "PROBLEM\n"^
(" exn = " ^
clean(Printexc.to_string exn
) ^
"\n")
361 let time_str = time_per_file_str ()
363 (* we may miss some file because cfiles is shorter than outfiles.
364 * For instance the detected local headers are not in cfiles, so
365 * may have less failed. But at least have some failed.
367 cfiles
+> List.iter
(fun infile
->
368 push2
(infile ^
(t_to_s Failed
), [s;time_str]) final_files;
371 !final_files +> List.iter
(fun (file
, additional_strs
) ->
372 Common.command2
("cp " ^
newout ^
" " ^ file
);
373 with_open_outfile file
(fun (pr
, chan
) ->
374 additional_strs
+> List.iter
(fun s -> pr
(s ^
"\n"))
380 let test_regression_okfailed () =
383 let chop_ext f
= f
+> Filename.chop_extension
in
385 let newscore = Common.empty_score
() in
387 Common.cmd_to_list
("find . -name \"*.ok\"")
389 Common.cmd_to_list
("find . -name \"*.spatch_ok\"")
391 let failed = Common.cmd_to_list
("find . -name \"*.failed\"") in
393 if null
(oks ++ failed)
394 then failwith
"no ok/failed file, you certainly did a make clean"
396 oks +> List.iter
(fun s ->
397 Hashtbl.add
newscore (chop_ext s) Common.Ok
399 failed +> List.iter
(fun s ->
400 Hashtbl.add
newscore (chop_ext s) (Common.Pb
"fail")
402 pr2
"--------------------------------";
403 pr2
"regression testing information";
404 pr2
"--------------------------------";
405 Common.regression_testing
newscore ("score_failed.marshalled")
409 (* ------------------------------------------------------------------------ *)
410 (* quite similar to test_ok_failed. Maybe could factorize code *)
411 let compare_with_expected outfiles =
413 outfiles +> List.iter
(fun (infile
, outopt
) ->
414 let (dir
, base, ext
) = Common.dbe_of_filename infile
in
415 let expected_suffix =
419 | s -> failwith
("weird C file, not a .c or .h :" ^
s)
422 Common.filename_of_dbe
(dir
, base, expected_suffix) in
424 Common.filename_of_dbe
(dir
,"corrected_"^
base,expected_suffix)
427 match outopt
, Common.lfile_exists
expected_res with
429 | Some
outfile, false ->
430 let s =("PB: input file " ^ infile ^
" modified but no .res") in
435 | Some
outfile -> outfile
438 let diff = Compare_c.compare_default
outfile expected_res in
439 let s1 = (Compare_c.compare_result_to_string
diff) in
440 if fst
diff =*= Compare_c.Correct
441 then pr2_no_nl
(infile ^
" " ^
s1)
443 if Common.lfile_exists
expected_res2
445 let diff = Compare_c.compare_default
outfile expected_res2 in
446 let s2 = Compare_c.compare_result_to_string
diff in
447 if fst
diff =*= Compare_c.Correct
448 then pr2
(infile ^
" is spatchOK " ^
s2)
449 else pr2
(infile ^
" is failed " ^
s2)
451 else pr2
(infile ^
" is failed " ^
s1)
454 (*****************************************************************************)
455 (* Subsystem testing *)
456 (*****************************************************************************)
458 let test_parse_cocci file
=
459 if not
(file
=~
".*\\.cocci")
460 then pr2
"warning: seems not a .cocci file";
462 let (_
,xs,_
,_
,_
,_
,(grep_tokens
,query
,_
)) =
463 Parse_cocci.process file
(Some
!Config.std_iso
) false in
464 xs +> List.iter
Pretty_print_cocci.unparse
;
465 Format.print_newline
();
466 (* compile ocaml script code *)
467 (match Prepare_ocamlcocci.prepare file
xs with
469 | Some ocaml_script_file
->
471 Prepare_ocamlcocci.load_file ocaml_script_file
;
473 (if not
!Common.save_tmp_files
474 then Prepare_ocamlcocci.clean_file ocaml_script_file
);
475 (* Print the list of registered functions *)
476 Prepare_ocamlcocci.test
());
477 Printf.printf
"grep tokens\n";
478 (match grep_tokens
with
479 None
-> pr
"No query"
480 | Some
x -> pr
(String.concat
" || " x));
481 match !Flag.scanner
with
482 Flag.NoScanner
| Flag.Grep
-> ()
483 | Flag.Glimpse
| Flag.IdUtils
| Flag.Google _
->
484 Printf.printf
"%s tokens\n"
485 (if !Flag.scanner
= Flag.Glimpse
then "glimpse" else "google");
487 None
-> pr
"No query"
488 | Some
x -> pr
(String.concat
"\nor on glimpse failure\n" x))
492 (*****************************************************************************)
493 (* to be called by ocaml toplevel, to test. *)
494 (*****************************************************************************)
496 (* no point to memoize this one *)
497 let sp_of_file file iso
= Parse_cocci.process file iso
false
503 let flows_of_ast astc =
504 astc +> Common.map_filter (fun e -> ast_to_flow_with_error_messages e)
509 let one_ctl ctls = List.hd (List.hd ctls)