2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller
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 module FC
= Flag_cocci
26 (*****************************************************************************)
28 (*****************************************************************************)
30 (* In addition to flags that can be tweaked via -xxx options (cf the
31 * full list of options in "the spatch options" section below), the
32 * spatch program also depends on external files, described in
33 * globals/config.ml, mainly a standard.h and standard.iso file *)
35 let cocci_file = ref ""
37 let output_file = ref ""
38 let inplace_modif = ref false (* but keeps a .cocci_orig *)
39 let outplace_modif = ref false (* generates a .cocci_res *)
41 (* somehow obsolete now *)
44 let include_headers = ref false
45 let kbuild_info = ref ""
47 let macro_file = ref ""
50 let test_mode = ref false
51 let test_all = ref false
52 let test_okfailed = ref false
53 let test_regression_okfailed = ref false
54 let expected_score_file = ref ""
60 (* works with -test but also in "normal" spatch mode *)
61 let compare_with_expected = ref false
63 let distrib_index = ref (None
: int option)
64 let distrib_max = ref (None
: int option)
65 let mod_distrib = ref false
68 (*****************************************************************************)
70 (*****************************************************************************)
72 (* pair of (list of flags to set true, list of flags to set false *)
77 (* FC.show_diff; just leave this as it is *)
86 FC.show_before_fixed_flow
;
89 FC.show_binding_in_out
;
93 Flag_parsing_c.show_parsing_error
;
95 Flag_parsing_c.verbose_lexing
;
96 Flag_parsing_c.verbose_parsing
;
97 Flag_parsing_c.verbose_type
;
98 Flag_parsing_c.verbose_cfg
;
99 Flag_parsing_c.verbose_unparsing
;
100 Flag_parsing_c.verbose_visit
;
102 Flag_matcher.verbose_matcher
;
103 Flag_matcher.debug_engine
;
105 Flag_parsing_c.debug_unparsing
;
107 Flag_parsing_cocci.show_SP
;
108 Flag_parsing_cocci.show_iso_failures
;
110 Flag_ctl.verbose_ctl_engine
;
111 Flag_ctl.verbose_match
;
116 (* some information that is useful in seeing why a semantic patch doesn't
118 let debug_profile = (
125 FC.show_binding_in_out
;
126 FC.show_dependencies
;
128 Flag_parsing_cocci.show_iso_failures
;
132 Flag_parsing_c.verbose_cfg
;
133 Flag_parsing_c.verbose_unparsing
;
134 Flag_parsing_c.verbose_visit
;
136 Flag_matcher.verbose_matcher
;
138 Flag_parsing_c.show_parsing_error
;
146 FC.show_before_fixed_flow
;
150 Flag_parsing_cocci.show_SP
;
151 Flag_ctl.verbose_ctl_engine
;
152 Flag_ctl.verbose_match
;
153 Flag_matcher.debug_engine
;
154 Flag_parsing_c.debug_unparsing
;
155 Flag_parsing_c.verbose_type
;
156 Flag_parsing_c.verbose_parsing
;
171 FC.show_before_fixed_flow
;
174 FC.show_binding_in_out
;
176 Flag_parsing_cocci.show_SP
;
177 Flag_parsing_cocci.show_iso_failures
;
178 Flag_ctl.verbose_ctl_engine
;
179 Flag_ctl.verbose_match
;
180 Flag_matcher.debug_engine
;
181 Flag_parsing_c.debug_unparsing
;
182 Flag_parsing_c.verbose_type
;
183 Flag_parsing_c.verbose_parsing
;
187 let (set_to_true
, set_to_false
) = p
in
188 List.iter
(fun x
-> x
:= false) set_to_false
;
189 List.iter
(fun x
-> x
:= true) set_to_true
191 (*****************************************************************************)
192 (* The spatch options *)
193 (*****************************************************************************)
196 "Usage: " ^ basename
Sys.argv
.(0) ^
197 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
198 "\n" ^
"Options are:"
200 (* forward reference trick *)
201 let short_usage_func = ref (fun () -> ())
202 let long_usage_func = ref (fun () -> ())
205 (* The short_options are user-oriented. The other options are for
206 * the developers of coccinelle or advanced-users that know
207 * quite well the underlying semantics of coccinelle.
211 (* will be printed when use only ./spatch. For the rest you have to
212 * use -longhelp to see them.
214 let short_options = [
215 "-sp_file", Arg.Set_string
cocci_file,
216 " <file> the semantic patch file";
218 "-o", Arg.Set_string
output_file,
219 " <file> the output file";
220 "-inplace", Arg.Set
inplace_modif,
221 " do the modification on the file directly";
222 "-outplace", Arg.Set
outplace_modif,
223 " store modifications in a .cocci_res file";
225 "-U", Arg.Int
(fun n
-> Flag_parsing_c.diff_lines
:= Some
(i_to_s n
)),
226 " set number of diff context lines";
227 "-partial_match", Arg.Set
Flag_ctl.partial_match
,
228 " report partial matches of the SP on the C file";
230 "-iso_file", Arg.Set_string
Config.std_iso
,
231 " <file> (default=" ^
!Config.std_iso ^
")";
232 "-macro_file", Arg.Set_string
macro_file,
234 "-macro_file_builtins", Arg.Set_string
Config.std_h
,
235 " <file> (default=" ^
!Config.std_h ^
")";
238 Arg.Unit
(function _
-> FC.include_options
:= FC.I_ALL_INCLUDES
),
239 " causes all available include files to be used";
241 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NO_INCLUDES
),
242 " causes not even local include files to be used";
244 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NORMAL_INCLUDES
),
245 " causes local include files to be used";
246 "-include_headers", Arg.Set
include_headers,
247 " process header files independently";
248 "-I", Arg.String
(function x
-> FC.include_path
:= Some x
),
249 " <dir> containing the header files (optional)";
253 " <dir> process all files in directory recursively";
255 "-use_glimpse", Arg.Set
Flag.use_glimpse
,
256 " works with -dir, use info generated by glimpseindex";
257 "-patch", Arg.String
(function s
-> Flag.patch
:= Some s
),
258 (" <dir> path name with respect to which a patch should be created\n"^
259 " \"\" for a file in the current directory");
260 "-kbuild_info", Arg.Set_string
kbuild_info,
261 " <file> improve -dir by grouping related c files";
262 "-pyoutput", Arg.Set_string
Flag.pyoutput
,
263 " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>";
266 "-version", Arg.Unit
(fun () ->
267 pr2
(spf
"spatch version: %s" Config.version
);
272 "-date", Arg.Unit
(fun () ->
273 pr2
"version: $Date: 2009/05/08 16:15:31 $";
274 raise
(Common.UnixExit
0)
278 "-shorthelp", Arg.Unit
(fun () ->
280 raise
(Common.UnixExit
0)
282 " see short list of options";
283 "-longhelp", Arg.Unit
(fun () ->
285 raise
(Common.UnixExit
0)
287 " see all the available options in different categories";
288 "-help", Arg.Unit
(fun () ->
290 raise
(Common.UnixExit
0)
293 "--help", Arg.Unit
(fun () ->
295 raise
(Common.UnixExit
0)
301 (* the format is a list of triples:
302 * (title of section * (optional) explanation of sections * option list)
304 let other_options = [
305 "aliases and obsolete options",
308 "-sp", Arg.Set_string
cocci_file, " short option of -sp_file";
309 "-iso", Arg.Set_string
Config.std_iso
, " short option of -iso_file";
311 "-cocci_file", Arg.Set_string
cocci_file,
312 " <file> the semantic patch file";
313 "-c", Arg.Set_string
cocci_file, " short option of -sp_file";
316 "most useful show options",
319 "-show_diff" , Arg.Set
FC.show_diff
, " ";
320 "-no_show_diff" , Arg.Clear
FC.show_diff
, " ";
321 "-show_flow" , Arg.Set
FC.show_flow
, " ";
322 (* works in conjunction with -show_ctl_text *)
325 (function _
-> FC.show_ctl_text
:= true; FC.inline_let_ctl
:= true), " ";
326 "-ctl_show_mcodekind",
328 (function _
-> FC.show_ctl_text
:= true; FC.show_mcodekind_in_ctl
:= true),
330 "-show_bindings", Arg.Set
FC.show_binding_in_out
, " ";
331 "-show_transinfo", Arg.Set
Flag.show_transinfo
, " ";
332 "-show_misc", Arg.Set
Flag.show_misc
, " ";
333 "-show_trying", Arg.Set
Flag.show_trying
,
334 " show the name of each function being processed";
335 "-show_dependencies",
336 Arg.Unit
(function _
-> FC.show_dependencies
:= true;
337 FC.show_binding_in_out
:= true),
338 " show the dependencies related to each rule";
341 "verbose subsystems options",
344 "-verbose_ctl_engine",
345 Arg.Unit
(function _
->
346 Flag_ctl.verbose_ctl_engine
:= true; FC.show_ctl_text
:= true) , " ";
347 "-verbose_match", Arg.Set
Flag_ctl.verbose_match
, " ";
348 "-verbose_engine", Arg.Set
Flag_matcher.debug_engine
, " ";
349 "-graphical_trace", Arg.Set
Flag_ctl.graphical_trace
, " generate a pdf file representing the matching process";
351 Arg.Unit
(function _
->
352 Flag_ctl.graphical_trace
:= true; Flag_ctl.gt_without_label
:= true),
353 " remove graph label (requires option -graphical_trace)";
355 "-parse_error_msg", Arg.Set
Flag_parsing_c.verbose_parsing
, " ";
356 "-type_error_msg", Arg.Set
Flag_parsing_c.verbose_type
, " ";
357 (* could also use Flag_parsing_c.options_verbose *)
360 "other show options",
363 "-show_c" , Arg.Set
FC.show_c
, " ";
364 "-show_cocci" , Arg.Set
FC.show_cocci
, " ";
365 "-show_before_fixed_flow" , Arg.Set
FC.show_before_fixed_flow
, " ";
366 "-show_ctl_tex" , Arg.Set
FC.show_ctl_tex
, " ";
367 "-show_ctl_text" , Arg.Set
FC.show_ctl_text
, " ";
368 "-show_SP" , Arg.Set
Flag_parsing_cocci.show_SP
, " ";
372 "debug C parsing/unparsing",
375 "-debug_cpp", Arg.Set
Flag_parsing_c.debug_cpp
, " ";
376 "-debug_lexer", Arg.Set
Flag_parsing_c.debug_lexer
, " ";
377 "-debug_etdt", Arg.Set
Flag_parsing_c.debug_etdt
, " ";
378 "-debug_typedef", Arg.Set
Flag_parsing_c.debug_typedef
, " ";
380 "-filter_msg", Arg.Set
Flag_parsing_c.filter_msg
,
381 " filter some cpp message when the macro is a \"known\" cpp construct";
382 "-filter_define_error",Arg.Set
Flag_parsing_c.filter_define_error
," ";
383 "-filter_msg_define_error",Arg.Set
Flag_parsing_c.filter_msg_define_error
,
384 " filter the error msg";
385 "-filter_passed_level", Arg.Set_int
Flag_parsing_c.filter_passed_level
," ";
386 (* debug cfg doesn't seem to have any effect, so drop it as an option *)
387 (* "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " "; *)
388 "-debug_unparsing", Arg.Set
Flag_parsing_c.debug_unparsing
, " ";
391 (* could use Flag_parsing_c.options_debug_with_title instead *)
394 "shortcut for enabling/disabling a set of debugging options at once",
397 (* todo: other profile ? *)
398 "-quiet", Arg.Unit
(fun () -> run_profile quiet_profile), " ";
399 "-debug", Arg.Unit
(fun () -> run_profile debug_profile), " ";
400 "-pad", Arg.Unit
(fun () -> run_profile pad_profile), " ";
407 "-profile", Arg.Unit
(function () -> Common.profile
:= Common.PALL
) ,
408 " gather timing information about the main coccinelle functions";
409 "-bench", Arg.Int
(function x
-> Flag_ctl.bench
:= x
),
410 " <level> for profiling the CTL engine";
411 "-timeout", Arg.Int
(fun x
-> FC.timeout
:= Some x
),
412 " <sec> timeout in seconds";
413 "-steps", Arg.Int
(fun x
-> Flag_ctl.steps
:= Some x
),
414 " max number of model checking steps per code unit";
415 "-iso_limit", Arg.Int
(fun x
-> Flag_parsing_cocci.iso_limit
:= Some x
),
416 " max depth of iso application";
417 "-no_iso_limit", Arg.Unit
(fun _
-> Flag_parsing_cocci.iso_limit
:= None
),
418 " disable limit on max depth of iso application";
419 "-track_iso", Arg.Set
Flag.track_iso_usage
,
420 " gather information about isomorphism usage";
425 (*post_engine not included, because it doesn't use isos*)
426 PSOME
["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]),
427 " gather information about the cost of isomorphism usage"
432 "change of algorithm options",
435 (* no popl in official version
436 "-popl", Arg.Set FC.popl,
437 " simplified SmPL, for the popl paper";
441 (function _ -> FC.popl := true; Flag_popl.mark_all := true),
442 " simplified SmPL, for the popl paper";
444 "-popl_keep_all_wits",
446 (function _ -> FC.popl := true; Flag_popl.keep_all_wits := true),
447 " simplified SmPL, for the popl paper";
452 Flag.make_hrule
:= Some s
; FC.include_options
:= FC.I_NO_INCLUDES
),
453 " semantic patch generation";
455 "-loop", Arg.Set
Flag_ctl.loop_in_src_code
, " ";
457 "-l1", Arg.Clear
Flag_parsing_c.label_strategy_2
, " ";
458 "-ifdef_to_if", Arg.Set
FC.ifdef_to_if
,
459 " convert ifdef to if (experimental)";
460 "-no_ifdef_to_if", Arg.Clear
FC.ifdef_to_if
,
461 " convert ifdef to if (experimental)";
463 "-disable_multi_pass", Arg.Set
Flag_parsing_c.disable_multi_pass
, " ";
465 "-noif0_passing", Arg.Clear
Flag_parsing_c.if0_passing
,
467 "-noadd_typedef_root", Arg.Clear
Flag_parsing_c.add_typedef_root
, " ";
468 (* could use Flag_parsing_c.options_algo instead *)
471 "-disallow_nested_exps", Arg.Set
Flag_matcher.disallow_nested_exps
,
472 "disallow an expresion pattern from matching a term and its subterm";
473 "-disable_worth_trying_opt", Arg.Clear
FC.worth_trying_opt
,
475 "-only_return_is_error_exit",
476 Arg.Set
Flag_matcher.only_return_is_error_exit
,
477 "if this flag is not set, then break and continue are also error exits";
478 (* the following is a hack to make it easier to add code in sgrep-like
479 code, essentially to compensate for the fact that we don't have
480 any way of printing things out *)
481 "-allow_inconsistent_paths",
482 Arg.Set
Flag_matcher.allow_inconsistent_paths
,
483 " if this flag is set don't check for inconsistent paths; dangerous";
484 "-int_bits", Arg.Int
Flag_parsing_c.set_int_bits
,
485 " the number of bits in an unsigned int";
486 "-long_bits", Arg.Int
Flag_parsing_c.set_long_bits
,
487 " the number of bits in an unsigned long";
488 "-linux_spacing", Arg.Unit
Flag_parsing_c.set_linux_spacing
,
489 " spacing of + code follows the conventions of Linux";
490 "-smpl_spacing", Arg.Unit
Flag_parsing_c.set_smpl_spacing
,
491 " spacing of + code follows the semantic patch";
497 "-debugger", Arg.Set
Common.debugger
,
498 " option to set if launch spatch in ocamldebug";
499 "-disable_once", Arg.Set
Common.disable_pr2_once
,
500 " to print more messages";
501 "-show_trace_profile", Arg.Set
Common.show_trace_profile
,
503 "-save_tmp_files", Arg.Set
Common.save_tmp_files
, " ";
509 "-index", Arg.Int
(function x
-> distrib_index := Some x
) ,
510 " the processor to use for this run of spatch";
511 "-max", Arg.Int
(function x
-> distrib_max := Some x
) ,
512 " the number of processors available";
513 "-mod_distrib", Arg.Set
mod_distrib,
514 " use mod to distribute files among the processors";
520 "-use_cache", Arg.Set
Flag_parsing_c.use_cache
,
521 " use .ast_raw pre-parsed cached C file";
522 (* could use Flag_parsing_c.options_pad instead *)
527 "test mode and test options (works with tests/ or .ok files)",
528 "The test options don't work with the -sp_file and so on.",
530 "-test", Arg.Set
test_mode,
531 " <file> launch spatch on tests/file.[c,cocci]";
532 "-testall", Arg.Set
test_all,
533 " launch spatch on all files in tests/ having a .res";
534 "-test_okfailed", Arg.Set
test_okfailed,
535 " generates .{ok,failed,spatch_ok} files using .res files";
536 "-test_regression_okfailed", Arg.Set
test_regression_okfailed,
537 " process the .{ok,failed,spatch_ok} files in current dir";
539 "-compare_with_expected", Arg.Set
compare_with_expected,
540 " use also file.res";
541 "-expected_score_file", Arg.Set_string
expected_score_file,
542 " which score file to compare with in -testall";
543 "-relax_include_path", Arg.Set
FC.relax_include_path
,
549 ("The action options don't work with the -sp_file and so on." ^
"\n" ^
550 "It's for the other (internal) uses of the spatch program."
553 (* -token_c, -parse_c, etc *)
554 ((Common.options_of_actions
action (Test_parsing_c.actions
())) ++
556 (let s = "-parse_cocci" in s, Arg.Unit
(fun () -> action := s),
558 (let s = "-compare_c" in s, Arg.Unit
(fun () -> action := s),
565 short_options ++ List.concat
(List.map
Common.thd3
other_options)
568 (* I don't want the -help and --help that are appended by Arg.align *)
570 Arg.align xs
+> List.rev
+> Common.drop
2 +> List.rev
572 (* copy paste of Arg.parse. Don't want the default -help msg *)
573 let arg_parse2 l f msg
=
575 Arg.parse_argv
Sys.argv l f msg
;
577 | Arg.Bad msg
-> (* eprintf "%s" msg; exit 2; *)
578 let xs = Common.lines msg
in
579 (* take only head, it's where the error msg is *)
582 raise
(Common.UnixExit
(2))
583 | Arg.Help msg
-> (* printf "%s" msg; exit 0; *)
584 raise Impossible
(* -help is specified in speclist *)
590 Common.short_usage usage_msg short_options;
592 pr2
"Example of use:";
593 pr2
" ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
599 Common.long_usage usage_msg short_options other_options
601 let _ = short_usage_func := short_usage
602 let _ = long_usage_func := long_usage
604 (*****************************************************************************)
606 (*****************************************************************************)
608 let adjust_stdin cfile k
=
614 let (dir, base
, ext
) = Common.dbe_of_filename cfile
in
615 let varfile = Common.filename_of_dbe
(dir, base
, "var") in
616 if ext
=$
= "c" && Common.lfile_exists
varfile
619 with Invalid_argument
("Filename.chop_extension") -> None
621 Common.redirect_stdin_opt
newin k
623 let glimpse_filter (coccifile
, isofile
) dir =
624 let (_metavars
,astcocci
,_free_var_lists
,_negated_positions
,
625 _used_after_lists
,_positions_lists
,_,query
) =
626 Cocci.sp_of_file coccifile
(Some isofile
) in
628 None
-> pr2
"no glimpse keyword inferred from snippet"; None
630 let suffixes = if !include_headers then ["c";"h"] else ["c"] in
631 pr2
("glimpse request = " ^ query
);
632 let command = spf
"glimpse -y -H %s -N -W -w '%s'" dir query
in
633 let (glimpse_res
,stat
) = Common.cmd_to_list_and_status
command in
635 Unix.WEXITED
(0) | Unix.WEXITED
(1) ->
639 (fun file
-> List.mem
(Common.filesuffix file
) suffixes))
640 | _ -> None
(* error, eg due to pattern too big *)
644 (*****************************************************************************)
646 (*****************************************************************************)
652 (* a more general solution would be to use
653 * Common.files_of_dir_or_files (x::xs)
654 * as some elements in xs may also be directories, or individual
657 if Common.is_directory x
660 adjust_stdin x
(fun () ->
661 if !cocci_file =$
= ""
662 then failwith
"I need a cocci file, use -sp_file <file>";
664 if !dir && !Flag.patch
=*= None
667 | [] -> Flag.patch
:= Some x
670 ("warning: patch output can only be created when only one\n"^
671 "directory is specified or when the -patch flag is used")
675 Common.profile_code
"Main.infiles computation" (fun () ->
676 match !dir, !kbuild_info, !Flag.use_glimpse
with
679 failwith
"-use_glimpse works only with -dir"
680 | true, s, true when s <> "" ->
681 failwith
"-use_glimpse does not work with -kbuild"
684 then failwith
"-use_glimpse can accept only one dir";
688 match glimpse_filter (!cocci_file, !Config.std_iso
) x
with
690 Common.cmd_to_list
(* same as "true, "", _" case *)
692 (* FIXME : Could we remove xs ?
693 -use_glimpse requires a singleton.
694 This is checked some lines before.
695 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
696 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
698 then ("find "^ x ^
" -name \"*.[ch]\"")
699 else ("find "^ x ^
" -name \"*.c\""))
700 | Some
files -> files in
701 files +> List.map
(fun x
-> [x
])
703 | false, _, _ -> [x
::xs]
707 then ("find "^
(join
" " (x
::xs))^
" -name \"*.[ch]\"")
708 else ("find "^
(join
" " (x
::xs))^
" -name \"*.c\""))
709 +> List.map
(fun x
-> [x
])
712 | true, kbuild_info_file
,_ ->
714 Common.cmd_to_list
("find "^
(join
" " (x
::xs))^
" -type d")
716 let info = Kbuild.parse_kbuild_info kbuild_info_file
in
717 let groups = Kbuild.files_in_dirs
dirs info in
719 groups +> List.map
(function Kbuild.Group
xs -> xs)
724 match (!distrib_index,!distrib_max) with
725 (None
,None
) -> infiles
726 | (Some index
,Some max
) ->
729 failwith
"index starts at 0, and so must be less than max");
732 let rec loop ct
= function
735 if (ct
mod max
) =|= index
736 then x
::(loop (ct
+1) xs)
737 else loop (ct
+1) xs in
741 let all_files = List.length
infiles in
742 let regions = (all_files + (max
- 1)) / max
in
743 let this_min = index
* regions in
744 let this_max = (index
+1) * regions in
745 let rec loop ct
= function
748 if this_min <= ct
&& ct
< this_max
749 then x
::(loop (ct
+1) xs)
750 else loop (ct
+1) xs in
753 | _ -> failwith
"inconsistent distribution information" in
756 Common.profile_code
"Main.outfiles computation" (fun () ->
758 Cocci.pre_engine
(!cocci_file, !Config.std_iso
) in
760 infiles +> List.map
(fun cfiles
->
761 pr2
("HANDLING: " ^
(join
" " cfiles
));
762 Common.timeout_function_opt
!FC.timeout
(fun () ->
763 Common.report_if_take_time
10 (join
" " cfiles
) (fun () ->
766 (* this is the main call *)
767 Cocci.full_engine
cocci_infos cfiles
769 | Common.UnixExit x
-> raise
(Common.UnixExit x
)
773 pr2
("EXN:" ^
Printexc.to_string e
);
777 Cocci.post_engine
cocci_infos;
782 Common.profile_code
"Main.result analysis" (fun () ->
784 Ctlcocci_integration.print_bench
();
786 let outfiles = Cocci.check_duplicate_modif
outfiles in
788 outfiles +> List.iter
(fun (infile
, outopt
) ->
789 outopt
+> Common.do_option
(fun outfile
->
792 Common.command2
("cp "^infile^
" "^infile^
".cocci_orig");
793 Common.command2
("cp "^outfile^
" "^infile
);
797 then Common.command2
("cp "^outfile^
" "^infile^
".cocci_res");
799 if !output_file =$
= ""
801 let tmpfile = "/tmp/"^
Common.basename infile
in
802 pr2
(spf
"One file modified. Result is here: %s" tmpfile);
803 Common.command2
("cp "^outfile^
" "^
tmpfile);
806 if !output_file <> "" then
808 | [infile
, Some outfile
] when infile
=$
= x
&& null
xs ->
809 Common.command2
("cp " ^outfile^
" " ^
!output_file);
810 | [infile
, None
] when infile
=$
= x
&& null
xs ->
811 Common.command2
("cp " ^infile^
" " ^
!output_file);
814 ("-o can not be applied because there is multiple " ^
818 if !compare_with_expected
819 then Testing.compare_with_expected outfiles))
821 | [] -> raise Impossible
824 (*****************************************************************************)
825 (* The coccinelle main entry point *)
826 (*****************************************************************************)
829 let arglist = Array.to_list
Sys.argv
in
831 if not
(null
(Common.inter_set
arglist
832 ["-cocci_file";"-sp_file";"-test";"-testall";
833 "-test_okfailed";"-test_regression_okfailed"]))
834 then run_profile quiet_profile;
838 (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*)
840 (* this call can set up many global flag variables via the cmd line *)
841 arg_parse2 (Arg.align
all_options) (fun x
-> args := x
::!args) usage_msg;
843 (* julia hack so that one can override directories specified on
844 * the command line. *)
848 if List.length
!args > 1
851 let chosen = List.hd
!args in
852 pr2
("ignoring all but the last specified directory: "^
chosen);
856 else List.hd
!args in
857 if !FC.include_path
=*= None
858 then FC.include_path
:= Some
(Filename.concat
chosen_dir "include"));
860 args := List.rev
!args;
862 if !cocci_file <> "" && (not
(!cocci_file =~
".*\\.\\(sgrep\\|spatch\\)$"))
863 then cocci_file := Common.adjust_ext_if_needed
!cocci_file ".cocci";
865 if !Config.std_iso
<> ""
866 then Config.std_iso
:= Common.adjust_ext_if_needed
!Config.std_iso
".iso";
867 if !Config.std_h
<> ""
868 then Config.std_h
:= Common.adjust_ext_if_needed
!Config.std_h
".h";
870 if !Config.std_h
<> ""
871 then Parse_c.init_defs_builtins
!Config.std_h
;
874 then Parse_c.init_defs_macros
!macro_file;
877 (* must be done after Arg.parse, because Common.profile is set by it *)
878 Common.profile_code
"Main total" (fun () ->
881 let all_actions = Test_parsing_c.actions
() in
885 (* --------------------------------------------------------- *)
886 (* The test framework. Works with tests/ or .ok and .failed *)
887 (* --------------------------------------------------------- *)
888 | [x
] when !test_mode ->
889 FC.include_path
:= Some
"tests/include";
890 Testing.testone x
!compare_with_expected
892 | [] when !test_all ->
893 FC.include_path
:= Some
"tests/include";
894 if !expected_score_file <> ""
895 then Testing.testall ~
expected_score_file:!expected_score_file ()
896 else Testing.testall
()
898 | [] when !test_regression_okfailed ->
899 Testing.test_regression_okfailed ()
901 | x
::xs when !test_okfailed ->
902 (* do its own timeout on FC.timeout internally *)
903 FC.relax_include_path
:= true;
904 adjust_stdin x
(fun () ->
905 Testing.test_okfailed !cocci_file (x
::xs)
908 (* --------------------------------------------------------- *)
909 (* Actions, useful to debug subpart of coccinelle *)
910 (* --------------------------------------------------------- *)
912 | xs when List.mem
!action (Common.action_list
all_actions) ->
913 Common.do_action
!action xs all_actions
915 | [file
] when !action =$
= "-parse_cocci" ->
916 Testing.test_parse_cocci file
918 (* I think this is used by some scripts in some Makefile for our
919 * big-tests. So dont remove.
921 | [file1
;file2
] when !action =$
= "-compare_c" ->
922 Test_parsing_c.test_compare_c file1 file2
(* result = unix code *)
924 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
927 (* --------------------------------------------------------- *)
928 (* This is the main entry *)
929 (* --------------------------------------------------------- *)
933 (* --------------------------------------------------------- *)
935 (* --------------------------------------------------------- *)
936 | [] -> short_usage()
939 if !Pycocci.initialised
&& (Pycocci.py_isinitialized
()) != 0 then begin
940 ignore
(Pycocci.pyrun_simplestring
"cocci.finalise()");
942 then Common.pr2
"Finalizing python\n";
943 Pycocci.py_finalize
();
948 let main_with_better_error_report () =
949 if !Common.debugger
then main ()
954 | Unix.Unix_error
(_, "stat", filename
) ->
955 pr2
(spf
"ERROR: File %s does not exist" filename
);
956 raise
(UnixExit
(-1))
959 (*****************************************************************************)
961 Common.main_boilerplate
(fun () ->
962 main_with_better_error_report ();
963 Ctlcocci_integration.print_bench
();