4 (*****************************************************************************)
6 (*****************************************************************************)
8 (* In addition to flags that can be tweaked via -xxx options (cf the
9 * full list of options in "the spatch options" section below), the
10 * spatch program also depends on external files, described in
11 * globals/config.ml, mainly a standard.h and standard.iso file *)
13 let cocci_file = ref ""
15 let output_file = ref ""
16 let inplace_modif = ref false (* but keeps a .cocci_orig *)
17 let outplace_modif = ref false (* generates a .cocci_res *)
18 let preprocess = ref false (* run the C preprocessor before cocci *)
19 let compat_mode = ref false
21 (* somehow obsolete now *)
24 let include_headers = ref false
25 let kbuild_info = ref ""
27 let macro_file = ref ""
30 let test_mode = ref false
31 let test_all = ref false
32 let test_okfailed = ref false
33 let test_regression_okfailed = ref false
34 let expected_score_file = ref ""
40 (* works with -test but also in "normal" spatch mode *)
41 let compare_with_expected = ref false
43 let distrib_index = ref (None
: int option)
44 let distrib_max = ref (None
: int option)
45 let mod_distrib = ref false
48 (*****************************************************************************)
50 (*****************************************************************************)
52 (* pair of (list of flags to set true, list of flags to set false *)
57 (* FC.show_diff; just leave this as it is *)
66 FC.show_before_fixed_flow
;
69 FC.show_binding_in_out
;
73 Flag_parsing_c.show_parsing_error
;
75 Flag_parsing_c.verbose_lexing
;
76 Flag_parsing_c.verbose_parsing
;
77 Flag_parsing_c.verbose_type
;
78 Flag_parsing_c.verbose_cfg
;
79 Flag_parsing_c.verbose_unparsing
;
80 Flag_parsing_c.verbose_visit
;
81 Flag_parsing_c.verbose_cpp_ast
;
83 Flag_matcher.verbose_matcher
;
84 Flag_matcher.debug_engine
;
86 Flag_parsing_c.debug_unparsing
;
88 Flag_parsing_cocci.show_SP
;
89 Flag_parsing_cocci.show_iso_failures
;
91 Flag_ctl.verbose_ctl_engine
;
92 Flag_ctl.verbose_match
;
97 (* some information that is useful in seeing why a semantic patch doesn't
106 FC.show_binding_in_out
;
107 FC.show_dependencies
;
109 Flag_parsing_cocci.show_iso_failures
;
113 Flag_parsing_c.verbose_cfg
;
114 Flag_parsing_c.verbose_unparsing
;
115 Flag_parsing_c.verbose_visit
;
117 Flag_matcher.verbose_matcher
;
119 Flag_parsing_c.show_parsing_error
;
127 FC.show_before_fixed_flow
;
131 Flag_parsing_cocci.show_SP
;
132 Flag_ctl.verbose_ctl_engine
;
133 Flag_ctl.verbose_match
;
134 Flag_matcher.debug_engine
;
135 Flag_parsing_c.debug_unparsing
;
136 Flag_parsing_c.verbose_type
;
137 Flag_parsing_c.verbose_parsing
;
152 FC.show_before_fixed_flow
;
155 FC.show_binding_in_out
;
157 Flag_parsing_cocci.show_SP
;
158 Flag_parsing_cocci.show_iso_failures
;
159 Flag_ctl.verbose_ctl_engine
;
160 Flag_ctl.verbose_match
;
161 Flag_matcher.debug_engine
;
162 Flag_parsing_c.debug_unparsing
;
163 Flag_parsing_c.verbose_type
;
164 Flag_parsing_c.verbose_parsing
;
168 let (set_to_true
, set_to_false
) = p
in
169 List.iter
(fun x
-> x
:= false) set_to_false
;
170 List.iter
(fun x
-> x
:= true) set_to_true
172 (*****************************************************************************)
173 (* The spatch options *)
174 (*****************************************************************************)
177 "Usage: " ^ basename
Sys.argv
.(0) ^
178 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
179 "\n" ^
"Options are:"
181 (* forward reference trick *)
182 let short_usage_func = ref (fun () -> ())
183 let long_usage_func = ref (fun () -> ())
186 (* The short_options are user-oriented. The other options are for
187 * the developers of coccinelle or advanced-users that know
188 * quite well the underlying semantics of coccinelle.
192 (* will be printed when use only ./spatch. For the rest you have to
193 * use -longhelp to see them.
195 let short_options = [
196 "-sp_file", Arg.Set_string
cocci_file,
197 " <file> the semantic patch file";
199 "-o", Arg.Set_string
output_file,
200 " <file> the output file";
201 "-inplace", Arg.Set
inplace_modif,
202 " do the modification on the file directly";
203 "-outplace", Arg.Set
outplace_modif,
204 " store modifications in a .cocci_res file";
206 "-U", Arg.Int
(fun n
-> Flag_parsing_c.diff_lines
:= Some
(i_to_s n
)),
207 " set number of diff context lines";
208 "-partial_match", Arg.Set
Flag_ctl.partial_match
,
209 " report partial matches of the SP on the C file";
211 "-iso_file", Arg.Set_string
Config.std_iso
,
212 " <file> (default=" ^
!Config.std_iso ^
")";
213 "-macro_file", Arg.Set_string
macro_file,
215 "-macro_file_builtins", Arg.Set_string
Config.std_h
,
216 " <file> (default=" ^
!Config.std_h ^
")";
219 Arg.Unit
(function _
-> FC.include_options
:= FC.I_ALL_INCLUDES
),
220 " causes all available include files to be used";
222 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NO_INCLUDES
),
223 " causes not even local include files to be used";
225 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NORMAL_INCLUDES
),
226 " causes local include files to be used";
227 "-include_headers", Arg.Set
include_headers,
228 " process header files independently";
229 "-I", Arg.String
(function x
-> FC.include_path
:= Some x
),
230 " <dir> containing the header files (optional)";
232 "-preprocess", Arg.Set
preprocess,
233 " run the C preprocessor before applying the semantic match";
235 "-c", Arg.Set
compat_mode, " gcc/cpp compatibility mode";
238 " <dir> process all files in directory recursively";
240 "-use_glimpse", Arg.Unit
(function _
-> Flag.scanner
:= Flag.Glimpse
),
241 " works with -dir, use info generated by glimpseindex";
242 "-use_google", Arg.String
(function s
-> Flag.scanner
:= Flag.Google s
),
243 " find relevant files using google code search";
244 "-patch", Arg.String
(function s
-> Flag.patch
:= Some s
),
245 (" <dir> path name with respect to which a patch should be created\n"^
246 " \"\" for a file in the current directory");
247 "-kbuild_info", Arg.Set_string
kbuild_info,
248 " <file> improve -dir by grouping related c files";
249 "-pyoutput", Arg.Set_string
Flag.pyoutput
,
250 " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>";
253 "-version", Arg.Unit
(fun () ->
254 let withpython = if Pycocci.python_support
then "with" else "without" in
255 pr2
(spf
"spatch version %s %s Python support" Config.version
withpython);
260 "-date", Arg.Unit
(fun () ->
261 pr2
"version: $Date: 2010/01/04 11:16:30 $";
262 raise
(Common.UnixExit
0)
266 "-shorthelp", Arg.Unit
(fun () ->
268 raise
(Common.UnixExit
0)
270 " see short list of options";
271 "-longhelp", Arg.Unit
(fun () ->
273 raise
(Common.UnixExit
0)
275 " see all the available options in different categories";
276 "-help", Arg.Unit
(fun () ->
278 raise
(Common.UnixExit
0)
281 "--help", Arg.Unit
(fun () ->
283 raise
(Common.UnixExit
0)
288 (* the format is a list of triples:
289 * (title of section * (optional) explanation of sections * option list)
291 let other_options = [
292 "aliases and obsolete options",
295 "-sp", Arg.Set_string
cocci_file, " short option of -sp_file";
296 "-iso", Arg.Set_string
Config.std_iso
, " short option of -iso_file";
298 "-cocci_file", Arg.Set_string
cocci_file,
299 " <file> the semantic patch file";
300 (* "-c", Arg.Set_string cocci_file, " short option of -sp_file"; *)
303 "most useful show options",
306 "-show_diff" , Arg.Set
FC.show_diff
, " ";
307 "-no_show_diff" , Arg.Clear
FC.show_diff
, " ";
308 "-show_flow" , Arg.Set
FC.show_flow
, " ";
309 (* works in conjunction with -show_ctl_text *)
312 (function _
-> FC.show_ctl_text
:= true; FC.inline_let_ctl
:= true), " ";
313 "-ctl_show_mcodekind",
315 (function _
-> FC.show_ctl_text
:= true; FC.show_mcodekind_in_ctl
:= true),
317 "-show_bindings", Arg.Set
FC.show_binding_in_out
, " ";
318 "-show_transinfo", Arg.Set
Flag.show_transinfo
, " ";
319 "-show_misc", Arg.Set
Flag.show_misc
, " ";
320 "-show_trying", Arg.Set
Flag.show_trying
,
321 " show the name of each function being processed";
322 "-show_dependencies",
323 Arg.Unit
(function _
-> FC.show_dependencies
:= true;
324 FC.show_binding_in_out
:= true),
325 " show the dependencies related to each rule";
328 "verbose subsystems options",
331 "-verbose_ctl_engine",
332 Arg.Unit
(function _
->
333 Flag_ctl.verbose_ctl_engine
:= true; FC.show_ctl_text
:= true) , " ";
334 "-verbose_match", Arg.Set
Flag_ctl.verbose_match
, " ";
335 "-verbose_engine", Arg.Set
Flag_matcher.debug_engine
, " ";
336 "-graphical_trace", Arg.Set
Flag_ctl.graphical_trace
, " generate a pdf file representing the matching process";
338 Arg.Unit
(function _
->
339 Flag_ctl.graphical_trace
:= true; Flag_ctl.gt_without_label
:= true),
340 " remove graph label (requires option -graphical_trace)";
342 "-parse_error_msg", Arg.Set
Flag_parsing_c.verbose_parsing
, " ";
343 "-type_error_msg", Arg.Set
Flag_parsing_c.verbose_type
, " ";
344 (* could also use Flag_parsing_c.options_verbose *)
347 "other show options",
350 "-show_c" , Arg.Set
FC.show_c
, " ";
351 "-show_cocci" , Arg.Set
FC.show_cocci
, " ";
352 "-show_before_fixed_flow" , Arg.Set
FC.show_before_fixed_flow
, " ";
353 "-show_ctl_tex" , Arg.Set
FC.show_ctl_tex
, " ";
354 "-show_ctl_text" , Arg.Set
FC.show_ctl_text
, " ";
355 "-show_SP" , Arg.Set
Flag_parsing_cocci.show_SP
, " ";
359 "debug C parsing/unparsing",
362 "-debug_cpp", Arg.Set
Flag_parsing_c.debug_cpp
, " ";
363 "-debug_lexer", Arg.Set
Flag_parsing_c.debug_lexer
, " ";
364 "-debug_etdt", Arg.Set
Flag_parsing_c.debug_etdt
, " ";
365 "-debug_typedef", Arg.Set
Flag_parsing_c.debug_typedef
, " ";
367 "-filter_msg", Arg.Set
Flag_parsing_c.filter_msg
,
368 " filter some cpp message when the macro is a \"known\" cpp construct";
369 "-filter_define_error", Arg.Set
Flag_parsing_c.filter_define_error
," ";
370 "-filter_msg_define_error", Arg.Set
Flag_parsing_c.filter_msg_define_error
,
371 " filter the error msg";
372 "-filter_passed_level", Arg.Set_int
Flag_parsing_c.filter_passed_level
," ";
373 (* debug cfg doesn't seem to have any effect, so drop it as an option *)
374 (* "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " "; *)
375 "-debug_unparsing", Arg.Set
Flag_parsing_c.debug_unparsing
, " ";
378 (* could use Flag_parsing_c.options_debug_with_title instead *)
381 "shortcut for enabling/disabling a set of debugging options at once",
384 (* todo: other profile ? *)
385 "-quiet", Arg.Unit
(fun () -> run_profile quiet_profile), " ";
386 "-debug", Arg.Unit
(fun () -> run_profile debug_profile), " ";
387 "-pad", Arg.Unit
(fun () -> run_profile pad_profile), " ";
394 "-profile", Arg.Unit
(function () -> Common.profile
:= Common.PALL
) ,
395 " gather timing information about the main coccinelle functions";
396 "-bench", Arg.Int
(function x
-> Flag_ctl.bench
:= x
),
397 " <level> for profiling the CTL engine";
398 "-timeout", Arg.Int
(fun x
-> FC.timeout
:= Some x
),
399 " <sec> timeout in seconds";
400 "-steps", Arg.Int
(fun x
-> Flag_ctl.steps
:= Some x
),
401 " max number of model checking steps per code unit";
402 "-iso_limit", Arg.Int
(fun x
-> Flag_parsing_cocci.iso_limit
:= Some x
),
403 " max depth of iso application";
404 "-no_iso_limit", Arg.Unit
(fun _
-> Flag_parsing_cocci.iso_limit
:= None
),
405 " disable limit on max depth of iso application";
406 "-track_iso", Arg.Set
Flag.track_iso_usage
,
407 " gather information about isomorphism usage";
412 (*post_engine not included, because it doesn't use isos*)
413 PSOME
["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]),
414 " gather information about the cost of isomorphism usage"
419 "change of algorithm options",
422 "-popl", Arg.Set
FC.popl
,
423 " simplified SmPL, for the popl paper";
427 (function _
-> FC.popl
:= true; Flag_popl.mark_all
:= true),
428 " simplified SmPL, for the popl paper";
430 "-popl_keep_all_wits",
432 (function _
-> FC.popl
:= true; Flag_popl.keep_all_wits
:= true),
433 " simplified SmPL, for the popl paper";
437 Flag.make_hrule
:= Some s
; FC.include_options
:= FC.I_NO_INCLUDES
),
438 " semantic patch generation";
440 "-keep_comments", Arg.Set
Flag_parsing_c.keep_comments
,
441 " keep comments around removed code";
443 "-loop", Arg.Set
Flag_ctl.loop_in_src_code
, " ";
444 "-no_loops", Arg.Set
Flag_parsing_c.no_loops
,
445 " drop all back edges derived from looping constructs - unsafe";
446 "-no_gotos", Arg.Set
Flag_parsing_c.no_gotos
,
447 " drop all jumps derived from gotos - unsafe";
449 "-l1", Arg.Clear
Flag_parsing_c.label_strategy_2
, " ";
450 "-ifdef_to_if", Arg.Set
FC.ifdef_to_if
,
451 " convert ifdef to if (experimental)";
452 "-no_ifdef_to_if", Arg.Clear
FC.ifdef_to_if
,
453 " convert ifdef to if (experimental)";
455 "-disable_multi_pass", Arg.Set
Flag_parsing_c.disable_multi_pass
, " ";
457 "-noif0_passing", Arg.Clear
Flag_parsing_c.if0_passing
,
459 "-noadd_typedef_root", Arg.Clear
Flag_parsing_c.add_typedef_root
, " ";
460 (* could use Flag_parsing_c.options_algo instead *)
463 "-disallow_nested_exps", Arg.Set
Flag_matcher.disallow_nested_exps
,
464 " disallow an expresion pattern from matching a term and its subterm";
465 "-disable_worth_trying_opt", Arg.Clear
FC.worth_trying_opt
,
467 "-only_return_is_error_exit",
468 Arg.Set
Flag_matcher.only_return_is_error_exit
,
469 "if this flag is not set, then break and continue are also error exits";
470 (* the following is a hack to make it easier to add code in sgrep-like
471 code, essentially to compensate for the fact that we don't have
472 any way of printing things out *)
473 "-allow_inconsistent_paths",
474 Arg.Set
Flag_matcher.allow_inconsistent_paths
,
475 " if this flag is set don't check for inconsistent paths; dangerous";
476 "-no_safe_expressions",
477 Arg.Set
Flag_matcher.no_safe_expressions
,
478 " make an expression disjunction not prioritise the topmost disjunct";
479 "-int_bits", Arg.Int
Flag_parsing_c.set_int_bits
,
480 " the number of bits in an unsigned int";
481 "-long_bits", Arg.Int
Flag_parsing_c.set_long_bits
,
482 " the number of bits in an unsigned long";
483 "-linux_spacing", Arg.Unit
Flag_parsing_c.set_linux_spacing
,
484 " spacing of + code follows the conventions of Linux";
485 "-smpl_spacing", Arg.Unit
Flag_parsing_c.set_smpl_spacing
,
486 " spacing of + code follows the semantic patch";
487 "-D", Arg.String
Flag_parsing_cocci.set_defined_virtual_rules
,
488 " indicate that a virtual rule should be considered to be matched";
494 "-debugger", Arg.Set
Common.debugger
,
495 " option to set if launch spatch in ocamldebug";
496 "-disable_once", Arg.Set
Common.disable_pr2_once
,
497 " to print more messages";
498 "-show_trace_profile", Arg.Set
Common.show_trace_profile
,
500 "-save_tmp_files", Arg.Set
Common.save_tmp_files
, " ";
506 "-index", Arg.Int
(function x
-> distrib_index := Some x
) ,
507 " the processor to use for this run of spatch";
508 "-max", Arg.Int
(function x
-> distrib_max := Some x
) ,
509 " the number of processors available";
510 "-mod_distrib", Arg.Set
mod_distrib,
511 " use mod to distribute files among the processors";
517 "-use_cache", Arg.Set
Flag_parsing_c.use_cache
,
518 " use .ast_raw pre-parsed cached C file";
519 (* could use Flag_parsing_c.options_pad instead *)
524 "test mode and test options (works with tests/ or .ok files)",
525 "The test options don't work with the -sp_file and so on.",
527 "-test", Arg.Set
test_mode,
528 " <file> launch spatch on tests/file.[c,cocci]";
529 "-testall", Arg.Set
test_all,
530 " launch spatch on all files in tests/ having a .res";
531 "-test_okfailed", Arg.Set
test_okfailed,
532 " generates .{ok,failed,spatch_ok} files using .res files";
533 "-test_regression_okfailed", Arg.Set
test_regression_okfailed,
534 " process the .{ok,failed,spatch_ok} files in current dir";
536 "-compare_with_expected", Arg.Set
compare_with_expected,
537 " use also file.res";
538 "-expected_score_file", Arg.Set_string
expected_score_file,
539 " which score file to compare with in -testall";
540 "-relax_include_path", Arg.Set
FC.relax_include_path
,
545 ("The action options don't work with the -sp_file and so on." ^
"\n" ^
546 "It's for the other (internal) uses of the spatch program."
549 (* -token_c, -parse_c, etc *)
550 ((Common.options_of_actions
action (Test_parsing_c.actions
())) ++
552 (let s = "-parse_cocci" in s, Arg.Unit
(fun () -> action := s),
554 (let s = "-compare_c" in s, Arg.Unit
(fun () -> action := s),
561 short_options ++ List.concat
(List.map
Common.thd3
other_options)
563 (* I don't want the -help and --help that are appended by Arg.align *)
565 Arg.align xs
+> List.rev
+> Common.drop
2 +> List.rev
567 (* copy paste of Arg.parse. Don't want the default -help msg *)
568 let arg_parse2 l f msg
=
570 Arg.parse_argv
Sys.argv l f msg
;
572 | Arg.Bad msg
-> (* eprintf "%s" msg; exit 2; *)
573 let xs = Common.lines msg
in
574 (* take only head, it's where the error msg is *)
577 raise
(Common.UnixExit
(2))
578 | Arg.Help msg
-> (* printf "%s" msg; exit 0; *)
579 raise Impossible
(* -help is specified in speclist *)
585 Common.short_usage usage_msg short_options;
587 pr2
"Example of use:";
588 pr2
" ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
594 Common.long_usage usage_msg short_options other_options
596 let _ = short_usage_func := short_usage
597 let _ = long_usage_func := long_usage
599 (*****************************************************************************)
601 (*****************************************************************************)
603 let adjust_stdin cfile k
=
609 let (dir, base
, ext
) = Common.dbe_of_filename cfile
in
610 let varfile = Common.filename_of_dbe
(dir, base
, "var") in
611 if ext
=$
= "c" && Common.lfile_exists
varfile
614 with Invalid_argument
("Filename.chop_extension") -> None
616 Common.redirect_stdin_opt
newin k
618 let glimpse_filter (coccifile
, isofile
) dir =
619 let (_metavars
,astcocci
,_free_var_lists
,_negated_positions
,
620 _used_after_lists
,_positions_lists
,_,query
) =
621 Cocci.sp_of_file coccifile
(Some isofile
) in
623 None
-> pr2
"no glimpse keyword inferred from snippet"; None
625 (let suffixes = if !include_headers then ["c";"h"] else ["c"] in
626 pr2
("glimpse request = " ^ query
);
627 let command = spf
"glimpse -y -H %s -N -W -w '%s'" dir query
in
628 let (glimpse_res
,stat
) = Common.cmd_to_list_and_status
command in
630 Unix.WEXITED
(0) | Unix.WEXITED
(1) ->
634 (fun file
-> List.mem
(Common.filesuffix file
) suffixes))
635 | _ -> None
(* error, eg due to pattern too big *))
636 | _ -> failwith
"not possible"
638 (*****************************************************************************)
640 (*****************************************************************************)
646 (* a more general solution would be to use
647 * Common.files_of_dir_or_files (x::xs)
648 * as some elements in xs may also be directories, or individual
651 if Common.is_directory x
654 adjust_stdin x
(fun () ->
655 if !cocci_file =$
= ""
656 then failwith
"I need a cocci file, use -sp_file <file>";
658 if !dir && !Flag.patch
=*= None
661 | [] -> Flag.patch
:= Some x
664 ("warning: patch output can only be created when only one\n"^
665 "directory is specified or when the -patch flag is used")
669 Common.profile_code
"Main.infiles computation" (fun () ->
670 match !dir, !kbuild_info, !Flag.scanner
= Flag.Glimpse
with
673 failwith
"-use_glimpse works only with -dir"
674 | true, s, true when s <> "" ->
675 failwith
"-use_glimpse does not work with -kbuild"
678 then failwith
"-use_glimpse can accept only one dir";
682 match glimpse_filter (!cocci_file, !Config.std_iso
) x
with
684 Common.cmd_to_list
(* same as "true, "", _" case *)
686 (* FIXME : Could we remove xs ?
687 -use_glimpse requires a singleton.
688 This is checked some lines before.
689 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
690 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
692 then ("find "^ x ^
" -name \"*.[ch]\"")
693 else ("find "^ x ^
" -name \"*.c\""))
694 | Some
files -> files in
695 files +> List.map
(fun x
-> [x
])
697 | false, _, _ -> [x
::xs]
701 then ("find "^
(join
" " (x
::xs))^
" -name \"*.[ch]\"")
702 else ("find "^
(join
" " (x
::xs))^
" -name \"*.c\""))
703 +> List.map
(fun x
-> [x
])
706 | true, kbuild_info_file
,_ ->
708 Common.cmd_to_list
("find "^
(join
" " (x
::xs))^
" -type d")
710 let info = Kbuild.parse_kbuild_info kbuild_info_file
in
711 let groups = Kbuild.files_in_dirs
dirs info in
713 groups +> List.map
(function Kbuild.Group
xs -> xs)
718 match (!distrib_index,!distrib_max) with
719 (None
,None
) -> infiles
720 | (Some index
,Some max
) ->
723 failwith
"index starts at 0, and so must be less than max");
726 let rec loop ct
= function
729 if (ct
mod max
) =|= index
730 then x
::(loop (ct
+1) xs)
731 else loop (ct
+1) xs in
735 let all_files = List.length
infiles in
736 let regions = (all_files + (max
- 1)) / max
in
737 let this_min = index
* regions in
738 let this_max = (index
+1) * regions in
739 let rec loop ct
= function
742 if this_min <= ct
&& ct
< this_max
743 then x
::(loop (ct
+1) xs)
744 else loop (ct
+1) xs in
747 | _ -> failwith
"inconsistent distribution information" in
750 Common.profile_code
"Main.outfiles computation" (fun () ->
752 Cocci.pre_engine
(!cocci_file, !Config.std_iso
) in
754 infiles +> List.map
(fun cfiles
->
755 pr2
("HANDLING: " ^
(join
" " cfiles
));
756 Common.timeout_function_opt
!FC.timeout
(fun () ->
757 Common.report_if_take_time
10 (join
" " cfiles
) (fun () ->
758 (*let s = profile_diagnostic() in*)
762 if !output_file <> "" && !compat_mode then
767 Common.redirect_stdout_opt
optfile (fun () ->
768 (* this is the main call *)
769 Cocci.full_engine
cocci_infos cfiles
772 | Common.UnixExit x
-> raise
(Common.UnixExit x
)
777 pr2(profile_diagnostic());*)
780 pr2
("EXN:" ^
Printexc.to_string e
);
784 Cocci.post_engine
cocci_infos;
789 Common.profile_code
"Main.result analysis" (fun () ->
790 Ctlcocci_integration.print_bench
();
791 let outfiles = Cocci.check_duplicate_modif
outfiles in
792 outfiles +> List.iter
(fun (infile
, outopt
) ->
793 outopt
+> Common.do_option
(fun outfile
->
796 Common.command2
("cp "^infile^
" "^infile^
".cocci_orig");
797 Common.command2
("cp "^outfile^
" "^infile
);
801 then Common.command2
("cp "^outfile^
" "^infile^
".cocci_res");
803 (* potential source of security pb if the /tmp/ file is
804 * a symlink, so simpler to not produce any regular file
805 * (files created by Common.new_temp_file are still ok)
809 if !output_file =$= ""
811 let tmpfile = "/tmp/"^Common.basename infile in
812 pr2 (spf "One file modified. Result is here: %s" tmpfile);
813 Common.command2 ("cp "^outfile^" "^tmpfile);
817 if !output_file <> "" && not
!compat_mode then
819 | [infile
, Some outfile
] when infile
=$
= x
&& null
xs ->
820 Common.command2
("cp " ^outfile^
" " ^
!output_file);
821 | [infile
, None
] when infile
=$
= x
&& null
xs ->
822 Common.command2
("cp " ^infile^
" " ^
!output_file);
825 ("-o can not be applied because there is multiple " ^
828 if !compare_with_expected
829 then Testing.compare_with_expected outfiles))
831 | [] -> raise Impossible
834 (*****************************************************************************)
835 (* The coccinelle main entry point *)
836 (*****************************************************************************)
839 let arglist = Array.to_list
Sys.argv
in
841 if not
(null
(Common.inter_set
arglist
842 ["-cocci_file";"-sp_file";"-sp";"-test";"-testall";
843 "-test_okfailed";"-test_regression_okfailed"]))
844 then run_profile quiet_profile;
848 (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*)
850 (* this call can set up many global flag variables via the cmd line *)
851 arg_parse2 (Arg.align
all_options) (fun x
-> args := x
::!args) usage_msg;
853 (* julia hack so that one can override directories specified on
854 * the command line. *)
858 if List.length
!args > 1
861 let chosen = List.hd
!args in
862 pr2
("ignoring all but the last specified directory: "^
chosen);
866 else List.hd
!args in
867 if !FC.include_path
=*= None
868 then FC.include_path
:= Some
(Filename.concat
chosen_dir "include"));
870 args := List.rev
!args;
872 if !cocci_file <> "" && (not
(!cocci_file =~
".*\\.\\(sgrep\\|spatch\\)$"))
873 then cocci_file := Common.adjust_ext_if_needed
!cocci_file ".cocci";
875 if !Config.std_iso
<> ""
876 then Config.std_iso
:= Common.adjust_ext_if_needed
!Config.std_iso
".iso";
877 if !Config.std_h
<> ""
878 then Config.std_h
:= Common.adjust_ext_if_needed
!Config.std_h
".h";
880 if !Config.std_h
<> ""
881 then Parse_c.init_defs_builtins
!Config.std_h
;
884 then Parse_c.init_defs_macros
!macro_file;
887 (* must be done after Arg.parse, because Common.profile is set by it *)
888 Common.profile_code
"Main total" (fun () ->
891 let all_actions = Test_parsing_c.actions
() in
895 (* --------------------------------------------------------- *)
896 (* The test framework. Works with tests/ or .ok and .failed *)
897 (* --------------------------------------------------------- *)
898 | [x
] when !test_mode ->
899 FC.include_path
:= Some
"tests/include";
900 Testing.testone x
!compare_with_expected
902 | [] when !test_all ->
903 FC.include_path
:= Some
"tests/include";
904 if !expected_score_file <> ""
905 then Testing.testall ~
expected_score_file:!expected_score_file ()
906 else Testing.testall
()
908 | [] when !test_regression_okfailed ->
909 Testing.test_regression_okfailed ()
911 | x
::xs when !test_okfailed ->
912 (* do its own timeout on FC.timeout internally *)
913 FC.relax_include_path
:= true;
914 adjust_stdin x
(fun () ->
915 Testing.test_okfailed !cocci_file (x
::xs)
918 (* --------------------------------------------------------- *)
919 (* Actions, useful to debug subpart of coccinelle *)
920 (* --------------------------------------------------------- *)
922 | xs when List.mem
!action (Common.action_list
all_actions) ->
923 Common.do_action
!action xs all_actions
925 | [file
] when !action =$
= "-parse_cocci" ->
926 Testing.test_parse_cocci file
928 (* I think this is used by some scripts in some Makefile for our
929 * big-tests. So dont remove.
931 | [file1
;file2
] when !action =$
= "-compare_c" ->
932 Test_parsing_c.test_compare_c file1 file2
(* result = unix code *)
934 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
937 (* --------------------------------------------------------- *)
938 (* This is the main entry *)
939 (* --------------------------------------------------------- *)
940 | x
::xs -> main_action (x
::xs)
942 (* --------------------------------------------------------- *)
944 (* --------------------------------------------------------- *)
945 | [] -> short_usage()
947 if !Pycocci.initialised
&& (Pycocci.py_isinitialized
()) != 0 then begin
948 ignore
(Pycocci.pyrun_simplestring
"cocci.finalise()");
950 then Common.pr2
"Finalizing python\n";
951 Pycocci.py_finalize
();
956 let main_with_better_error_report () =
957 if !Common.debugger
then main ()
962 | Unix.Unix_error
(_, "stat", filename
) ->
963 pr2
(spf
"ERROR: File %s does not exist" filename
);
964 raise
(UnixExit
(-1))
966 (*****************************************************************************)
968 Common.main_boilerplate
(fun () ->
969 main_with_better_error_report ();
970 Ctlcocci_integration.print_bench
();