[RFC] -frelative-path option to change __FILE__

Hi, all. We have a longstanding bug sitting on us (okay, me) about pruning the length of __FILE__, either to just the basename or to a relative source path. (For those inside Apple, this is <rdar://problem/4440158> and related.) The usual solution for this is just to embed a strrchr call in a second macro, but that's not so nice for globals, and doesn't handle the relative path case so well.

I propose a new flag, tentatively named -frelative-path=, which handles this by setting __FILE__ to a canonicalized relative path if it is set. I'm going to use ~/foo/bar/../baz/baz.cpp as the file name in these examples:

-frelative-path= (or absent)
  /Users/jrose/foo/bar/../baz/baz.cpp
-frelative-path=.
  baz.cpp
-frelative-path=/
  /Users/jrose/foo/baz/baz.cpp
-frelative-path=/Users/jrose/foo
  baz/baz.cpp
-frelative-path=../.. (always shows two folders up from the current source file)
  foo/baz/baz.cpp

Thoughts? Ideas on the flag name? Pushback? Missing use cases?

Jordan

I think this would be very useful, at least for FreeBSD. There's a similar uncommitted patch for GCC (47047 – Support for path translation in __FILE__) which implements a rewrite flag called -iremap=src:dst I posted a partial implementation of -iremap for Clang on this list in May but lacked the wits to produce a working implementation.

The only benefit I see with -iremap compared to your proposal is that with -iremap=/foor/bar/baz:src it's possible to maintain the FreeBSD idiom that source code is placed in src/. I don't think it's essential.

Speaking from FreeBSD, I see two main use cases for an -iremap/-frelative-path flag:

1) Since __FILE__ is embedded in debug output, a binary built with a source tree located in /foo/bar/ can easily be debugged on a machine where source code is located in /bar/baz/

2) Two binaries built within /foo/bar/ and /bar/baz/ will produce deterministic output (all other things being equal).

Erik