Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Line number for index out of bounds
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Line number for index out of bounds
On 3/6/06, Jonathan Harrop <jdh30@jdh30.plus.com> wrote:
> On Mon Mar  6 11:14 , Richard Jones <rich@annexia.org> sent:
> >* Surround every possible array index with a try ... with expression
>
> Perhaps you could write a camlp4 macro to wrap uses of a.(i) in a try ... with block that logs the
> line number?
>

Hi,

I have a little camlp4 extension that wraps every function definition
with a try...with block that logs the exception with the location and
re-raise it afterwards.

The code is quite short but it needs the camlp4 version that I am developing:

============================================================
value rec map_pwel =
 let add_debug_expr e =
   let _loc = Loc.make_absolute (MLast.loc_of_expr e) in
   let msg = "Exception tracer at " ^ Loc.to_string _loc ^ " (%s)@." in
   <:expr<
     try $e$
     with exc ->
       do {
         Format.eprintf $str:msg$ (Printexc.to_string exc);
         raise exc
       } >> in
 let map_pwe (patt, owhen, expr) = (patt, owhen, add_debug_expr expr)
 in List.map map_pwe

and map_expr =
 fun
 [ <:expr< fun [ $list:pwel$ ] >> as e ->
     let _loc = MLast.loc_of_expr e in
     <:expr< fun [ $list:map_pwel pwel$ ] >>
 | x -> x ];

Pcaml.register_str_item_filter (MLast.Map.Expr.str_item map_expr);
=============================================================

You can use it like that:

$ cat test.ml
let a = Array.make 10 0
let f () = a.(11)
let g = f
let h = g
let main = h ()

$ ocamlc  -pp 'camlp4o fi_exc_tracer.cmo' test.ml -o test

$ ./test
Exception tracer at File "/tmp/test.ml", line 2, characters 11-17
(Invalid_argument("index out of bounds"))
Fatal error: exception Invalid_argument("index out of bounds")

A tarball of this camlp4 version is available at
http://gallium.inria.fr/~pouillar/ocaml-3.10+dev2-and-camlp4-beta-r22414.tar.bz2,
but it's beta code so use it at your own risks and without any kind of
warranty.

Cheers,

--
Nicolas Pouillard