caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Anders Fugmann <anders@fugmann.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] filename and line number.
Date: Tue, 2 Aug 2011 14:45:17 +0200	[thread overview]
Message-ID: <CAPFanBF=OfWNgYJixFuKgJvFz57Tq045SNVA+O93EBEM_otFqA@mail.gmail.com> (raw)
In-Reply-To: <4E37EBA3.6040800@fugmann.net>

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 <anders@fugmann.net> 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
>
>


  reply	other threads:[~2011-08-02 12:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-02 12:20 Anders Fugmann
2011-08-02 12:45 ` Gabriel Scherer [this message]
2011-08-02 21:21   ` Martin Jambon
2011-08-03  6:04     ` Anders Fugmann
2011-08-03  7:10       ` Gabriel Scherer
2011-08-04  7:58         ` Anders Fugmann
2011-08-04 13:03           ` forum
2011-08-08 19:08             ` Anders Peter Fugmann
2011-08-09 16:31               ` forum
2011-08-03 19:57   ` Till Varoquaux
2011-08-03 20:52     ` Gabriel Scherer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPFanBF=OfWNgYJixFuKgJvFz57Tq045SNVA+O93EBEM_otFqA@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=anders@fugmann.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).