Release coccinelle-0.1
[bpt/coccinelle.git] / main.ml
1 (*
2 * Copyright 2005-2008, 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.
5 *
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.
9 *
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.
14 *
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/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
23 open Common
24
25 (*****************************************************************************)
26 (* Flags *)
27 (*****************************************************************************)
28
29 (* In addition to flags that can be tweaked via -xxx options (cf the
30 * full list of options in "the spatch options" section below), the
31 * spatch program also depends on external files, described in
32 * globals/config.ml, mainly a standard.h and standard.iso file *)
33
34 let cocci_file = ref ""
35
36 let output_file = ref ""
37 let inplace_modif = ref false (* but keeps a .cocci_orig *)
38 let outplace_modif = ref false (* generates a .cocci_res *)
39
40 (* could be avoided by using Common.files_of_dir_or_files instead *)
41 let dir = ref false
42
43 let include_headers = ref false
44 let kbuild_info = ref ""
45
46 (* test mode *)
47 let test_mode = ref false
48 let test_all = ref false
49 let test_okfailed = ref false
50 let test_regression_okfailed = ref false
51
52
53 (* action mode *)
54 let action = ref ""
55
56 (* works with -test but also in "normal" spatch mode *)
57 let compare_with_expected = ref false
58
59
60 let distrib_index = ref (None : int option)
61 let distrib_max = ref (None : int option)
62 let mod_distrib = ref false
63
64
65 (*****************************************************************************)
66 (* Profiles *)
67 (*****************************************************************************)
68
69 (* pair of (list of flags to set true, list of flags to set false *)
70 let quiet_profile = (
71 [
72 ],
73 [
74 (* Flag_cocci.show_diff; just leave this as it is *)
75
76 Flag.show_misc;
77 Flag.show_trying;
78
79 Flag_cocci.show_c;
80 Flag_cocci.show_cocci;
81 Flag_cocci.show_flow;
82 Flag_cocci.show_before_fixed_flow;
83 Flag_cocci.show_ctl_tex;
84 Flag_cocci.show_ctl_text;
85 Flag_cocci.show_transinfo;
86 Flag_cocci.show_binding_in_out;
87
88 Flag_parsing_cocci.show_SP;
89 Flag_parsing_cocci.show_iso_failures;
90 Flag_ctl.verbose_ctl_engine;
91 Flag_ctl.verbose_match;
92 Flag_engine.debug_engine;
93 Flag_parsing_c.debug_unparsing;
94 Flag_parsing_c.verbose_type;
95 Flag_parsing_c.verbose_parsing;
96 ])
97
98 let pad_profile = (
99 [
100 Flag_cocci.show_diff;
101 ],
102 [
103
104 Flag.show_misc;
105
106 Flag_cocci.show_c;
107 Flag_cocci.show_cocci;
108 Flag_cocci.show_flow;
109 Flag_cocci.show_before_fixed_flow;
110 Flag_cocci.show_ctl_tex;
111 Flag_cocci.show_ctl_text;
112 Flag_cocci.show_transinfo;
113 Flag_cocci.show_binding_in_out;
114
115 Flag_parsing_cocci.show_SP;
116 Flag_parsing_cocci.show_iso_failures;
117 Flag_ctl.verbose_ctl_engine;
118 Flag_ctl.verbose_match;
119 Flag_engine.debug_engine;
120 Flag_parsing_c.debug_unparsing;
121 Flag_parsing_c.verbose_type;
122 Flag_parsing_c.verbose_parsing;
123 ])
124
125 (*****************************************************************************)
126 (* The spatch options *)
127 (*****************************************************************************)
128
129 let usage_msg =
130 "Usage: " ^ basename Sys.argv.(0) ^
131 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
132 "\n" ^ "Options are:"
133
134 (* forward reference trick *)
135 let short_usage_func = ref (fun () -> ())
136 let long_usage_func = ref (fun () -> ())
137
138
139 (* The short_options are user-oriented. The other options are for
140 * the developers of coccinelle or advanced-users that know
141 * quite well the underlying semantics of coccinelle.
142 *)
143
144
145 (* will be printed when use only ./spatch. For the rest you have to
146 * use -longhelp to see them.
147 *)
148 let short_options = [
149 "-sp_file", Arg.Set_string cocci_file,
150 " <file> the semantic patch file";
151
152 "-o", Arg.Set_string output_file,
153 " <file> the output file";
154 "-inplace", Arg.Set inplace_modif,
155 " do the modification on the file directly";
156 "-outplace", Arg.Set outplace_modif,
157 " store modifications in a .cocci_res file";
158
159 "-U", Arg.Int (fun n -> Flag_parsing_c.diff_lines := Some (i_to_s n)),
160 " set number of diff context lines";
161 "-partial_match", Arg.Set Flag_ctl.partial_match,
162 " report partial matches of the SP on the C file";
163
164 "-iso_file", Arg.Set_string Config.std_iso,
165 " <file> (default=" ^ !Config.std_iso ^")";
166 "-macro_file", Arg.Set_string Config.std_h,
167 " <file> (default=" ^ !Config.std_h ^ ")";
168
169 "-all_includes", Arg.Set Flag_cocci.all_includes,
170 " causes all available include files to be used";
171 "-no_includes", Arg.Set Flag_cocci.no_includes,
172 " causes not even local include files to be used";
173 "-I", Arg.Set_string Flag_cocci.include_path,
174 " <dir> containing the Linux headers (optional)";
175
176
177 "-dir", Arg.Set dir,
178 " <dir> process all files in directory recursively";
179
180 "-include_headers", Arg.Set include_headers,
181 " process header files independently";
182 "-use_glimpse", Arg.Set Flag.use_glimpse,
183 " works with -dir, use info generated by glimpseindex";
184 "-patch", Arg.String (function s -> Flag.patch := Some s),
185 (" <dir> path name with respect to which a patch should be created\n"^
186 " \"\" for a file in the current directory");
187 "-kbuild_info", Arg.Set_string kbuild_info,
188 " <file> improve -dir by grouping related c files";
189 "-pyoutput", Arg.Set_string Flag.pyoutput,
190 " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>";
191
192
193 "-version", Arg.Unit (fun () ->
194 pr2 (spf "spatch version: %s" Config.version);
195 exit 0;
196 ),
197 " guess what";
198
199 "-date", Arg.Unit (fun () ->
200 pr2 "version: $Date: 2008/09/26 00:44:57 $";
201 raise (Common.UnixExit 0)
202 ),
203 " guess what";
204
205 "-shorthelp", Arg.Unit (fun () ->
206 !short_usage_func();
207 raise (Common.UnixExit 0)
208 ),
209 " see short list of options";
210 "-longhelp", Arg.Unit (fun () ->
211 !long_usage_func();
212 raise (Common.UnixExit 0)
213 ),
214 " see all the available options in different categories";
215 "-help", Arg.Unit (fun () ->
216 !long_usage_func();
217 raise (Common.UnixExit 0)
218 ),
219 " ";
220 "--help", Arg.Unit (fun () ->
221 !long_usage_func();
222 raise (Common.UnixExit 0)
223 ),
224 " ";
225
226 ]
227
228 (* the format is a list of triples:
229 * (title of section * (optional) explanation of sections * option list)
230 *)
231 let other_options = [
232 "aliases and obsolete options",
233 "",
234 [
235 "-cocci_file", Arg.Set_string cocci_file,
236 " <file> the semantic patch file";
237 "-c", Arg.Set_string cocci_file, " short option of -cocci_file";
238 "-iso", Arg.Set_string Config.std_iso, " short option of -iso_file";
239 "-D", Arg.Set_string Config.std_h, " short option of -macro_file";
240 ];
241
242 "most useful show options",
243 "",
244 [
245 "-show_diff" , Arg.Set Flag_cocci.show_diff, " ";
246 "-no_show_diff" , Arg.Clear Flag_cocci.show_diff, " ";
247 "-show_flow" , Arg.Set Flag_cocci.show_flow, " ";
248 "-no_show_ctl_text" , Arg.Clear Flag_cocci.show_ctl_text, " ";
249 (* works in conjunction with -show_ctl *)
250 "-ctl_inline_let", Arg.Set Flag_cocci.inline_let_ctl, " ";
251 "-ctl_show_mcodekind", Arg.Set Flag_cocci.show_mcodekind_in_ctl, " ";
252 "-show_binding_in_out", Arg.Set Flag_cocci.show_binding_in_out, " ";
253 "-no_show_transinfo", Arg.Clear Flag_cocci.show_transinfo, " ";
254 "-no_show_misc", Arg.Clear Flag.show_misc, " ";
255 "-show_trying", Arg.Set Flag.show_trying,
256 " show the name of each function being processed";
257 ];
258
259 "verbose subsystems options",
260 "",
261 [
262 "-verbose_ctl_engine", Arg.Set Flag_ctl.verbose_ctl_engine, " ";
263 "-verbose_match", Arg.Set Flag_ctl.verbose_match, " ";
264 "-verbose_engine", Arg.Set Flag_engine.debug_engine, " ";
265
266 "-no_parse_error_msg", Arg.Clear Flag_parsing_c.verbose_parsing, " ";
267 "-no_type_error_msg", Arg.Clear Flag_parsing_c.verbose_type, " ";
268 (* could also use Flag_parsing_c.options_verbose *)
269 ];
270
271 "other show options",
272 "",
273 [
274 "-show_c" , Arg.Set Flag_cocci.show_c, " ";
275 "-show_cocci" , Arg.Set Flag_cocci.show_cocci, " ";
276 "-show_before_fixed_flow" , Arg.Set Flag_cocci.show_before_fixed_flow, " ";
277 "-show_ctl_tex" , Arg.Set Flag_cocci.show_ctl_tex, " ";
278 "-show_SP_julia" , Arg.Set Flag_parsing_cocci.show_SP, " ";
279 ];
280
281
282 "debug C parsing/unparsing",
283 "",
284 [
285 "-debug_cpp", Arg.Set Flag_parsing_c.debug_cpp, " ";
286 "-debug_lexer", Arg.Set Flag_parsing_c.debug_lexer , " ";
287 "-debug_etdt", Arg.Set Flag_parsing_c.debug_etdt , " ";
288 "-debug_typedef", Arg.Set Flag_parsing_c.debug_typedef, " ";
289
290 "-filter_msg", Arg.Set Flag_parsing_c.filter_msg ,
291 " filter some cpp message when the macro is a \"known\" cpp construct";
292 "-filter_define_error",Arg.Set Flag_parsing_c.filter_define_error," ";
293 "-filter_classic_passed",Arg.Set Flag_parsing_c.filter_classic_passed," ";
294
295 "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " ";
296 "-debug_unparsing", Arg.Set Flag_parsing_c.debug_unparsing, " ";
297
298 ];
299 (* could use Flag_parsing_c.options_debug_with_title instead *)
300
301
302 "shortcut for enabling/disabling a set of debugging options at once",
303 "",
304 [
305 (* todo: other profile ? *)
306 "-quiet", Arg.Unit (fun () ->
307 let (set_to_true, set_to_false) = quiet_profile in
308 List.iter (fun x -> x := false) set_to_false;
309 List.iter (fun x -> x := true) set_to_true;
310 ), " ";
311
312 "-pad", Arg.Unit (fun () ->
313 let (set_to_true, set_to_false) = pad_profile in
314 List.iter (fun x -> x := false) set_to_false;
315 List.iter (fun x -> x := true) set_to_true;
316 ), " ";
317
318 ];
319
320 "bench options",
321 "",
322 [
323 "-profile", Arg.Unit (function () -> Common.profile := Common.PALL) ,
324 " gather timing information about the main coccinelle functions";
325 "-bench", Arg.Int (function x -> Flag_ctl.bench := x),
326 " <level> for profiling the CTL engine";
327 "-timeout", Arg.Int (fun x -> Flag_cocci.timeout := Some x),
328 " <sec> timeout in seconds";
329 "-steps", Arg.Int (fun x -> Flag_ctl.steps := Some x),
330 " max number of model checking steps per code unit";
331 "-track_iso", Arg.Set Flag.track_iso_usage,
332 " gather information about isomorphism usage";
333 "-profile_iso",
334 Arg.Unit
335 (function () ->
336 Common.profile:=PSOME ["parse cocci";"mysat";"asttoctl2";"full_engine"]),
337 " gather information about the cost of isomorphism usage"
338 ];
339
340
341
342 "change of algorithm options",
343 "",
344 [
345 "-popl", Arg.Set Flag_cocci.popl,
346 " simplified SmPL, for the popl paper";
347
348 "-popl_mark_all",
349 Arg.Unit
350 (function _ -> Flag_cocci.popl := true; Flag_popl.mark_all := true),
351 " simplified SmPL, for the popl paper";
352
353 "-popl_keep_all_wits",
354 Arg.Unit
355 (function _ -> Flag_cocci.popl := true; Flag_popl.keep_all_wits := true),
356 " simplified SmPL, for the popl paper";
357
358 "-hrule", Arg.String (function s -> Flag.make_hrule := Some s),
359 " semantic patch generation";
360
361 "-loop", Arg.Set Flag_ctl.loop_in_src_code, " ";
362
363 "-l1", Arg.Clear Flag_parsing_c.label_strategy_2, " ";
364 "-ifdef", Arg.Set Flag_parsing_c.ifdef_to_if,
365 " convert ifdef to if (buggy!)";
366 "-noif0_passing", Arg.Clear Flag_parsing_c.if0_passing,
367 " ";
368 "-noadd_typedef_root", Arg.Clear Flag_parsing_c.add_typedef_root, " ";
369 (* could use Flag_parsing_c.options_algo instead *)
370
371
372 "-disallow_nested_exps", Arg.Set Flag_engine.disallow_nested_exps,
373 "disallow an expresion pattern from matching a term and its subterm";
374 "-disable_worth_trying_opt", Arg.Clear Flag_cocci.worth_trying_opt,
375 " ";
376 "-only_return_is_error_exit",
377 Arg.Set Flag_engine.only_return_is_error_exit,
378 "if this flag is not set, then break and continue are also error exits";
379 (* the following is a hack to make it easier to add code in sgrep-like
380 code, essentially to compensate for the fact that we don't have
381 any way of printing things out *)
382 "-allow_inconsistent_paths",
383 Arg.Set Flag_engine.allow_inconsistent_paths,
384 "if this flag is set don't check for inconsistent paths; dangerous";
385 ];
386
387 "misc options",
388 "",
389 [
390 "-debugger", Arg.Set Common.debugger ,
391 " option to set if launch spatch in ocamldebug";
392 "-disable_once", Arg.Set Common.disable_pr2_once,
393 " to print more messages";
394 "-save_tmp_files", Arg.Set Common.save_tmp_files, " ";
395 ];
396
397 "concurrency",
398 "",
399 [
400 "-index", Arg.Int (function x -> distrib_index := Some x) ,
401 " the processor to use for this run of spatch";
402 "-max", Arg.Int (function x -> distrib_max := Some x) ,
403 " the number of processors available";
404 "-mod_distrib", Arg.Set mod_distrib,
405 " use mod to distribute files among the processors";
406 ];
407
408 "pad options",
409 "",
410 [
411 "-use_cache", Arg.Set Flag_parsing_c.use_cache,
412 " use .ast_raw pre-parsed cached C file";
413 (* could use Flag_parsing_c.options_pad instead *)
414 ];
415
416
417
418 "test mode and test options (works with tests/ or .ok files)",
419 "The test options don't work with the -sp_file and so on.",
420 [
421 "-test", Arg.Set test_mode,
422 " <file> launch spatch on tests/file.[c,cocci]";
423 "-testall", Arg.Set test_all,
424 " launch spatch on all files in tests/ having a .res";
425 "-test_okfailed", Arg.Set test_okfailed,
426 " generates .{ok,failed,spatch_ok} files using .res files";
427 "-test_regression_okfailed", Arg.Set test_regression_okfailed,
428 " process the .{ok,failed,spatch_ok} files in current dir";
429
430 "-compare_with_expected", Arg.Set compare_with_expected,
431 " use also file.res";
432 "-relax_include_path", Arg.Set Flag_cocci.relax_include_path,
433 " ";
434
435 ];
436
437 "action mode",
438 ("The action options don't work with the -sp_file and so on." ^ "\n" ^
439 "It's for the other (internal) uses of the spatch program."
440 ),
441
442 (* -token_c, -parse_c, etc *)
443 ((Common.options_of_actions action (Test_parsing_c.actions())) ++
444 [
445 (let s = "-parse_cocci" in s, Arg.Unit (fun () -> action := s),
446 " <file>");
447 (let s = "-compare_c" in s, Arg.Unit (fun () -> action := s),
448 " <file1> <file2>");
449 ]);
450 ]
451
452
453 let all_options =
454 short_options ++ List.concat (List.map Common.thd3 other_options)
455
456
457
458 (* I don't want the -help and --help that are appended by Arg.align *)
459 let arg_align2 xs =
460 Arg.align xs +> List.rev +> Common.drop 2 +> List.rev
461
462 (* copy paste of Arg.parse. Don't want the default -help msg *)
463 let arg_parse2 l f msg =
464 (try
465 Arg.parse_argv Sys.argv l f msg;
466 with
467 | Arg.Bad msg -> (* eprintf "%s" msg; exit 2; *)
468 let xs = Common.lines msg in
469 (* take only head, it's where the error msg is *)
470 pr2 (List.hd xs);
471 !short_usage_func();
472 raise (Common.UnixExit (2))
473 | Arg.Help msg -> (* printf "%s" msg; exit 0; *)
474 raise Impossible (* -help is specified in speclist *)
475 )
476
477
478 let short_usage () =
479 begin
480 Common.short_usage usage_msg short_options;
481 pr2 "";
482 pr2 "Example of use:";
483 pr2 " ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
484 pr2 "";
485 end
486
487
488 let long_usage () =
489 Common.long_usage usage_msg short_options other_options
490
491 let _ = short_usage_func := short_usage
492 let _ = long_usage_func := long_usage
493
494 (*****************************************************************************)
495 (* Helpers *)
496 (*****************************************************************************)
497
498 let adjust_stdin cfile k =
499 if !dir
500 then k()
501 else
502 let newin =
503 let (dir, base, ext) = Common.dbe_of_filename cfile in
504 let varfile = Common.filename_of_dbe (dir, base, "var") in
505 if ext = "c" && Common.lfile_exists varfile
506 then Some varfile
507 else None in
508 Common.redirect_stdin_opt newin k
509
510 let glimpse_filter (coccifile, isofile) dir =
511 let (astcocci,_free_var_lists,_negated_positions,
512 _used_after_lists,_positions_lists,_,query) =
513 Cocci.sp_of_file coccifile (Some isofile) in
514 match query with
515 None -> pr2 "no glimpse keyword infered from snippet"; None
516 | Some query ->
517 let suffixes = if !include_headers then ["c";"h"] else ["c"] in
518 pr2 ("glimpse request = " ^ query);
519 let command = spf "glimpse -y -H %s -N -W -w '%s'" dir query in
520 let (glimpse_res,stat) = Common.cmd_to_list_and_status command in
521 match stat with
522 Unix.WEXITED(0) | Unix.WEXITED(1) ->
523 Some
524 (glimpse_res +>
525 List.filter
526 (fun file -> List.mem (Common.filesuffix file) suffixes))
527 | _ -> None (* error, eg due to pattern too big *)
528
529
530
531
532 (*****************************************************************************)
533 (* The coccinelle main entry point *)
534 (*****************************************************************************)
535 let main () =
536 begin
537 let args = ref [] in
538
539 arg_parse2 (Arg.align all_options) (fun x -> args := x::!args) usage_msg;
540
541 (if !dir && List.length !args > 1
542 then
543 begin
544 let chosen = List.hd !args in
545 pr2 ("ignoring all but the last specified directory: "^chosen);
546 args := [chosen]
547 end);
548 args := List.rev !args;
549
550 (if !Flag_cocci.all_includes && !Flag_cocci.no_includes
551 then failwith "cannot set both all_includes and no_includes");
552
553 if !cocci_file <> "" && (not (!cocci_file =~ ".*\\.\\(sgrep\\|spatch\\)$"))
554 then cocci_file := Common.adjust_ext_if_needed !cocci_file ".cocci";
555
556 if !Config.std_iso <> ""
557 then Config.std_iso := Common.adjust_ext_if_needed !Config.std_iso ".iso";
558 if !Config.std_h <> ""
559 then Config.std_h := Common.adjust_ext_if_needed !Config.std_h ".h";
560
561 if !Config.std_h <> ""
562 then Parse_c.init_defs !Config.std_h;
563
564
565 (* must be done after Arg.parse, because Common.profile is set by it *)
566 Common.profile_code "Main total" (fun () ->
567
568
569 let all_actions = Test_parsing_c.actions() in
570
571 (match (!args) with
572
573 (* --------------------------------------------------------- *)
574 (* The test framework. Works with tests/ or .ok and .failed *)
575 (* --------------------------------------------------------- *)
576 | [x] when !test_mode ->
577 Flag_cocci.include_path := "tests/include";
578 Testing.testone x !compare_with_expected
579
580 | [] when !test_all ->
581 Flag_cocci.include_path := "tests/include";
582 Testing.testall ()
583
584 | [] when !test_regression_okfailed ->
585 Testing.test_regression_okfailed ()
586
587 | x::xs when !test_okfailed ->
588 (* do its own timeout on Flag_cocci.timeout internally *)
589 Flag_cocci.relax_include_path := true;
590 adjust_stdin x (fun () ->
591 Testing.test_okfailed !cocci_file (x::xs)
592 )
593
594 (* --------------------------------------------------------- *)
595 (* Actions, useful to debug subpart of coccinelle *)
596 (* --------------------------------------------------------- *)
597
598 | xs when List.mem !action (Common.action_list all_actions) ->
599 Common.do_action !action xs all_actions
600
601 | [file] when !action = "-parse_cocci" ->
602 Testing.test_parse_cocci file
603
604 (* I think this is used by some scripts in some Makefile for our
605 * big-tests. So dont remove.
606 *)
607 | [file1;file2] when !action = "-compare_c" ->
608 Test_parsing_c.test_compare_c file1 file2 (* result = unix code *)
609
610 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
611
612
613 (* --------------------------------------------------------- *)
614 (* This is the main entry *)
615 (* --------------------------------------------------------- *)
616 | x::xs ->
617 adjust_stdin x (fun () ->
618 if !cocci_file = ""
619 then failwith "I need a cocci file, use -sp_file <file>";
620
621 if !dir && !Flag.patch = None
622 then
623 (match xs with
624 | [] -> Flag.patch := Some x
625 | _ ->
626 pr2
627 ("warning: patch output can only be created when only one\n"^
628 "directory is specified or when the -patch flag is used")
629 );
630
631 let infiles =
632 Common.profile_code "Main.infiles computation" (fun () ->
633 match !dir, !kbuild_info, !Flag.use_glimpse with
634 (* glimpse *)
635 | false, _, true ->
636 failwith "-use_glimpse works only with -dir"
637 | true, s, true when s <> "" ->
638 failwith "-use_glimpse does not work with -kbuild"
639 | true, "", true ->
640 if not (null xs)
641 then failwith "-use_glimpse can accept only one dir";
642
643 let files =
644 match glimpse_filter (!cocci_file, !Config.std_iso) x with
645 None ->
646 Common.cmd_to_list (* same as "true, "", _" case *)
647 (if !include_headers
648 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
649 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
650 | Some files -> files in
651 files +> List.map (fun x -> [x])
652 (* normal *)
653 | false, _, _ -> [x::xs]
654 | true, "", _ ->
655 Common.cmd_to_list
656 (if !include_headers
657 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
658 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
659 +> List.map (fun x -> [x])
660
661 (* kbuild *)
662 | true, kbuild_info_file,_ ->
663 let dirs =
664 Common.cmd_to_list ("find "^(join " " (x::xs))^" -type d")
665 in
666 let info = Kbuild.parse_kbuild_info kbuild_info_file in
667 let groups = Kbuild.files_in_dirs dirs info in
668
669 groups +> List.map (function Kbuild.Group xs -> xs)
670 )
671 in
672
673 let infiles =
674 match (!distrib_index,!distrib_max) with
675 (None,None) -> infiles
676 | (Some index,Some max) ->
677 (if index >= max
678 then
679 failwith "index starts at 0, and so must be less than max");
680 if !mod_distrib
681 then
682 let rec loop ct = function
683 [] -> []
684 | x::xs ->
685 if (ct mod max) = index
686 then x::(loop (ct+1) xs)
687 else loop (ct+1) xs in
688 loop 0 infiles
689 else
690 begin
691 let all_files = List.length infiles in
692 let regions = (all_files + (max - 1)) / max in
693 let this_min = index * regions in
694 let this_max = (index+1) * regions in
695 let rec loop ct = function
696 [] -> []
697 | x::xs ->
698 if this_min <= ct && ct < this_max
699 then x::(loop (ct+1) xs)
700 else loop (ct+1) xs in
701 loop 0 infiles
702 end
703 | _ -> failwith "inconsistent distribution information" in
704
705 let outfiles =
706 Common.profile_code "Main.outfiles computation" (fun () ->
707 infiles +> List.map (fun cfiles ->
708 pr2 ("HANDLING: " ^ (join " " cfiles));
709 Common.timeout_function_opt !Flag_cocci.timeout (fun () ->
710 Common.report_if_take_time 10 (join " " cfiles) (fun () ->
711 (* Unix.sleep 1; *)
712 try
713 (* this is the main call *)
714 Cocci.full_engine (!cocci_file, !Config.std_iso) cfiles
715 with
716 | Common.UnixExit x -> raise (Common.UnixExit x)
717 | e ->
718 if !dir
719 then begin
720 pr2 ("EXN:" ^ Printexc.to_string e);
721 [] (* *)
722 end
723 else raise e)))
724 ) +> List.concat
725 in
726
727 Common.profile_code "Main.result analysis" (fun () ->
728
729 Ctlcocci_integration.print_bench();
730
731 let outfiles = Cocci.check_duplicate_modif outfiles in
732
733 outfiles +> List.iter (fun (infile, outopt) ->
734 outopt +> Common.do_option (fun outfile ->
735 if !inplace_modif
736 then begin
737 Common.command2 ("cp "^infile^" "^infile^".cocci_orig");
738 Common.command2 ("cp "^outfile^" "^infile);
739 end;
740
741 if !outplace_modif
742 then Common.command2 ("cp "^outfile^" "^infile^".cocci_res");
743
744 if !output_file = ""
745 then begin
746 let tmpfile = "/tmp/"^Common.basename infile in
747 pr2 (spf "One file modified. Result is here: %s" tmpfile);
748 Common.command2 ("cp "^outfile^" "^tmpfile);
749 end
750 ));
751 if !output_file <> "" then
752 (match outfiles with
753 | [infile, Some outfile] when infile = x && null xs ->
754 Common.command2 ("cp " ^outfile^ " " ^ !output_file);
755 | [infile, None] when infile = x && null xs ->
756 Common.command2 ("cp " ^infile^ " " ^ !output_file);
757 | _ ->
758 failwith
759 ("-o can not be applied because there is multiple " ^
760 "modified files");
761 );
762
763 if !compare_with_expected
764 then Testing.compare_with_expected outfiles))
765
766 (* --------------------------------------------------------- *)
767 (* empty entry *)
768 (* --------------------------------------------------------- *)
769 | [] -> short_usage()
770
771 ));
772 if !Pycocci.initialised && (Pycocci.py_isinitialized ()) != 0 then begin
773 ignore(Pycocci.pyrun_simplestring "cocci.finalise()");
774 if !Flag.show_misc
775 then Common.pr2 "Finalizing python\n";
776 Pycocci.py_finalize ();
777 end
778 end
779
780 (*****************************************************************************)
781 let _ =
782 Common.main_boilerplate (fun () ->
783 main ();
784 Ctlcocci_integration.print_bench();
785 )