English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2006-03-07 (09:39)
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?


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
     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 =
 [ <: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
but it's beta code so use it at your own risks and without any kind of


Nicolas Pouillard