2 * Copyright 2005-2009, 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.
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.
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.
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/>.
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
25 module CCI
= Ctlcocci_integration
26 module TAC
= Type_annoter_c
28 (*****************************************************************************)
29 (* This file is a kind of driver. It gathers all the important functions
30 * from coccinelle in one place. The different entities in coccinelle are:
34 * - flow (contain nodes)
35 * - ctl (contain rule_elems)
36 * This file contains functions to transform one in another.
38 (*****************************************************************************)
40 (* --------------------------------------------------------------------- *)
42 (* --------------------------------------------------------------------- *)
43 let cprogram_of_file file
=
44 let (program2
, _stat
) = Parse_c.parse_print_error_heuristic file
in
47 let cprogram_of_file_cached file
=
48 let (program2
, _stat
) = Parse_c.parse_cache file
in
49 if !Flag_cocci.ifdef_to_if
51 program2
+> Parse_c.with_program2
(fun asts
->
52 Cpp_ast_c.cpp_ifdef_statementize asts
56 let cfile_of_program program2_with_ppmethod outf
=
57 Unparse_c.pp_program program2_with_ppmethod outf
59 (* for memoization, contains only one entry, the one for the SP *)
60 let _hparse = Hashtbl.create
101
61 let _hctl = Hashtbl.create
101
63 (* --------------------------------------------------------------------- *)
65 (* --------------------------------------------------------------------- *)
66 let sp_of_file2 file iso
=
67 Common.memoized
_hparse (file
, iso
) (fun () ->
68 Parse_cocci.process file iso
false)
69 let sp_of_file file iso
=
70 Common.profile_code
"parse cocci" (fun () -> sp_of_file2 file iso
)
73 (* --------------------------------------------------------------------- *)
75 (* --------------------------------------------------------------------- *)
77 Ograph_extended.print_ograph_mutable flow
"/tmp/test.dot" true
80 let ast_to_flow_with_error_messages2 x
=
82 try Ast_to_flow.ast_to_control_flow x
83 with Ast_to_flow.Error x
->
84 Ast_to_flow.report_error x
;
87 flowopt +> do_option
(fun flow
->
88 (* This time even if there is a deadcode, we still have a
89 * flow graph, so I can try the transformation and hope the
90 * deadcode will not bother us.
92 try Ast_to_flow.deadcode_detection flow
93 with Ast_to_flow.Error
(Ast_to_flow.DeadCode x
) ->
94 Ast_to_flow.report_error
(Ast_to_flow.DeadCode x
);
97 let ast_to_flow_with_error_messages a
=
98 Common.profile_code
"flow" (fun () -> ast_to_flow_with_error_messages2 a
)
101 (* --------------------------------------------------------------------- *)
103 (* --------------------------------------------------------------------- *)
105 let ctls_of_ast2 ast ua pos
=
107 (function ast
-> function (ua
,pos
) ->
110 then failwith
"no popl here" (* Popl.popl ast *)
111 else Asttoctl2.asttoctl ast ua pos
)
112 (Asttomember.asttomember ast ua
))
113 ast
(List.combine ua pos
)
115 let ctls_of_ast ast ua
=
116 Common.profile_code
"asttoctl2" (fun () -> ctls_of_ast2 ast ua
)
118 (*****************************************************************************)
119 (* Some debugging functions *)
120 (*****************************************************************************)
124 let show_or_not_cfile2 cfile
=
125 if !Flag_cocci.show_c
then begin
126 Common.pr2_xxxxxxxxxxxxxxxxx
();
127 pr2
("processing C file: " ^ cfile
);
128 Common.pr2_xxxxxxxxxxxxxxxxx
();
129 Common.command2
("cat " ^ cfile
);
131 let show_or_not_cfile a
=
132 Common.profile_code
"show_xxx" (fun () -> show_or_not_cfile2 a
)
134 let show_or_not_cfiles cfiles
= List.iter
show_or_not_cfile cfiles
137 let show_or_not_cocci2 coccifile isofile
=
138 if !Flag_cocci.show_cocci
then begin
139 Common.pr2_xxxxxxxxxxxxxxxxx
();
140 pr2
("processing semantic patch file: " ^ coccifile
);
141 isofile
+> (fun s
-> pr2
("with isos from: " ^ s
));
142 Common.pr2_xxxxxxxxxxxxxxxxx
();
143 Common.command2
("cat " ^ coccifile
);
146 let show_or_not_cocci a b
=
147 Common.profile_code
"show_xxx" (fun () -> show_or_not_cocci2 a b
)
151 let show_or_not_diff2 cfile outfile show_only_minus
=
152 if !Flag_cocci.show_diff
then begin
153 match Common.fst
(Compare_c.compare_default cfile outfile
) with
154 Compare_c.Correct
-> () (* diff only in spacing, etc *)
156 (* may need --strip-trailing-cr under windows *)
160 match !Flag_parsing_c.diff_lines
with
161 | None
-> "diff -u -p " ^ cfile ^
" " ^ outfile
162 | Some n
-> "diff -U "^n^
" -p "^cfile^
" "^outfile
in
164 let res = Common.cmd_to_list
line in
165 match (!Flag.patch
,res) with
166 (* create something that looks like the output of patch *)
167 (Some prefix
,minus_file
::plus_file
::rest
) ->
168 let drop_prefix file
=
172 let lp = String.length prefix
in
173 String.sub file
lp ((String.length file
) - lp) in
175 match List.rev
(Str.split
(Str.regexp
" ") line) with
176 new_file
::old_file
::cmdrev
->
180 (List.rev
("/tmp/nothing" :: old_file
:: cmdrev
))
182 let old_base_file = drop_prefix old_file
in
185 (("b"^
old_base_file)::("a"^
old_base_file)::cmdrev
))
186 | _
-> failwith
"bad command" in
187 let (minus_line
,plus_line
) =
189 then (minus_file
,plus_file
)
191 match (Str.split
(Str.regexp
"[ \t]") minus_file
,
192 Str.split
(Str.regexp
"[ \t]") plus_file
) with
193 ("---"::old_file
::old_rest
,"+++"::new_file
::new_rest
) ->
194 let old_base_file = drop_prefix old_file
in
196 ("---"::("a"^
old_base_file)::old_rest
),
198 ("+++"::("b"^
old_base_file)::new_rest
))
201 (Printf.sprintf
"bad diff header lines: %s %s"
202 (String.concat
":" l1
) (String.concat
":" l2
)) in
203 diff_line::minus_line
::plus_line
::rest
205 xs +> List.iter
(fun s
->
206 if s
=~
"^\\+" && show_only_minus
210 let show_or_not_diff a b c
=
211 Common.profile_code
"show_xxx" (fun () -> show_or_not_diff2 a b c
)
214 (* the derived input *)
216 let show_or_not_ctl_tex2 astcocci ctls
=
217 if !Flag_cocci.show_ctl_tex
then begin
218 Ctltotex.totex
("/tmp/__cocci_ctl.tex") astcocci ctls
;
219 Common.command2
("cd /tmp; latex __cocci_ctl.tex; " ^
220 "dvips __cocci_ctl.dvi -o __cocci_ctl.ps;" ^
221 "gv __cocci_ctl.ps &");
223 let show_or_not_ctl_tex a b
=
224 Common.profile_code
"show_xxx" (fun () -> show_or_not_ctl_tex2 a b
)
228 let show_or_not_rule_name ast rulenb
=
229 if !Flag_cocci.show_ctl_text
or !Flag.show_trying
or
230 !Flag.show_transinfo
or !Flag_cocci.show_binding_in_out
235 Ast_cocci.CocciRule
(nm
, (deps
, drops
, exists
), x
, _
, _
) -> nm
236 | _
-> i_to_s rulenb
in
237 Common.pr_xxxxxxxxxxxxxxxxx
();
239 Common.pr_xxxxxxxxxxxxxxxxx
()
242 let show_or_not_scr_rule_name rulenb
=
243 if !Flag_cocci.show_ctl_text
or !Flag.show_trying
or
244 !Flag.show_transinfo
or !Flag_cocci.show_binding_in_out
247 let name = i_to_s rulenb
in
248 Common.pr_xxxxxxxxxxxxxxxxx
();
249 pr
("script rule " ^
name ^
" = ");
250 Common.pr_xxxxxxxxxxxxxxxxx
()
253 let show_or_not_ctl_text2 ctl ast rulenb
=
254 if !Flag_cocci.show_ctl_text
then begin
256 adjust_pp_with_indent
(fun () ->
257 Format.force_newline
();
258 Pretty_print_cocci.print_plus_flag
:= true;
259 Pretty_print_cocci.print_minus_flag
:= true;
260 Pretty_print_cocci.unparse ast
;
265 adjust_pp_with_indent
(fun () ->
266 Format.force_newline
();
267 Pretty_print_engine.pp_ctlcocci
268 !Flag_cocci.show_mcodekind_in_ctl
!Flag_cocci.inline_let_ctl ctl
;
272 let show_or_not_ctl_text a b c
=
273 Common.profile_code
"show_xxx" (fun () -> show_or_not_ctl_text2 a b c
)
277 (* running information *)
278 let get_celem celem
: string =
280 Ast_c.Definition
({Ast_c.f_name
= namefuncs
;},_
) ->
281 Ast_c.str_of_name namefuncs
283 (Ast_c.DeclList
([{Ast_c.v_namei
= Some
(name, _
);}, _
], _
)) ->
284 Ast_c.str_of_name
name
287 let show_or_not_celem2 prelude celem
=
290 | Ast_c.Definition
({Ast_c.f_name
= namefuncs
},_
) ->
291 let funcs = Ast_c.str_of_name namefuncs
in
292 Flag.current_element
:= funcs;
293 (" function: ",funcs)
295 (Ast_c.DeclList
([{Ast_c.v_namei
= Some
(name,_
)}, _
], _
)) ->
296 let s = Ast_c.str_of_name
name in
297 Flag.current_element
:= s;
300 Flag.current_element
:= "something_else";
301 (" ","something else");
303 if !Flag.show_trying
then pr2
(prelude ^ tag ^ trying
)
305 let show_or_not_celem a b
=
306 Common.profile_code
"show_xxx" (fun () -> show_or_not_celem2 a b
)
309 let show_or_not_trans_info2 trans_info
=
310 if !Flag.show_transinfo
then begin
311 if null trans_info
then pr2
"transformation info is empty"
313 pr2
"transformation info returned:";
315 List.sort
(function (i1
,_
,_
) -> function (i2
,_
,_
) -> compare i1 i2
)
319 trans_info +> List.iter
(fun (i
, subst
, re
) ->
320 pr2
("transform state: " ^
(Common.i_to_s i
));
322 adjust_pp_with_indent_and_header
"with rule_elem: " (fun () ->
323 Pretty_print_cocci.print_plus_flag
:= true;
324 Pretty_print_cocci.print_minus_flag
:= true;
325 Pretty_print_cocci.rule_elem
"" re
;
327 adjust_pp_with_indent_and_header
"with binding: " (fun () ->
328 Pretty_print_engine.pp_binding subst
;
335 let show_or_not_trans_info a
=
336 Common.profile_code
"show_xxx" (fun () -> show_or_not_trans_info2 a
)
340 let show_or_not_binding2 s binding
=
341 if !Flag_cocci.show_binding_in_out
then begin
342 adjust_pp_with_indent_and_header
("binding " ^
s ^
" = ") (fun () ->
343 Pretty_print_engine.pp_binding binding
346 let show_or_not_binding a b
=
347 Common.profile_code
"show_xxx" (fun () -> show_or_not_binding2 a b
)
351 (*****************************************************************************)
352 (* Some helper functions *)
353 (*****************************************************************************)
355 let worth_trying cfiles tokens
=
356 (* drop the following line for a list of list by rules. since we don't
357 allow multiple minirules, all the tokens within a rule should be in
358 a single CFG entity *)
359 let tokens = Common.union_all
tokens in
360 if not
!Flag_cocci.windows
&& not
(null
tokens)
362 (* could also modify the code in get_constants.ml *)
363 let tokens = tokens +> List.map
(fun s ->
365 | _
when s =~
"^[A-Za-z_][A-Za-z_0-9]*$" ->
368 | _
when s =~
"^[A-Za-z_]" ->
371 | _
when s =~
".*[A-Za-z_]$" ->
376 let com = sprintf
"egrep -q '(%s)' %s" (join
"|" tokens) (join
" " cfiles
)
378 (match Sys.command
com with
379 | 0 (* success *) -> true
382 then Printf.printf
"grep failed: %s\n" com);
383 false (* no match, so not worth trying *)
387 let check_macro_in_sp_and_adjust tokens =
388 let tokens = Common.union_all
tokens in
389 tokens +> List.iter
(fun s ->
390 if Hashtbl.mem
!Parsing_hacks._defs
s
392 pr2
"warning: macro in semantic patch was in macro definitions";
393 pr2
("disabling macro expansion for " ^
s);
394 Hashtbl.remove
!Parsing_hacks._defs
s
399 let contain_loop gopt
=
402 g#nodes#tolist
+> List.exists
(fun (xi
, node
) ->
403 Control_flow_c.extract_is_loop node
405 | None
-> true (* means nothing, if no g then will not model check *)
409 let sp_contain_typed_metavar_z toplevel_list_list
=
410 let bind x y
= x
or y
in
411 let option_default = false in
412 let mcode _ _
= option_default in
413 let donothing r k e
= k e
in
415 let expression r k e
=
416 match Ast_cocci.unwrap e
with
417 | Ast_cocci.MetaExpr
(_
,_
,_
,Some t
,_
,_
) -> true
418 | Ast_cocci.MetaExpr
(_
,_
,_
,_
,Ast_cocci.LocalID
,_
) -> true
423 Visitor_ast.combiner bind option_default
424 mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
425 donothing donothing donothing donothing
426 donothing expression donothing donothing donothing donothing donothing
427 donothing donothing donothing donothing donothing
429 toplevel_list_list
+>
431 (function (nm
,_
,rule
) ->
432 (List.exists
combiner.Visitor_ast.combiner_top_level rule
))
435 let sp_contain_typed_metavar rules
=
436 sp_contain_typed_metavar_z
440 Ast_cocci.CocciRule
(a
,b
,c
,d
,_
) -> (a
,b
,c
)
441 | _
-> failwith
"error in filter")
445 Ast_cocci.CocciRule
(a
,b
,c
,d
,Ast_cocci.Normal
) -> true
451 (* finding among the #include the one that we need to parse
452 * because they may contain useful type definition or because
453 * we may have to modify them
455 * For the moment we base in part our heuristic on the name of the file, e.g.
456 * serio.c is related we think to #include <linux/serio.h>
459 let interpret_include_path _
=
460 match !Flag_cocci.include_path
with
464 let (includes_to_parse
:
465 (Common.filename
* Parse_c.program2
) list
->
466 Flag_cocci.include_options
-> 'a
) = fun xs choose_includes
->
467 match choose_includes
with
468 Flag_cocci.I_UNSPECIFIED
-> failwith
"not possible"
469 | Flag_cocci.I_NO_INCLUDES
-> []
471 let all_includes = x
=*= Flag_cocci.I_ALL_INCLUDES
in
472 xs +> List.map
(fun (file
, cs
) ->
473 let dir = Common.dirname file
in
475 cs
+> Common.map_filter
(fun (c
,_info_item
) ->
479 {Ast_c.i_include
= ((x
,ii
)); i_rel_pos
= info_h_pos
;}) ->
482 let f = Filename.concat
dir (Common.join
"/" xs) in
483 (* for our tests, all the files are flat in the current dir *)
484 if not
(Sys.file_exists
f) && !Flag_cocci.relax_include_path
486 let attempt2 = Filename.concat
dir (Common.last
xs) in
487 if not
(Sys.file_exists
f) && all_includes
488 then Some
(Filename.concat
(interpret_include_path())
489 (Common.join
"/" xs))
493 | Ast_c.NonLocal
xs ->
495 Common.fileprefix
(Common.last
xs) =$
= Common.fileprefix file
497 Some
(Filename.concat
(interpret_include_path())
498 (Common.join
"/" xs))
500 | Ast_c.Weird _
-> None
506 let rec interpret_dependencies local global
= function
507 Ast_cocci.Dep
s -> List.mem
s local
508 | Ast_cocci.AntiDep
s ->
509 (if !Flag_ctl.steps
!= None
510 then failwith
"steps and ! dependency incompatible");
511 not
(List.mem
s local
)
512 | Ast_cocci.EverDep
s -> List.mem
s global
513 | Ast_cocci.NeverDep
s ->
514 (if !Flag_ctl.steps
!= None
515 then failwith
"steps and ! dependency incompatible");
516 not
(List.mem
s global
)
517 | Ast_cocci.AndDep
(s1
,s2
) ->
518 (interpret_dependencies local global s1
) &&
519 (interpret_dependencies local global s2
)
520 | Ast_cocci.OrDep
(s1
,s2
) ->
521 (interpret_dependencies local global s1
) or
522 (interpret_dependencies local global s2
)
523 | Ast_cocci.NoDep
-> true
525 let rec print_dependencies str local global dep
=
526 if !Flag_cocci.show_dependencies
531 let rec loop = function
532 Ast_cocci.Dep
s | Ast_cocci.AntiDep
s ->
533 if not
(List.mem
s !seen)
537 then pr2
(s^
" satisfied")
538 else pr2
(s^
" not satisfied");
541 | Ast_cocci.EverDep
s | Ast_cocci.NeverDep
s ->
542 if not
(List.mem
s !seen)
546 then pr2
(s^
" satisfied")
547 else pr2
(s^
" not satisfied");
550 | Ast_cocci.AndDep
(s1
,s2
) ->
553 | Ast_cocci.OrDep
(s1
,s2
) ->
556 | Ast_cocci.NoDep
-> () in
562 (* --------------------------------------------------------------------- *)
563 (* #include relative position in the file *)
564 (* --------------------------------------------------------------------- *)
566 (* compute the set of new prefixes
568 * "a/b/x"; (* in fact it is now a list of string so ["a";"b";"x"] *)
572 * it would give
for the first element
573 * ""; "a"; "a/b"; "a/b/x"
577 * update
: if the
include is inside a ifdef a put nothing
. cf
-test incl
.
578 * this is because we dont want code added inside ifdef
.
581 let compute_new_prefixes xs =
582 xs +> Common.map_withenv
(fun already
xs ->
583 let subdirs_prefixes = Common.inits
xs in
584 let new_first = subdirs_prefixes +> List.filter
(fun x
->
585 not
(List.mem x already
)
594 (* does via side effect on the ref in the Include in Ast_c *)
595 let rec update_include_rel_pos cs
=
596 let only_include = cs
+> Common.map_filter
(fun c
->
598 | Ast_c.CppTop
(Ast_c.Include
{Ast_c.i_include
= ((x
,_
));
600 i_is_in_ifdef
= inifdef
}) ->
602 | Ast_c.Weird _
-> None
611 let (locals
, nonlocals
) =
612 only_include +> Common.partition_either
(fun (c
, aref
) ->
614 | Ast_c.Local x
-> Left
(x
, aref
)
615 | Ast_c.NonLocal x
-> Right
(x
, aref
)
616 | Ast_c.Weird x
-> raise Impossible
619 update_rel_pos_bis locals
;
620 update_rel_pos_bis nonlocals
;
622 and update_rel_pos_bis
xs =
623 let xs'
= List.map fst
xs in
624 let the_first = compute_new_prefixes xs'
in
625 let the_last = List.rev
(compute_new_prefixes (List.rev
xs'
)) in
626 let merged = Common.zip
xs (Common.zip
the_first the_last) in
627 merged +> List.iter
(fun ((x
, aref
), (the_first, the_last)) ->
630 Ast_c.first_of
= the_first;
631 Ast_c.last_of
= the_last;
640 (*****************************************************************************)
641 (* All the information needed around the C elements and Cocci rules *)
642 (*****************************************************************************)
644 type toplevel_c_info
= {
645 ast_c
: Ast_c.toplevel
; (* contain refs so can be modified *)
646 tokens_c
: Parser_c.token list
;
649 flow
: Control_flow_c.cflow
option; (* it's the "fixed" flow *)
652 env_typing_before
: TAC.environment
;
653 env_typing_after
: TAC.environment
;
655 was_modified
: bool ref;
660 type toplevel_cocci_info_script_rule
= {
661 scr_ast_rule
: string * (string * (string * string)) list
* string;
663 scr_dependencies
: Ast_cocci.dependency
;
668 type toplevel_cocci_info_cocci_rule
= {
669 ctl
: Lib_engine.ctlcocci
* (CCI.pred list list
);
670 metavars
: Ast_cocci.metavar list
;
671 ast_rule
: Ast_cocci.rule
;
672 isexp
: bool; (* true if + code is an exp, only for Flag.make_hrule *)
675 dependencies
: Ast_cocci.dependency
;
676 (* There are also some hardcoded rule names in parse_cocci.ml:
677 * let reserved_names = ["all";"optional_storage";"optional_qualifier"]
679 dropped_isos
: string list
;
680 free_vars
: Ast_cocci.meta_name list
;
681 negated_pos_vars
: Ast_cocci.meta_name list
;
682 used_after
: Ast_cocci.meta_name list
;
683 positions
: Ast_cocci.meta_name list
;
686 ruletype
: Ast_cocci.ruletype
;
688 was_matched
: bool ref;
691 type toplevel_cocci_info
=
692 ScriptRuleCocciInfo
of toplevel_cocci_info_script_rule
693 | InitialScriptRuleCocciInfo
of toplevel_cocci_info_script_rule
694 | FinalScriptRuleCocciInfo
of toplevel_cocci_info_script_rule
695 | CocciRuleCocciInfo
of toplevel_cocci_info_cocci_rule
697 type cocci_info
= toplevel_cocci_info list
* string list list
(* tokens *)
699 type kind_file
= Header
| Source
703 was_modified_once
: bool ref;
704 asts
: toplevel_c_info list
;
709 let g_contain_typedmetavar = ref false
712 let last_env_toplevel_c_info xs =
713 (Common.last
xs).env_typing_after
715 let concat_headers_and_c (ccs
: file_info list
)
716 : (toplevel_c_info
* string) list
=
717 (List.concat
(ccs
+> List.map
(fun x
->
718 x
.asts
+> List.map
(fun x'
->
721 let for_unparser xs =
722 xs +> List.map
(fun x
->
723 (x
.ast_c
, (x
.fullstring
, x
.tokens_c
)), Unparse_c.PPviastr
726 let gen_pdf_graph () =
727 (Ctl_engine.get_graph_files
()) +> List.iter
(fun outfile
->
728 Printf.printf
"Generation of %s%!" outfile
;
729 let filename_stack = Ctl_engine.get_graph_comp_files outfile
in
730 List.iter
(fun filename
->
731 ignore
(Unix.system
("dot " ^ filename ^
" -Tpdf -o " ^ filename ^
".pdf;"))
733 let (head
,tail
) = (List.hd
filename_stack, List.tl
filename_stack) in
734 ignore
(Unix.system
("cp " ^ head ^
".pdf " ^ outfile ^
".pdf;"));
735 tail
+> List.iter
(fun filename
->
736 ignore
(Unix.system
("mv " ^ outfile ^
".pdf /tmp/tmp.pdf;"));
737 ignore
(Unix.system
("pdftk " ^ filename ^
".pdf /tmp/tmp.pdf cat output " ^ outfile ^
".pdf"));
739 ignore
(Unix.system
("rm /tmp/tmp.pdf;"));
740 List.iter
(fun filename
->
741 ignore
(Unix.system
("rm " ^ filename ^
" " ^ filename ^
".pdf;"))
743 Printf.printf
" - Done\n")
746 (* --------------------------------------------------------------------- *)
747 let prepare_cocci ctls free_var_lists negated_pos_lists
748 used_after_lists positions_list metavars astcocci
=
750 let gathered = Common.index_list_1
751 (zip
(zip
(zip
(zip
(zip
(zip ctls metavars
) astcocci
) free_var_lists
)
752 negated_pos_lists
) used_after_lists
) positions_list
)
755 (fun (((((((ctl_toplevel_list
,metavars
),ast
),free_var_list
),
756 negated_pos_list
),used_after_list
),positions_list
),rulenb
) ->
758 let is_script_rule r
=
760 Ast_cocci.ScriptRule _
761 | Ast_cocci.InitialScriptRule _
| Ast_cocci.FinalScriptRule _
-> true
764 if not
(List.length ctl_toplevel_list
=|= 1) && not
(is_script_rule ast
)
765 then failwith
"not handling multiple minirules";
768 Ast_cocci.ScriptRule
(lang
,deps
,mv
,code
) ->
771 scr_ast_rule
= (lang
, mv
, code
);
773 scr_dependencies
= deps
;
777 in ScriptRuleCocciInfo
r
778 | Ast_cocci.InitialScriptRule
(lang
,code
) ->
780 let deps = Ast_cocci.NoDep
in
783 scr_ast_rule
= (lang
, mv, code
);
785 scr_dependencies
= deps;
789 in InitialScriptRuleCocciInfo
r
790 | Ast_cocci.FinalScriptRule
(lang
,code
) ->
792 let deps = Ast_cocci.NoDep
in
795 scr_ast_rule
= (lang
, mv, code
);
797 scr_dependencies
= deps;
801 in FinalScriptRuleCocciInfo
r
802 | Ast_cocci.CocciRule
803 (rulename
,(dependencies
,dropped_isos
,z
),restast
,isexp
,ruletype
) ->
806 ctl
= List.hd ctl_toplevel_list
;
809 isexp
= List.hd isexp
;
811 dependencies
= dependencies
;
812 dropped_isos
= dropped_isos
;
813 free_vars
= List.hd free_var_list
;
814 negated_pos_vars
= List.hd negated_pos_list
;
815 used_after
= List.hd used_after_list
;
816 positions
= List.hd positions_list
;
819 was_matched
= ref false;
824 (* --------------------------------------------------------------------- *)
826 let build_info_program cprogram env
=
828 let (cs
, parseinfos
) =
829 Common.unzip cprogram
in
832 parseinfos
+> List.map
(fun (s,toks
) -> toks
) +> List.flatten
in
834 (* I use cs' but really annotate_xxx work by doing side effects on cs *)
836 Comment_annotater_c.annotate_program
alltoks cs in
838 Type_annoter_c.annotate_program env
(*!g_contain_typedmetavar*) cs'
841 zip
cs_with_envs parseinfos
+> List.map
(fun ((c
, (enva
,envb
)), parseinfo
)->
842 let (fullstr
, tokens) = parseinfo
in
845 ast_to_flow_with_error_messages c
+> Common.map_option
(fun flow ->
846 let flow = Ast_to_flow.annotate_loop_nodes
flow in
848 (* remove the fake nodes for julia *)
849 let fixed_flow = CCI.fix_flow_ctl
flow in
851 if !Flag_cocci.show_flow
then print_flow fixed_flow;
852 if !Flag_cocci.show_before_fixed_flow
then print_flow flow;
859 ast_c
= c
; (* contain refs so can be modified *)
861 fullstring
= fullstr
;
865 contain_loop = contain_loop flow;
867 env_typing_before
= enva
;
868 env_typing_after
= envb
;
870 was_modified
= ref false;
876 (* Optimisation. Try not unparse/reparse the whole file when have modifs *)
877 let rebuild_info_program cs file isexp
=
878 cs +> List.map
(fun c
->
881 let file = Common.new_temp_file
"cocci_small_output" ".c" in
883 [(c
.ast_c
, (c
.fullstring
, c
.tokens_c
)), Unparse_c.PPnormal
]
886 (* Common.command2 ("cat " ^ file); *)
887 let cprogram = cprogram_of_file file in
888 let xs = build_info_program cprogram c
.env_typing_before
in
890 (* TODO: assert env has not changed,
891 * if yes then must also reparse what follows even if not modified.
892 * Do that only if contain_typedmetavar of course, so good opti.
894 (* Common.list_init xs *) (* get rid of the FinalDef *)
900 let rebuild_info_c_and_headers ccs isexp
=
901 ccs
+> List.iter
(fun c_or_h
->
902 if c_or_h
.asts
+> List.exists
(fun c
-> !(c
.was_modified
))
903 then c_or_h
.was_modified_once
:= true;
905 ccs
+> List.map
(fun c_or_h
->
907 asts
= rebuild_info_program c_or_h
.asts c_or_h
.full_fname isexp
}
916 let prepare_c files choose_includes
: file_info list
=
917 let cprograms = List.map
cprogram_of_file_cached files
in
918 let includes = includes_to_parse
(zip files
cprograms) choose_includes
in
920 (* todo?: may not be good to first have all the headers and then all the c *)
922 (includes +> List.map
(fun hpath
-> Right hpath
))
924 ((zip files
cprograms) +> List.map
(fun (file, asts
) -> Left
(file, asts
)))
927 let env = ref !TAC.initial_env
in
929 let ccs = all +> Common.map_filter
(fun x
->
932 if not
(Common.lfile_exists hpath
)
934 pr2
("TYPE: header " ^ hpath ^
" not found");
938 let h_cs = cprogram_of_file_cached hpath
in
939 let info_h_cs = build_info_program h_cs !env in
943 else last_env_toplevel_c_info info_h_cs
946 fname
= Common.basename hpath
;
949 was_modified_once
= ref false;
953 | Left
(file, cprogram) ->
954 (* todo?: don't update env ? *)
955 let cs = build_info_program cprogram !env in
956 (* we do that only for the c, not for the h *)
957 ignore
(update_include_rel_pos (cs +> List.map
(fun x
-> x
.ast_c
)));
959 fname
= Common.basename
file;
962 was_modified_once
= ref false;
971 (*****************************************************************************)
972 (* Processing the ctls and toplevel C elements *)
973 (*****************************************************************************)
975 (* The main algorithm =~
976 * The algorithm is roughly:
977 * for_all ctl rules in SP
978 * for_all minirule in rule (no more)
979 * for_all binding (computed during previous phase)
981 * match control flow of function vs minirule
982 * with the binding and update the set of possible
983 * bindings, and returned the possibly modified function.
984 * pretty print modified C elements and reparse it.
987 * On ne prends que les newbinding ou returned_any_state est vrai.
988 * Si ca ne donne rien, on prends ce qu'il y avait au depart.
989 * Mais au nouveau depart de quoi ?
990 * - si ca donne rien apres avoir traité toutes les fonctions avec ce binding ?
991 * - ou alors si ca donne rien, apres avoir traité toutes les fonctions
992 * avec tous les bindings du round d'avant ?
994 * Julia pense qu'il faut prendre la premiere solution.
995 * Example: on a deux environnements candidats, E1 et E2 apres avoir traité
996 * la regle ctl 1. On arrive sur la regle ctl 2.
997 * E1 ne donne rien pour la regle 2, on garde quand meme E1 pour la regle 3.
998 * E2 donne un match a un endroit et rend E2' alors on utilise ca pour
1001 * I have not to look at used_after_list to decide to restart from
1002 * scratch. I just need to look if the binding list is empty.
1003 * Indeed, let's suppose that a SP have 3 regions/rules. If we
1004 * don't find a match for the first region, then if this first
1005 * region does not bind metavariable used after, that is if
1006 * used_after_list is empty, then mysat(), even if does not find a
1007 * match, will return a Left, with an empty transformation_info,
1008 * and so current_binding will grow. On the contrary if the first
1009 * region must bind some metavariables used after, and that we
1010 * dont find any such region, then mysat() will returns lots of
1011 * Right, and current_binding will not grow, and so we will have
1012 * an empty list of binding, and we will catch such a case.
1014 * opti: julia says that because the binding is
1015 * determined by the used_after_list, the items in the list
1016 * are kind of sorted, so could optimise the insert_set operations.
1020 (* r(ule), c(element in C code), e(nvironment) *)
1022 let merge_env new_e old_e
=
1025 function (e
,rules
) as elem
->
1026 let (same
,diff
) = List.partition
(function (e1
,_
) -> e
=*= e1
) old_e
in
1029 | [(_
,old_rules
)] -> (e
,Common.union_set rules old_rules
) :: diff
1030 | _
-> failwith
"duplicate environment entries")
1033 let apply_python_rule r cache newes e rules_that_have_matched
1034 rules_that_have_ever_matched
=
1035 show_or_not_scr_rule_name r.scr_ruleid
;
1036 if not
(interpret_dependencies rules_that_have_matched
1037 !rules_that_have_ever_matched
r.scr_dependencies
)
1040 print_dependencies "dependencies for script not satisfied:"
1041 rules_that_have_matched
1042 !rules_that_have_ever_matched
r.scr_dependencies
;
1043 show_or_not_binding "in environment" e
;
1044 (cache
, (e
, rules_that_have_matched
)::newes
)
1048 let (_
, mv, _
) = r.scr_ast_rule
in
1049 if List.for_all
(Pycocci.contains_binding e
) mv
1052 let relevant_bindings =
1054 (function ((re
,rm
),_
) ->
1055 List.exists
(function (_
,(r,m
)) -> r =$
= re
&& m
=$
= rm
) mv)
1058 if List.mem
relevant_bindings cache
1062 print_dependencies "dependencies for script satisfied:"
1063 rules_that_have_matched
1064 !rules_that_have_ever_matched
r.scr_dependencies
;
1065 show_or_not_binding "in" e
;
1066 Pycocci.build_classes
(List.map
(function (x
,y
) -> x
) e
);
1067 Pycocci.construct_variables
mv e
;
1068 let _ = Pycocci.pyrun_simplestring
1069 ("import coccinelle\nfrom coccinelle "^
1070 "import *\ncocci = Cocci()\n" ^
1072 relevant_bindings :: cache
1074 if !Pycocci.inc_match
1075 then (new_cache, merge_env [(e
, rules_that_have_matched
)] newes
)
1076 else (new_cache, newes
)
1078 else (cache
, merge_env [(e
, rules_that_have_matched
)] newes
)
1081 let rec apply_cocci_rule r rules_that_have_ever_matched es
1082 (ccs:file_info list
ref) =
1083 Common.profile_code
r.rulename
(fun () ->
1084 show_or_not_rule_name r.ast_rule
r.ruleid
;
1085 show_or_not_ctl_text r.ctl
r.ast_rule
r.ruleid
;
1087 let reorganized_env =
1088 reassociate_positions
r.free_vars
r.negated_pos_vars
!es
in
1090 (* looping over the environments *)
1091 let (_,newes
(* envs for next round/rule *)) =
1093 (function (cache
,newes
) ->
1094 function ((e
,rules_that_have_matched
),relevant_bindings) ->
1095 if not
(interpret_dependencies rules_that_have_matched
1096 !rules_that_have_ever_matched
r.dependencies
)
1100 ("dependencies for rule "^
r.rulename^
" not satisfied:")
1101 rules_that_have_matched
1102 !rules_that_have_ever_matched
r.dependencies
;
1103 show_or_not_binding "in environment" e
;
1106 [(e
+> List.filter
(fun (s,v
) -> List.mem
s r.used_after
),
1107 rules_that_have_matched
)]
1112 try List.assoc
relevant_bindings cache
1116 ("dependencies for rule "^
r.rulename^
" satisfied:")
1117 rules_that_have_matched
1118 !rules_that_have_ever_matched
r.dependencies
;
1119 show_or_not_binding "in" e
;
1120 show_or_not_binding "relevant in" relevant_bindings;
1122 (* applying the rule *)
1123 (match r.ruletype
with
1125 let children_e = ref [] in
1127 (* looping over the functions and toplevel elements in
1129 concat_headers_and_c !ccs +> List.iter
(fun (c
,f) ->
1132 (* does also some side effects on c and r *)
1134 process_a_ctl_a_env_a_toplevel
r
1135 relevant_bindings c
f in
1136 match processed with
1138 | Some newbindings
->
1139 newbindings
+> List.iter
(fun newbinding
->
1141 Common.insert_set newbinding
!children_e)
1142 ); (* end iter cs *)
1145 | Ast_cocci.Generated
->
1146 process_a_generated_a_env_a_toplevel
r
1147 relevant_bindings !ccs;
1150 let old_bindings_to_keep =
1152 (e
+> List.filter
(fun (s,v
) -> List.mem
s r.used_after
)) in
1154 if null
new_bindings
1157 (*use the old bindings, specialized to the used_after_list*)
1158 if !Flag_ctl.partial_match
1161 "Empty list of bindings, I will restart from old env";
1162 [(old_bindings_to_keep,rules_that_have_matched
)]
1165 (* combine the new bindings with the old ones, and
1166 specialize to the used_after_list *)
1167 let old_variables = List.map fst
old_bindings_to_keep in
1168 (* have to explicitly discard the inherited variables
1169 because we want the inherited value of the positions
1170 variables not the extended one created by
1171 reassociate_positions. want to reassociate freshly
1172 according to the free variables of each rule. *)
1173 let new_bindings_to_add =
1179 List.mem
s r.used_after
&&
1180 not
(List.mem
s old_variables)))) in
1182 (function new_binding_to_add
->
1185 old_bindings_to_keep new_binding_to_add
),
1186 r.rulename
::rules_that_have_matched
))
1187 new_bindings_to_add in
1188 ((relevant_bindings,new_bindings)::cache
,
1189 merge_env new_e newes
))
1190 ([],[]) reorganized_env in (* end iter es *)
1192 then Common.push2
r.rulename rules_that_have_ever_matched
;
1196 (* apply the tagged modifs and reparse *)
1197 if not
!Flag.sgrep_mode2
1198 then ccs := rebuild_info_c_and_headers !ccs r.isexp
)
1200 and reassociate_positions free_vars negated_pos_vars envs
=
1201 (* issues: isolate the bindings that are relevant to a given rule.
1202 separate out the position variables
1203 associate all of the position variables for a given set of relevant
1204 normal variable bindings with each set of relevant normal variable
1205 bindings. Goal: if eg if@p (E) matches in two places, then both inherited
1206 occurrences of E should see both bindings of p, not just its own.
1207 Otherwise, a position constraint for something that matches in two
1208 places will never be useful, because the position can always be
1209 different from the other one. *)
1213 List.filter
(function (x
,_) -> List.mem x free_vars
) e
)
1215 let splitted_relevant =
1216 (* separate the relevant variables into the non-position ones and the
1221 (function (non_pos
,pos
) ->
1222 function (v
,_) as x
->
1223 if List.mem v negated_pos_vars
1224 then (non_pos
,x
::pos
)
1225 else (x
::non_pos
,pos
))
1228 let splitted_relevant =
1230 (function (non_pos
,pos
) ->
1231 (List.sort compare non_pos
,List.sort compare pos
))
1232 splitted_relevant in
1235 (function non_pos
->
1237 if List.mem np non_pos
then non_pos
else np
::non_pos
)
1238 [] splitted_relevant in
1239 let extended_relevant =
1240 (* extend the position variables with the values found at other identical
1241 variable bindings *)
1243 (function non_pos
->
1246 (function (other_non_pos
,other_pos
) ->
1247 (* do we want equal? or just somehow compatible? eg non_pos
1248 binds only E, but other_non_pos binds both E and E1 *)
1249 non_pos
=*= other_non_pos
)
1250 splitted_relevant in
1254 (combine_pos negated_pos_vars
1255 (List.map
(function (_,x
) -> x
) others)))))
1258 (List.map
(function (non_pos
,_) -> List.assoc non_pos
extended_relevant)
1261 and combine_pos negated_pos_vars
others =
1265 Ast_c.MetaPosValList
1268 (function positions
->
1269 function other_list
->
1271 match List.assoc posvar other_list
with
1272 Ast_c.MetaPosValList l1
->
1273 Common.union_set l1 positions
1274 | _ -> failwith
"bad value for a position variable"
1275 with Not_found
-> positions
)
1279 and process_a_generated_a_env_a_toplevel2
r env = function
1284 (rule
,_) when rule
=$
= r.rulename
-> false
1285 | (_,"ARGS") -> false
1288 let env_domain = List.map
(function (nm
,vl
) -> nm
) env in
1292 let (rl
,_) = Ast_cocci.get_meta_name md
in
1295 if Common.include_set
free_vars env_domain
1296 then Unparse_hrule.pp_rule
metavars r.ast_rule
env cfile
.full_fname
1297 | _ -> failwith
"multiple files not supported"
1299 and process_a_generated_a_env_a_toplevel rule
env ccs =
1300 Common.profile_code
"process_a_ctl_a_env_a_toplevel"
1301 (fun () -> process_a_generated_a_env_a_toplevel2 rule
env ccs)
1303 (* does side effects on C ast and on Cocci info rule *)
1304 and process_a_ctl_a_env_a_toplevel2
r e c
f =
1305 indent_do
(fun () ->
1306 show_or_not_celem "trying" c
.ast_c
;
1307 Flag.currentfile
:= Some
(f ^
":" ^
get_celem c
.ast_c
);
1308 let (trans_info, returned_any_states
, inherited_bindings
, newbindings
) =
1309 Common.save_excursion
Flag_ctl.loop_in_src_code
(fun () ->
1310 Flag_ctl.loop_in_src_code
:= !Flag_ctl.loop_in_src_code
||c
.contain_loop;
1312 (***************************************)
1313 (* !Main point! The call to the engine *)
1314 (***************************************)
1315 let model_ctl = CCI.model_for_ctl
r.dropped_isos
(Common.some c
.flow) e
1316 in CCI.mysat
model_ctl r.ctl
(r.used_after
, e
)
1319 if not returned_any_states
1322 show_or_not_celem "found match in" c
.ast_c
;
1323 show_or_not_trans_info trans_info;
1324 List.iter
(show_or_not_binding "out") newbindings
;
1326 r.was_matched
:= true;
1328 if not
(null
trans_info)
1330 c
.was_modified
:= true;
1332 (* les "more than one var in a decl" et "already tagged token"
1333 * font crasher coccinelle. Si on a 5 fichiers, donc on a 5
1334 * failed. Le try limite le scope des crashes pendant la
1335 * trasformation au fichier concerne. *)
1337 (* modify ast via side effect *)
1338 ignore
(Transformation_c.transform
r.rulename
r.dropped_isos
1339 inherited_bindings
trans_info (Common.some c
.flow));
1340 with Timeout
-> raise Timeout
| UnixExit i
-> raise
(UnixExit i
)
1343 Some
(List.map
(function x
-> x
@inherited_bindings
) newbindings
)
1347 and process_a_ctl_a_env_a_toplevel a b c
f=
1348 Common.profile_code
"process_a_ctl_a_env_a_toplevel"
1349 (fun () -> process_a_ctl_a_env_a_toplevel2 a b c
f)
1352 let rec bigloop2 rs
(ccs: file_info list
) =
1353 let es = ref [(Ast_c.emptyMetavarsBinding
,[])] in
1354 let ccs = ref ccs in
1355 let rules_that_have_ever_matched = ref [] in
1357 (* looping over the rules *)
1358 rs
+> List.iter
(fun r ->
1360 InitialScriptRuleCocciInfo
r | FinalScriptRuleCocciInfo
r -> ()
1361 | ScriptRuleCocciInfo
r ->
1362 if !Flag_cocci.show_ctl_text
then begin
1363 Common.pr_xxxxxxxxxxxxxxxxx
();
1364 pr
("script: " ^
r.language
);
1365 Common.pr_xxxxxxxxxxxxxxxxx
();
1367 adjust_pp_with_indent
(fun () ->
1368 Format.force_newline
();
1369 let (l
,mv,code
) = r.scr_ast_rule
in
1370 let deps = r.scr_dependencies
in
1371 Pretty_print_cocci.unparse
1372 (Ast_cocci.ScriptRule
(l
,deps,mv,code
)));
1375 if !Flag.show_misc
then print_endline
"RESULT =";
1379 (function (cache
, newes
) ->
1380 function (e
, rules_that_have_matched
) ->
1381 match r.language
with
1383 apply_python_rule r cache newes e rules_that_have_matched
1384 rules_that_have_ever_matched
1386 concat_headers_and_c !ccs +> List.iter
(fun (c
,_) ->
1389 Printf.printf
"Flow: %s\r\nFlow!\r\n%!" c
.fullstring
);
1392 Printf.printf
"Unknown language: %s\n" r.language
;
1398 | CocciRuleCocciInfo
r ->
1399 apply_cocci_rule r rules_that_have_ever_matched es ccs);
1401 if !Flag.sgrep_mode2
1403 (* sgrep can lead to code that is not parsable, but we must
1404 * still call rebuild_info_c_and_headers to pretty print the
1405 * action (MINUS), so that later the diff will show what was
1406 * matched by sgrep. But we don't want the parsing error message
1407 * hence the following flag setting. So this code propably
1408 * will generate a NotParsedCorrectly for the matched parts
1409 * and the very final pretty print and diff will work
1411 Flag_parsing_c.verbose_parsing
:= false;
1412 ccs := rebuild_info_c_and_headers !ccs false
1414 !ccs (* return final C asts *)
1417 Common.profile_code
"bigloop" (fun () -> bigloop2 a b
)
1419 let initial_final_bigloop2 ty rebuild
r =
1420 if !Flag_cocci.show_ctl_text
then
1422 Common.pr_xxxxxxxxxxxxxxxxx
();
1423 pr
(ty ^
": " ^
r.language
);
1424 Common.pr_xxxxxxxxxxxxxxxxx
();
1426 adjust_pp_with_indent
(fun () ->
1427 Format.force_newline
();
1428 Pretty_print_cocci.unparse
(rebuild
r.scr_ast_rule
));
1431 match r.language
with
1433 (* include_match makes no sense in an initial or final rule, although
1434 er have no way to prevent it *)
1435 let _ = apply_python_rule r [] [] [] [] (ref []) in ()
1437 Printf.printf
"Unknown language for initial/final script: %s\n"
1440 let initial_final_bigloop a b c
=
1441 Common.profile_code
"initial_final_bigloop"
1442 (fun () -> initial_final_bigloop2 a b c
)
1444 (*****************************************************************************)
1445 (* The main functions *)
1446 (*****************************************************************************)
1448 let pre_engine2 (coccifile
, isofile
) =
1449 show_or_not_cocci coccifile isofile
;
1450 Pycocci.set_coccifile coccifile
;
1453 if not
(Common.lfile_exists
isofile)
1455 pr2
("warning: Can't find default iso file: " ^
isofile);
1458 else Some
isofile in
1460 (* useful opti when use -dir *)
1461 let (metavars,astcocci
,free_var_lists
,negated_pos_lists
,used_after_lists
,
1462 positions_lists
,toks
,_) =
1463 sp_of_file coccifile
isofile in
1464 let ctls = ctls_of_ast astcocci used_after_lists positions_lists
in
1466 g_contain_typedmetavar := sp_contain_typed_metavar astcocci
;
1468 check_macro_in_sp_and_adjust toks
;
1470 show_or_not_ctl_tex astcocci
ctls;
1473 prepare_cocci ctls free_var_lists negated_pos_lists
1474 used_after_lists positions_lists
metavars astcocci
in
1478 (function languages
->
1480 InitialScriptRuleCocciInfo
(r) ->
1481 (if List.mem
r.language languages
1482 then failwith
("double initializer found for "^
r.language
));
1483 initial_final_bigloop "initial"
1484 (function(x
,_,y
) -> Ast_cocci.InitialScriptRule
(x
,y
))
1486 r.language
::languages
1492 Common.profile_code
"pre_engine" (fun () -> pre_engine2 a
)
1494 let full_engine2 (cocci_infos,toks
) cfiles
=
1496 show_or_not_cfiles cfiles
;
1498 (* optimisation allowing to launch coccinelle on all the drivers *)
1499 if !Flag_cocci.worth_trying_opt
&& not
(worth_trying cfiles toks
)
1502 pr2
("not worth trying:" ^
Common.join
" " cfiles
);
1503 cfiles
+> List.map
(fun s -> s, None
)
1508 if !Flag.show_misc
then Common.pr_xxxxxxxxxxxxxxxxx
();
1509 if !Flag.show_misc
then pr
"let's go";
1510 if !Flag.show_misc
then Common.pr_xxxxxxxxxxxxxxxxx
();
1512 let choose_includes =
1513 match !Flag_cocci.include_options
with
1514 Flag_cocci.I_UNSPECIFIED
->
1515 if !g_contain_typedmetavar
1516 then Flag_cocci.I_NORMAL_INCLUDES
1517 else Flag_cocci.I_NO_INCLUDES
1519 let c_infos = prepare_c cfiles
choose_includes in
1521 (* ! the big loop ! *)
1522 let c_infos'
= bigloop cocci_infos c_infos in
1524 if !Flag.show_misc
then Common.pr_xxxxxxxxxxxxxxxxx
();
1525 if !Flag.show_misc
then pr
"Finished";
1526 if !Flag.show_misc
then Common.pr_xxxxxxxxxxxxxxxxx
();
1527 if !Flag_ctl.graphical_trace
then gen_pdf_graph ();
1529 c_infos'
+> List.map
(fun c_or_h
->
1530 if !(c_or_h
.was_modified_once
)
1534 Common.new_temp_file
"cocci-output" ("-" ^ c_or_h
.fname
) in
1536 if c_or_h
.fkind
=*= Header
1537 then pr2
("a header file was modified: " ^ c_or_h
.fname
);
1539 (* and now unparse everything *)
1540 cfile_of_program (for_unparser c_or_h
.asts
) outfile;
1542 let show_only_minus = !Flag.sgrep_mode2
in
1543 show_or_not_diff c_or_h
.fpath
outfile show_only_minus;
1546 if !Flag.sgrep_mode2
then None
else Some
outfile)
1548 else (c_or_h
.fpath
, None
))
1551 let full_engine a b
=
1552 Common.profile_code
"full_engine" (fun () -> full_engine2 a b
)
1554 let post_engine2 (cocci_infos,_) =
1557 (function languages
->
1559 FinalScriptRuleCocciInfo
(r) ->
1560 (if List.mem
r.language languages
1561 then failwith
("double finalizer found for "^
r.language
));
1562 initial_final_bigloop "final"
1563 (function(x
,_,y
) -> Ast_cocci.FinalScriptRule
(x
,y
))
1565 r.language
::languages
1571 Common.profile_code
"post_engine" (fun () -> post_engine2 a
)
1573 (*****************************************************************************)
1574 (* check duplicate from result of full_engine *)
1575 (*****************************************************************************)
1577 let check_duplicate_modif2 xs =
1578 (* opti: let groups = Common.groupBy (fun (a,resa) (b,resb) -> a =$= b) xs *)
1579 pr2
("Check duplication for " ^ i_to_s
(List.length
xs) ^
" files");
1580 let groups = Common.group_assoc_bykey_eff
xs in
1581 groups +> Common.map_filter
(fun (file, xs) ->
1583 | [] -> raise Impossible
1584 | [res] -> Some
(file, res)
1588 if not
(List.for_all
(fun res2
-> res2
=*= None
) xs)
1590 pr2
("different modification result for " ^
file);
1593 else Some
(file, None
)
1595 if not
(List.for_all
(fun res2
->
1599 let diff = Common.cmd_to_list
("diff -u -b -B "^
res^
" "^res2
)
1603 pr2
("different modification result for " ^
file);
1606 else Some
(file, Some
res)
1610 let check_duplicate_modif a
=
1611 Common.profile_code
"check_duplicate" (fun () -> check_duplicate_modif2 a
)