2 * Copyright 2005-2010, 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 * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
25 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
26 * This file is part of Coccinelle.
28 * Coccinelle is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, according to version 2 of the License.
32 * Coccinelle is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
40 * The authors reserve the right to distribute this or future versions of
41 * Coccinelle under other licenses.
46 module FC
= Flag_cocci
48 (*****************************************************************************)
50 (*****************************************************************************)
52 (* In addition to flags that can be tweaked via -xxx options (cf the
53 * full list of options in "the spatch options" section below), the
54 * spatch program also depends on external files, described in
55 * globals/config.ml, mainly a standard.h and standard.iso file *)
57 let cocci_file = ref ""
59 let output_file = ref ""
60 let inplace_modif = ref false (* but keeps nothing *)
62 ref (None
: string option) (* suffix for backup if one is desired *)
63 let outplace_modif = ref false (* generates a .cocci_res *)
64 let preprocess = ref false (* run the C preprocessor before cocci *)
65 let compat_mode = ref false
67 (* somehow obsolete now *)
70 let include_headers = ref false
71 let kbuild_info = ref ""
73 let macro_file = ref ""
76 let test_mode = ref false
77 let test_all = ref false
78 let test_okfailed = ref false
79 let test_regression_okfailed = ref false
80 let expected_score_file = ref ""
86 (* works with -test but also in "normal" spatch mode *)
87 let compare_with_expected = ref false
89 let distrib_index = ref (None
: int option)
90 let distrib_max = ref (None
: int option)
91 let mod_distrib = ref false
94 (*****************************************************************************)
96 (*****************************************************************************)
98 (* pair of (list of flags to set true, list of flags to set false *)
99 let very_quiet_profile = (
103 (* FC.show_diff; just leave this as it is *)
105 Common.print_to_stderr
;
113 FC.show_before_fixed_flow
;
116 FC.show_binding_in_out
;
120 Flag_parsing_c.show_parsing_error
;
122 Flag_parsing_c.verbose_lexing
;
123 Flag_parsing_c.verbose_parsing
;
124 Flag_parsing_c.verbose_type
;
125 Flag_parsing_c.verbose_cfg
;
126 Flag_parsing_c.verbose_unparsing
;
127 Flag_parsing_c.verbose_visit
;
128 Flag_parsing_c.verbose_cpp_ast
;
130 Flag_matcher.verbose_matcher
;
131 Flag_matcher.debug_engine
;
133 Flag_parsing_c.debug_unparsing
;
135 Flag_parsing_cocci.show_SP
;
136 Flag_parsing_cocci.show_iso_failures
;
138 Flag_ctl.verbose_ctl_engine
;
139 Flag_ctl.verbose_match
;
144 let quiet_profile = (
146 Common.print_to_stderr
149 (* FC.show_diff; just leave this as it is *)
158 FC.show_before_fixed_flow
;
161 FC.show_binding_in_out
;
165 Flag_parsing_c.show_parsing_error
;
167 Flag_parsing_c.verbose_lexing
;
168 Flag_parsing_c.verbose_parsing
;
169 Flag_parsing_c.verbose_type
;
170 Flag_parsing_c.verbose_cfg
;
171 Flag_parsing_c.verbose_unparsing
;
172 Flag_parsing_c.verbose_visit
;
173 Flag_parsing_c.verbose_cpp_ast
;
175 Flag_matcher.verbose_matcher
;
176 Flag_matcher.debug_engine
;
178 Flag_parsing_c.debug_unparsing
;
180 Flag_parsing_cocci.show_SP
;
181 Flag_parsing_cocci.show_iso_failures
;
183 Flag_ctl.verbose_ctl_engine
;
184 Flag_ctl.verbose_match
;
189 (* some information that is useful in seeing why a semantic patch doesn't
191 let debug_profile = (
193 Common.print_to_stderr
;
199 FC.show_binding_in_out
;
200 FC.show_dependencies
;
202 Flag_parsing_cocci.show_iso_failures
;
206 Flag_parsing_c.verbose_cfg
;
207 Flag_parsing_c.verbose_unparsing
;
208 Flag_parsing_c.verbose_visit
;
210 Flag_matcher.verbose_matcher
;
212 Flag_parsing_c.show_parsing_error
;
220 FC.show_before_fixed_flow
;
224 Flag_parsing_cocci.show_SP
;
225 Flag_ctl.verbose_ctl_engine
;
226 Flag_ctl.verbose_match
;
227 Flag_matcher.debug_engine
;
228 Flag_parsing_c.debug_unparsing
;
229 Flag_parsing_c.verbose_type
;
230 Flag_parsing_c.verbose_parsing
;
236 Common.print_to_stderr
;
246 FC.show_before_fixed_flow
;
249 FC.show_binding_in_out
;
251 Flag_parsing_cocci.show_SP
;
252 Flag_parsing_cocci.show_iso_failures
;
253 Flag_ctl.verbose_ctl_engine
;
254 Flag_ctl.verbose_match
;
255 Flag_matcher.debug_engine
;
256 Flag_parsing_c.debug_unparsing
;
257 Flag_parsing_c.verbose_type
;
258 Flag_parsing_c.verbose_parsing
;
262 let (set_to_true
, set_to_false
) = p
in
263 List.iter
(fun x
-> x
:= false) set_to_false
;
264 List.iter
(fun x
-> x
:= true) set_to_true
266 (*****************************************************************************)
267 (* The spatch options *)
268 (*****************************************************************************)
271 "Usage: " ^ basename
Sys.argv
.(0) ^
272 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
273 "\n" ^
"Options are:"
275 (* forward reference trick *)
276 let short_usage_func = ref (fun () -> ())
277 let long_usage_func = ref (fun () -> ())
280 (* The short_options are user-oriented. The other options are for
281 * the developers of coccinelle or advanced-users that know
282 * quite well the underlying semantics of coccinelle.
286 (* will be printed when use only ./spatch. For the rest you have to
287 * use -longhelp to see them.
289 let short_options = [
290 "-sp_file", Arg.Set_string
cocci_file,
291 " <file> the semantic patch file";
293 "-o", Arg.Set_string
output_file,
294 " <file> the output file";
295 "-in_place", Arg.Set
inplace_modif,
296 " do the modification on the file directly";
297 "-backup_suffix", Arg.String
(function s
-> backup_suffix := Some s
),
298 " suffix to use when making a backup for inplace";
299 "-out_place", Arg.Set
outplace_modif,
300 " store modifications in a .cocci_res file";
302 "-U", Arg.Int
(fun n
-> Flag_parsing_c.diff_lines
:= Some
(i_to_s n
)),
303 " set number of diff context lines";
304 "-partial_match", Arg.Set
Flag_ctl.partial_match
,
305 " report partial matches of the SP on the C file";
307 "-iso_file", Arg.Set_string
Config.std_iso
,
308 " <file> (default=" ^
!Config.std_iso ^
")";
309 "-macro_file", Arg.Set_string
macro_file,
311 "-macro_file_builtins", Arg.Set_string
Config.std_h
,
312 " <file> (default=" ^
!Config.std_h ^
")";
315 Arg.Unit
(function _
-> FC.include_options
:= FC.I_ALL_INCLUDES
),
316 " causes all available include files to be used";
318 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NO_INCLUDES
),
319 " causes not even local include files to be used";
321 Arg.Unit
(function _
-> FC.include_options
:= FC.I_NORMAL_INCLUDES
),
322 " causes local include files to be used";
323 "-include_headers", Arg.Set
include_headers,
324 " process header files independently";
325 "-I", Arg.String
(fun x
->
326 FC.include_path
:= x
::!FC.include_path
328 " <dir> containing the header files (optional)";
330 "-preprocess", Arg.Set
preprocess,
331 " run the C preprocessor before applying the semantic match";
333 "-c", Arg.Set
compat_mode, " gcc/cpp compatibility mode";
336 " <dir> process all files in directory recursively";
338 "-use_glimpse", Arg.Unit
(function _
-> Flag.scanner
:= Flag.Glimpse
),
339 " works with -dir, use info generated by glimpseindex";
340 "-use_google", Arg.String
(function s
-> Flag.scanner
:= Flag.Google s
),
341 " find relevant files using google code search";
343 Arg.String
(function s
-> Flag.patch
:= Some
(Cocci.normalize_path s
)),
344 (" <dir> path name with respect to which a patch should be created\n"^
345 " \"\" for a file in the current directory");
346 "-kbuild_info", Arg.Set_string
kbuild_info,
347 " <file> improve -dir by grouping related c files";
348 "-pyoutput", Arg.Set_string
Flag.pyoutput
,
349 " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>";
352 "-version", Arg.Unit
(fun () ->
353 let withpython = if Pycocci.python_support
then "with" else "without" in
354 pr2
(spf
"spatch version %s %s Python support" Config.version
withpython);
359 "-date", Arg.Unit
(fun () ->
360 pr2
"version: $Date: 2010/05/03 11:10:04 $";
361 raise
(Common.UnixExit
0)
365 "-shorthelp", Arg.Unit
(fun () ->
367 raise
(Common.UnixExit
0)
369 " see short list of options";
370 "-longhelp", Arg.Unit
(fun () ->
372 raise
(Common.UnixExit
0)
374 " see all the available options in different categories";
375 "-help", Arg.Unit
(fun () ->
377 raise
(Common.UnixExit
0)
380 "--help", Arg.Unit
(fun () ->
382 raise
(Common.UnixExit
0)
387 (* the format is a list of triples:
388 * (title of section * (optional) explanation of sections * option list)
390 let other_options = [
391 "aliases and obsolete options",
394 "-sp", Arg.Set_string
cocci_file, " short option of -sp_file";
395 "-iso", Arg.Set_string
Config.std_iso
, " short option of -iso_file";
397 "-cocci_file", Arg.Set_string
cocci_file,
398 " <file> the semantic patch file";
399 (* "-c", Arg.Set_string cocci_file, " short option of -sp_file"; *)
402 "most useful show options",
405 "-show_diff" , Arg.Set
FC.show_diff
, " ";
406 "-no_show_diff" , Arg.Clear
FC.show_diff
, " ";
407 "-show_flow" , Arg.Set
FC.show_flow
, " ";
408 (* works in conjunction with -show_ctl_text *)
411 (function _
-> FC.show_ctl_text
:= true; FC.inline_let_ctl
:= true), " ";
412 "-ctl_show_mcodekind",
414 (function _
-> FC.show_ctl_text
:= true; FC.show_mcodekind_in_ctl
:= true),
416 "-show_bindings", Arg.Set
FC.show_binding_in_out
, " ";
417 "-show_transinfo", Arg.Set
Flag.show_transinfo
, " ";
418 "-show_misc", Arg.Set
Flag.show_misc
, " ";
419 "-show_trying", Arg.Set
Flag.show_trying
,
420 " show the name of each function being processed";
421 "-show_dependencies",
422 Arg.Unit
(function _
-> FC.show_dependencies
:= true;
423 FC.show_binding_in_out
:= true),
424 " show the dependencies related to each rule";
427 "verbose subsystems options",
430 "-verbose_ctl_engine",
431 Arg.Unit
(function _
->
432 Flag_ctl.verbose_ctl_engine
:= true; FC.show_ctl_text
:= true) , " ";
433 "-verbose_match", Arg.Set
Flag_ctl.verbose_match
, " ";
434 "-verbose_engine", Arg.Set
Flag_matcher.debug_engine
, " ";
435 "-graphical_trace", Arg.Set
Flag_ctl.graphical_trace
, " generate a pdf file representing the matching process";
437 Arg.Unit
(function _
->
438 Flag_ctl.graphical_trace
:= true; Flag_ctl.gt_without_label
:= true),
439 " remove graph label (requires option -graphical_trace)";
441 "-parse_error_msg", Arg.Set
Flag_parsing_c.show_parsing_error
, " ";
443 Arg.Unit
(fun _
-> Flag_parsing_c.verbose_parsing
:= true;
444 Flag_parsing_c.show_parsing_error
:= true), " ";
445 "-type_error_msg", Arg.Set
Flag_parsing_c.verbose_type
, " ";
446 (* could also use Flag_parsing_c.options_verbose *)
449 "other show options",
452 "-show_c" , Arg.Set
FC.show_c
, " ";
453 "-show_cocci" , Arg.Set
FC.show_cocci
, " ";
454 "-show_before_fixed_flow" , Arg.Set
FC.show_before_fixed_flow
, " ";
455 "-show_ctl_tex" , Arg.Set
FC.show_ctl_tex
, " ";
456 "-show_ctl_text" , Arg.Set
FC.show_ctl_text
, " ";
457 "-show_SP" , Arg.Set
Flag_parsing_cocci.show_SP
, " ";
461 "debug C parsing/unparsing",
464 "-debug_cpp", Arg.Set
Flag_parsing_c.debug_cpp
, " ";
465 "-debug_lexer", Arg.Set
Flag_parsing_c.debug_lexer
, " ";
466 "-debug_etdt", Arg.Set
Flag_parsing_c.debug_etdt
, " ";
467 "-debug_typedef", Arg.Set
Flag_parsing_c.debug_typedef
, " ";
469 "-filter_msg", Arg.Set
Flag_parsing_c.filter_msg
,
470 " filter some cpp message when the macro is a \"known\" cpp construct";
471 "-filter_define_error", Arg.Set
Flag_parsing_c.filter_define_error
," ";
472 "-filter_msg_define_error", Arg.Set
Flag_parsing_c.filter_msg_define_error
,
473 " filter the error msg";
474 "-filter_passed_level", Arg.Set_int
Flag_parsing_c.filter_passed_level
," ";
475 (* debug cfg doesn't seem to have any effect, so drop it as an option *)
476 (* "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " "; *)
477 "-debug_unparsing", Arg.Set
Flag_parsing_c.debug_unparsing
, " ";
480 (* could use Flag_parsing_c.options_debug_with_title instead *)
483 "shortcut for enabling/disabling a set of debugging options at once",
486 (* todo: other profile ? *)
487 "-quiet", Arg.Unit
(fun () -> run_profile quiet_profile), " ";
488 "-very_quiet", Arg.Unit
(fun () -> run_profile very_quiet_profile), " ";
489 "-debug", Arg.Unit
(fun () -> run_profile debug_profile), " ";
490 "-pad", Arg.Unit
(fun () -> run_profile pad_profile), " ";
497 "-profile", Arg.Unit
(function () -> Common.profile
:= Common.PALL
) ,
498 " gather timing information about the main coccinelle functions";
499 "-bench", Arg.Int
(function x
-> Flag_ctl.bench
:= x
),
500 " <level> for profiling the CTL engine";
501 "-timeout", Arg.Int
(fun x
-> FC.timeout
:= Some x
),
502 " <sec> timeout in seconds";
503 "-steps", Arg.Int
(fun x
-> Flag_ctl.steps
:= Some x
),
504 " max number of model checking steps per code unit";
505 "-iso_limit", Arg.Int
(fun x
-> Flag_parsing_cocci.iso_limit
:= Some x
),
506 " max depth of iso application";
507 "-no_iso_limit", Arg.Unit
(fun _
-> Flag_parsing_cocci.iso_limit
:= None
),
508 " disable limit on max depth of iso application";
509 "-track_iso", Arg.Set
Flag.track_iso_usage
,
510 " gather information about isomorphism usage";
515 (*post_engine not included, because it doesn't use isos*)
516 PSOME
["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]),
517 " gather information about the cost of isomorphism usage"
522 "change of algorithm options",
525 "-popl", Arg.Set
FC.popl
,
526 " simplified SmPL, for the popl paper";
530 (function _
-> FC.popl
:= true; Flag_popl.mark_all
:= true),
531 " simplified SmPL, for the popl paper";
533 "-popl_keep_all_wits",
535 (function _
-> FC.popl
:= true; Flag_popl.keep_all_wits
:= true),
536 " simplified SmPL, for the popl paper";
540 Flag.make_hrule
:= Some s
; FC.include_options
:= FC.I_NO_INCLUDES
),
541 " semantic patch generation";
543 "-keep_comments", Arg.Set
Flag_parsing_c.keep_comments
,
544 " keep comments around removed code";
546 "-loop", Arg.Set
Flag_ctl.loop_in_src_code
, " ";
547 "-no_loops", Arg.Set
Flag_parsing_c.no_loops
,
548 " drop all back edges derived from looping constructs - unsafe";
549 "-no_gotos", Arg.Set
Flag_parsing_c.no_gotos
,
550 " drop all jumps derived from gotos - unsafe";
552 "-l1", Arg.Clear
Flag_parsing_c.label_strategy_2
, " ";
553 "-ifdef_to_if", Arg.Set
FC.ifdef_to_if
,
554 " convert ifdef to if (experimental)";
555 "-no_ifdef_to_if", Arg.Clear
FC.ifdef_to_if
,
556 " convert ifdef to if (experimental)";
558 "-disable_multi_pass", Arg.Set
Flag_parsing_c.disable_multi_pass
, " ";
560 "-noif0_passing", Arg.Clear
Flag_parsing_c.if0_passing
,
562 "-noadd_typedef_root", Arg.Clear
Flag_parsing_c.add_typedef_root
, " ";
563 (* could use Flag_parsing_c.options_algo instead *)
566 "-disallow_nested_exps", Arg.Set
Flag_matcher.disallow_nested_exps
,
567 " disallow an expresion pattern from matching a term and its subterm";
568 "-disable_worth_trying_opt", Arg.Clear
FC.worth_trying_opt
,
570 "-only_return_is_error_exit",
571 Arg.Set
Flag_matcher.only_return_is_error_exit
,
572 "if this flag is not set, then break and continue are also error exits";
573 (* the following is a hack to make it easier to add code in sgrep-like
574 code, essentially to compensate for the fact that we don't have
575 any way of printing things out *)
576 "-allow_inconsistent_paths",
577 Arg.Set
Flag_matcher.allow_inconsistent_paths
,
578 " if this flag is set don't check for inconsistent paths; dangerous";
579 "-no_safe_expressions",
580 Arg.Set
Flag_matcher.no_safe_expressions
,
581 " make an expression disjunction not prioritise the topmost disjunct";
582 "-int_bits", Arg.Int
Flag_parsing_c.set_int_bits
,
583 " the number of bits in an unsigned int";
584 "-long_bits", Arg.Int
Flag_parsing_c.set_long_bits
,
585 " the number of bits in an unsigned long";
586 "-linux_spacing", Arg.Unit
Flag_parsing_c.set_linux_spacing
,
587 " spacing of + code follows the conventions of Linux";
588 "-smpl_spacing", Arg.Unit
Flag_parsing_c.set_smpl_spacing
,
589 " spacing of + code follows the semantic patch";
590 "-D", Arg.String
Flag.set_defined_virtual_rules
,
591 " indicate that a virtual rule should be considered to be matched";
597 "-debugger", Arg.Set
Common.debugger
,
598 " option to set if launch spatch in ocamldebug";
599 "-disable_once", Arg.Set
Common.disable_pr2_once
,
600 " to print more messages";
601 "-show_trace_profile", Arg.Set
Common.show_trace_profile
,
603 "-save_tmp_files", Arg.Set
Common.save_tmp_files
, " ";
609 "-index", Arg.Int
(function x
-> distrib_index := Some x
) ,
610 " the processor to use for this run of spatch";
611 "-max", Arg.Int
(function x
-> distrib_max := Some x
) ,
612 " the number of processors available";
613 "-mod_distrib", Arg.Set
mod_distrib,
614 " use mod to distribute files among the processors";
620 "-use_cache", Arg.Set
Flag_parsing_c.use_cache
,
621 " use .ast_raw pre-parsed cached C file";
622 (* could use Flag_parsing_c.options_pad instead *)
627 "test mode and test options (works with tests/ or .ok files)",
628 "The test options don't work with the -sp_file and so on.",
630 "-test", Arg.Set
test_mode,
631 " <file> launch spatch on tests/file.[c,cocci]";
632 "-testall", Arg.Set
test_all,
633 " launch spatch on all files in tests/ having a .res";
634 "-test_okfailed", Arg.Set
test_okfailed,
635 " generates .{ok,failed,spatch_ok} files using .res files";
636 "-test_regression_okfailed", Arg.Set
test_regression_okfailed,
637 " process the .{ok,failed,spatch_ok} files in current dir";
639 "-compare_with_expected", Arg.Set
compare_with_expected,
640 " use also file.res";
641 "-expected_score_file", Arg.Set_string
expected_score_file,
642 " which score file to compare with in -testall";
643 "-relax_include_path", Arg.Set
FC.relax_include_path
,
648 ("The action options don't work with the -sp_file and so on." ^
"\n" ^
649 "It's for the other (internal) uses of the spatch program."
652 (* -token_c, -parse_c, etc *)
653 ((Common.options_of_actions
action (Test_parsing_c.actions
())) ++
655 (let s = "-parse_cocci" in s, Arg.Unit
(fun () -> action := s),
657 (let s = "-compare_c" in s, Arg.Unit
(fun () -> action := s),
664 short_options ++ List.concat
(List.map
Common.thd3
other_options)
666 (* I don't want the -help and --help that are appended by Arg.align *)
668 Arg.align xs
+> List.rev
+> Common.drop
2 +> List.rev
670 (* copy paste of Arg.parse. Don't want the default -help msg *)
671 let arg_parse2 l f msg
=
673 Arg.parse_argv
Sys.argv l f msg
;
675 | Arg.Bad msg
-> (* eprintf "%s" msg; exit 2; *)
676 let xs = Common.lines msg
in
677 (* take only head, it's where the error msg is *)
680 raise
(Common.UnixExit
(2))
681 | Arg.Help msg
-> (* printf "%s" msg; exit 0; *)
682 raise Impossible
(* -help is specified in speclist *)
688 Common.short_usage usage_msg short_options;
690 pr2
"Example of use:";
691 pr2
" ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
697 Common.long_usage usage_msg short_options other_options
699 let _ = short_usage_func := short_usage
700 let _ = long_usage_func := long_usage
702 (*****************************************************************************)
704 (*****************************************************************************)
706 let adjust_stdin cfile k
=
712 let (dir, base
, ext
) = Common.dbe_of_filename cfile
in
713 let varfile = Common.filename_of_dbe
(dir, base
, "var") in
714 if ext
=$
= "c" && Common.lfile_exists
varfile
717 with Invalid_argument
("Filename.chop_extension") -> None
719 Common.redirect_stdin_opt
newin k
721 let glimpse_filter (coccifile
, isofile
) dir =
722 let (_metavars
,astcocci
,_free_var_lists
,_negated_positions
,
723 _used_after_lists
,_positions_lists
,_,query
) =
724 Cocci.sp_of_file coccifile
(Some isofile
) in
726 None
-> pr2
"no glimpse keyword inferred from snippet"; None
728 (let suffixes = if !include_headers then ["c";"h"] else ["c"] in
729 Printf.fprintf stderr
"%s\n" ("glimpse request = " ^ query
);
730 let command = spf
"glimpse -y -H %s -N -W -w '%s'" dir query
in
731 let (glimpse_res
,stat
) = Common.cmd_to_list_and_status
command in
733 Unix.WEXITED
(0) | Unix.WEXITED
(1) ->
737 (fun file
-> List.mem
(Common.filesuffix file
) suffixes))
738 | _ -> None
(* error, eg due to pattern too big *))
739 | _ -> failwith
"not possible"
741 (*****************************************************************************)
743 (*****************************************************************************)
749 (* a more general solution would be to use
750 * Common.files_of_dir_or_files (x::xs)
751 * as some elements in xs may also be directories, or individual
754 if Common.is_directory x
757 adjust_stdin x
(fun () ->
758 if !cocci_file =$
= ""
759 then failwith
"I need a cocci file, use -sp_file <file>";
761 if !dir && !Flag.patch
=*= None
764 | [] -> Flag.patch
:= Some
(Cocci.normalize_path x
)
767 ("warning: patch output can only be created when only one\n"^
768 "directory is specified or when the -patch flag is used")
772 Common.profile_code
"Main.infiles computation" (fun () ->
773 match !dir, !kbuild_info, !Flag.scanner
= Flag.Glimpse
with
776 failwith
"-use_glimpse works only with -dir"
777 | true, s, true when s <> "" ->
778 failwith
"-use_glimpse does not work with -kbuild"
781 then failwith
"-use_glimpse can accept only one dir";
785 match glimpse_filter (!cocci_file, !Config.std_iso
) x
with
787 Common.cmd_to_list
(* same as "true, "", _" case *)
789 (* FIXME : Could we remove xs ?
790 -use_glimpse requires a singleton.
791 This is checked some lines before.
792 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
793 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
795 then ("find "^ x ^
" -name \"*.[ch]\"")
796 else ("find "^ x ^
" -name \"*.c\""))
797 | Some
files -> files in
798 files +> List.map
(fun x
-> [x
])
800 | false, _, _ -> [x
::xs]
804 then ("find "^
(join
" " (x
::xs))^
" -name \"*.[ch]\"")
805 else ("find "^
(join
" " (x
::xs))^
" -name \"*.c\""))
806 +> List.map
(fun x
-> [x
])
809 | true, kbuild_info_file
,_ ->
811 Common.cmd_to_list
("find "^
(join
" " (x
::xs))^
" -type d")
813 let info = Kbuild.parse_kbuild_info kbuild_info_file
in
814 let groups = Kbuild.files_in_dirs
dirs info in
816 groups +> List.map
(function Kbuild.Group
xs -> xs)
821 match (!distrib_index,!distrib_max) with
822 (None
,None
) -> infiles
823 | (Some index
,Some max
) ->
826 failwith
"index starts at 0, and so must be less than max");
829 let rec loop ct
= function
832 if (ct
mod max
) =|= index
833 then x
::(loop (ct
+1) xs)
834 else loop (ct
+1) xs in
838 let all_files = List.length
infiles in
839 let regions = (all_files + (max
- 1)) / max
in
840 let this_min = index
* regions in
841 let this_max = (index
+1) * regions in
842 let rec loop ct
= function
845 if this_min <= ct
&& ct
< this_max
846 then x
::(loop (ct
+1) xs)
847 else loop (ct
+1) xs in
850 | _ -> failwith
"inconsistent distribution information" in
853 Common.profile_code
"Main.outfiles computation" (fun () ->
855 Cocci.pre_engine
(!cocci_file, !Config.std_iso
) in
857 infiles +> List.map
(fun cfiles
->
858 pr2
("HANDLING: " ^
(join
" " cfiles
));
859 Common.timeout_function_opt
!FC.timeout
(fun () ->
860 Common.report_if_take_time
10 (join
" " cfiles
) (fun () ->
861 (*let s = profile_diagnostic() in*)
865 if !output_file <> "" && !compat_mode then
870 Common.redirect_stdout_opt
optfile (fun () ->
871 (* this is the main call *)
872 Cocci.full_engine
cocci_infos cfiles
875 | Common.UnixExit x
-> raise
(Common.UnixExit x
)
880 pr2(profile_diagnostic());*)
883 pr2
("EXN:" ^
Printexc.to_string e
);
887 Cocci.post_engine
cocci_infos;
892 Common.profile_code
"Main.result analysis" (fun () ->
893 Ctlcocci_integration.print_bench
();
894 let outfiles = Cocci.check_duplicate_modif
outfiles in
895 outfiles +> List.iter
(fun (infile
, outopt
) ->
896 outopt
+> Common.do_option
(fun outfile
->
899 (match !backup_suffix with
900 Some
backup_suffix ->
901 Common.command2
("cp "^infile^
" "^infile^
backup_suffix)
903 Common.command2
("cp "^outfile^
" "^infile
);
907 then Common.command2
("cp "^outfile^
" "^infile^
".cocci_res");
909 (* potential source of security pb if the /tmp/ file is
910 * a symlink, so simpler to not produce any regular file
911 * (files created by Common.new_temp_file are still ok)
915 if !output_file =$= ""
917 let tmpfile = "/tmp/"^Common.basename infile in
918 pr2 (spf "One file modified. Result is here: %s" tmpfile);
919 Common.command2 ("cp "^outfile^" "^tmpfile);
923 if !output_file <> "" && not
!compat_mode then
925 | [infile
, Some outfile
] when infile
=$
= x
&& null
xs ->
926 Common.command2
("cp " ^outfile^
" " ^
!output_file);
927 | [infile
, None
] when infile
=$
= x
&& null
xs ->
928 Common.command2
("cp " ^infile^
" " ^
!output_file);
931 ("-o can not be applied because there is multiple " ^
934 if !compare_with_expected
935 then Testing.compare_with_expected outfiles))
937 | [] -> raise Impossible
940 (*****************************************************************************)
941 (* The coccinelle main entry point *)
942 (*****************************************************************************)
945 let arglist = Array.to_list
Sys.argv
in
947 if not
(null
(Common.inter_set
arglist
948 ["-cocci_file";"-sp_file";"-sp";"-test";"-testall";
949 "-test_okfailed";"-test_regression_okfailed"]))
950 then run_profile quiet_profile;
954 (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*)
956 (* this call can set up many global flag variables via the cmd line *)
957 arg_parse2 (Arg.align
all_options) (fun x
-> args := x
::!args) usage_msg;
959 (* julia hack so that one can override directories specified on
960 * the command line. *)
964 if List.length
!args > 1
967 let chosen = List.hd
!args in
968 pr2
("ignoring all but the last specified directory: "^
chosen);
972 else List.hd
!args in
973 if !FC.include_path
=*= []
974 then FC.include_path
:= [Filename.concat
chosen_dir "include"]);
976 args := List.rev
!args;
978 if !cocci_file <> "" && (not
(!cocci_file =~
".*\\.\\(sgrep\\|spatch\\)$"))
979 then cocci_file := Common.adjust_ext_if_needed
!cocci_file ".cocci";
981 if !Config.std_iso
<> ""
982 then Config.std_iso
:= Common.adjust_ext_if_needed
!Config.std_iso
".iso";
983 if !Config.std_h
<> ""
984 then Config.std_h
:= Common.adjust_ext_if_needed
!Config.std_h
".h";
986 if !Config.std_h
<> ""
987 then Parse_c.init_defs_builtins
!Config.std_h
;
990 then Parse_c.init_defs_macros
!macro_file;
993 (* must be done after Arg.parse, because Common.profile is set by it *)
994 Common.profile_code
"Main total" (fun () ->
997 let all_actions = Test_parsing_c.actions
() in
1001 (* --------------------------------------------------------- *)
1002 (* The test framework. Works with tests/ or .ok and .failed *)
1003 (* --------------------------------------------------------- *)
1004 | [x
] when !test_mode ->
1006 let prefix = "tests/" in
1007 let testfile = x ^
".cocci" in
1008 if Sys.file_exists
(prefix ^
testfile) then
1010 FC.include_path
:= [prefix^
"include"];
1011 Testing.testone
prefix x
!compare_with_expected
1014 if Sys.file_exists
testfile then
1016 FC.include_path
:= ["include"];
1017 Testing.testone
"" x
!compare_with_expected
1020 pr2
(spf
"ERROR: File %s does not exist" testfile)
1023 | [] when !test_all ->
1024 FC.include_path
:= ["tests/include"];
1025 if !expected_score_file <> ""
1026 then Testing.testall ~
expected_score_file:!expected_score_file ()
1027 else Testing.testall
()
1029 | [] when !test_regression_okfailed ->
1030 Testing.test_regression_okfailed ()
1032 | x
::xs when !test_okfailed ->
1033 (* do its own timeout on FC.timeout internally *)
1034 FC.relax_include_path
:= true;
1035 adjust_stdin x
(fun () ->
1036 Testing.test_okfailed !cocci_file (x
::xs)
1039 (* --------------------------------------------------------- *)
1040 (* Actions, useful to debug subpart of coccinelle *)
1041 (* --------------------------------------------------------- *)
1043 | xs when List.mem
!action (Common.action_list
all_actions) ->
1044 Common.do_action
!action xs all_actions
1046 | [file
] when !action =$
= "-parse_cocci" ->
1047 Testing.test_parse_cocci file
1049 (* I think this is used by some scripts in some Makefile for our
1050 * big-tests. So dont remove.
1052 | [file1
;file2
] when !action =$
= "-compare_c" ->
1053 Test_parsing_c.test_compare_c file1 file2
(* result = unix code *)
1055 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
1058 (* --------------------------------------------------------- *)
1059 (* This is the main entry *)
1060 (* --------------------------------------------------------- *)
1061 | x
::xs -> main_action (x
::xs)
1063 (* --------------------------------------------------------- *)
1065 (* --------------------------------------------------------- *)
1066 | [] -> short_usage()
1068 if !Pycocci.initialised
&& (Pycocci.py_isinitialized
()) != 0 then begin
1069 ignore
(Pycocci.pyrun_simplestring
"cocci.finalise()");
1071 then Common.pr2
"Finalizing python\n";
1072 Pycocci.py_finalize
();
1077 let main_with_better_error_report () =
1078 if !Common.debugger
then main ()
1083 | Unix.Unix_error
(e
, "stat", filename
) ->
1085 (spf
"ERROR: File %s does not exist: %s"
1086 filename
(Unix.error_message e
));
1087 raise
(UnixExit
(-1))
1089 (*****************************************************************************)
1091 Common.main_boilerplate
(fun () ->
1092 main_with_better_error_report ();
1093 Ctlcocci_integration.print_bench
();