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.
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 *)
40 let preprocess = ref false (* run the C preprocessor before cocci *)
41 let compat_mode = ref false
43 (* somehow obsolete now *)
46 let include_headers = ref false
47 let kbuild_info = ref ""
49 let macro_file = ref ""
52 let test_mode = ref false
53 let test_all = ref false
54 let test_okfailed = ref false
55 let test_regression_okfailed = ref false
56 let expected_score_file = ref ""
62 (* works with -test but also in "normal" spatch mode *)
63 let compare_with_expected = ref false
65 let distrib_index = ref (None
: int option)
66 let distrib_max = ref (None
: int option)
67 let mod_distrib = ref false
70 (*****************************************************************************)
72 (*****************************************************************************)
74 (* pair of (list of flags to set true, list of flags to set false *)
79 (* FC.show_diff; just leave this as it is *)
88 FC.show_before_fixed_flow
;
91 FC.show_binding_in_out
;
95 Flag_parsing_c.show_parsing_error
;
97 Flag_parsing_c.verbose_lexing
;
98 Flag_parsing_c.verbose_parsing
;
99 Flag_parsing_c.verbose_type
;
100 Flag_parsing_c.verbose_cfg
;
101 Flag_parsing_c.verbose_unparsing
;
102 Flag_parsing_c.verbose_visit
;
103 Flag_parsing_c.verbose_cpp_ast
;
105 Flag_matcher.verbose_matcher
;
106 Flag_matcher.debug_engine
;
108 Flag_parsing_c.debug_unparsing
;
110 Flag_parsing_cocci.show_SP
;
111 Flag_parsing_cocci.show_iso_failures
;
113 Flag_ctl.verbose_ctl_engine
;
114 Flag_ctl.verbose_match
;
119 (* some information that is useful in seeing why a semantic patch doesn't
121 let debug_profile = (
128 FC.show_binding_in_out
;
129 FC.show_dependencies
;
131 Flag_parsing_cocci.show_iso_failures
;
135 Flag_parsing_c.verbose_cfg
;
136 Flag_parsing_c.verbose_unparsing
;
137 Flag_parsing_c.verbose_visit
;
139 Flag_matcher.verbose_matcher
;
141 Flag_parsing_c.show_parsing_error
;
149 FC.show_before_fixed_flow
;
153 Flag_parsing_cocci.show_SP
;
154 Flag_ctl.verbose_ctl_engine
;
155 Flag_ctl.verbose_match
;
156 Flag_matcher.debug_engine
;
157 Flag_parsing_c.debug_unparsing
;
158 Flag_parsing_c.verbose_type
;
159 Flag_parsing_c.verbose_parsing
;
174 FC.show_before_fixed_flow
;
177 FC.show_binding_in_out
;
179 Flag_parsing_cocci.show_SP
;
180 Flag_parsing_cocci.show_iso_failures
;
181 Flag_ctl.verbose_ctl_engine
;
182 Flag_ctl.verbose_match
;
183 Flag_matcher.debug_engine
;
184 Flag_parsing_c.debug_unparsing
;
185 Flag_parsing_c.verbose_type
;
186 Flag_parsing_c.verbose_parsing
;
190 let (set_to_true
, set_to_false
) = p
in
191 List.iter
(fun x
-> x
:= false) set_to_false
;
192 List.iter
(fun x
-> x
:= true) set_to_true
194 (*****************************************************************************)
195 (* The spatch options *)
196 (*****************************************************************************)
199 "Usage: " ^ basename
Sys.argv
.(0) ^
200 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
201 "\n" ^
"Options are:"
203 (* forward reference trick *)
204 let short_usage_func = ref (fun () -> ())
205 let long_usage_func = ref (fun () -> ())
208 (* The short_options are user-oriented. The other options are for
209 * the developers of coccinelle or advanced-users that know
210 * quite well the underlying semantics of coccinelle.
214 (* will be printed when use only ./spatch. For the rest you have to
215 * use -longhelp to see them.
217 let short_options = [
218 "-sp_file", Arg.Set_string
cocci_file,
219 " <file> the semantic patch file";
221 "-o", Arg.Set_string
output_file,
222 " <file> the output file";
223 "-inplace", Arg.Set
inplace_modif,
224 " do the modification on the file directly";
225 "-outplace", Arg.Set
outplace_modif,
226 " store modifications in a .cocci_res file";
228 "-U", Arg.Int
(fun n
-> Flag_parsing_c.diff_lines
:= Some
(i_to_s n
)),
229 " set number of diff context lines";
230 "-partial_match", Arg.Set
Flag_ctl.partial_match
,
231 " report partial matches of the SP on the C file";
233 "-iso_file", Arg.Set_string
Config.std_iso
,
234 " <file> (default=" ^
!Config.std_iso ^
")";
235 "-macro_file", Arg.Set_string
macro_file,
237 "-macro_file_builtins", Arg.Set_string
Config.std_h
,
238 " <file> (default=" ^
!Config.std_h ^
")";
241 Arg.Unit
(function _
-> FC.include_options
:= FC.I_ALL_INCLUDES
),
242 " causes all available include files to be used";
244 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NO_INCLUDES
),
245 " causes not even local include files to be used";
247 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NORMAL_INCLUDES
),
248 " causes local include files to be used";
249 "-include_headers", Arg.Set
include_headers,
250 " process header files independently";
251 "-I", Arg.String
(function x
-> FC.include_path
:= Some x
),
252 " <dir> containing the header files (optional)";
254 "-preprocess", Arg.Set
preprocess,
255 " run the C preprocessor before applying the semantic match";
257 "-c", Arg.Set
compat_mode, " gcc/cpp compatibility mode";
260 " <dir> process all files in directory recursively";
262 "-use_glimpse", Arg.Unit
(function _
-> Flag.scanner
:= Flag.Glimpse
),
263 " works with -dir, use info generated by glimpseindex";
264 "-use_google", Arg.String
(function s
-> Flag.scanner
:= Flag.Google s
),
265 " find relevant files using google code search";
266 "-patch", Arg.String
(function s
-> Flag.patch
:= Some s
),
267 (" <dir> path name with respect to which a patch should be created\n"^
268 " \"\" for a file in the current directory");
269 "-kbuild_info", Arg.Set_string
kbuild_info,
270 " <file> improve -dir by grouping related c files";
271 "-pyoutput", Arg.Set_string
Flag.pyoutput
,
272 " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>";
275 "-version", Arg.Unit
(fun () ->
276 let withpython = if Pycocci.python_support
then "with" else "without" in
277 pr2
(spf
"spatch version %s %s Python support" Config.version
withpython);
282 "-date", Arg.Unit
(fun () ->
283 pr2
"version: $Date: 2010/01/04 11:16:30 $";
284 raise
(Common.UnixExit
0)
288 "-shorthelp", Arg.Unit
(fun () ->
290 raise
(Common.UnixExit
0)
292 " see short list of options";
293 "-longhelp", Arg.Unit
(fun () ->
295 raise
(Common.UnixExit
0)
297 " see all the available options in different categories";
298 "-help", Arg.Unit
(fun () ->
300 raise
(Common.UnixExit
0)
303 "--help", Arg.Unit
(fun () ->
305 raise
(Common.UnixExit
0)
310 (* the format is a list of triples:
311 * (title of section * (optional) explanation of sections * option list)
313 let other_options = [
314 "aliases and obsolete options",
317 "-sp", Arg.Set_string
cocci_file, " short option of -sp_file";
318 "-iso", Arg.Set_string
Config.std_iso
, " short option of -iso_file";
320 "-cocci_file", Arg.Set_string
cocci_file,
321 " <file> the semantic patch file";
322 (* "-c", Arg.Set_string cocci_file, " short option of -sp_file"; *)
325 "most useful show options",
328 "-show_diff" , Arg.Set
FC.show_diff
, " ";
329 "-no_show_diff" , Arg.Clear
FC.show_diff
, " ";
330 "-show_flow" , Arg.Set
FC.show_flow
, " ";
331 (* works in conjunction with -show_ctl_text *)
334 (function _
-> FC.show_ctl_text
:= true; FC.inline_let_ctl
:= true), " ";
335 "-ctl_show_mcodekind",
337 (function _
-> FC.show_ctl_text
:= true; FC.show_mcodekind_in_ctl
:= true),
339 "-show_bindings", Arg.Set
FC.show_binding_in_out
, " ";
340 "-show_transinfo", Arg.Set
Flag.show_transinfo
, " ";
341 "-show_misc", Arg.Set
Flag.show_misc
, " ";
342 "-show_trying", Arg.Set
Flag.show_trying
,
343 " show the name of each function being processed";
344 "-show_dependencies",
345 Arg.Unit
(function _
-> FC.show_dependencies
:= true;
346 FC.show_binding_in_out
:= true),
347 " show the dependencies related to each rule";
350 "verbose subsystems options",
353 "-verbose_ctl_engine",
354 Arg.Unit
(function _
->
355 Flag_ctl.verbose_ctl_engine
:= true; FC.show_ctl_text
:= true) , " ";
356 "-verbose_match", Arg.Set
Flag_ctl.verbose_match
, " ";
357 "-verbose_engine", Arg.Set
Flag_matcher.debug_engine
, " ";
358 "-graphical_trace", Arg.Set
Flag_ctl.graphical_trace
, " generate a pdf file representing the matching process";
360 Arg.Unit
(function _
->
361 Flag_ctl.graphical_trace
:= true; Flag_ctl.gt_without_label
:= true),
362 " remove graph label (requires option -graphical_trace)";
364 "-parse_error_msg", Arg.Set
Flag_parsing_c.verbose_parsing
, " ";
365 "-type_error_msg", Arg.Set
Flag_parsing_c.verbose_type
, " ";
366 (* could also use Flag_parsing_c.options_verbose *)
369 "other show options",
372 "-show_c" , Arg.Set
FC.show_c
, " ";
373 "-show_cocci" , Arg.Set
FC.show_cocci
, " ";
374 "-show_before_fixed_flow" , Arg.Set
FC.show_before_fixed_flow
, " ";
375 "-show_ctl_tex" , Arg.Set
FC.show_ctl_tex
, " ";
376 "-show_ctl_text" , Arg.Set
FC.show_ctl_text
, " ";
377 "-show_SP" , Arg.Set
Flag_parsing_cocci.show_SP
, " ";
381 "debug C parsing/unparsing",
384 "-debug_cpp", Arg.Set
Flag_parsing_c.debug_cpp
, " ";
385 "-debug_lexer", Arg.Set
Flag_parsing_c.debug_lexer
, " ";
386 "-debug_etdt", Arg.Set
Flag_parsing_c.debug_etdt
, " ";
387 "-debug_typedef", Arg.Set
Flag_parsing_c.debug_typedef
, " ";
389 "-filter_msg", Arg.Set
Flag_parsing_c.filter_msg
,
390 " filter some cpp message when the macro is a \"known\" cpp construct";
391 "-filter_define_error", Arg.Set
Flag_parsing_c.filter_define_error
," ";
392 "-filter_msg_define_error", Arg.Set
Flag_parsing_c.filter_msg_define_error
,
393 " filter the error msg";
394 "-filter_passed_level", Arg.Set_int
Flag_parsing_c.filter_passed_level
," ";
395 (* debug cfg doesn't seem to have any effect, so drop it as an option *)
396 (* "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " "; *)
397 "-debug_unparsing", Arg.Set
Flag_parsing_c.debug_unparsing
, " ";
400 (* could use Flag_parsing_c.options_debug_with_title instead *)
403 "shortcut for enabling/disabling a set of debugging options at once",
406 (* todo: other profile ? *)
407 "-quiet", Arg.Unit
(fun () -> run_profile quiet_profile), " ";
408 "-debug", Arg.Unit
(fun () -> run_profile debug_profile), " ";
409 "-pad", Arg.Unit
(fun () -> run_profile pad_profile), " ";
416 "-profile", Arg.Unit
(function () -> Common.profile
:= Common.PALL
) ,
417 " gather timing information about the main coccinelle functions";
418 "-bench", Arg.Int
(function x
-> Flag_ctl.bench
:= x
),
419 " <level> for profiling the CTL engine";
420 "-timeout", Arg.Int
(fun x
-> FC.timeout
:= Some x
),
421 " <sec> timeout in seconds";
422 "-steps", Arg.Int
(fun x
-> Flag_ctl.steps
:= Some x
),
423 " max number of model checking steps per code unit";
424 "-iso_limit", Arg.Int
(fun x
-> Flag_parsing_cocci.iso_limit
:= Some x
),
425 " max depth of iso application";
426 "-no_iso_limit", Arg.Unit
(fun _
-> Flag_parsing_cocci.iso_limit
:= None
),
427 " disable limit on max depth of iso application";
428 "-track_iso", Arg.Set
Flag.track_iso_usage
,
429 " gather information about isomorphism usage";
434 (*post_engine not included, because it doesn't use isos*)
435 PSOME
["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]),
436 " gather information about the cost of isomorphism usage"
441 "change of algorithm options",
444 "-popl", Arg.Set
FC.popl
,
445 " simplified SmPL, for the popl paper";
449 (function _
-> FC.popl
:= true; Flag_popl.mark_all
:= true),
450 " simplified SmPL, for the popl paper";
452 "-popl_keep_all_wits",
454 (function _
-> FC.popl
:= true; Flag_popl.keep_all_wits
:= true),
455 " simplified SmPL, for the popl paper";
459 Flag.make_hrule
:= Some s
; FC.include_options
:= FC.I_NO_INCLUDES
),
460 " semantic patch generation";
462 "-keep_comments", Arg.Set
Flag_parsing_c.keep_comments
,
463 " keep comments around removed code";
465 "-loop", Arg.Set
Flag_ctl.loop_in_src_code
, " ";
466 "-no_loops", Arg.Set
Flag_parsing_c.no_loops
,
467 " drop all back edges derived from looping constructs - unsafe";
468 "-no_gotos", Arg.Set
Flag_parsing_c.no_gotos
,
469 " drop all jumps derived from gotos - unsafe";
471 "-l1", Arg.Clear
Flag_parsing_c.label_strategy_2
, " ";
472 "-ifdef_to_if", Arg.Set
FC.ifdef_to_if
,
473 " convert ifdef to if (experimental)";
474 "-no_ifdef_to_if", Arg.Clear
FC.ifdef_to_if
,
475 " convert ifdef to if (experimental)";
477 "-disable_multi_pass", Arg.Set
Flag_parsing_c.disable_multi_pass
, " ";
479 "-noif0_passing", Arg.Clear
Flag_parsing_c.if0_passing
,
481 "-noadd_typedef_root", Arg.Clear
Flag_parsing_c.add_typedef_root
, " ";
482 (* could use Flag_parsing_c.options_algo instead *)
485 "-disallow_nested_exps", Arg.Set
Flag_matcher.disallow_nested_exps
,
486 " disallow an expresion pattern from matching a term and its subterm";
487 "-disable_worth_trying_opt", Arg.Clear
FC.worth_trying_opt
,
489 "-only_return_is_error_exit",
490 Arg.Set
Flag_matcher.only_return_is_error_exit
,
491 "if this flag is not set, then break and continue are also error exits";
492 (* the following is a hack to make it easier to add code in sgrep-like
493 code, essentially to compensate for the fact that we don't have
494 any way of printing things out *)
495 "-allow_inconsistent_paths",
496 Arg.Set
Flag_matcher.allow_inconsistent_paths
,
497 " if this flag is set don't check for inconsistent paths; dangerous";
498 "-no_safe_expressions",
499 Arg.Set
Flag_matcher.no_safe_expressions
,
500 " make an expression disjunction not prioritise the topmost disjunct";
501 "-int_bits", Arg.Int
Flag_parsing_c.set_int_bits
,
502 " the number of bits in an unsigned int";
503 "-long_bits", Arg.Int
Flag_parsing_c.set_long_bits
,
504 " the number of bits in an unsigned long";
505 "-linux_spacing", Arg.Unit
Flag_parsing_c.set_linux_spacing
,
506 " spacing of + code follows the conventions of Linux";
507 "-smpl_spacing", Arg.Unit
Flag_parsing_c.set_smpl_spacing
,
508 " spacing of + code follows the semantic patch";
509 "-D", Arg.String
Flag_parsing_cocci.set_defined_virtual_rules
,
510 " indicate that a virtual rule should be considered to be matched";
516 "-debugger", Arg.Set
Common.debugger
,
517 " option to set if launch spatch in ocamldebug";
518 "-disable_once", Arg.Set
Common.disable_pr2_once
,
519 " to print more messages";
520 "-show_trace_profile", Arg.Set
Common.show_trace_profile
,
522 "-save_tmp_files", Arg.Set
Common.save_tmp_files
, " ";
528 "-index", Arg.Int
(function x
-> distrib_index := Some x
) ,
529 " the processor to use for this run of spatch";
530 "-max", Arg.Int
(function x
-> distrib_max := Some x
) ,
531 " the number of processors available";
532 "-mod_distrib", Arg.Set
mod_distrib,
533 " use mod to distribute files among the processors";
539 "-use_cache", Arg.Set
Flag_parsing_c.use_cache
,
540 " use .ast_raw pre-parsed cached C file";
541 (* could use Flag_parsing_c.options_pad instead *)
546 "test mode and test options (works with tests/ or .ok files)",
547 "The test options don't work with the -sp_file and so on.",
549 "-test", Arg.Set
test_mode,
550 " <file> launch spatch on tests/file.[c,cocci]";
551 "-testall", Arg.Set
test_all,
552 " launch spatch on all files in tests/ having a .res";
553 "-test_okfailed", Arg.Set
test_okfailed,
554 " generates .{ok,failed,spatch_ok} files using .res files";
555 "-test_regression_okfailed", Arg.Set
test_regression_okfailed,
556 " process the .{ok,failed,spatch_ok} files in current dir";
558 "-compare_with_expected", Arg.Set
compare_with_expected,
559 " use also file.res";
560 "-expected_score_file", Arg.Set_string
expected_score_file,
561 " which score file to compare with in -testall";
562 "-relax_include_path", Arg.Set
FC.relax_include_path
,
567 ("The action options don't work with the -sp_file and so on." ^
"\n" ^
568 "It's for the other (internal) uses of the spatch program."
571 (* -token_c, -parse_c, etc *)
572 ((Common.options_of_actions
action (Test_parsing_c.actions
())) ++
574 (let s = "-parse_cocci" in s, Arg.Unit
(fun () -> action := s),
576 (let s = "-compare_c" in s, Arg.Unit
(fun () -> action := s),
583 short_options ++ List.concat
(List.map
Common.thd3
other_options)
585 (* I don't want the -help and --help that are appended by Arg.align *)
587 Arg.align xs
+> List.rev
+> Common.drop
2 +> List.rev
589 (* copy paste of Arg.parse. Don't want the default -help msg *)
590 let arg_parse2 l f msg
=
592 Arg.parse_argv
Sys.argv l f msg
;
594 | Arg.Bad msg
-> (* eprintf "%s" msg; exit 2; *)
595 let xs = Common.lines msg
in
596 (* take only head, it's where the error msg is *)
599 raise
(Common.UnixExit
(2))
600 | Arg.Help msg
-> (* printf "%s" msg; exit 0; *)
601 raise Impossible
(* -help is specified in speclist *)
607 Common.short_usage usage_msg short_options;
609 pr2
"Example of use:";
610 pr2
" ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
616 Common.long_usage usage_msg short_options other_options
618 let _ = short_usage_func := short_usage
619 let _ = long_usage_func := long_usage
621 (*****************************************************************************)
623 (*****************************************************************************)
625 let adjust_stdin cfile k
=
631 let (dir, base
, ext
) = Common.dbe_of_filename cfile
in
632 let varfile = Common.filename_of_dbe
(dir, base
, "var") in
633 if ext
=$
= "c" && Common.lfile_exists
varfile
636 with Invalid_argument
("Filename.chop_extension") -> None
638 Common.redirect_stdin_opt
newin k
640 let glimpse_filter (coccifile
, isofile
) dir =
641 let (_metavars
,astcocci
,_free_var_lists
,_negated_positions
,
642 _used_after_lists
,_positions_lists
,_,query
) =
643 Cocci.sp_of_file coccifile
(Some isofile
) in
645 None
-> pr2
"no glimpse keyword inferred from snippet"; None
647 (let suffixes = if !include_headers then ["c";"h"] else ["c"] in
648 pr2
("glimpse request = " ^ query
);
649 let command = spf
"glimpse -y -H %s -N -W -w '%s'" dir query
in
650 let (glimpse_res
,stat
) = Common.cmd_to_list_and_status
command in
652 Unix.WEXITED
(0) | Unix.WEXITED
(1) ->
656 (fun file
-> List.mem
(Common.filesuffix file
) suffixes))
657 | _ -> None
(* error, eg due to pattern too big *))
658 | _ -> failwith
"not possible"
660 (*****************************************************************************)
662 (*****************************************************************************)
668 (* a more general solution would be to use
669 * Common.files_of_dir_or_files (x::xs)
670 * as some elements in xs may also be directories, or individual
673 if Common.is_directory x
676 adjust_stdin x
(fun () ->
677 if !cocci_file =$
= ""
678 then failwith
"I need a cocci file, use -sp_file <file>";
680 if !dir && !Flag.patch
=*= None
683 | [] -> Flag.patch
:= Some x
686 ("warning: patch output can only be created when only one\n"^
687 "directory is specified or when the -patch flag is used")
691 Common.profile_code
"Main.infiles computation" (fun () ->
692 match !dir, !kbuild_info, !Flag.scanner
= Flag.Glimpse
with
695 failwith
"-use_glimpse works only with -dir"
696 | true, s, true when s <> "" ->
697 failwith
"-use_glimpse does not work with -kbuild"
700 then failwith
"-use_glimpse can accept only one dir";
704 match glimpse_filter (!cocci_file, !Config.std_iso
) x
with
706 Common.cmd_to_list
(* same as "true, "", _" case *)
708 (* FIXME : Could we remove xs ?
709 -use_glimpse requires a singleton.
710 This is checked some lines before.
711 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
712 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
714 then ("find "^ x ^
" -name \"*.[ch]\"")
715 else ("find "^ x ^
" -name \"*.c\""))
716 | Some
files -> files in
717 files +> List.map
(fun x
-> [x
])
719 | false, _, _ -> [x
::xs]
723 then ("find "^
(join
" " (x
::xs))^
" -name \"*.[ch]\"")
724 else ("find "^
(join
" " (x
::xs))^
" -name \"*.c\""))
725 +> List.map
(fun x
-> [x
])
728 | true, kbuild_info_file
,_ ->
730 Common.cmd_to_list
("find "^
(join
" " (x
::xs))^
" -type d")
732 let info = Kbuild.parse_kbuild_info kbuild_info_file
in
733 let groups = Kbuild.files_in_dirs
dirs info in
735 groups +> List.map
(function Kbuild.Group
xs -> xs)
740 match (!distrib_index,!distrib_max) with
741 (None
,None
) -> infiles
742 | (Some index
,Some max
) ->
745 failwith
"index starts at 0, and so must be less than max");
748 let rec loop ct
= function
751 if (ct
mod max
) =|= index
752 then x
::(loop (ct
+1) xs)
753 else loop (ct
+1) xs in
757 let all_files = List.length
infiles in
758 let regions = (all_files + (max
- 1)) / max
in
759 let this_min = index
* regions in
760 let this_max = (index
+1) * regions in
761 let rec loop ct
= function
764 if this_min <= ct
&& ct
< this_max
765 then x
::(loop (ct
+1) xs)
766 else loop (ct
+1) xs in
769 | _ -> failwith
"inconsistent distribution information" in
772 Common.profile_code
"Main.outfiles computation" (fun () ->
774 Cocci.pre_engine
(!cocci_file, !Config.std_iso
) in
776 infiles +> List.map
(fun cfiles
->
777 pr2
("HANDLING: " ^
(join
" " cfiles
));
778 Common.timeout_function_opt
!FC.timeout
(fun () ->
779 Common.report_if_take_time
10 (join
" " cfiles
) (fun () ->
780 (*let s = profile_diagnostic() in*)
784 if !output_file <> "" && !compat_mode then
789 Common.redirect_stdout_opt
optfile (fun () ->
790 (* this is the main call *)
791 Cocci.full_engine
cocci_infos cfiles
794 | Common.UnixExit x
-> raise
(Common.UnixExit x
)
799 pr2(profile_diagnostic());*)
802 pr2
("EXN:" ^
Printexc.to_string e
);
806 Cocci.post_engine
cocci_infos;
811 Common.profile_code
"Main.result analysis" (fun () ->
812 Ctlcocci_integration.print_bench
();
813 let outfiles = Cocci.check_duplicate_modif
outfiles in
814 outfiles +> List.iter
(fun (infile
, outopt
) ->
815 outopt
+> Common.do_option
(fun outfile
->
818 Common.command2
("cp "^infile^
" "^infile^
".cocci_orig");
819 Common.command2
("cp "^outfile^
" "^infile
);
823 then Common.command2
("cp "^outfile^
" "^infile^
".cocci_res");
825 (* potential source of security pb if the /tmp/ file is
826 * a symlink, so simpler to not produce any regular file
827 * (files created by Common.new_temp_file are still ok)
831 if !output_file =$= ""
833 let tmpfile = "/tmp/"^Common.basename infile in
834 pr2 (spf "One file modified. Result is here: %s" tmpfile);
835 Common.command2 ("cp "^outfile^" "^tmpfile);
839 if !output_file <> "" && not
!compat_mode then
841 | [infile
, Some outfile
] when infile
=$
= x
&& null
xs ->
842 Common.command2
("cp " ^outfile^
" " ^
!output_file);
843 | [infile
, None
] when infile
=$
= x
&& null
xs ->
844 Common.command2
("cp " ^infile^
" " ^
!output_file);
847 ("-o can not be applied because there is multiple " ^
850 if !compare_with_expected
851 then Testing.compare_with_expected outfiles))
853 | [] -> raise Impossible
856 (*****************************************************************************)
857 (* The coccinelle main entry point *)
858 (*****************************************************************************)
861 let arglist = Array.to_list
Sys.argv
in
863 if not
(null
(Common.inter_set
arglist
864 ["-cocci_file";"-sp_file";"-sp";"-test";"-testall";
865 "-test_okfailed";"-test_regression_okfailed"]))
866 then run_profile quiet_profile;
870 (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*)
872 (* this call can set up many global flag variables via the cmd line *)
873 arg_parse2 (Arg.align
all_options) (fun x
-> args := x
::!args) usage_msg;
875 (* julia hack so that one can override directories specified on
876 * the command line. *)
880 if List.length
!args > 1
883 let chosen = List.hd
!args in
884 pr2
("ignoring all but the last specified directory: "^
chosen);
888 else List.hd
!args in
889 if !FC.include_path
=*= None
890 then FC.include_path
:= Some
(Filename.concat
chosen_dir "include"));
892 args := List.rev
!args;
894 if !cocci_file <> "" && (not
(!cocci_file =~
".*\\.\\(sgrep\\|spatch\\)$"))
895 then cocci_file := Common.adjust_ext_if_needed
!cocci_file ".cocci";
897 if !Config.std_iso
<> ""
898 then Config.std_iso
:= Common.adjust_ext_if_needed
!Config.std_iso
".iso";
899 if !Config.std_h
<> ""
900 then Config.std_h
:= Common.adjust_ext_if_needed
!Config.std_h
".h";
902 if !Config.std_h
<> ""
903 then Parse_c.init_defs_builtins
!Config.std_h
;
906 then Parse_c.init_defs_macros
!macro_file;
909 (* must be done after Arg.parse, because Common.profile is set by it *)
910 Common.profile_code
"Main total" (fun () ->
913 let all_actions = Test_parsing_c.actions
() in
917 (* --------------------------------------------------------- *)
918 (* The test framework. Works with tests/ or .ok and .failed *)
919 (* --------------------------------------------------------- *)
920 | [x
] when !test_mode ->
921 FC.include_path
:= Some
"tests/include";
922 Testing.testone x
!compare_with_expected
924 | [] when !test_all ->
925 FC.include_path
:= Some
"tests/include";
926 if !expected_score_file <> ""
927 then Testing.testall ~
expected_score_file:!expected_score_file ()
928 else Testing.testall
()
930 | [] when !test_regression_okfailed ->
931 Testing.test_regression_okfailed ()
933 | x
::xs when !test_okfailed ->
934 (* do its own timeout on FC.timeout internally *)
935 FC.relax_include_path
:= true;
936 adjust_stdin x
(fun () ->
937 Testing.test_okfailed !cocci_file (x
::xs)
940 (* --------------------------------------------------------- *)
941 (* Actions, useful to debug subpart of coccinelle *)
942 (* --------------------------------------------------------- *)
944 | xs when List.mem
!action (Common.action_list
all_actions) ->
945 Common.do_action
!action xs all_actions
947 | [file
] when !action =$
= "-parse_cocci" ->
948 Testing.test_parse_cocci file
950 (* I think this is used by some scripts in some Makefile for our
951 * big-tests. So dont remove.
953 | [file1
;file2
] when !action =$
= "-compare_c" ->
954 Test_parsing_c.test_compare_c file1 file2
(* result = unix code *)
956 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
959 (* --------------------------------------------------------- *)
960 (* This is the main entry *)
961 (* --------------------------------------------------------- *)
962 | x
::xs -> main_action (x
::xs)
964 (* --------------------------------------------------------- *)
966 (* --------------------------------------------------------- *)
967 | [] -> short_usage()
969 if !Pycocci.initialised
&& (Pycocci.py_isinitialized
()) != 0 then begin
970 ignore
(Pycocci.pyrun_simplestring
"cocci.finalise()");
972 then Common.pr2
"Finalizing python\n";
973 Pycocci.py_finalize
();
978 let main_with_better_error_report () =
979 if !Common.debugger
then main ()
984 | Unix.Unix_error
(_, "stat", filename
) ->
985 pr2
(spf
"ERROR: File %s does not exist" filename
);
986 raise
(UnixExit
(-1))
988 (*****************************************************************************)
990 Common.main_boilerplate
(fun () ->
991 main_with_better_error_report ();
992 Ctlcocci_integration.print_bench
();