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