Commit | Line | Data |
---|---|---|
9f8e26f4 | 1 | (* |
90aeb998 C |
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 | |
5636bb2c C |
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 | 25 | open Common |
faf9a90c | 26 | module 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 | ||
37 | let cocci_file = ref "" | |
38 | ||
39 | let output_file = ref "" | |
5636bb2c C |
40 | let inplace_modif = ref false (* but keeps nothing *) |
41 | let backup_suffix = | |
42 | ref (None : string option) (* suffix for backup if one is desired *) | |
34e49164 | 43 | let outplace_modif = ref false (* generates a .cocci_res *) |
978fd7e5 C |
44 | let preprocess = ref false (* run the C preprocessor before cocci *) |
45 | let compat_mode = ref false | |
90aeb998 | 46 | let ignore_unknown_opt = ref false |
34e49164 | 47 | |
708f4980 | 48 | (* somehow obsolete now *) |
978fd7e5 | 49 | let dir = ref false |
34e49164 C |
50 | |
51 | let include_headers = ref false | |
52 | let kbuild_info = ref "" | |
53 | ||
978fd7e5 | 54 | let macro_file = ref "" |
708f4980 | 55 | |
34e49164 C |
56 | (* test mode *) |
57 | let test_mode = ref false | |
58 | let test_all = ref false | |
59 | let test_okfailed = ref false | |
60 | let test_regression_okfailed = ref false | |
708f4980 | 61 | let expected_score_file = ref "" |
34e49164 C |
62 | |
63 | ||
64 | (* action mode *) | |
65 | let action = ref "" | |
66 | ||
67 | (* works with -test but also in "normal" spatch mode *) | |
68 | let compare_with_expected = ref false | |
69 | ||
34e49164 C |
70 | let distrib_index = ref (None : int option) |
71 | let distrib_max = ref (None : int option) | |
72 | let 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 |
80 | let 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 |
125 | let 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 |
171 | work properly *) | |
172 | let 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 |
214 | let 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 |
242 | let 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 |
251 | let 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 *) | |
257 | let short_usage_func = ref (fun () -> ()) | |
258 | let 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 |
270 | let 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 | ||
faf9a90c C |
295 | "-all_includes", |
296 | Arg.Unit (function _ -> FC.include_options := FC.I_ALL_INCLUDES), | |
34e49164 | 297 | " causes all available include files to be used"; |
faf9a90c C |
298 | "-no_includes", |
299 | Arg.Unit (function _ -> FC.include_options := FC.I_NO_INCLUDES), | |
34e49164 | 300 | " causes not even local include files to be used"; |
faf9a90c C |
301 | "-local_includes", |
302 | Arg.Unit (function _ -> FC.include_options := FC.I_NORMAL_INCLUDES), | |
303 | " causes local include files to be used"; | |
90aeb998 C |
304 | "-ignore_unknown_options", Arg.Set ignore_unknown_opt, |
305 | " For integration in a toolchain (must be set before the first unknown option)"; | |
faf9a90c C |
306 | "-include_headers", Arg.Set include_headers, |
307 | " process header files independently"; | |
c3e37e97 C |
308 | "-I", Arg.String (fun x -> |
309 | FC.include_path:= x::!FC.include_path | |
310 | ), | |
b1b2de81 | 311 | " <dir> containing the header files (optional)"; |
34e49164 | 312 | |
978fd7e5 C |
313 | "-preprocess", Arg.Set preprocess, |
314 | " run the C preprocessor before applying the semantic match"; | |
315 | ||
316 | "-c", Arg.Set compat_mode, " gcc/cpp compatibility mode"; | |
34e49164 C |
317 | |
318 | "-dir", Arg.Set dir, | |
319 | " <dir> process all files in directory recursively"; | |
320 | ||
951c7801 | 321 | "-use_glimpse", Arg.Unit (function _ -> Flag.scanner := Flag.Glimpse), |
34e49164 | 322 | " works with -dir, use info generated by glimpseindex"; |
951c7801 C |
323 | "-use_google", Arg.String (function s -> Flag.scanner := Flag.Google s), |
324 | " find relevant files using google code search"; | |
c3e37e97 C |
325 | "-patch", |
326 | Arg.String (function s -> Flag.patch := Some (Cocci.normalize_path s)), | |
34e49164 C |
327 | (" <dir> path name with respect to which a patch should be created\n"^ |
328 | " \"\" for a file in the current directory"); | |
978fd7e5 | 329 | "-kbuild_info", Arg.Set_string kbuild_info, |
34e49164 C |
330 | " <file> improve -dir by grouping related c files"; |
331 | "-pyoutput", Arg.Set_string Flag.pyoutput, | |
332 | " Sets output routine: Standard values: <coccilib.output.Gtk|coccilib.output.Console>"; | |
333 | ||
334 | ||
978fd7e5 | 335 | "-version", Arg.Unit (fun () -> |
7f004419 C |
336 | let withpython = if Pycocci.python_support then "with" else "without" in |
337 | pr2 (spf "spatch version %s %s Python support" Config.version withpython); | |
34e49164 | 338 | exit 0; |
978fd7e5 | 339 | ), |
34e49164 C |
340 | " guess what"; |
341 | ||
978fd7e5 | 342 | "-date", Arg.Unit (fun () -> |
aba5c457 | 343 | pr2 "version: $Date: 2010/06/20 01:46:52 $"; |
34e49164 | 344 | raise (Common.UnixExit 0) |
978fd7e5 | 345 | ), |
34e49164 C |
346 | " guess what"; |
347 | ||
978fd7e5 | 348 | "-shorthelp", Arg.Unit (fun () -> |
34e49164 C |
349 | !short_usage_func(); |
350 | raise (Common.UnixExit 0) | |
978fd7e5 | 351 | ), |
34e49164 | 352 | " see short list of options"; |
978fd7e5 | 353 | "-longhelp", Arg.Unit (fun () -> |
34e49164 C |
354 | !long_usage_func(); |
355 | raise (Common.UnixExit 0) | |
978fd7e5 | 356 | ), |
34e49164 | 357 | " see all the available options in different categories"; |
978fd7e5 | 358 | "-help", Arg.Unit (fun () -> |
34e49164 C |
359 | !long_usage_func(); |
360 | raise (Common.UnixExit 0) | |
361 | ), | |
362 | " "; | |
978fd7e5 | 363 | "--help", Arg.Unit (fun () -> |
34e49164 C |
364 | !long_usage_func(); |
365 | raise (Common.UnixExit 0) | |
366 | ), | |
367 | " "; | |
34e49164 C |
368 | ] |
369 | ||
370 | (* the format is a list of triples: | |
371 | * (title of section * (optional) explanation of sections * option list) | |
372 | *) | |
373 | let other_options = [ | |
978fd7e5 | 374 | "aliases and obsolete options", |
34e49164 | 375 | "", |
978fd7e5 | 376 | [ |
708f4980 C |
377 | "-sp", Arg.Set_string cocci_file, " short option of -sp_file"; |
378 | "-iso", Arg.Set_string Config.std_iso, " short option of -iso_file"; | |
379 | ||
978fd7e5 | 380 | "-cocci_file", Arg.Set_string cocci_file, |
34e49164 | 381 | " <file> the semantic patch file"; |
978fd7e5 | 382 | (* "-c", Arg.Set_string cocci_file, " short option of -sp_file"; *) |
34e49164 C |
383 | ]; |
384 | ||
978fd7e5 | 385 | "most useful show options", |
34e49164 C |
386 | "", |
387 | [ | |
faf9a90c C |
388 | "-show_diff" , Arg.Set FC.show_diff, " "; |
389 | "-no_show_diff" , Arg.Clear FC.show_diff, " "; | |
390 | "-show_flow" , Arg.Set FC.show_flow, " "; | |
391 | (* works in conjunction with -show_ctl_text *) | |
978fd7e5 | 392 | "-ctl_inline_let", |
faf9a90c C |
393 | Arg.Unit |
394 | (function _ -> FC.show_ctl_text := true; FC.inline_let_ctl := true), " "; | |
395 | "-ctl_show_mcodekind", | |
396 | Arg.Unit | |
397 | (function _ -> FC.show_ctl_text := true; FC.show_mcodekind_in_ctl := true), | |
398 | " "; | |
399 | "-show_bindings", Arg.Set FC.show_binding_in_out, " "; | |
400 | "-show_transinfo", Arg.Set Flag.show_transinfo, " "; | |
401 | "-show_misc", Arg.Set Flag.show_misc, " "; | |
34e49164 C |
402 | "-show_trying", Arg.Set Flag.show_trying, |
403 | " show the name of each function being processed"; | |
1be43e12 | 404 | "-show_dependencies", |
faf9a90c C |
405 | Arg.Unit (function _ -> FC.show_dependencies := true; |
406 | FC.show_binding_in_out := true), | |
1be43e12 | 407 | " show the dependencies related to each rule"; |
34e49164 C |
408 | ]; |
409 | ||
978fd7e5 | 410 | "verbose subsystems options", |
34e49164 C |
411 | "", |
412 | [ | |
faf9a90c C |
413 | "-verbose_ctl_engine", |
414 | Arg.Unit (function _ -> | |
415 | Flag_ctl.verbose_ctl_engine := true; FC.show_ctl_text := true) , " "; | |
485bce71 C |
416 | "-verbose_match", Arg.Set Flag_ctl.verbose_match, " "; |
417 | "-verbose_engine", Arg.Set Flag_matcher.debug_engine, " "; | |
418 | "-graphical_trace", Arg.Set Flag_ctl.graphical_trace, " generate a pdf file representing the matching process"; | |
faf9a90c C |
419 | "-gt_without_label", |
420 | Arg.Unit (function _ -> | |
421 | Flag_ctl.graphical_trace := true; Flag_ctl.gt_without_label := true), | |
422 | " remove graph label (requires option -graphical_trace)"; | |
34e49164 | 423 | |
ae4735db C |
424 | "-parse_error_msg", Arg.Set Flag_parsing_c.show_parsing_error, " "; |
425 | "-verbose_parsing", | |
426 | Arg.Unit (fun _ -> Flag_parsing_c.verbose_parsing := true; | |
427 | Flag_parsing_c.show_parsing_error := true), " "; | |
faf9a90c | 428 | "-type_error_msg", Arg.Set Flag_parsing_c.verbose_type, " "; |
34e49164 C |
429 | (* could also use Flag_parsing_c.options_verbose *) |
430 | ]; | |
431 | ||
432 | "other show options", | |
433 | "", | |
434 | [ | |
faf9a90c C |
435 | "-show_c" , Arg.Set FC.show_c, " "; |
436 | "-show_cocci" , Arg.Set FC.show_cocci, " "; | |
437 | "-show_before_fixed_flow" , Arg.Set FC.show_before_fixed_flow, " "; | |
438 | "-show_ctl_tex" , Arg.Set FC.show_ctl_tex, " "; | |
439 | "-show_ctl_text" , Arg.Set FC.show_ctl_text, " "; | |
440 | "-show_SP" , Arg.Set Flag_parsing_cocci.show_SP, " "; | |
34e49164 C |
441 | ]; |
442 | ||
443 | ||
444 | "debug C parsing/unparsing", | |
445 | "", | |
446 | [ | |
447 | "-debug_cpp", Arg.Set Flag_parsing_c.debug_cpp, " "; | |
448 | "-debug_lexer", Arg.Set Flag_parsing_c.debug_lexer , " "; | |
449 | "-debug_etdt", Arg.Set Flag_parsing_c.debug_etdt , " "; | |
450 | "-debug_typedef", Arg.Set Flag_parsing_c.debug_typedef, " "; | |
451 | ||
978fd7e5 | 452 | "-filter_msg", Arg.Set Flag_parsing_c.filter_msg , |
34e49164 | 453 | " filter some cpp message when the macro is a \"known\" cpp construct"; |
978fd7e5 C |
454 | "-filter_define_error", Arg.Set Flag_parsing_c.filter_define_error," "; |
455 | "-filter_msg_define_error", Arg.Set Flag_parsing_c.filter_msg_define_error, | |
708f4980 | 456 | " filter the error msg"; |
978fd7e5 | 457 | "-filter_passed_level", Arg.Set_int Flag_parsing_c.filter_passed_level," "; |
faf9a90c C |
458 | (* debug cfg doesn't seem to have any effect, so drop it as an option *) |
459 | (* "-debug_cfg", Arg.Set Flag_parsing_c.debug_cfg , " "; *) | |
978fd7e5 | 460 | "-debug_unparsing", Arg.Set Flag_parsing_c.debug_unparsing, " "; |
34e49164 C |
461 | |
462 | ]; | |
463 | (* could use Flag_parsing_c.options_debug_with_title instead *) | |
464 | ||
465 | ||
466 | "shortcut for enabling/disabling a set of debugging options at once", | |
467 | "", | |
468 | [ | |
469 | (* todo: other profile ? *) | |
faf9a90c | 470 | "-quiet", Arg.Unit (fun () -> run_profile quiet_profile), " "; |
ae4735db | 471 | "-very_quiet", Arg.Unit (fun () -> run_profile very_quiet_profile), " "; |
faf9a90c C |
472 | "-debug", Arg.Unit (fun () -> run_profile debug_profile), " "; |
473 | "-pad", Arg.Unit (fun () -> run_profile pad_profile), " "; | |
34e49164 C |
474 | |
475 | ]; | |
476 | ||
477 | "bench options", | |
478 | "", | |
479 | [ | |
978fd7e5 | 480 | "-profile", Arg.Unit (function () -> Common.profile := Common.PALL) , |
34e49164 | 481 | " gather timing information about the main coccinelle functions"; |
978fd7e5 | 482 | "-bench", Arg.Int (function x -> Flag_ctl.bench := x), |
34e49164 | 483 | " <level> for profiling the CTL engine"; |
978fd7e5 | 484 | "-timeout", Arg.Int (fun x -> FC.timeout := Some x), |
34e49164 | 485 | " <sec> timeout in seconds"; |
978fd7e5 | 486 | "-steps", Arg.Int (fun x -> Flag_ctl.steps := Some x), |
34e49164 | 487 | " max number of model checking steps per code unit"; |
b1b2de81 C |
488 | "-iso_limit", Arg.Int (fun x -> Flag_parsing_cocci.iso_limit := Some x), |
489 | " max depth of iso application"; | |
490 | "-no_iso_limit", Arg.Unit (fun _ -> Flag_parsing_cocci.iso_limit := None), | |
491 | " disable limit on max depth of iso application"; | |
34e49164 C |
492 | "-track_iso", Arg.Set Flag.track_iso_usage, |
493 | " gather information about isomorphism usage"; | |
494 | "-profile_iso", | |
495 | Arg.Unit | |
496 | (function () -> | |
b1b2de81 C |
497 | Common.profile := |
498 | (*post_engine not included, because it doesn't use isos*) | |
499 | PSOME ["parse cocci";"mysat";"asttoctl2";"pre_engine";"full_engine"]), | |
34e49164 C |
500 | " gather information about the cost of isomorphism usage" |
501 | ]; | |
502 | ||
503 | ||
504 | ||
505 | "change of algorithm options", | |
951c7801 C |
506 | "", |
507 | [ | |
978fd7e5 | 508 | "-popl", Arg.Set FC.popl, |
34e49164 C |
509 | " simplified SmPL, for the popl paper"; |
510 | ||
511 | "-popl_mark_all", | |
512 | Arg.Unit | |
978fd7e5 | 513 | (function _ -> FC.popl := true; Flag_popl.mark_all := true), |
34e49164 C |
514 | " simplified SmPL, for the popl paper"; |
515 | ||
516 | "-popl_keep_all_wits", | |
517 | Arg.Unit | |
978fd7e5 | 518 | (function _ -> FC.popl := true; Flag_popl.keep_all_wits := true), |
34e49164 C |
519 | " simplified SmPL, for the popl paper"; |
520 | ||
faf9a90c C |
521 | "-hrule", Arg.String |
522 | (function s -> | |
523 | Flag.make_hrule := Some s; FC.include_options := FC.I_NO_INCLUDES), | |
34e49164 C |
524 | " semantic patch generation"; |
525 | ||
7f004419 C |
526 | "-keep_comments", Arg.Set Flag_parsing_c.keep_comments, |
527 | " keep comments around removed code"; | |
528 | ||
34e49164 | 529 | "-loop", Arg.Set Flag_ctl.loop_in_src_code, " "; |
978fd7e5 C |
530 | "-no_loops", Arg.Set Flag_parsing_c.no_loops, |
531 | " drop all back edges derived from looping constructs - unsafe"; | |
951c7801 C |
532 | "-no_gotos", Arg.Set Flag_parsing_c.no_gotos, |
533 | " drop all jumps derived from gotos - unsafe"; | |
34e49164 C |
534 | |
535 | "-l1", Arg.Clear Flag_parsing_c.label_strategy_2, " "; | |
978fd7e5 | 536 | "-ifdef_to_if", Arg.Set FC.ifdef_to_if, |
485bce71 | 537 | " convert ifdef to if (experimental)"; |
978fd7e5 | 538 | "-no_ifdef_to_if", Arg.Clear FC.ifdef_to_if, |
708f4980 C |
539 | " convert ifdef to if (experimental)"; |
540 | ||
541 | "-disable_multi_pass", Arg.Set Flag_parsing_c.disable_multi_pass, " "; | |
485bce71 | 542 | |
978fd7e5 | 543 | "-noif0_passing", Arg.Clear Flag_parsing_c.if0_passing, |
34e49164 | 544 | " "; |
978fd7e5 | 545 | "-noadd_typedef_root", Arg.Clear Flag_parsing_c.add_typedef_root, " "; |
34e49164 C |
546 | (* could use Flag_parsing_c.options_algo instead *) |
547 | ||
548 | ||
485bce71 | 549 | "-disallow_nested_exps", Arg.Set Flag_matcher.disallow_nested_exps, |
7f004419 | 550 | " disallow an expresion pattern from matching a term and its subterm"; |
faf9a90c | 551 | "-disable_worth_trying_opt", Arg.Clear FC.worth_trying_opt, |
34e49164 C |
552 | " "; |
553 | "-only_return_is_error_exit", | |
485bce71 | 554 | Arg.Set Flag_matcher.only_return_is_error_exit, |
34e49164 C |
555 | "if this flag is not set, then break and continue are also error exits"; |
556 | (* the following is a hack to make it easier to add code in sgrep-like | |
557 | code, essentially to compensate for the fact that we don't have | |
558 | any way of printing things out *) | |
559 | "-allow_inconsistent_paths", | |
485bce71 | 560 | Arg.Set Flag_matcher.allow_inconsistent_paths, |
708f4980 | 561 | " if this flag is set don't check for inconsistent paths; dangerous"; |
978fd7e5 C |
562 | "-no_safe_expressions", |
563 | Arg.Set Flag_matcher.no_safe_expressions, | |
564 | " make an expression disjunction not prioritise the topmost disjunct"; | |
708f4980 C |
565 | "-int_bits", Arg.Int Flag_parsing_c.set_int_bits, |
566 | " the number of bits in an unsigned int"; | |
567 | "-long_bits", Arg.Int Flag_parsing_c.set_long_bits, | |
568 | " the number of bits in an unsigned long"; | |
569 | "-linux_spacing", Arg.Unit Flag_parsing_c.set_linux_spacing, | |
570 | " spacing of + code follows the conventions of Linux"; | |
571 | "-smpl_spacing", Arg.Unit Flag_parsing_c.set_smpl_spacing, | |
572 | " spacing of + code follows the semantic patch"; | |
ae4735db | 573 | "-D", Arg.String Flag.set_defined_virtual_rules, |
951c7801 | 574 | " indicate that a virtual rule should be considered to be matched"; |
34e49164 C |
575 | ]; |
576 | ||
577 | "misc options", | |
578 | "", | |
579 | [ | |
978fd7e5 | 580 | "-debugger", Arg.Set Common.debugger, |
34e49164 | 581 | " option to set if launch spatch in ocamldebug"; |
978fd7e5 | 582 | "-disable_once", Arg.Set Common.disable_pr2_once, |
34e49164 | 583 | " to print more messages"; |
978fd7e5 | 584 | "-show_trace_profile", Arg.Set Common.show_trace_profile, |
708f4980 | 585 | " show trace"; |
978fd7e5 | 586 | "-save_tmp_files", Arg.Set Common.save_tmp_files, " "; |
34e49164 C |
587 | ]; |
588 | ||
589 | "concurrency", | |
590 | "", | |
591 | [ | |
978fd7e5 | 592 | "-index", Arg.Int (function x -> distrib_index := Some x) , |
34e49164 | 593 | " the processor to use for this run of spatch"; |
978fd7e5 | 594 | "-max", Arg.Int (function x -> distrib_max := Some x) , |
34e49164 C |
595 | " the number of processors available"; |
596 | "-mod_distrib", Arg.Set mod_distrib, | |
597 | " use mod to distribute files among the processors"; | |
598 | ]; | |
599 | ||
600 | "pad options", | |
601 | "", | |
602 | [ | |
978fd7e5 | 603 | "-use_cache", Arg.Set Flag_parsing_c.use_cache, |
34e49164 C |
604 | " use .ast_raw pre-parsed cached C file"; |
605 | (* could use Flag_parsing_c.options_pad instead *) | |
606 | ]; | |
607 | ||
608 | ||
609 | ||
610 | "test mode and test options (works with tests/ or .ok files)", | |
611 | "The test options don't work with the -sp_file and so on.", | |
612 | [ | |
978fd7e5 | 613 | "-test", Arg.Set test_mode, |
34e49164 | 614 | " <file> launch spatch on tests/file.[c,cocci]"; |
978fd7e5 | 615 | "-testall", Arg.Set test_all, |
34e49164 C |
616 | " launch spatch on all files in tests/ having a .res"; |
617 | "-test_okfailed", Arg.Set test_okfailed, | |
618 | " generates .{ok,failed,spatch_ok} files using .res files"; | |
619 | "-test_regression_okfailed", Arg.Set test_regression_okfailed, | |
620 | " process the .{ok,failed,spatch_ok} files in current dir"; | |
621 | ||
978fd7e5 C |
622 | "-compare_with_expected", Arg.Set compare_with_expected, |
623 | " use also file.res"; | |
624 | "-expected_score_file", Arg.Set_string expected_score_file, | |
625 | " which score file to compare with in -testall"; | |
faf9a90c | 626 | "-relax_include_path", Arg.Set FC.relax_include_path, |
34e49164 | 627 | " "; |
34e49164 C |
628 | ]; |
629 | ||
630 | "action mode", | |
631 | ("The action options don't work with the -sp_file and so on." ^ "\n" ^ | |
632 | "It's for the other (internal) uses of the spatch program." | |
633 | ), | |
634 | ||
635 | (* -token_c, -parse_c, etc *) | |
636 | ((Common.options_of_actions action (Test_parsing_c.actions())) ++ | |
637 | [ | |
638 | (let s = "-parse_cocci" in s, Arg.Unit (fun () -> action := s), | |
639 | " <file>"); | |
640 | (let s = "-compare_c" in s, Arg.Unit (fun () -> action := s), | |
641 | " <file1> <file2>"); | |
642 | ]); | |
643 | ] | |
644 | ||
645 | ||
978fd7e5 | 646 | let all_options = |
34e49164 | 647 | short_options ++ List.concat (List.map Common.thd3 other_options) |
978fd7e5 | 648 | |
34e49164 C |
649 | (* I don't want the -help and --help that are appended by Arg.align *) |
650 | let arg_align2 xs = | |
651 | Arg.align xs +> List.rev +> Common.drop 2 +> List.rev | |
652 | ||
90aeb998 C |
653 | (* |
654 | Ignore unknown option | |
655 | ||
656 | This simplifies the integration of Coccinelle in toolchain. For | |
657 | instance, spatch can then be used as a checker in the Linux build | |
658 | system. | |
659 | ||
660 | *) | |
661 | let check_include_path () = | |
662 | let opt = Array.get Sys.argv !Arg.current in | |
663 | let is_include_re = Str.regexp "-I\\(.*\\)" in | |
664 | if Str.string_match is_include_re opt 0 then | |
665 | let path = Str.matched_group 1 opt in | |
666 | FC.include_path:= path::!FC.include_path | |
667 | else () | |
668 | ||
669 | let rec arg_parse_no_fail l f msg = | |
670 | try | |
671 | check_include_path (); | |
672 | Arg.parse_argv Sys.argv l f msg; | |
673 | with | |
674 | | Arg.Bad emsg -> | |
675 | arg_parse_no_fail l f msg | |
676 | | Arg.Help msg -> (* printf "%s" msg; exit 0; *) | |
677 | raise Impossible (* -help is specified in speclist *) | |
678 | ||
34e49164 C |
679 | (* copy paste of Arg.parse. Don't want the default -help msg *) |
680 | let arg_parse2 l f msg = | |
681 | (try | |
682 | Arg.parse_argv Sys.argv l f msg; | |
683 | with | |
90aeb998 C |
684 | | Arg.Bad emsg -> (* eprintf "%s" msg; exit 2; *) |
685 | if not !ignore_unknown_opt then | |
686 | begin | |
687 | let xs = Common.lines emsg in | |
688 | (* take only head, it's where the error msg is *) | |
689 | pr2 (List.hd xs); | |
690 | !short_usage_func(); | |
691 | raise (Common.UnixExit (2)) | |
692 | end | |
693 | else | |
694 | arg_parse_no_fail l f msg; | |
34e49164 C |
695 | | Arg.Help msg -> (* printf "%s" msg; exit 0; *) |
696 | raise Impossible (* -help is specified in speclist *) | |
697 | ) | |
698 | ||
699 | ||
700 | let short_usage () = | |
701 | begin | |
978fd7e5 | 702 | Common.short_usage usage_msg short_options; |
34e49164 C |
703 | pr2 ""; |
704 | pr2 "Example of use:"; | |
705 | pr2 " ./spatch -sp_file foo.cocci foo.c -o /tmp/newfoo.c"; | |
706 | pr2 ""; | |
707 | end | |
708 | ||
709 | ||
978fd7e5 | 710 | let long_usage () = |
34e49164 C |
711 | Common.long_usage usage_msg short_options other_options |
712 | ||
713 | let _ = short_usage_func := short_usage | |
714 | let _ = long_usage_func := long_usage | |
715 | ||
716 | (*****************************************************************************) | |
717 | (* Helpers *) | |
718 | (*****************************************************************************) | |
719 | ||
720 | let adjust_stdin cfile k = | |
721 | if !dir | |
722 | then k() | |
723 | else | |
978fd7e5 | 724 | let newin = |
485bce71 C |
725 | try |
726 | let (dir, base, ext) = Common.dbe_of_filename cfile in | |
727 | let varfile = Common.filename_of_dbe (dir, base, "var") in | |
b1b2de81 | 728 | if ext =$= "c" && Common.lfile_exists varfile |
485bce71 | 729 | then Some varfile |
978fd7e5 | 730 | else None |
485bce71 C |
731 | with Invalid_argument("Filename.chop_extension") -> None |
732 | in | |
34e49164 C |
733 | Common.redirect_stdin_opt newin k |
734 | ||
978fd7e5 | 735 | let glimpse_filter (coccifile, isofile) dir = |
faf9a90c | 736 | let (_metavars,astcocci,_free_var_lists,_negated_positions, |
7f004419 | 737 | _used_after_lists,_positions_lists,_,query) = |
34e49164 C |
738 | Cocci.sp_of_file coccifile (Some isofile) in |
739 | match query with | |
faf9a90c | 740 | None -> pr2 "no glimpse keyword inferred from snippet"; None |
951c7801 C |
741 | | Some [query] -> |
742 | (let suffixes = if !include_headers then ["c";"h"] else ["c"] in | |
5636bb2c | 743 | Printf.fprintf stderr "%s\n" ("glimpse request = " ^ query); |
34e49164 C |
744 | let command = spf "glimpse -y -H %s -N -W -w '%s'" dir query in |
745 | let (glimpse_res,stat) = Common.cmd_to_list_and_status command in | |
746 | match stat with | |
747 | Unix.WEXITED(0) | Unix.WEXITED(1) -> | |
748 | Some | |
749 | (glimpse_res +> | |
750 | List.filter | |
751 | (fun file -> List.mem (Common.filesuffix file) suffixes)) | |
951c7801 C |
752 | | _ -> None (* error, eg due to pattern too big *)) |
753 | | _ -> failwith "not possible" | |
34e49164 | 754 | |
34e49164 | 755 | (*****************************************************************************) |
b1b2de81 | 756 | (* Main action *) |
34e49164 | 757 | (*****************************************************************************) |
34e49164 | 758 | |
978fd7e5 | 759 | let main_action xs = |
b1b2de81 C |
760 | match xs with |
761 | | x::xs -> | |
708f4980 | 762 | |
978fd7e5 C |
763 | (* a more general solution would be to use |
764 | * Common.files_of_dir_or_files (x::xs) | |
708f4980 C |
765 | * as some elements in xs may also be directories, or individual |
766 | * files. | |
767 | *) | |
768 | if Common.is_directory x | |
769 | then dir := true; | |
770 | ||
771 | adjust_stdin x (fun () -> | |
772 | if !cocci_file =$= "" | |
773 | then failwith "I need a cocci file, use -sp_file <file>"; | |
774 | ||
775 | if !dir && !Flag.patch =*= None | |
776 | then | |
777 | (match xs with | |
c3e37e97 | 778 | | [] -> Flag.patch := Some (Cocci.normalize_path x) |
708f4980 C |
779 | | _ -> |
780 | pr2 | |
781 | ("warning: patch output can only be created when only one\n"^ | |
782 | "directory is specified or when the -patch flag is used") | |
783 | ); | |
aba5c457 | 784 | Flag.dir := x; |
708f4980 | 785 | |
978fd7e5 C |
786 | let infiles = |
787 | Common.profile_code "Main.infiles computation" (fun () -> | |
951c7801 | 788 | match !dir, !kbuild_info, !Flag.scanner = Flag.Glimpse with |
708f4980 | 789 | (* glimpse *) |
978fd7e5 | 790 | | false, _, true -> |
708f4980 | 791 | failwith "-use_glimpse works only with -dir" |
978fd7e5 | 792 | | true, s, true when s <> "" -> |
708f4980 | 793 | failwith "-use_glimpse does not work with -kbuild" |
978fd7e5 | 794 | | true, "", true -> |
708f4980 C |
795 | if not (null xs) |
796 | then failwith "-use_glimpse can accept only one dir"; | |
797 | ||
708f4980 C |
798 | let files = |
799 | match glimpse_filter (!cocci_file, !Config.std_iso) x with | |
800 | None -> | |
801 | Common.cmd_to_list (* same as "true, "", _" case *) | |
802 | (if !include_headers | |
803 | (* FIXME : Could we remove xs ? | |
804 | -use_glimpse requires a singleton. | |
805 | This is checked some lines before. | |
806 | then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"") | |
807 | else ("find "^(join " " (x::xs))^" -name \"*.c\"")) | |
808 | *) | |
809 | then ("find "^ x ^" -name \"*.[ch]\"") | |
810 | else ("find "^ x ^" -name \"*.c\"")) | |
811 | | Some files -> files in | |
812 | files +> List.map (fun x -> [x]) | |
813 | (* normal *) | |
814 | | false, _, _ -> [x::xs] | |
978fd7e5 | 815 | | true, "", _ -> |
708f4980 C |
816 | Common.cmd_to_list |
817 | (if !include_headers | |
818 | then ("find "^(join " " (x::xs))^" -name \"*.[ch]\"") | |
34e49164 | 819 | else ("find "^(join " " (x::xs))^" -name \"*.c\"")) |
708f4980 C |
820 | +> List.map (fun x -> [x]) |
821 | ||
822 | (* kbuild *) | |
978fd7e5 C |
823 | | true, kbuild_info_file,_ -> |
824 | let dirs = | |
825 | Common.cmd_to_list ("find "^(join " " (x::xs))^" -type d") | |
708f4980 C |
826 | in |
827 | let info = Kbuild.parse_kbuild_info kbuild_info_file in | |
828 | let groups = Kbuild.files_in_dirs dirs info in | |
978fd7e5 | 829 | |
708f4980 C |
830 | groups +> List.map (function Kbuild.Group xs -> xs) |
831 | ) | |
832 | in | |
833 | ||
834 | let infiles = | |
835 | match (!distrib_index,!distrib_max) with | |
836 | (None,None) -> infiles | |
837 | | (Some index,Some max) -> | |
838 | (if index >= max | |
839 | then | |
840 | failwith "index starts at 0, and so must be less than max"); | |
841 | if !mod_distrib | |
842 | then | |
843 | let rec loop ct = function | |
844 | [] -> [] | |
845 | | x::xs -> | |
846 | if (ct mod max) =|= index | |
847 | then x::(loop (ct+1) xs) | |
848 | else loop (ct+1) xs in | |
849 | loop 0 infiles | |
850 | else | |
851 | begin | |
852 | let all_files = List.length infiles in | |
853 | let regions = (all_files + (max - 1)) / max in | |
854 | let this_min = index * regions in | |
855 | let this_max = (index+1) * regions in | |
34e49164 | 856 | let rec loop ct = function |
708f4980 | 857 | [] -> [] |
34e49164 | 858 | | x::xs -> |
708f4980 | 859 | if this_min <= ct && ct < this_max |
34e49164 C |
860 | then x::(loop (ct+1) xs) |
861 | else loop (ct+1) xs in | |
862 | loop 0 infiles | |
708f4980 C |
863 | end |
864 | | _ -> failwith "inconsistent distribution information" in | |
978fd7e5 C |
865 | |
866 | let outfiles = | |
708f4980 C |
867 | Common.profile_code "Main.outfiles computation" (fun () -> |
868 | let cocci_infos = | |
869 | Cocci.pre_engine (!cocci_file, !Config.std_iso) in | |
870 | let res = | |
978fd7e5 | 871 | infiles +> List.map (fun cfiles -> |
708f4980 | 872 | pr2 ("HANDLING: " ^ (join " " cfiles)); |
978fd7e5 C |
873 | Common.timeout_function_opt !FC.timeout (fun () -> |
874 | Common.report_if_take_time 10 (join " " cfiles) (fun () -> | |
fc1ad971 | 875 | (*let s = profile_diagnostic() in*) |
34e49164 | 876 | (* Unix.sleep 1; *) |
978fd7e5 C |
877 | try |
878 | let optfile = | |
879 | if !output_file <> "" && !compat_mode then | |
880 | Some !output_file | |
881 | else | |
882 | None | |
883 | in | |
884 | Common.redirect_stdout_opt optfile (fun () -> | |
885 | (* this is the main call *) | |
886 | Cocci.full_engine cocci_infos cfiles | |
887 | ) | |
888 | with | |
889 | | Common.UnixExit x -> raise (Common.UnixExit x) | |
890 | | e -> | |
fc1ad971 C |
891 | (*pr2 "previous"; |
892 | pr2 s; | |
893 | pr2 "new"; | |
894 | pr2(profile_diagnostic());*) | |
978fd7e5 C |
895 | if !dir |
896 | then begin | |
897 | pr2 ("EXN:" ^ Printexc.to_string e); | |
898 | [] (* *) | |
899 | end | |
900 | else raise e))) in | |
901 | Cocci.post_engine cocci_infos; | |
902 | res | |
903 | ) +> List.concat | |
708f4980 C |
904 | in |
905 | ||
978fd7e5 | 906 | Common.profile_code "Main.result analysis" (fun () -> |
708f4980 | 907 | Ctlcocci_integration.print_bench(); |
708f4980 | 908 | let outfiles = Cocci.check_duplicate_modif outfiles in |
978fd7e5 C |
909 | outfiles +> List.iter (fun (infile, outopt) -> |
910 | outopt +> Common.do_option (fun outfile -> | |
708f4980 C |
911 | if !inplace_modif |
912 | then begin | |
5636bb2c C |
913 | (match !backup_suffix with |
914 | Some backup_suffix -> | |
915 | Common.command2 ("cp "^infile^" "^infile^backup_suffix) | |
916 | | None -> ()); | |
708f4980 C |
917 | Common.command2 ("cp "^outfile^" "^infile); |
918 | end; | |
919 | ||
920 | if !outplace_modif | |
921 | then Common.command2 ("cp "^outfile^" "^infile^".cocci_res"); | |
978fd7e5 C |
922 | |
923 | (* potential source of security pb if the /tmp/ file is | |
924 | * a symlink, so simpler to not produce any regular file | |
925 | * (files created by Common.new_temp_file are still ok) | |
926 | * anymore in /tmp. | |
927 | *) | |
928 | (* | |
929 | if !output_file =$= "" | |
930 | then begin | |
931 | let tmpfile = "/tmp/"^Common.basename infile in | |
932 | pr2 (spf "One file modified. Result is here: %s" tmpfile); | |
933 | Common.command2 ("cp "^outfile^" "^tmpfile); | |
934 | end | |
935 | *) | |
708f4980 | 936 | )); |
978fd7e5 C |
937 | if !output_file <> "" && not !compat_mode then |
938 | (match outfiles with | |
939 | | [infile, Some outfile] when infile =$= x && null xs -> | |
708f4980 | 940 | Common.command2 ("cp " ^outfile^ " " ^ !output_file); |
978fd7e5 | 941 | | [infile, None] when infile =$= x && null xs -> |
708f4980 | 942 | Common.command2 ("cp " ^infile^ " " ^ !output_file); |
978fd7e5 C |
943 | | _ -> |
944 | failwith | |
708f4980 C |
945 | ("-o can not be applied because there is multiple " ^ |
946 | "modified files"); | |
947 | ); | |
708f4980 C |
948 | if !compare_with_expected |
949 | then Testing.compare_with_expected outfiles)) | |
b1b2de81 C |
950 | |
951 | | [] -> raise Impossible | |
952 | ||
953 | ||
954 | (*****************************************************************************) | |
955 | (* The coccinelle main entry point *) | |
956 | (*****************************************************************************) | |
978fd7e5 | 957 | let main () = |
b1b2de81 C |
958 | begin |
959 | let arglist = Array.to_list Sys.argv in | |
960 | ||
961 | if not (null (Common.inter_set arglist | |
002099fc | 962 | ["-cocci_file";"-sp_file";"-sp";"-test";"-testall"; |
b1b2de81 C |
963 | "-test_okfailed";"-test_regression_okfailed"])) |
964 | then run_profile quiet_profile; | |
965 | ||
966 | let args = ref [] in | |
967 | ||
708f4980 C |
968 | (* Gc.set {(Gc.get ()) with Gc.stack_limit = 1024 * 1024};*) |
969 | ||
b1b2de81 C |
970 | (* this call can set up many global flag variables via the cmd line *) |
971 | arg_parse2 (Arg.align all_options) (fun x -> args := x::!args) usage_msg; | |
972 | ||
708f4980 C |
973 | (* julia hack so that one can override directories specified on |
974 | * the command line. *) | |
b1b2de81 C |
975 | (if !dir |
976 | then | |
977 | let chosen_dir = | |
978 | if List.length !args > 1 | |
979 | then | |
980 | begin | |
981 | let chosen = List.hd !args in | |
aba5c457 C |
982 | Flag.dir := chosen; |
983 | pr2 ("ignoring all but the last specified directory: "^chosen); | |
984 | args := [chosen]; | |
985 | chosen | |
b1b2de81 C |
986 | end |
987 | else List.hd !args in | |
c3e37e97 C |
988 | if !FC.include_path =*= [] |
989 | then FC.include_path := [Filename.concat chosen_dir "include"]); | |
708f4980 | 990 | |
b1b2de81 C |
991 | args := List.rev !args; |
992 | ||
993 | if !cocci_file <> "" && (not (!cocci_file =~ ".*\\.\\(sgrep\\|spatch\\)$")) | |
994 | then cocci_file := Common.adjust_ext_if_needed !cocci_file ".cocci"; | |
995 | ||
978fd7e5 | 996 | if !Config.std_iso <> "" |
b1b2de81 | 997 | then Config.std_iso := Common.adjust_ext_if_needed !Config.std_iso ".iso"; |
978fd7e5 | 998 | if !Config.std_h <> "" |
b1b2de81 C |
999 | then Config.std_h := Common.adjust_ext_if_needed !Config.std_h ".h"; |
1000 | ||
978fd7e5 | 1001 | if !Config.std_h <> "" |
708f4980 C |
1002 | then Parse_c.init_defs_builtins !Config.std_h; |
1003 | ||
978fd7e5 | 1004 | if !macro_file <> "" |
708f4980 | 1005 | then Parse_c.init_defs_macros !macro_file; |
b1b2de81 C |
1006 | |
1007 | ||
1008 | (* must be done after Arg.parse, because Common.profile is set by it *) | |
978fd7e5 | 1009 | Common.profile_code "Main total" (fun () -> |
b1b2de81 C |
1010 | |
1011 | ||
1012 | let all_actions = Test_parsing_c.actions() in | |
1013 | ||
1014 | (match (!args) with | |
1015 | ||
1016 | (* --------------------------------------------------------- *) | |
1017 | (* The test framework. Works with tests/ or .ok and .failed *) | |
1018 | (* --------------------------------------------------------- *) | |
978fd7e5 | 1019 | | [x] when !test_mode -> |
c3e37e97 | 1020 | begin |
aa721442 | 1021 | let prefix = "tests/" in |
5636bb2c C |
1022 | let testfile = x ^ ".cocci" in |
1023 | if Sys.file_exists (prefix ^ testfile) then | |
aa721442 | 1024 | begin |
5636bb2c C |
1025 | FC.include_path := [prefix^"include"]; |
1026 | Testing.testone prefix x !compare_with_expected | |
aa721442 C |
1027 | end |
1028 | else | |
5636bb2c C |
1029 | if Sys.file_exists testfile then |
1030 | begin | |
1031 | FC.include_path := ["include"]; | |
1032 | Testing.testone "" x !compare_with_expected | |
1033 | end | |
1034 | else | |
1035 | pr2 (spf "ERROR: File %s does not exist" testfile) | |
c3e37e97 | 1036 | end |
b1b2de81 | 1037 | |
978fd7e5 | 1038 | | [] when !test_all -> |
c3e37e97 | 1039 | FC.include_path := ["tests/include"]; |
708f4980 C |
1040 | if !expected_score_file <> "" |
1041 | then Testing.testall ~expected_score_file:!expected_score_file () | |
1042 | else Testing.testall () | |
b1b2de81 | 1043 | |
978fd7e5 | 1044 | | [] when !test_regression_okfailed -> |
b1b2de81 C |
1045 | Testing.test_regression_okfailed () |
1046 | ||
978fd7e5 | 1047 | | x::xs when !test_okfailed -> |
b1b2de81 C |
1048 | (* do its own timeout on FC.timeout internally *) |
1049 | FC.relax_include_path := true; | |
978fd7e5 | 1050 | adjust_stdin x (fun () -> |
b1b2de81 C |
1051 | Testing.test_okfailed !cocci_file (x::xs) |
1052 | ) | |
1053 | ||
1054 | (* --------------------------------------------------------- *) | |
1055 | (* Actions, useful to debug subpart of coccinelle *) | |
1056 | (* --------------------------------------------------------- *) | |
1057 | ||
1058 | | xs when List.mem !action (Common.action_list all_actions) -> | |
1059 | Common.do_action !action xs all_actions | |
1060 | ||
978fd7e5 | 1061 | | [file] when !action =$= "-parse_cocci" -> |
b1b2de81 C |
1062 | Testing.test_parse_cocci file |
1063 | ||
1064 | (* I think this is used by some scripts in some Makefile for our | |
1065 | * big-tests. So dont remove. | |
1066 | *) | |
978fd7e5 | 1067 | | [file1;file2] when !action =$= "-compare_c" -> |
b1b2de81 C |
1068 | Test_parsing_c.test_compare_c file1 file2 (* result = unix code *) |
1069 | ||
1070 | (* could add the Test_parsing_c.test_actions such as -parse_c & co *) | |
1071 | ||
1072 | ||
1073 | (* --------------------------------------------------------- *) | |
1074 | (* This is the main entry *) | |
1075 | (* --------------------------------------------------------- *) | |
fc1ad971 | 1076 | | x::xs -> main_action (x::xs) |
b1b2de81 | 1077 | |
34e49164 C |
1078 | (* --------------------------------------------------------- *) |
1079 | (* empty entry *) | |
1080 | (* --------------------------------------------------------- *) | |
1081 | | [] -> short_usage() | |
34e49164 C |
1082 | )); |
1083 | if !Pycocci.initialised && (Pycocci.py_isinitialized ()) != 0 then begin | |
1084 | ignore(Pycocci.pyrun_simplestring "cocci.finalise()"); | |
978fd7e5 | 1085 | if !Flag.show_misc |
34e49164 C |
1086 | then Common.pr2 "Finalizing python\n"; |
1087 | Pycocci.py_finalize (); | |
1088 | end | |
1089 | end | |
1090 | ||
708f4980 | 1091 | |
978fd7e5 C |
1092 | let main_with_better_error_report () = |
1093 | if !Common.debugger then main () | |
1094 | else | |
1095 | try | |
1096 | main () | |
708f4980 | 1097 | with |
aa721442 C |
1098 | | Unix.Unix_error (e, "stat", filename) -> |
1099 | pr2 | |
1100 | (spf "ERROR: File %s does not exist: %s" | |
1101 | filename (Unix.error_message e)); | |
708f4980 | 1102 | raise (UnixExit (-1)) |
708f4980 | 1103 | |
34e49164 | 1104 | (*****************************************************************************) |
708f4980 | 1105 | let start = |
978fd7e5 | 1106 | Common.main_boilerplate (fun () -> |
708f4980 | 1107 | main_with_better_error_report (); |
34e49164 C |
1108 | Ctlcocci_integration.print_bench(); |
1109 | ) |