Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / main.ml
CommitLineData
34e49164 1open Common
faf9a90c 2module FC = Flag_cocci
34e49164
C
3
4(*****************************************************************************)
5(* Flags *)
6(*****************************************************************************)
7
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 *)
12
13let cocci_file = ref ""
14
15let output_file = ref ""
16let inplace_modif = ref false (* but keeps a .cocci_orig *)
17let outplace_modif = ref false (* generates a .cocci_res *)
978fd7e5
C
18let preprocess = ref false (* run the C preprocessor before cocci *)
19let compat_mode = ref false
34e49164 20
708f4980 21(* somehow obsolete now *)
978fd7e5 22let dir = ref false
34e49164
C
23
24let include_headers = ref false
25let kbuild_info = ref ""
26
978fd7e5 27let macro_file = ref ""
708f4980 28
34e49164
C
29(* test mode *)
30let test_mode = ref false
31let test_all = ref false
32let test_okfailed = ref false
33let test_regression_okfailed = ref false
708f4980 34let expected_score_file = ref ""
34e49164
C
35
36
37(* action mode *)
38let action = ref ""
39
40(* works with -test but also in "normal" spatch mode *)
41let compare_with_expected = ref false
42
34e49164
C
43let distrib_index = ref (None : int option)
44let distrib_max = ref (None : int option)
45let mod_distrib = ref false
46
47
48(*****************************************************************************)
49(* Profiles *)
50(*****************************************************************************)
51
52(* pair of (list of flags to set true, list of flags to set false *)
53let quiet_profile = (
54 [
55 ],
56 [
faf9a90c 57 (* FC.show_diff; just leave this as it is *)
34e49164
C
58
59 Flag.show_misc;
60 Flag.show_trying;
faf9a90c 61 Flag.show_transinfo;
34e49164 62
faf9a90c
C
63 FC.show_c;
64 FC.show_cocci;
65 FC.show_flow;
66 FC.show_before_fixed_flow;
67 FC.show_ctl_tex;
68 FC.show_ctl_text;
69 FC.show_binding_in_out;
34e49164 70
708f4980
C
71 FC.verbose_cocci;
72
73 Flag_parsing_c.show_parsing_error;
74
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;
978fd7e5 81 Flag_parsing_c.verbose_cpp_ast;
708f4980
C
82
83 Flag_matcher.verbose_matcher;
84 Flag_matcher.debug_engine;
85
86 Flag_parsing_c.debug_unparsing;
87
34e49164
C
88 Flag_parsing_cocci.show_SP;
89 Flag_parsing_cocci.show_iso_failures;
708f4980 90
34e49164
C
91 Flag_ctl.verbose_ctl_engine;
92 Flag_ctl.verbose_match;
708f4980
C
93
94
34e49164
C
95 ])
96
faf9a90c
C
97(* some information that is useful in seeing why a semantic patch doesn't
98work properly *)
99let debug_profile = (
100 [
101 Flag.show_misc;
708f4980
C
102 Flag.show_transinfo;
103
faf9a90c
C
104 FC.show_diff;
105 FC.show_cocci;
106 FC.show_binding_in_out;
107 FC.show_dependencies;
708f4980 108
faf9a90c 109 Flag_parsing_cocci.show_iso_failures;
708f4980
C
110
111 FC.verbose_cocci;
112
113 Flag_parsing_c.verbose_cfg;
114 Flag_parsing_c.verbose_unparsing;
115 Flag_parsing_c.verbose_visit;
116
117 Flag_matcher.verbose_matcher;
118
119 Flag_parsing_c.show_parsing_error;
faf9a90c
C
120 ],
121 [
122
123 Flag.show_misc;
124
125 FC.show_c;
126 FC.show_flow;
127 FC.show_before_fixed_flow;
128 FC.show_ctl_tex;
129 FC.show_ctl_text;
130
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;
138 ])
139
34e49164
C
140let pad_profile = (
141 [
faf9a90c 142 FC.show_diff;
34e49164
C
143 ],
144 [
145
146 Flag.show_misc;
faf9a90c 147 Flag.show_transinfo;
34e49164 148
faf9a90c
C
149 FC.show_c;
150 FC.show_cocci;
151 FC.show_flow;
152 FC.show_before_fixed_flow;
153 FC.show_ctl_tex;
154 FC.show_ctl_text;
155 FC.show_binding_in_out;
34e49164
C
156
157 Flag_parsing_cocci.show_SP;
158 Flag_parsing_cocci.show_iso_failures;
159 Flag_ctl.verbose_ctl_engine;
160 Flag_ctl.verbose_match;
485bce71 161 Flag_matcher.debug_engine;
34e49164
C
162 Flag_parsing_c.debug_unparsing;
163 Flag_parsing_c.verbose_type;
164 Flag_parsing_c.verbose_parsing;
165 ])
166
faf9a90c
C
167let run_profile p =
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
171
34e49164
C
172(*****************************************************************************)
173(* The spatch options *)
174(*****************************************************************************)
175
978fd7e5
C
176let usage_msg =
177 "Usage: " ^ basename Sys.argv.(0) ^
178 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
34e49164
C
179 "\n" ^ "Options are:"
180
181(* forward reference trick *)
182let short_usage_func = ref (fun () -> ())
183let long_usage_func = ref (fun () -> ())
184
185
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.
189 *)
978fd7e5 190
34e49164
C
191
192(* will be printed when use only ./spatch. For the rest you have to
978fd7e5 193 * use -longhelp to see them.
34e49164 194 *)
978fd7e5
C
195let short_options = [
196 "-sp_file", Arg.Set_string cocci_file,
34e49164
C
197 " <file> the semantic patch file";
198
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";
205
978fd7e5 206 "-U", Arg.Int (fun n -> Flag_parsing_c.diff_lines := Some (i_to_s n)),
34e49164 207 " set number of diff context lines";
978fd7e5 208 "-partial_match", Arg.Set Flag_ctl.partial_match,
34e49164
C
209 " report partial matches of the SP on the C file";
210
978fd7e5 211 "-iso_file", Arg.Set_string Config.std_iso,
34e49164 212 " <file> (default=" ^ !Config.std_iso ^")";
708f4980
C
213 "-macro_file", Arg.Set_string macro_file,
214 " <file>";
215 "-macro_file_builtins", Arg.Set_string Config.std_h,
34e49164
C
216 " <file> (default=" ^ !Config.std_h ^ ")";
217
faf9a90c
C
218 "-all_includes",
219 Arg.Unit (function _ -> FC.include_options := FC.I_ALL_INCLUDES),
34e49164 220 " causes all available include files to be used";
faf9a90c
C
221 "-no_includes",
222 Arg.Unit (function _ -> FC.include_options := FC.I_NO_INCLUDES),
34e49164 223 " causes not even local include files to be used";
faf9a90c
C
224 "-local_includes",
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";
b1b2de81
C
229 "-I", Arg.String (function x -> FC.include_path := Some x),
230 " <dir> containing the header files (optional)";
34e49164 231
978fd7e5
C
232 "-preprocess", Arg.Set preprocess,
233 " run the C preprocessor before applying the semantic match";
234
235 "-c", Arg.Set compat_mode, " gcc/cpp compatibility mode";
34e49164
C
236
237 "-dir", Arg.Set dir,
238 " <dir> process all files in directory recursively";
239
951c7801 240 "-use_glimpse", Arg.Unit (function _ -> Flag.scanner := Flag.Glimpse),
34e49164 241 " works with -dir, use info generated by glimpseindex";
951c7801
C
242 "-use_google", Arg.String (function s -> Flag.scanner := Flag.Google s),
243 " find relevant files using google code search";
34e49164
C
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");
978fd7e5 247 "-kbuild_info", Arg.Set_string kbuild_info,
34e49164
C
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>";
251
252
978fd7e5 253 "-version", Arg.Unit (fun () ->
7f004419
C
254 let withpython = if Pycocci.python_support then "with" else "without" in
255 pr2 (spf "spatch version %s %s Python support" Config.version withpython);
34e49164 256 exit 0;
978fd7e5 257 ),
34e49164
C
258 " guess what";
259
978fd7e5 260 "-date", Arg.Unit (fun () ->
7f004419 261 pr2 "version: $Date: 2010/01/04 11:16:30 $";
34e49164 262 raise (Common.UnixExit 0)
978fd7e5 263 ),
34e49164
C
264 " guess what";
265
978fd7e5 266 "-shorthelp", Arg.Unit (fun () ->
34e49164
C
267 !short_usage_func();
268 raise (Common.UnixExit 0)
978fd7e5 269 ),
34e49164 270 " see short list of options";
978fd7e5 271 "-longhelp", Arg.Unit (fun () ->
34e49164
C
272 !long_usage_func();
273 raise (Common.UnixExit 0)
978fd7e5 274 ),
34e49164 275 " see all the available options in different categories";
978fd7e5 276 "-help", Arg.Unit (fun () ->
34e49164
C
277 !long_usage_func();
278 raise (Common.UnixExit 0)
279 ),
280 " ";
978fd7e5 281 "--help", Arg.Unit (fun () ->
34e49164
C
282 !long_usage_func();
283 raise (Common.UnixExit 0)
284 ),
285 " ";
34e49164
C
286]
287
288(* the format is a list of triples:
289 * (title of section * (optional) explanation of sections * option list)
290 *)
291let other_options = [
978fd7e5 292 "aliases and obsolete options",
34e49164 293 "",
978fd7e5 294 [
708f4980
C
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";
297
978fd7e5 298 "-cocci_file", Arg.Set_string cocci_file,
34e49164 299 " <file> the semantic patch file";
978fd7e5 300(* "-c", Arg.Set_string cocci_file, " short option of -sp_file"; *)
34e49164
C
301 ];
302
978fd7e5 303 "most useful show options",
34e49164
C
304 "",
305 [
faf9a90c
C
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 *)
978fd7e5 310 "-ctl_inline_let",
faf9a90c
C
311 Arg.Unit
312 (function _ -> FC.show_ctl_text := true; FC.inline_let_ctl := true), " ";
313 "-ctl_show_mcodekind",
314 Arg.Unit
315 (function _ -> FC.show_ctl_text := true; FC.show_mcodekind_in_ctl := true),
316 " ";
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, " ";
34e49164
C
320 "-show_trying", Arg.Set Flag.show_trying,
321 " show the name of each function being processed";
1be43e12 322 "-show_dependencies",
faf9a90c
C
323 Arg.Unit (function _ -> FC.show_dependencies := true;
324 FC.show_binding_in_out := true),
1be43e12 325 " show the dependencies related to each rule";
34e49164
C
326 ];
327
978fd7e5 328 "verbose subsystems options",
34e49164
C
329 "",
330 [
faf9a90c
C
331 "-verbose_ctl_engine",
332 Arg.Unit (function _ ->
333 Flag_ctl.verbose_ctl_engine := true; FC.show_ctl_text := true) , " ";
485bce71
C
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";
faf9a90c
C
337 "-gt_without_label",
338 Arg.Unit (function _ ->
339 Flag_ctl.graphical_trace := true; Flag_ctl.gt_without_label := true),
340 " remove graph label (requires option -graphical_trace)";
34e49164 341
faf9a90c
C
342 "-parse_error_msg", Arg.Set Flag_parsing_c.verbose_parsing, " ";
343 "-type_error_msg", Arg.Set Flag_parsing_c.verbose_type, " ";
34e49164
C
344 (* could also use Flag_parsing_c.options_verbose *)
345 ];
346
347 "other show options",
348 "",
349 [
faf9a90c
C
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, " ";
34e49164
C
356 ];
357
358
359 "debug C parsing/unparsing",
360 "",
361 [
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, " ";
366
978fd7e5 367 "-filter_msg", Arg.Set Flag_parsing_c.filter_msg ,
34e49164 368 " filter some cpp message when the macro is a \"known\" cpp construct";
978fd7e5
C
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,
708f4980 371 " filter the error msg";
978fd7e5 372 "-filter_passed_level", Arg.Set_int Flag_parsing_c.filter_passed_level," ";
faf9a90c
C
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 , " "; *)
978fd7e5 375 "-debug_unparsing", Arg.Set Flag_parsing_c.debug_unparsing, " ";
34e49164
C
376
377 ];
378 (* could use Flag_parsing_c.options_debug_with_title instead *)
379
380
381 "shortcut for enabling/disabling a set of debugging options at once",
382 "",
383 [
384 (* todo: other profile ? *)
faf9a90c
C
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), " ";
34e49164
C
388
389 ];
390
391 "bench options",
392 "",
393 [
978fd7e5 394 "-profile", Arg.Unit (function () -> Common.profile := Common.PALL) ,
34e49164 395 " gather timing information about the main coccinelle functions";
978fd7e5 396 "-bench", Arg.Int (function x -> Flag_ctl.bench := x),
34e49164 397 " <level> for profiling the CTL engine";
978fd7e5 398 "-timeout", Arg.Int (fun x -> FC.timeout := Some x),
34e49164 399 " <sec> timeout in seconds";
978fd7e5 400 "-steps", Arg.Int (fun x -> Flag_ctl.steps := Some x),
34e49164 401 " max number of model checking steps per code unit";
b1b2de81
C
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";
34e49164
C
406 "-track_iso", Arg.Set Flag.track_iso_usage,
407 " gather information about isomorphism usage";
408 "-profile_iso",
409 Arg.Unit
410 (function () ->
b1b2de81
C
411 Common.profile :=
412 (*post_engine not included, because it doesn't use isos*)
413 PSOME ["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]),
34e49164
C
414 " gather information about the cost of isomorphism usage"
415 ];
416
417
418
419 "change of algorithm options",
951c7801
C
420 "",
421 [
978fd7e5 422 "-popl", Arg.Set FC.popl,
34e49164
C
423 " simplified SmPL, for the popl paper";
424
425 "-popl_mark_all",
426 Arg.Unit
978fd7e5 427 (function _ -> FC.popl := true; Flag_popl.mark_all := true),
34e49164
C
428 " simplified SmPL, for the popl paper";
429
430 "-popl_keep_all_wits",
431 Arg.Unit
978fd7e5 432 (function _ -> FC.popl := true; Flag_popl.keep_all_wits := true),
34e49164
C
433 " simplified SmPL, for the popl paper";
434
faf9a90c
C
435 "-hrule", Arg.String
436 (function s ->
437 Flag.make_hrule := Some s; FC.include_options := FC.I_NO_INCLUDES),
34e49164
C
438 " semantic patch generation";
439
7f004419
C
440 "-keep_comments", Arg.Set Flag_parsing_c.keep_comments,
441 " keep comments around removed code";
442
34e49164 443 "-loop", Arg.Set Flag_ctl.loop_in_src_code, " ";
978fd7e5
C
444 "-no_loops", Arg.Set Flag_parsing_c.no_loops,
445 " drop all back edges derived from looping constructs - unsafe";
951c7801
C
446 "-no_gotos", Arg.Set Flag_parsing_c.no_gotos,
447 " drop all jumps derived from gotos - unsafe";
34e49164
C
448
449 "-l1", Arg.Clear Flag_parsing_c.label_strategy_2, " ";
978fd7e5 450 "-ifdef_to_if", Arg.Set FC.ifdef_to_if,
485bce71 451 " convert ifdef to if (experimental)";
978fd7e5 452 "-no_ifdef_to_if", Arg.Clear FC.ifdef_to_if,
708f4980
C
453 " convert ifdef to if (experimental)";
454
455 "-disable_multi_pass", Arg.Set Flag_parsing_c.disable_multi_pass, " ";
485bce71 456
978fd7e5 457 "-noif0_passing", Arg.Clear Flag_parsing_c.if0_passing,
34e49164 458 " ";
978fd7e5 459 "-noadd_typedef_root", Arg.Clear Flag_parsing_c.add_typedef_root, " ";
34e49164
C
460 (* could use Flag_parsing_c.options_algo instead *)
461
462
485bce71 463 "-disallow_nested_exps", Arg.Set Flag_matcher.disallow_nested_exps,
7f004419 464 " disallow an expresion pattern from matching a term and its subterm";
faf9a90c 465 "-disable_worth_trying_opt", Arg.Clear FC.worth_trying_opt,
34e49164
C
466 " ";
467 "-only_return_is_error_exit",
485bce71 468 Arg.Set Flag_matcher.only_return_is_error_exit,
34e49164
C
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",
485bce71 474 Arg.Set Flag_matcher.allow_inconsistent_paths,
708f4980 475 " if this flag is set don't check for inconsistent paths; dangerous";
978fd7e5
C
476 "-no_safe_expressions",
477 Arg.Set Flag_matcher.no_safe_expressions,
478 " make an expression disjunction not prioritise the topmost disjunct";
708f4980
C
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";
951c7801
C
487 "-D", Arg.String Flag_parsing_cocci.set_defined_virtual_rules,
488 " indicate that a virtual rule should be considered to be matched";
34e49164
C
489 ];
490
491 "misc options",
492 "",
493 [
978fd7e5 494 "-debugger", Arg.Set Common.debugger,
34e49164 495 " option to set if launch spatch in ocamldebug";
978fd7e5 496 "-disable_once", Arg.Set Common.disable_pr2_once,
34e49164 497 " to print more messages";
978fd7e5 498 "-show_trace_profile", Arg.Set Common.show_trace_profile,
708f4980 499 " show trace";
978fd7e5 500 "-save_tmp_files", Arg.Set Common.save_tmp_files, " ";
34e49164
C
501 ];
502
503 "concurrency",
504 "",
505 [
978fd7e5 506 "-index", Arg.Int (function x -> distrib_index := Some x) ,
34e49164 507 " the processor to use for this run of spatch";
978fd7e5 508 "-max", Arg.Int (function x -> distrib_max := Some x) ,
34e49164
C
509 " the number of processors available";
510 "-mod_distrib", Arg.Set mod_distrib,
511 " use mod to distribute files among the processors";
512 ];
513
514 "pad options",
515 "",
516 [
978fd7e5 517 "-use_cache", Arg.Set Flag_parsing_c.use_cache,
34e49164
C
518 " use .ast_raw pre-parsed cached C file";
519 (* could use Flag_parsing_c.options_pad instead *)
520 ];
521
522
523
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.",
526 [
978fd7e5 527 "-test", Arg.Set test_mode,
34e49164 528 " <file> launch spatch on tests/file.[c,cocci]";
978fd7e5 529 "-testall", Arg.Set test_all,
34e49164
C
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";
535
978fd7e5
C
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";
faf9a90c 540 "-relax_include_path", Arg.Set FC.relax_include_path,
34e49164 541 " ";
34e49164
C
542 ];
543
544 "action mode",
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."
547 ),
548
549 (* -token_c, -parse_c, etc *)
550 ((Common.options_of_actions action (Test_parsing_c.actions())) ++
551 [
552 (let s = "-parse_cocci" in s, Arg.Unit (fun () -> action := s),
553 " <file>");
554 (let s = "-compare_c" in s, Arg.Unit (fun () -> action := s),
555 " <file1> <file2>");
556 ]);
557]
558
559
978fd7e5 560let all_options =
34e49164 561 short_options ++ List.concat (List.map Common.thd3 other_options)
978fd7e5 562
34e49164
C
563(* I don't want the -help and --help that are appended by Arg.align *)
564let arg_align2 xs =
565 Arg.align xs +> List.rev +> Common.drop 2 +> List.rev
566
567(* copy paste of Arg.parse. Don't want the default -help msg *)
568let arg_parse2 l f msg =
569 (try
570 Arg.parse_argv Sys.argv l f msg;
571 with
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 *)
575 pr2 (List.hd xs);
576 !short_usage_func();
577 raise (Common.UnixExit (2))
578 | Arg.Help msg -> (* printf "%s" msg; exit 0; *)
579 raise Impossible (* -help is specified in speclist *)
580 )
581
582
583let short_usage () =
584 begin
978fd7e5 585 Common.short_usage usage_msg short_options;
34e49164
C
586 pr2 "";
587 pr2 "Example of use:";
588 pr2 " ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
589 pr2 "";
590 end
591
592
978fd7e5 593let long_usage () =
34e49164
C
594 Common.long_usage usage_msg short_options other_options
595
596let _ = short_usage_func := short_usage
597let _ = long_usage_func := long_usage
598
599(*****************************************************************************)
600(* Helpers *)
601(*****************************************************************************)
602
603let adjust_stdin cfile k =
604 if !dir
605 then k()
606 else
978fd7e5 607 let newin =
485bce71
C
608 try
609 let (dir, base, ext) = Common.dbe_of_filename cfile in
610 let varfile = Common.filename_of_dbe (dir, base, "var") in
b1b2de81 611 if ext =$= "c" && Common.lfile_exists varfile
485bce71 612 then Some varfile
978fd7e5 613 else None
485bce71
C
614 with Invalid_argument("Filename.chop_extension") -> None
615 in
34e49164
C
616 Common.redirect_stdin_opt newin k
617
978fd7e5 618let glimpse_filter (coccifile, isofile) dir =
faf9a90c 619 let (_metavars,astcocci,_free_var_lists,_negated_positions,
7f004419 620 _used_after_lists,_positions_lists,_,query) =
34e49164
C
621 Cocci.sp_of_file coccifile (Some isofile) in
622 match query with
faf9a90c 623 None -> pr2 "no glimpse keyword inferred from snippet"; None
951c7801
C
624 | Some [query] ->
625 (let suffixes = if !include_headers then ["c";"h"] else ["c"] in
34e49164
C
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
629 match stat with
630 Unix.WEXITED(0) | Unix.WEXITED(1) ->
631 Some
632 (glimpse_res +>
633 List.filter
634 (fun file -> List.mem (Common.filesuffix file) suffixes))
951c7801
C
635 | _ -> None (* error, eg due to pattern too big *))
636 | _ -> failwith "not possible"
34e49164 637
34e49164 638(*****************************************************************************)
b1b2de81 639(* Main action *)
34e49164 640(*****************************************************************************)
34e49164 641
978fd7e5 642let main_action xs =
b1b2de81
C
643 match xs with
644 | x::xs ->
708f4980 645
978fd7e5
C
646 (* a more general solution would be to use
647 * Common.files_of_dir_or_files (x::xs)
708f4980
C
648 * as some elements in xs may also be directories, or individual
649 * files.
650 *)
651 if Common.is_directory x
652 then dir := true;
653
654 adjust_stdin x (fun () ->
655 if !cocci_file =$= ""
656 then failwith "I need a cocci file, use -sp_file <file>";
657
658 if !dir && !Flag.patch =*= None
659 then
660 (match xs with
661 | [] -> Flag.patch := Some x
662 | _ ->
663 pr2
664 ("warning: patch output can only be created when only one\n"^
665 "directory is specified or when the -patch flag is used")
666 );
667
978fd7e5
C
668 let infiles =
669 Common.profile_code "Main.infiles computation" (fun () ->
951c7801 670 match !dir, !kbuild_info, !Flag.scanner = Flag.Glimpse with
708f4980 671 (* glimpse *)
978fd7e5 672 | false, _, true ->
708f4980 673 failwith "-use_glimpse works only with -dir"
978fd7e5 674 | true, s, true when s <> "" ->
708f4980 675 failwith "-use_glimpse does not work with -kbuild"
978fd7e5 676 | true, "", true ->
708f4980
C
677 if not (null xs)
678 then failwith "-use_glimpse can accept only one dir";
679
680 Flag.dir := x;
681 let files =
682 match glimpse_filter (!cocci_file, !Config.std_iso) x with
683 None ->
684 Common.cmd_to_list (* same as "true, "", _" case *)
685 (if !include_headers
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\""))
691 *)
692 then ("find "^ x ^" -name \"*.[ch]\"")
693 else ("find "^ x ^" -name \"*.c\""))
694 | Some files -> files in
695 files +> List.map (fun x -> [x])
696 (* normal *)
697 | false, _, _ -> [x::xs]
978fd7e5 698 | true, "", _ ->
708f4980
C
699 Common.cmd_to_list
700 (if !include_headers
701 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
34e49164 702 else ("find "^(join " " (x::xs))^" -name \"*.c\""))
708f4980
C
703 +> List.map (fun x -> [x])
704
705 (* kbuild *)
978fd7e5
C
706 | true, kbuild_info_file,_ ->
707 let dirs =
708 Common.cmd_to_list ("find "^(join " " (x::xs))^" -type d")
708f4980
C
709 in
710 let info = Kbuild.parse_kbuild_info kbuild_info_file in
711 let groups = Kbuild.files_in_dirs dirs info in
978fd7e5 712
708f4980
C
713 groups +> List.map (function Kbuild.Group xs -> xs)
714 )
715 in
716
717 let infiles =
718 match (!distrib_index,!distrib_max) with
719 (None,None) -> infiles
720 | (Some index,Some max) ->
721 (if index >= max
722 then
723 failwith "index starts at 0, and so must be less than max");
724 if !mod_distrib
725 then
726 let rec loop ct = function
727 [] -> []
728 | x::xs ->
729 if (ct mod max) =|= index
730 then x::(loop (ct+1) xs)
731 else loop (ct+1) xs in
732 loop 0 infiles
733 else
734 begin
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
34e49164 739 let rec loop ct = function
708f4980 740 [] -> []
34e49164 741 | x::xs ->
708f4980 742 if this_min <= ct && ct < this_max
34e49164
C
743 then x::(loop (ct+1) xs)
744 else loop (ct+1) xs in
745 loop 0 infiles
708f4980
C
746 end
747 | _ -> failwith "inconsistent distribution information" in
978fd7e5
C
748
749 let outfiles =
708f4980
C
750 Common.profile_code "Main.outfiles computation" (fun () ->
751 let cocci_infos =
752 Cocci.pre_engine (!cocci_file, !Config.std_iso) in
753 let res =
978fd7e5 754 infiles +> List.map (fun cfiles ->
708f4980 755 pr2 ("HANDLING: " ^ (join " " cfiles));
978fd7e5
C
756 Common.timeout_function_opt !FC.timeout (fun () ->
757 Common.report_if_take_time 10 (join " " cfiles) (fun () ->
fc1ad971 758 (*let s = profile_diagnostic() in*)
34e49164 759 (* Unix.sleep 1; *)
978fd7e5
C
760 try
761 let optfile =
762 if !output_file <> "" && !compat_mode then
763 Some !output_file
764 else
765 None
766 in
767 Common.redirect_stdout_opt optfile (fun () ->
768 (* this is the main call *)
769 Cocci.full_engine cocci_infos cfiles
770 )
771 with
772 | Common.UnixExit x -> raise (Common.UnixExit x)
773 | e ->
fc1ad971
C
774 (*pr2 "previous";
775 pr2 s;
776 pr2 "new";
777 pr2(profile_diagnostic());*)
978fd7e5
C
778 if !dir
779 then begin
780 pr2 ("EXN:" ^ Printexc.to_string e);
781 [] (* *)
782 end
783 else raise e))) in
784 Cocci.post_engine cocci_infos;
785 res
786 ) +> List.concat
708f4980
C
787 in
788
978fd7e5 789 Common.profile_code "Main.result analysis" (fun () ->
708f4980 790 Ctlcocci_integration.print_bench();
708f4980 791 let outfiles = Cocci.check_duplicate_modif outfiles in
978fd7e5
C
792 outfiles +> List.iter (fun (infile, outopt) ->
793 outopt +> Common.do_option (fun outfile ->
708f4980
C
794 if !inplace_modif
795 then begin
796 Common.command2 ("cp "^infile^" "^infile^".cocci_orig");
797 Common.command2 ("cp "^outfile^" "^infile);
798 end;
799
800 if !outplace_modif
801 then Common.command2 ("cp "^outfile^" "^infile^".cocci_res");
978fd7e5
C
802
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)
806 * anymore in /tmp.
807 *)
808 (*
809 if !output_file =$= ""
810 then begin
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);
814 end
815 *)
708f4980 816 ));
978fd7e5
C
817 if !output_file <> "" && not !compat_mode then
818 (match outfiles with
819 | [infile, Some outfile] when infile =$= x && null xs ->
708f4980 820 Common.command2 ("cp " ^outfile^ " " ^ !output_file);
978fd7e5 821 | [infile, None] when infile =$= x && null xs ->
708f4980 822 Common.command2 ("cp " ^infile^ " " ^ !output_file);
978fd7e5
C
823 | _ ->
824 failwith
708f4980
C
825 ("-o can not be applied because there is multiple " ^
826 "modified files");
827 );
708f4980
C
828 if !compare_with_expected
829 then Testing.compare_with_expected outfiles))
b1b2de81
C
830
831 | [] -> raise Impossible
832
833
834(*****************************************************************************)
835(* The coccinelle main entry point *)
836(*****************************************************************************)
978fd7e5 837let main () =
b1b2de81
C
838 begin
839 let arglist = Array.to_list Sys.argv in
840
841 if not (null (Common.inter_set arglist
002099fc 842 ["-cocci_file";"-sp_file";"-sp";"-test";"-testall";
b1b2de81
C
843 "-test_okfailed";"-test_regression_okfailed"]))
844 then run_profile quiet_profile;
845
846 let args = ref [] in
847
708f4980
C
848 (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*)
849
b1b2de81
C
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;
852
708f4980
C
853 (* julia hack so that one can override directories specified on
854 * the command line. *)
b1b2de81
C
855 (if !dir
856 then
857 let chosen_dir =
858 if List.length !args > 1
859 then
860 begin
861 let chosen = List.hd !args in
862 pr2 ("ignoring all but the last specified directory: "^chosen);
863 args := [chosen];
864 chosen
865 end
866 else List.hd !args in
867 if !FC.include_path =*= None
868 then FC.include_path := Some (Filename.concat chosen_dir "include"));
708f4980 869
b1b2de81
C
870 args := List.rev !args;
871
872 if !cocci_file <> "" && (not (!cocci_file =~ ".*\\.\\(sgrep\\|spatch\\)$"))
873 then cocci_file := Common.adjust_ext_if_needed !cocci_file ".cocci";
874
978fd7e5 875 if !Config.std_iso <> ""
b1b2de81 876 then Config.std_iso := Common.adjust_ext_if_needed !Config.std_iso ".iso";
978fd7e5 877 if !Config.std_h <> ""
b1b2de81
C
878 then Config.std_h := Common.adjust_ext_if_needed !Config.std_h ".h";
879
978fd7e5 880 if !Config.std_h <> ""
708f4980
C
881 then Parse_c.init_defs_builtins !Config.std_h;
882
978fd7e5 883 if !macro_file <> ""
708f4980 884 then Parse_c.init_defs_macros !macro_file;
b1b2de81
C
885
886
887 (* must be done after Arg.parse, because Common.profile is set by it *)
978fd7e5 888 Common.profile_code "Main total" (fun () ->
b1b2de81
C
889
890
891 let all_actions = Test_parsing_c.actions() in
892
893 (match (!args) with
894
895 (* --------------------------------------------------------- *)
896 (* The test framework. Works with tests/ or .ok and .failed *)
897 (* --------------------------------------------------------- *)
978fd7e5 898 | [x] when !test_mode ->
b1b2de81
C
899 FC.include_path := Some "tests/include";
900 Testing.testone x !compare_with_expected
901
978fd7e5 902 | [] when !test_all ->
b1b2de81 903 FC.include_path := Some "tests/include";
708f4980
C
904 if !expected_score_file <> ""
905 then Testing.testall ~expected_score_file:!expected_score_file ()
906 else Testing.testall ()
b1b2de81 907
978fd7e5 908 | [] when !test_regression_okfailed ->
b1b2de81
C
909 Testing.test_regression_okfailed ()
910
978fd7e5 911 | x::xs when !test_okfailed ->
b1b2de81
C
912 (* do its own timeout on FC.timeout internally *)
913 FC.relax_include_path := true;
978fd7e5 914 adjust_stdin x (fun () ->
b1b2de81
C
915 Testing.test_okfailed !cocci_file (x::xs)
916 )
917
918 (* --------------------------------------------------------- *)
919 (* Actions, useful to debug subpart of coccinelle *)
920 (* --------------------------------------------------------- *)
921
922 | xs when List.mem !action (Common.action_list all_actions) ->
923 Common.do_action !action xs all_actions
924
978fd7e5 925 | [file] when !action =$= "-parse_cocci" ->
b1b2de81
C
926 Testing.test_parse_cocci file
927
928 (* I think this is used by some scripts in some Makefile for our
929 * big-tests. So dont remove.
930 *)
978fd7e5 931 | [file1;file2] when !action =$= "-compare_c" ->
b1b2de81
C
932 Test_parsing_c.test_compare_c file1 file2 (* result = unix code *)
933
934 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
935
936
937 (* --------------------------------------------------------- *)
938 (* This is the main entry *)
939 (* --------------------------------------------------------- *)
fc1ad971 940 | x::xs -> main_action (x::xs)
b1b2de81 941
34e49164
C
942 (* --------------------------------------------------------- *)
943 (* empty entry *)
944 (* --------------------------------------------------------- *)
945 | [] -> short_usage()
34e49164
C
946 ));
947 if !Pycocci.initialised && (Pycocci.py_isinitialized ()) != 0 then begin
948 ignore(Pycocci.pyrun_simplestring "cocci.finalise()");
978fd7e5 949 if !Flag.show_misc
34e49164
C
950 then Common.pr2 "Finalizing python\n";
951 Pycocci.py_finalize ();
952 end
953 end
954
708f4980 955
978fd7e5
C
956let main_with_better_error_report () =
957 if !Common.debugger then main ()
958 else
959 try
960 main ()
708f4980 961 with
978fd7e5 962 | Unix.Unix_error (_, "stat", filename) ->
708f4980
C
963 pr2 (spf "ERROR: File %s does not exist" filename);
964 raise (UnixExit (-1))
708f4980 965
34e49164 966(*****************************************************************************)
708f4980 967let start =
978fd7e5 968 Common.main_boilerplate (fun () ->
708f4980 969 main_with_better_error_report ();
34e49164
C
970 Ctlcocci_integration.print_bench();
971 )