There is a "macro" syntax extension that is distributed with Camlp4,
and can do basic cpp-like stuff, including __FILE__ and a __LOCATION__
macros.
For example, the following content, named test.ml:
let test =
__LOCATION__
When processed through 'camlp4o pa_macro.cmo', will result in:
let test = Loc.of_tuple ("test.ml", 2, 13, 17, 2, 13, 29, false)
(To compile: ocamlc -pp 'camlp4o pa_macro.cmo' ...)
The "Loc.of_tuple" call is a reference to a function implemented in
Camlp4 Loc module; if you make you project depend (at runtime, not
camlp4-time) on Camlp4 loc-handling libraries, you'll get functions to
manipulate the location and its information. You can also define your
own Loc module in test.ml:
module Loc = struct
let of_tuple
((file_name, start_line, start_bol, start_off, stop_line,
stop_bol, stop_off, is_ghost) as loc) =
loc
end
let test =
__LOCATION__
The source code (and some documentation in the head comment) for the
"macro" camlp4 extension is in
camlp4/Camlp4Parsers/Camlp4MacroParser.ml in the ocaml source tree.
The meaning of the weird tuple arguments can be found in the Camlp4
documentation. I have a not exactly up-to-date (I guess ocaml 3.11)
version of the documentation on my website, see:
http://bluestorm.info/camlp4//camlp4-doc/Sig.Loc.html
Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
macros which may be in a more directly exploitable format. I have
never tried it though. See:
http://martin.jambon.free.fr/cppo.html
On Tue, Aug 2, 2011 at 2:20 PM, Anders Fugmann <[email protected]> wrote:
> Hi,
>
> Do there exist a way to get filename and linenumber of the calling function
> - Or at least the of the current filename and line number?
>
> I guess this would involve a syntax camlp4 syntax extension, but I'm not a
> camlp4 wizard and google did not come up with any suggestions.
>
> Regards
> Anders Fugmann
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs