Coccinelle release 1.0.0-rc4
[bpt/coccinelle.git] / main.ml
CommitLineData
f537ebc4
C
1(*
2 * Copyright 2010, INRIA, University of Copenhagen
3 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
4 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
5 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
6 * This file is part of Coccinelle.
7 *
8 * Coccinelle is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, according to version 2 of the License.
11 *
12 * Coccinelle is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * The authors reserve the right to distribute this or future versions of
21 * Coccinelle under other licenses.
22 *)
23
24
34e49164 25open Common
faf9a90c 26module FC = Flag_cocci
34e49164
C
27
28(*****************************************************************************)
29(* Flags *)
30(*****************************************************************************)
31
32(* In addition to flags that can be tweaked via -xxx options (cf the
33 * full list of options in "the spatch options" section below), the
34 * spatch program also depends on external files, described in
35 * globals/config.ml, mainly a standard.h and standard.iso file *)
36
37let cocci_file = ref ""
38
39let output_file = ref ""
5636bb2c
C
40let inplace_modif = ref false (* but keeps nothing *)
41let backup_suffix =
42 ref (None : string option) (* suffix for backup if one is desired *)
34e49164 43let outplace_modif = ref false (* generates a .cocci_res *)
978fd7e5
C
44let preprocess = ref false (* run the C preprocessor before cocci *)
45let compat_mode = ref false
90aeb998 46let ignore_unknown_opt = ref false
34e49164 47
708f4980 48(* somehow obsolete now *)
978fd7e5 49let dir = ref false
34e49164
C
50
51let include_headers = ref false
52let kbuild_info = ref ""
53
978fd7e5 54let macro_file = ref ""
708f4980 55
34e49164
C
56(* test mode *)
57let test_mode = ref false
58let test_all = ref false
59let test_okfailed = ref false
60let test_regression_okfailed = ref false
708f4980 61let expected_score_file = ref ""
34e49164
C
62
63
64(* action mode *)
65let action = ref ""
66
67(* works with -test but also in "normal" spatch mode *)
68let compare_with_expected = ref false
69
34e49164
C
70let distrib_index = ref (None : int option)
71let distrib_max = ref (None : int option)
72let mod_distrib = ref false
73
74
75(*****************************************************************************)
76(* Profiles *)
77(*****************************************************************************)
78
79(* pair of (list of flags to set true, list of flags to set false *)
ae4735db
C
80let very_quiet_profile = (
81 [
82 ],
83 [
84 (* FC.show_diff; just leave this as it is *)
85
86 Common.print_to_stderr;
87 Flag.show_misc;
88 Flag.show_trying;
89 Flag.show_transinfo;
90
91 FC.show_c;
92 FC.show_cocci;
93 FC.show_flow;
94 FC.show_before_fixed_flow;
95 FC.show_ctl_tex;
96 FC.show_ctl_text;
97 FC.show_binding_in_out;
98
99 FC.verbose_cocci;
100
101 Flag_parsing_c.show_parsing_error;
102
103 Flag_parsing_c.verbose_lexing;
104 Flag_parsing_c.verbose_parsing;
105 Flag_parsing_c.verbose_type;
106 Flag_parsing_c.verbose_cfg;
107 Flag_parsing_c.verbose_unparsing;
108 Flag_parsing_c.verbose_visit;
109 Flag_parsing_c.verbose_cpp_ast;
110
111 Flag_matcher.verbose_matcher;
112 Flag_matcher.debug_engine;
113
114 Flag_parsing_c.debug_unparsing;
115
116 Flag_parsing_cocci.show_SP;
117 Flag_parsing_cocci.show_iso_failures;
118
119 Flag_ctl.verbose_ctl_engine;
120 Flag_ctl.verbose_match;
121
122
123 ])
124
34e49164
C
125let quiet_profile = (
126 [
ae4735db 127 Common.print_to_stderr
34e49164
C
128 ],
129 [
faf9a90c 130 (* FC.show_diff; just leave this as it is *)
34e49164
C
131
132 Flag.show_misc;
133 Flag.show_trying;
faf9a90c 134 Flag.show_transinfo;
34e49164 135
faf9a90c
C
136 FC.show_c;
137 FC.show_cocci;
138 FC.show_flow;
139 FC.show_before_fixed_flow;
140 FC.show_ctl_tex;
141 FC.show_ctl_text;
142 FC.show_binding_in_out;
34e49164 143
708f4980
C
144 FC.verbose_cocci;
145
146 Flag_parsing_c.show_parsing_error;
147
148 Flag_parsing_c.verbose_lexing;
149 Flag_parsing_c.verbose_parsing;
150 Flag_parsing_c.verbose_type;
151 Flag_parsing_c.verbose_cfg;
152 Flag_parsing_c.verbose_unparsing;
153 Flag_parsing_c.verbose_visit;
978fd7e5 154 Flag_parsing_c.verbose_cpp_ast;
708f4980
C
155
156 Flag_matcher.verbose_matcher;
157 Flag_matcher.debug_engine;
158
159 Flag_parsing_c.debug_unparsing;
160
34e49164
C
161 Flag_parsing_cocci.show_SP;
162 Flag_parsing_cocci.show_iso_failures;
708f4980 163
34e49164
C
164 Flag_ctl.verbose_ctl_engine;
165 Flag_ctl.verbose_match;
708f4980
C
166
167
34e49164
C
168 ])
169
faf9a90c
C
170(* some information that is useful in seeing why a semantic patch doesn't
171work properly *)
172let debug_profile = (
173 [
ae4735db 174 Common.print_to_stderr;
faf9a90c 175 Flag.show_misc;
708f4980
C
176 Flag.show_transinfo;
177
faf9a90c
C
178 FC.show_diff;
179 FC.show_cocci;
180 FC.show_binding_in_out;
181 FC.show_dependencies;
708f4980 182
faf9a90c 183 Flag_parsing_cocci.show_iso_failures;
708f4980
C
184
185 FC.verbose_cocci;
186
187 Flag_parsing_c.verbose_cfg;
188 Flag_parsing_c.verbose_unparsing;
189 Flag_parsing_c.verbose_visit;
190
191 Flag_matcher.verbose_matcher;
192
193 Flag_parsing_c.show_parsing_error;
faf9a90c
C
194 ],
195 [
196
197 Flag.show_misc;
198
199 FC.show_c;
200 FC.show_flow;
201 FC.show_before_fixed_flow;
202 FC.show_ctl_tex;
203 FC.show_ctl_text;
204
205 Flag_parsing_cocci.show_SP;
206 Flag_ctl.verbose_ctl_engine;
207 Flag_ctl.verbose_match;
208 Flag_matcher.debug_engine;
209 Flag_parsing_c.debug_unparsing;
210 Flag_parsing_c.verbose_type;
211 Flag_parsing_c.verbose_parsing;
212 ])
213
34e49164
C
214let pad_profile = (
215 [
faf9a90c 216 FC.show_diff;
ae4735db 217 Common.print_to_stderr;
34e49164
C
218 ],
219 [
220
221 Flag.show_misc;
faf9a90c 222 Flag.show_transinfo;
34e49164 223
faf9a90c
C
224 FC.show_c;
225 FC.show_cocci;
226 FC.show_flow;
227 FC.show_before_fixed_flow;
228 FC.show_ctl_tex;
229 FC.show_ctl_text;
230 FC.show_binding_in_out;
34e49164
C
231
232 Flag_parsing_cocci.show_SP;
233 Flag_parsing_cocci.show_iso_failures;
234 Flag_ctl.verbose_ctl_engine;
235 Flag_ctl.verbose_match;
485bce71 236 Flag_matcher.debug_engine;
34e49164
C
237 Flag_parsing_c.debug_unparsing;
238 Flag_parsing_c.verbose_type;
239 Flag_parsing_c.verbose_parsing;
240 ])
241
faf9a90c
C
242let run_profile p =
243 let (set_to_true, set_to_false) = p in
244 List.iter (fun x -> x := false) set_to_false;
245 List.iter (fun x -> x := true) set_to_true
246
34e49164
C
247(*****************************************************************************)
248(* The spatch options *)
249(*****************************************************************************)
250
978fd7e5
C
251let usage_msg =
252 "Usage: " ^ basename Sys.argv.(0) ^
253 " -sp_file <SP> <infile> [-o <outfile>] [-iso_file <iso>] [options]" ^
34e49164
C
254 "\n" ^ "Options are:"
255
256(* forward reference trick *)
257let short_usage_func = ref (fun () -> ())
258let long_usage_func = ref (fun () -> ())
259
260
261(* The short_options are user-oriented. The other options are for
262 * the developers of coccinelle or advanced-users that know
263 * quite well the underlying semantics of coccinelle.
264 *)
978fd7e5 265
34e49164
C
266
267(* will be printed when use only ./spatch. For the rest you have to
978fd7e5 268 * use -longhelp to see them.
34e49164 269 *)
978fd7e5
C
270let short_options = [
271 "-sp_file", Arg.Set_string cocci_file,
34e49164
C
272 " <file> the semantic patch file";
273
274 "-o", Arg.Set_string output_file,
275 " <file> the output file";
5636bb2c 276 "-in_place", Arg.Set inplace_modif,
34e49164 277 " do the modification on the file directly";
5636bb2c
C
278 "-backup_suffix", Arg.String (function s -> backup_suffix := Some s),
279 " suffix to use when making a backup for inplace";
280 "-out_place", Arg.Set outplace_modif,
34e49164
C
281 " store modifications in a .cocci_res file";
282
978fd7e5 283 "-U", Arg.Int (fun n -> Flag_parsing_c.diff_lines := Some (i_to_s n)),
34e49164 284 " set number of diff context lines";
978fd7e5 285 "-partial_match", Arg.Set Flag_ctl.partial_match,
34e49164
C
286 " report partial matches of the SP on the C file";
287
978fd7e5 288 "-iso_file", Arg.Set_string Config.std_iso,
34e49164 289 " <file> (default=" ^ !Config.std_iso ^")";
708f4980
C
290 "-macro_file", Arg.Set_string macro_file,
291 " <file>";
292 "-macro_file_builtins", Arg.Set_string Config.std_h,
34e49164
C
293 " <file> (default=" ^ !Config.std_h ^ ")";
294
1eddfd50
C
295 "-recursive_includes",
296 Arg.Unit (function _ -> FC.include_options := FC.I_REALLY_ALL_INCLUDES),
297 " causes all available include files, both those included in the C file(s) and those included in header files, to be used";
faf9a90c
C
298 "-all_includes",
299 Arg.Unit (function _ -> FC.include_options := FC.I_ALL_INCLUDES),
1eddfd50 300 " causes all available include files included in the C file(s) to be used";
faf9a90c
C
301 "-no_includes",
302 Arg.Unit (function _ -> FC.include_options := FC.I_NO_INCLUDES),
34e49164 303 " causes not even local include files to be used";
faf9a90c
C
304 "-local_includes",
305 Arg.Unit (function _ -> FC.include_options := FC.I_NORMAL_INCLUDES),
306 " causes local include files to be used";
90aeb998
C
307 "-ignore_unknown_options", Arg.Set ignore_unknown_opt,
308 " For integration in a toolchain (must be set before the first unknown option)";
faf9a90c
C
309 "-include_headers", Arg.Set include_headers,
310 " process header files independently";
c3e37e97
C
311 "-I", Arg.String (fun x ->
312 FC.include_path:= x::!FC.include_path
313 ),
b1b2de81 314 " <dir> containing the header files (optional)";
34e49164 315
978fd7e5
C
316 "-preprocess", Arg.Set preprocess,
317 " run the C preprocessor before applying the semantic match";
318
319 "-c", Arg.Set compat_mode, " gcc/cpp compatibility mode";
34e49164
C
320
321 "-dir", Arg.Set dir,
322 " <dir> process all files in directory recursively";
323
951c7801 324 "-use_glimpse", Arg.Unit (function _ -> Flag.scanner := Flag.Glimpse),
34e49164 325 " works with -dir, use info generated by glimpseindex";
951c7801
C
326 "-use_google", Arg.String (function s -> Flag.scanner := Flag.Google s),
327 " find relevant files using google code search";
1eddfd50
C
328 "-use_idutils", Arg.Unit (function s -> Flag.scanner := Flag.IdUtils),
329 " find relevant files using id-utils";
c3e37e97
C
330 "-patch",
331 Arg.String (function s -> Flag.patch := Some (Cocci.normalize_path s)),
34e49164
C
332 (" <dir> path name with respect to which a patch should be created\n"^
333 " \"\" for a file in the current directory");
978fd7e5 334 "-kbuild_info", Arg.Set_string kbuild_info,
34e49164
C
335 " <file> improve -dir by grouping related c files";
336 "-pyoutput", Arg.Set_string Flag.pyoutput,
337 " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>";
338
339
978fd7e5 340 "-version", Arg.Unit (fun () ->
7f004419
C
341 let withpython = if Pycocci.python_support then "with" else "without" in
342 pr2 (spf "spatch version %s %s Python support" Config.version withpython);
34e49164 343 exit 0;
978fd7e5 344 ),
34e49164
C
345 " guess what";
346
978fd7e5 347 "-date", Arg.Unit (fun () ->
5427db06 348 pr2 "version: $Date: 2011/06/23 11:11:16 $";
34e49164 349 raise (Common.UnixExit 0)
978fd7e5 350 ),
34e49164
C
351 " guess what";
352
978fd7e5 353 "-shorthelp", Arg.Unit (fun () ->
34e49164
C
354 !short_usage_func();
355 raise (Common.UnixExit 0)
978fd7e5 356 ),
34e49164 357 " see short list of options";
978fd7e5 358 "-longhelp", Arg.Unit (fun () ->
34e49164
C
359 !long_usage_func();
360 raise (Common.UnixExit 0)
978fd7e5 361 ),
34e49164 362 " see all the available options in different categories";
978fd7e5 363 "-help", Arg.Unit (fun () ->
34e49164
C
364 !long_usage_func();
365 raise (Common.UnixExit 0)
366 ),
367 " ";
978fd7e5 368 "--help", Arg.Unit (fun () ->
34e49164
C
369 !long_usage_func();
370 raise (Common.UnixExit 0)
371 ),
372 " ";
34e49164
C
373]
374
375(* the format is a list of triples:
376 * (title of section * (optional) explanation of sections * option list)
377 *)
378let other_options = [
978fd7e5 379 "aliases and obsolete options",
34e49164 380 "",
978fd7e5 381 [
708f4980
C
382 "-sp", Arg.Set_string cocci_file, " short option of -sp_file";
383 "-iso", Arg.Set_string Config.std_iso, " short option of -iso_file";
384
978fd7e5 385 "-cocci_file", Arg.Set_string cocci_file,
34e49164 386 " <file> the semantic patch file";
978fd7e5 387(* "-c", Arg.Set_string cocci_file, " short option of -sp_file"; *)
34e49164
C
388 ];
389
978fd7e5 390 "most useful show options",
34e49164
C
391 "",
392 [
faf9a90c
C
393 "-show_diff" , Arg.Set FC.show_diff, " ";
394 "-no_show_diff" , Arg.Clear FC.show_diff, " ";
395 "-show_flow" , Arg.Set FC.show_flow, " ";
396 (* works in conjunction with -show_ctl_text *)
978fd7e5 397 "-ctl_inline_let",
faf9a90c
C
398 Arg.Unit
399 (function _ -> FC.show_ctl_text := true; FC.inline_let_ctl := true), " ";
400 "-ctl_show_mcodekind",
401 Arg.Unit
402 (function _ -> FC.show_ctl_text := true; FC.show_mcodekind_in_ctl := true),
403 " ";
404 "-show_bindings", Arg.Set FC.show_binding_in_out, " ";
405 "-show_transinfo", Arg.Set Flag.show_transinfo, " ";
406 "-show_misc", Arg.Set Flag.show_misc, " ";
34e49164
C
407 "-show_trying", Arg.Set Flag.show_trying,
408 " show the name of each function being processed";
1be43e12 409 "-show_dependencies",
faf9a90c
C
410 Arg.Unit (function _ -> FC.show_dependencies := true;
411 FC.show_binding_in_out := true),
1be43e12 412 " show the dependencies related to each rule";
34e49164
C
413 ];
414
978fd7e5 415 "verbose subsystems options",
34e49164
C
416 "",
417 [
faf9a90c
C
418 "-verbose_ctl_engine",
419 Arg.Unit (function _ ->
420 Flag_ctl.verbose_ctl_engine := true; FC.show_ctl_text := true) , " ";
485bce71
C
421 "-verbose_match", Arg.Set Flag_ctl.verbose_match, " ";
422 "-verbose_engine", Arg.Set Flag_matcher.debug_engine, " ";
423 "-graphical_trace", Arg.Set Flag_ctl.graphical_trace, " generate a pdf file representing the matching process";
faf9a90c
C
424 "-gt_without_label",
425 Arg.Unit (function _ ->
426 Flag_ctl.graphical_trace := true; Flag_ctl.gt_without_label := true),
427 " remove graph label (requires option -graphical_trace)";
34e49164 428
ae4735db
C
429 "-parse_error_msg", Arg.Set Flag_parsing_c.show_parsing_error, " ";
430 "-verbose_parsing",
431 Arg.Unit (fun _ -> Flag_parsing_c.verbose_parsing := true;
432 Flag_parsing_c.show_parsing_error := true), " ";
faf9a90c 433 "-type_error_msg", Arg.Set Flag_parsing_c.verbose_type, " ";
34e49164
C
434 (* could also use Flag_parsing_c.options_verbose *)
435 ];
436
437 "other show options",
438 "",
439 [
faf9a90c
C
440 "-show_c" , Arg.Set FC.show_c, " ";
441 "-show_cocci" , Arg.Set FC.show_cocci, " ";
442 "-show_before_fixed_flow" , Arg.Set FC.show_before_fixed_flow, " ";
443 "-show_ctl_tex" , Arg.Set FC.show_ctl_tex, " ";
444 "-show_ctl_text" , Arg.Set FC.show_ctl_text, " ";
445 "-show_SP" , Arg.Set Flag_parsing_cocci.show_SP, " ";
34e49164
C
446 ];
447
448
449 "debug C parsing/unparsing",
450 "",
451 [
452 "-debug_cpp", Arg.Set Flag_parsing_c.debug_cpp, " ";
453 "-debug_lexer", Arg.Set Flag_parsing_c.debug_lexer , " ";
454 "-debug_etdt", Arg.Set Flag_parsing_c.debug_etdt , " ";
455 "-debug_typedef", Arg.Set Flag_parsing_c.debug_typedef, " ";
456
978fd7e5 457 "-filter_msg", Arg.Set Flag_parsing_c.filter_msg ,
34e49164 458 " filter some cpp message when the macro is a \"known\" cpp construct";
978fd7e5
C
459 "-filter_define_error", Arg.Set Flag_parsing_c.filter_define_error," ";
460 "-filter_msg_define_error", Arg.Set Flag_parsing_c.filter_msg_define_error,
708f4980 461 " filter the error msg";
978fd7e5 462 "-filter_passed_level", Arg.Set_int Flag_parsing_c.filter_passed_level," ";
faf9a90c
C
463(* debug cfg doesn't seem to have any effect, so drop it as an option *)
464(* "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " "; *)
978fd7e5 465 "-debug_unparsing", Arg.Set Flag_parsing_c.debug_unparsing, " ";
34e49164
C
466
467 ];
468 (* could use Flag_parsing_c.options_debug_with_title instead *)
469
470
471 "shortcut for enabling/disabling a set of debugging options at once",
472 "",
473 [
474 (* todo: other profile ? *)
faf9a90c 475 "-quiet", Arg.Unit (fun () -> run_profile quiet_profile), " ";
ae4735db 476 "-very_quiet", Arg.Unit (fun () -> run_profile very_quiet_profile), " ";
faf9a90c
C
477 "-debug", Arg.Unit (fun () -> run_profile debug_profile), " ";
478 "-pad", Arg.Unit (fun () -> run_profile pad_profile), " ";
34e49164
C
479
480 ];
481
482 "bench options",
483 "",
484 [
978fd7e5 485 "-profile", Arg.Unit (function () -> Common.profile := Common.PALL) ,
34e49164 486 " gather timing information about the main coccinelle functions";
978fd7e5 487 "-bench", Arg.Int (function x -> Flag_ctl.bench := x),
34e49164 488 " <level> for profiling the CTL engine";
978fd7e5 489 "-timeout", Arg.Int (fun x -> FC.timeout := Some x),
34e49164 490 " <sec> timeout in seconds";
978fd7e5 491 "-steps", Arg.Int (fun x -> Flag_ctl.steps := Some x),
34e49164 492 " max number of model checking steps per code unit";
b1b2de81
C
493 "-iso_limit", Arg.Int (fun x -> Flag_parsing_cocci.iso_limit := Some x),
494 " max depth of iso application";
495 "-no_iso_limit", Arg.Unit (fun _ -> Flag_parsing_cocci.iso_limit := None),
496 " disable limit on max depth of iso application";
34e49164
C
497 "-track_iso", Arg.Set Flag.track_iso_usage,
498 " gather information about isomorphism usage";
413ffc02
C
499 "-disable_iso",
500 Arg.String
501 (fun s -> Flag_parsing_cocci.disabled_isos :=
502 s :: !Flag_parsing_cocci.disabled_isos),
503 " disable a specific isomorphism";
34e49164
C
504 "-profile_iso",
505 Arg.Unit
506 (function () ->
b1b2de81
C
507 Common.profile :=
508 (*post_engine not included, because it doesn't use isos*)
509 PSOME ["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]),
34e49164
C
510 " gather information about the cost of isomorphism usage"
511 ];
512
513
514
515 "change of algorithm options",
951c7801
C
516 "",
517 [
978fd7e5 518 "-popl", Arg.Set FC.popl,
34e49164
C
519 " simplified SmPL, for the popl paper";
520
521 "-popl_mark_all",
522 Arg.Unit
978fd7e5 523 (function _ -> FC.popl := true; Flag_popl.mark_all := true),
34e49164
C
524 " simplified SmPL, for the popl paper";
525
526 "-popl_keep_all_wits",
527 Arg.Unit
978fd7e5 528 (function _ -> FC.popl := true; Flag_popl.keep_all_wits := true),
34e49164
C
529 " simplified SmPL, for the popl paper";
530
faf9a90c
C
531 "-hrule", Arg.String
532 (function s ->
533 Flag.make_hrule := Some s; FC.include_options := FC.I_NO_INCLUDES),
34e49164
C
534 " semantic patch generation";
535
7f004419
C
536 "-keep_comments", Arg.Set Flag_parsing_c.keep_comments,
537 " keep comments around removed code";
538
34e49164 539 "-loop", Arg.Set Flag_ctl.loop_in_src_code, " ";
978fd7e5
C
540 "-no_loops", Arg.Set Flag_parsing_c.no_loops,
541 " drop all back edges derived from looping constructs - unsafe";
951c7801
C
542 "-no_gotos", Arg.Set Flag_parsing_c.no_gotos,
543 " drop all jumps derived from gotos - unsafe";
34e49164
C
544
545 "-l1", Arg.Clear Flag_parsing_c.label_strategy_2, " ";
978fd7e5 546 "-ifdef_to_if", Arg.Set FC.ifdef_to_if,
485bce71 547 " convert ifdef to if (experimental)";
978fd7e5 548 "-no_ifdef_to_if", Arg.Clear FC.ifdef_to_if,
708f4980
C
549 " convert ifdef to if (experimental)";
550
551 "-disable_multi_pass", Arg.Set Flag_parsing_c.disable_multi_pass, " ";
485bce71 552
978fd7e5 553 "-noif0_passing", Arg.Clear Flag_parsing_c.if0_passing,
34e49164 554 " ";
978fd7e5 555 "-noadd_typedef_root", Arg.Clear Flag_parsing_c.add_typedef_root, " ";
34e49164
C
556 (* could use Flag_parsing_c.options_algo instead *)
557
558
485bce71 559 "-disallow_nested_exps", Arg.Set Flag_matcher.disallow_nested_exps,
7f004419 560 " disallow an expresion pattern from matching a term and its subterm";
faf9a90c 561 "-disable_worth_trying_opt", Arg.Clear FC.worth_trying_opt,
34e49164
C
562 " ";
563 "-only_return_is_error_exit",
485bce71 564 Arg.Set Flag_matcher.only_return_is_error_exit,
34e49164
C
565 "if this flag is not set, then break and continue are also error exits";
566 (* the following is a hack to make it easier to add code in sgrep-like
567 code, essentially to compensate for the fact that we don't have
568 any way of printing things out *)
569 "-allow_inconsistent_paths",
485bce71 570 Arg.Set Flag_matcher.allow_inconsistent_paths,
708f4980 571 " if this flag is set don't check for inconsistent paths; dangerous";
978fd7e5
C
572 "-no_safe_expressions",
573 Arg.Set Flag_matcher.no_safe_expressions,
574 " make an expression disjunction not prioritise the topmost disjunct";
708f4980
C
575 "-int_bits", Arg.Int Flag_parsing_c.set_int_bits,
576 " the number of bits in an unsigned int";
577 "-long_bits", Arg.Int Flag_parsing_c.set_long_bits,
578 " the number of bits in an unsigned long";
579 "-linux_spacing", Arg.Unit Flag_parsing_c.set_linux_spacing,
580 " spacing of + code follows the conventions of Linux";
581 "-smpl_spacing", Arg.Unit Flag_parsing_c.set_smpl_spacing,
582 " spacing of + code follows the semantic patch";
ae4735db 583 "-D", Arg.String Flag.set_defined_virtual_rules,
951c7801 584 " indicate that a virtual rule should be considered to be matched";
3a314143
C
585 "-c++", Arg.Set Flag.c_plus_plus,
586 " make a small attempt to parse C++ files"
34e49164
C
587 ];
588
589 "misc options",
590 "",
591 [
978fd7e5 592 "-debugger", Arg.Set Common.debugger,
34e49164 593 " option to set if launch spatch in ocamldebug";
978fd7e5 594 "-disable_once", Arg.Set Common.disable_pr2_once,
34e49164 595 " to print more messages";
978fd7e5 596 "-show_trace_profile", Arg.Set Common.show_trace_profile,
708f4980 597 " show trace";
978fd7e5 598 "-save_tmp_files", Arg.Set Common.save_tmp_files, " ";
34e49164
C
599 ];
600
601 "concurrency",
602 "",
603 [
978fd7e5 604 "-index", Arg.Int (function x -> distrib_index := Some x) ,
34e49164 605 " the processor to use for this run of spatch";
978fd7e5 606 "-max", Arg.Int (function x -> distrib_max := Some x) ,
34e49164
C
607 " the number of processors available";
608 "-mod_distrib", Arg.Set mod_distrib,
609 " use mod to distribute files among the processors";
610 ];
611
612 "pad options",
613 "",
614 [
978fd7e5 615 "-use_cache", Arg.Set Flag_parsing_c.use_cache,
34e49164 616 " use .ast_raw pre-parsed cached C file";
5427db06
C
617 "-cache_prefix",
618 Arg.String (function s ->
619 Flag_parsing_c.cache_prefix := Some s;
620 Flag_parsing_c.use_cache := true),
621 " directory of cached ASTs, sets -use_cache";
34e49164
C
622 (* could use Flag_parsing_c.options_pad instead *)
623 ];
624
625
626
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.",
629 [
978fd7e5 630 "-test", Arg.Set test_mode,
34e49164 631 " <file> launch spatch on tests/file.[c,cocci]";
978fd7e5 632 "-testall", Arg.Set test_all,
34e49164
C
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";
638
978fd7e5
C
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";
faf9a90c 643 "-relax_include_path", Arg.Set FC.relax_include_path,
34e49164 644 " ";
34e49164
C
645 ];
646
647 "action mode",
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."
650 ),
651
652 (* -token_c, -parse_c, etc *)
653 ((Common.options_of_actions action (Test_parsing_c.actions())) ++
654 [
655 (let s = "-parse_cocci" in s, Arg.Unit (fun () -> action := s),
656 " <file>");
657 (let s = "-compare_c" in s, Arg.Unit (fun () -> action := s),
658 " <file1> <file2>");
659 ]);
660]
661
662
978fd7e5 663let all_options =
34e49164 664 short_options ++ List.concat (List.map Common.thd3 other_options)
978fd7e5 665
34e49164
C
666(* I don't want the -help and --help that are appended by Arg.align *)
667let arg_align2 xs =
668 Arg.align xs +> List.rev +> Common.drop 2 +> List.rev
669
90aeb998
C
670(*
671 Ignore unknown option
672
673 This simplifies the integration of Coccinelle in toolchain. For
674 instance, spatch can then be used as a checker in the Linux build
675 system.
676
677*)
678let check_include_path () =
679 let opt = Array.get Sys.argv !Arg.current in
680 let is_include_re = Str.regexp "-I\\(.*\\)" in
681 if Str.string_match is_include_re opt 0 then
682 let path = Str.matched_group 1 opt in
683 FC.include_path:= path::!FC.include_path
684 else ()
685
686let rec arg_parse_no_fail l f msg =
687 try
688 check_include_path ();
689 Arg.parse_argv Sys.argv l f msg;
690 with
691 | Arg.Bad emsg ->
692 arg_parse_no_fail l f msg
693 | Arg.Help msg -> (* printf "%s" msg; exit 0; *)
694 raise Impossible (* -help is specified in speclist *)
695
34e49164
C
696(* copy paste of Arg.parse. Don't want the default -help msg *)
697let arg_parse2 l f msg =
698 (try
699 Arg.parse_argv Sys.argv l f msg;
700 with
90aeb998
C
701 | Arg.Bad emsg -> (* eprintf "%s" msg; exit 2; *)
702 if not !ignore_unknown_opt then
703 begin
704 let xs = Common.lines emsg in
705 (* take only head, it's where the error msg is *)
706 pr2 (List.hd xs);
707 !short_usage_func();
708 raise (Common.UnixExit (2))
709 end
710 else
711 arg_parse_no_fail l f msg;
34e49164
C
712 | Arg.Help msg -> (* printf "%s" msg; exit 0; *)
713 raise Impossible (* -help is specified in speclist *)
714 )
715
716
717let short_usage () =
718 begin
978fd7e5 719 Common.short_usage usage_msg short_options;
34e49164
C
720 pr2 "";
721 pr2 "Example of use:";
722 pr2 " ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c";
723 pr2 "";
724 end
725
726
978fd7e5 727let long_usage () =
34e49164
C
728 Common.long_usage usage_msg short_options other_options
729
730let _ = short_usage_func := short_usage
731let _ = long_usage_func := long_usage
732
733(*****************************************************************************)
734(* Helpers *)
735(*****************************************************************************)
736
3a314143
C
737(* for fresh identifier information *)
738let adjust_stdin cfiles k =
739 match cfiles with
740 [] -> failwith "not possible"
741 | cfile::_ ->
742 let newin =
743 try
744 let (dir, base, ext) = Common.dbe_of_filename cfile in
745 let varfile = Common.filename_of_dbe (dir, base, "var") in
746 if ext =$= "c" && Common.lfile_exists varfile
747 then Some varfile
748 else None
749 with Invalid_argument("Filename.chop_extension") -> None in
750 Common.redirect_stdin_opt newin k
34e49164 751
978fd7e5 752let glimpse_filter (coccifile, isofile) dir =
faf9a90c 753 let (_metavars,astcocci,_free_var_lists,_negated_positions,
1eddfd50 754 _used_after_lists,_positions_lists,(_,query,_)) =
34e49164
C
755 Cocci.sp_of_file coccifile (Some isofile) in
756 match query with
1eddfd50 757 None -> pr2 "no inferred glimpse keywords"; None
413ffc02
C
758 | Some queries ->
759 let suffixes = if !include_headers then ["c";"h"] else ["c"] in
760 let rec loop = function
761 [] -> None (* error, eg due to pattern too big *)
762 | query::queries ->
763 Printf.fprintf stderr "%s\n" ("glimpse request = " ^ query);
764 let command = spf "glimpse -y -H %s -N -W -w '%s'" dir query in
765 let (glimpse_res,stat) = Common.cmd_to_list_and_status command in
766 match stat with
767 Unix.WEXITED(0) | Unix.WEXITED(1) ->
1eddfd50 768 Printf.fprintf stderr "got files\n"; flush stderr;
413ffc02
C
769 Some
770 (glimpse_res +>
771 List.filter
772 (fun file -> List.mem (Common.filesuffix file) suffixes))
773 | _ -> loop queries (* error, eg due to pattern too big *) in
774 loop queries
34e49164 775
1eddfd50
C
776let idutils_filter (coccifile, isofile) dir =
777 let (_metavars,astcocci,_free_var_lists,_negated_positions,
778 _used_after_lists,_positions_lists,(_,_,query)) =
779 Cocci.sp_of_file coccifile (Some isofile) in
780 match query with
781 None -> pr2 "no inferred idutils keywords"; None
782 | Some query ->
783 let suffixes = if !include_headers then ["c";"h"] else ["c"] in
784 let files = Id_utils.interpret dir query in
785 Printf.fprintf stderr "got files\n"; flush stderr;
786 Some
787 (files +>
788 List.filter (fun file -> List.mem (Common.filesuffix file) suffixes))
789
34e49164 790(*****************************************************************************)
b1b2de81 791(* Main action *)
34e49164 792(*****************************************************************************)
34e49164 793
190f1acf
C
794let get_files path =
795 let ch =
796 Common.cmd_to_list (* same as "true, "", _" case *)
797 (if !include_headers
798 (* FIXME : Could we remove xs ?
799 -use_glimpse requires a singleton.
800 This is checked some lines before.
801 then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"")
802 else ("find "^(join " " (x::xs))^" -name \"*.c\"")
803 *)
804 then ("find "^ path ^" -name \"*.[ch]\"")
805 else ("find "^ path ^" -name \"*.c\"")) in
806 let cpp =
807 if !Flag.c_plus_plus
808 then Common.cmd_to_list ("find "^ path ^" -name \"*.cpp\"")
809 else [] in
810 cpp @ ch
811
978fd7e5 812let main_action xs =
3a314143
C
813 Iteration.base_file_list := xs;
814 let rec toploop = function
815 [] -> raise Impossible
816 | x::xs ->
978fd7e5
C
817 (* a more general solution would be to use
818 * Common.files_of_dir_or_files (x::xs)
708f4980
C
819 * as some elements in xs may also be directories, or individual
820 * files.
821 *)
3a314143
C
822 dir := (Common.is_directory x);
823
824 if !cocci_file =$= ""
825 then failwith "I need a cocci file, use -sp_file <file>";
826
827 if !dir && !Flag.patch =*= None
828 then
829 (match xs with
830 | [] -> Flag.patch := Some (Cocci.normalize_path x)
831 | _ ->
832 pr2
833 ("warning: patch output can only be created when only one\n"^
834 "directory is specified or when the -patch flag is used")
835 );
836 Flag.dir := x;
837
838 let infiles =
839 Common.profile_code "Main.infiles computation" (fun () ->
840 match !dir, !kbuild_info, !Flag.scanner with
708f4980 841 (* glimpse *)
3a314143
C
842 | false, _, (Flag.Glimpse|Flag.IdUtils) -> [x::xs]
843 | true, s, (Flag.Glimpse|Flag.IdUtils) when s <> "" ->
844 failwith
845 "-use_glimpse or -id_utils does not work with -kbuild"
846 | true, "", Flag.Glimpse ->
847 (if not (null xs)
848 then failwith "-use_glimpse can accept only one dir");
849
850 let files =
851 match glimpse_filter (!cocci_file, !Config.std_iso) x with
190f1acf 852 None -> get_files x
3a314143
C
853 | Some files -> files in
854 files +> List.map (fun x -> [x])
855 | true, "", Flag.IdUtils ->
856 (if not (null xs)
857 then failwith "-id_utils can accept only one dir");
858
859 let files =
860 match idutils_filter (!cocci_file, !Config.std_iso) x with
190f1acf 861 None -> get_files x
3a314143
C
862 | Some files -> files in
863 files +> List.map (fun x -> [x])
708f4980 864 (* normal *)
3a314143
C
865 | false, _, _ -> [x::xs]
866 | true, "", _ ->
190f1acf 867 get_files (join " " (x::xs)) +> List.map (fun x -> [x])
3a314143 868
708f4980 869 (* kbuild *)
3a314143
C
870 | true, kbuild_info_file,_ ->
871 let dirs =
872 Common.cmd_to_list ("find "^(join " " (x::xs))^" -type d")
873 in
874 let info = Kbuild.parse_kbuild_info kbuild_info_file in
875 let groups = Kbuild.files_in_dirs dirs info in
876
877 groups +> List.map (function Kbuild.Group xs -> xs)
878 )
879 in
5427db06 880
3a314143
C
881 let infiles =
882 match (!distrib_index,!distrib_max) with
883 (None,None) -> infiles
884 | (Some index,Some max) ->
885 (if index >= max
886 then
887 failwith "index starts at 0, and so must be less than max");
888 if !mod_distrib
889 then
34e49164 890 let rec loop ct = function
3a314143 891 [] -> []
34e49164 892 | x::xs ->
3a314143 893 if (ct mod max) =|= index
34e49164
C
894 then x::(loop (ct+1) xs)
895 else loop (ct+1) xs in
896 loop 0 infiles
3a314143
C
897 else
898 begin
899 let all_files = List.length infiles in
900 let regions = (all_files + (max - 1)) / max in
901 let this_min = index * regions in
902 let this_max = (index+1) * regions in
903 let rec loop ct = function
904 [] -> []
905 | x::xs ->
906 if this_min <= ct && ct < this_max
907 then x::(loop (ct+1) xs)
908 else loop (ct+1) xs in
909 loop 0 infiles
910 end
911 | _ -> failwith "inconsistent distribution information" in
912
913 let (cocci_infos,outfiles) =
914 Common.profile_code "Main.outfiles computation" (fun () ->
915 let cocci_infos =
916 Cocci.pre_engine (!cocci_file, !Config.std_iso) in
917 let res =
918 infiles +> List.map (fun cfiles ->
919 pr2 ("HANDLING: " ^ (join " " cfiles));
920 Common.timeout_function_opt !FC.timeout (fun () ->
921 Common.report_if_take_time 10 (join " " cfiles) (fun () ->
922 try
923 let optfile =
924 if !output_file <> "" && !compat_mode then
925 Some !output_file
926 else
927 None
928 in
929 adjust_stdin cfiles (fun () ->
930 Common.redirect_stdout_opt optfile (fun () ->
978fd7e5 931 (* this is the main call *)
3a314143
C
932 Cocci.full_engine cocci_infos cfiles
933 ))
934 with
978fd7e5 935 | Common.UnixExit x -> raise (Common.UnixExit x)
88e71198
C
936 | Pycocci.Pycocciexception ->
937 raise Pycocci.Pycocciexception
978fd7e5
C
938 | e ->
939 if !dir
940 then begin
941 pr2 ("EXN:" ^ Printexc.to_string e);
942 [] (* *)
943 end
944 else raise e))) in
3a314143
C
945 (cocci_infos,res)) in
946 let outfiles = List.concat outfiles in
947 (match Iteration.get_pending_instance() with
5427db06
C
948 None ->
949 (x,xs,cocci_infos,outfiles)
3a314143
C
950 | Some (files,virt_rules,virt_ids) ->
951 if outfiles = [] or outfiles = [] or not !FC.show_diff
952 then
953 begin
954 Flag.defined_virtual_rules := virt_rules;
955 Flag.defined_virtual_env := virt_ids;
956 Common.erase_temp_files();
957 Common.clear_pr2_once();
958 toploop files
959 end
960 else
961 begin
962 Common.pr2
963 "Transformation not compatible with iteration. Aborting.";
964 (x,xs,cocci_infos,outfiles)
965 end) in
966 let (x,xs,cocci_infos,outfiles) = toploop xs in
967
968 Cocci.post_engine cocci_infos;
969 Common.profile_code "Main.result analysis" (fun () ->
970 Ctlcocci_integration.print_bench();
971 let outfiles = Cocci.check_duplicate_modif outfiles in
972 outfiles +> List.iter (fun (infile, outopt) ->
973 outopt +> Common.do_option (fun outfile ->
974 if !inplace_modif
975 then begin
976 (match !backup_suffix with
977 Some backup_suffix ->
978 Common.command2 ("cp "^infile^" "^infile^backup_suffix)
979 | None -> ());
980 Common.command2 ("cp "^outfile^" "^infile);
981 end;
982
983 if !outplace_modif
984 then Common.command2 ("cp "^outfile^" "^infile^".cocci_res");
985
978fd7e5 986 (* potential source of security pb if the /tmp/ file is
3a314143
C
987 * a symlink, so simpler to not produce any regular file
988 * (files created by Common.new_temp_file are still ok)
989 * anymore in /tmp.
990 *)
978fd7e5 991 (*
3a314143
C
992 if !output_file =$= ""
993 then begin
994 let tmpfile = "/tmp/"^Common.basename infile in
995 pr2 (spf "One file modified. Result is here: %s" tmpfile);
996 Common.command2 ("cp "^outfile^" "^tmpfile);
978fd7e5
C
997 end
998 *)
708f4980 999 ));
3a314143
C
1000 if !output_file <> "" && not !compat_mode then
1001 (match outfiles with
1002 | [infile, Some outfile] when infile =$= x && null xs ->
1003 Common.command2 ("cp " ^outfile^ " " ^ !output_file);
1004 | [infile, None] when infile =$= x && null xs ->
1005 Common.command2 ("cp " ^infile^ " " ^ !output_file);
1006 | _ ->
1007 failwith
1008 ("-o can not be applied because there are multiple " ^
1009 "modified files");
1010 );
1011 if !compare_with_expected
1012 then Testing.compare_with_expected outfiles)
1013
1014
b1b2de81
C
1015(*****************************************************************************)
1016(* The coccinelle main entry point *)
1017(*****************************************************************************)
978fd7e5 1018let main () =
b1b2de81
C
1019 begin
1020 let arglist = Array.to_list Sys.argv in
3a314143 1021
b1b2de81 1022 if not (null (Common.inter_set arglist
3a314143 1023 ["-cocci_file";"-sp_file";"-sp";"-test";"-testall";
b1b2de81
C
1024 "-test_okfailed";"-test_regression_okfailed"]))
1025 then run_profile quiet_profile;
3a314143 1026
b1b2de81 1027 let args = ref [] in
3a314143 1028
708f4980 1029 (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*)
3a314143 1030
b1b2de81
C
1031 (* this call can set up many global flag variables via the cmd line *)
1032 arg_parse2 (Arg.align all_options) (fun x -> args := x::!args) usage_msg;
3a314143 1033
708f4980 1034 (* julia hack so that one can override directories specified on
3a314143 1035 * the command line. *)
b1b2de81
C
1036 (if !dir
1037 then
1038 let chosen_dir =
3a314143
C
1039 if List.length !args > 1
1040 then
1041 begin
1042 let chosen = List.hd !args in
1043 Flag.dir := chosen;
1044 pr2 ("ignoring all but the last specified directory: "^chosen);
1045 args := [chosen];
1046 chosen
1047 end
1048 else List.hd !args in
c3e37e97
C
1049 if !FC.include_path =*= []
1050 then FC.include_path := [Filename.concat chosen_dir "include"]);
708f4980 1051
b1b2de81
C
1052 args := List.rev !args;
1053
1054 if !cocci_file <> "" && (not (!cocci_file =~ ".*\\.\\(sgrep\\|spatch\\)$"))
1055 then cocci_file := Common.adjust_ext_if_needed !cocci_file ".cocci";
1056
978fd7e5 1057 if !Config.std_iso <> ""
b1b2de81 1058 then Config.std_iso := Common.adjust_ext_if_needed !Config.std_iso ".iso";
978fd7e5 1059 if !Config.std_h <> ""
b1b2de81
C
1060 then Config.std_h := Common.adjust_ext_if_needed !Config.std_h ".h";
1061
978fd7e5 1062 if !Config.std_h <> ""
708f4980
C
1063 then Parse_c.init_defs_builtins !Config.std_h;
1064
978fd7e5 1065 if !macro_file <> ""
708f4980 1066 then Parse_c.init_defs_macros !macro_file;
b1b2de81
C
1067
1068
1069 (* must be done after Arg.parse, because Common.profile is set by it *)
978fd7e5 1070 Common.profile_code "Main total" (fun () ->
b1b2de81
C
1071
1072
1073 let all_actions = Test_parsing_c.actions() in
1074
1075 (match (!args) with
1076
1077 (* --------------------------------------------------------- *)
1078 (* The test framework. Works with tests/ or .ok and .failed *)
1079 (* --------------------------------------------------------- *)
978fd7e5 1080 | [x] when !test_mode ->
c3e37e97 1081 begin
aa721442 1082 let prefix = "tests/" in
5636bb2c
C
1083 let testfile = x ^ ".cocci" in
1084 if Sys.file_exists (prefix ^ testfile) then
aa721442 1085 begin
3a314143
C
1086 (if !FC.include_path = []
1087 then FC.include_path := [prefix^"include"]);
5636bb2c 1088 Testing.testone prefix x !compare_with_expected
aa721442
C
1089 end
1090 else
5636bb2c
C
1091 if Sys.file_exists testfile then
1092 begin
3a314143
C
1093 (if !FC.include_path = []
1094 then FC.include_path := ["include"]);
5636bb2c
C
1095 Testing.testone "" x !compare_with_expected
1096 end
1097 else
1098 pr2 (spf "ERROR: File %s does not exist" testfile)
c3e37e97 1099 end
b1b2de81 1100
978fd7e5 1101 | [] when !test_all ->
3a314143
C
1102 (if !FC.include_path = []
1103 then FC.include_path := ["tests/include"]);
708f4980
C
1104 if !expected_score_file <> ""
1105 then Testing.testall ~expected_score_file:!expected_score_file ()
1106 else Testing.testall ()
b1b2de81 1107
978fd7e5 1108 | [] when !test_regression_okfailed ->
b1b2de81
C
1109 Testing.test_regression_okfailed ()
1110
3a314143 1111 | ((x::xs) as cfiles) when !test_okfailed ->
b1b2de81
C
1112 (* do its own timeout on FC.timeout internally *)
1113 FC.relax_include_path := true;
3a314143
C
1114 adjust_stdin cfiles (fun () ->
1115 Testing.test_okfailed !cocci_file cfiles
b1b2de81
C
1116 )
1117
1118 (* --------------------------------------------------------- *)
1119 (* Actions, useful to debug subpart of coccinelle *)
1120 (* --------------------------------------------------------- *)
1121
1122 | xs when List.mem !action (Common.action_list all_actions) ->
1123 Common.do_action !action xs all_actions
1124
978fd7e5 1125 | [file] when !action =$= "-parse_cocci" ->
3a314143 1126 Testing.test_parse_cocci file
b1b2de81
C
1127
1128 (* I think this is used by some scripts in some Makefile for our
1129 * big-tests. So dont remove.
1130 *)
978fd7e5 1131 | [file1;file2] when !action =$= "-compare_c" ->
b1b2de81
C
1132 Test_parsing_c.test_compare_c file1 file2 (* result = unix code *)
1133
1134 (* could add the Test_parsing_c.test_actions such as -parse_c & co *)
1135
1136
1137 (* --------------------------------------------------------- *)
1138 (* This is the main entry *)
1139 (* --------------------------------------------------------- *)
fc1ad971 1140 | x::xs -> main_action (x::xs)
b1b2de81 1141
34e49164
C
1142 (* --------------------------------------------------------- *)
1143 (* empty entry *)
1144 (* --------------------------------------------------------- *)
1145 | [] -> short_usage()
34e49164
C
1146 ));
1147 if !Pycocci.initialised && (Pycocci.py_isinitialized ()) != 0 then begin
1148 ignore(Pycocci.pyrun_simplestring "cocci.finalise()");
978fd7e5 1149 if !Flag.show_misc
34e49164
C
1150 then Common.pr2 "Finalizing python\n";
1151 Pycocci.py_finalize ();
1152 end
1153 end
1154
708f4980 1155
978fd7e5
C
1156let main_with_better_error_report () =
1157 if !Common.debugger then main ()
1158 else
1159 try
1160 main ()
708f4980 1161 with
aa721442
C
1162 | Unix.Unix_error (e, "stat", filename) ->
1163 pr2
1164 (spf "ERROR: File %s does not exist: %s"
1165 filename (Unix.error_message e));
708f4980 1166 raise (UnixExit (-1))
785a3008 1167 | Parse_cocci.Bad_virt s ->
c491d8ee
C
1168 Common.pr2 (Printf.sprintf "virtual rule %s not supported" s);
1169 raise (UnixExit (-1))
708f4980 1170
34e49164 1171(*****************************************************************************)
708f4980 1172let start =
978fd7e5 1173 Common.main_boilerplate (fun () ->
708f4980 1174 main_with_better_error_report ();
34e49164
C
1175 Ctlcocci_integration.print_bench();
1176 )