The following patch reduces a bit more the chance of a stack overflow or
core dump when parsing the C file. It is a patch against the original
source code of version 0.2.4rc2.
julia
diff -u -p a/parsing_c/parse_c.ml b/parsing_c/parse_c.ml
--- a/parsing_c/parse_c.ml 2010-01-28 15:25:06.000000000 +0100
+++ b/parsing_c/parse_c.ml 2010-08-30 12:43:26.000000000 +0200
@@ -108,7 +108,7 @@ let print_bad line_error (start_line, en
(* Stats on what was passed/commentized *)
(*****************************************************************************)
-let commentized xs = xs +> Common.map_filter (function
+let commentized xs = xs +> Common.tail_map_filter (function
| Parser_c.TCommentCpp (cppkind, ii) ->
let s = Ast_c.str_of_info ii in
let legal_passing =
@@ -613,7 +613,7 @@ let get_one_elem ~pass tr (file, filelin
(* Call parser *)
(* -------------------------------------------------- *)
Common.profile_code_exclusif "YACC" (fun () ->
- Left (Parser_c.celem (lexer_function ~pass tr) lexbuf_fake)
+ Left (Parser_c.celem (lexer_function ~pass tr) lexbuf_fake)
)
with e ->
LP.restore_typedef_state();
@@ -691,7 +691,7 @@ let find_optional_macro_to_expand2 ~defs
let defs = Common.hash_of_list defs in
- let toks = toks +> Common.map (function
+ let toks = toks +> Common.tail_map (function
(* special cases to undo *)
| Parser_c.TMacroIterator (s, ii) ->
diff -u -p a/commons/common.mli b/commons/common.mli
--- a/commons/common.mli 2010-01-28 15:25:04.000000000 +0100
+++ b/commons/common.mli 2010-08-30 12:37:50.000000000 +0200
@@ -792,6 +792,7 @@ val partition_either3 :
val filter_some : 'a option list -> 'a list
val map_filter : ('a -> 'b option) -> 'a list -> 'b list
+val tail_map_filter : ('a -> 'b option) -> 'a list -> 'b list
val find_some : ('a -> 'b option) -> 'a list -> 'b
val list_to_single_or_exn: 'a list -> 'a
@@ -1757,6 +1758,7 @@ val empty_graph : 'a list * 'b list
(* mostly alias to functions in List *)
val map : ('a -> 'b) -> 'a list -> 'b list
+val tail_map : ('a -> 'b) -> 'a list -> 'b list
val filter : ('a -> bool) -> 'a list -> 'a list
val fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
diff -u -p a/commons/common.ml b/commons/common.ml
--- a/commons/common.ml 2010-05-24 08:06:28.000000000 +0200
+++ b/commons/common.ml 2010-08-30 12:39:21.000000000 +0200
@@ -2101,6 +2101,17 @@ let rec filter_some = function
let map_filter f xs = xs +> List.map f +> filter_some
+(* avoid recursion *)
+let tail_map_filter f xs =
+ List.rev
+ (List.fold_left
+ (function prev ->
+ function cur ->
+ match f cur with
+ Some x -> x :: prev
+ | None -> prev)
+ [] xs)
+
let rec find_some p = function
| [] -> raise Not_found
| x :: l ->
@@ -3691,14 +3702,15 @@ let rec group_by_mapped_key fkey l =
let (exclude_but_keep_attached: ('a -> bool) -> 'a list -> ('a * 'a list)
list)=
fun f xs ->
- let rec aux_filter acc = function
- | [] -> [] (* drop what was accumulated because nothing to attach to *)
+ let rec aux_filter acc ans = function
+ | [] -> (* drop what was accumulated because nothing to attach to *)
+ List.rev ans
| x::xs ->
if f x
- then aux_filter (x::acc) xs
- else (x, List.rev acc)::aux_filter [] xs
+ then aux_filter (x::acc) ans xs
+ else aux_filter [] ((x, List.rev acc)::ans) xs
in
- aux_filter [] xs
+ aux_filter [] [] xs
let _ = example
(exclude_but_keep_attached (fun x -> x =|= 3) [3;3;1;3;2;3;3;3] =*=
[(1,[3;3]);(2,[3])])
@@ -5235,6 +5247,12 @@ let head = List.hd
let tail = List.tl
let is_singleton = fun xs -> List.length xs =|= 1
+let tail_map f l = (* tail recursive map, using rev *)
+ let rec loop acc = function
+ [] -> acc
+ | x::xs -> loop ((f x) :: acc) xs in
+ List.rev(loop [] l)
+
(*****************************************************************************)
(* Geometry (raytracer) *)
(*****************************************************************************)
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)