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