www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Evaluating __FILE__ and __LINE__ of caller?

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
Is it possible to get at the file and line number of where a function is
being called from? For example:

	class A {
		int opIndex(int x) {
			/* do a bunch of checks */
			if (checkFailed)
				throw new RangeError(...);

			/* compute value */
			return value;
		}
	}

I'd like to be able to throw the exception referencing the file/line of
the caller, rather than opIndex() itself. Is this possible?


T

-- 
Two wrongs don't make a right; but three rights do make a left...
Mar 02 2012
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 3 March 2012 at 01:36:51 UTC, H. S. Teoh wrote:
 		int opIndex(int x) {
Make that (int x, string file = __FILE__, int line = __LINE__) and use file/line in there. The exception consturctors do this, so you can throw new Exception("msg", file, line); and get it passed on.
Mar 02 2012
parent reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le samedi 03 mars 2012 =C3=A0 02:39 +0100, Adam D. Ruppe a =C3=A9crit :
 On Saturday, 3 March 2012 at 01:36:51 UTC, H. S. Teoh wrote:
 		int opIndex(int x) {
=20 Make that =20 (int x, string file =3D __FILE__, int line =3D __LINE__) =20 and use file/line in there. The exception consturctors do this, so you can =20 throw new Exception("msg", file, line); =20 and get it passed on.
I think __LINE__ is size_t not int
Mar 03 2012
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/3/12, bioinfornatics <bioinfornatics fedoraproject.org> wrote:
 I think __LINE__ is size_t not int
I'd love to see a 2_147_483_648 line source file! :D
Mar 03 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, March 03, 2012 13:21:05 bioinfornatics wrote:
 Le samedi 03 mars 2012 =C3=A0 02:39 +0100, Adam D. Ruppe a =C3=A9crit=
:
 On Saturday, 3 March 2012 at 01:36:51 UTC, H. S. Teoh wrote:
 =09=09int opIndex(int x) {
=20 Make that =20 (int x, string file =3D __FILE__, int line =3D __LINE__) =20 and use file/line in there. The exception consturctors do this, so you can =20 throw new Exception("msg", file, line); =20 and get it passed on.
=20 I think __LINE__ is size_t not int
Yes, it's size_t. - Jonathan M Davis
Mar 03 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Mar 03, 2012 at 03:52:55PM +0100, Andrej Mitrovic wrote:
 On 3/3/12, bioinfornatics <bioinfornatics fedoraproject.org> wrote:
 I think __LINE__ is size_t not int
I'd love to see a 2_147_483_648 line source file! :D
It *could* happen if the source file was auto-generated... and the autogenerator was broken. :P Of course, with D's templates, CTFE, and compile-time introspection capabilities, I can't imagine when autogeneration would actually be required, but we're talking about hypotheticals here. T -- Debian GNU/Linux: Cray on your desktop.
Mar 03 2012
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/3/12, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:
 Of course, with D's templates, CTFE, and compile-time introspection
 capabilities, I can't imagine when autogeneration would actually be
 required, but we're talking about hypotheticals here.
It can be if you need an OOP D binding to a C/C++ library. E.g. QtD uses an autogenerator, GtkD as well.
Mar 03 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, March 03, 2012 23:01:38 Andrej Mitrovic wrote:
 On 3/3/12, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:
 Of course, with D's templates, CTFE, and compile-time introspection
 capabilities, I can't imagine when autogeneration would actually be
 required, but we're talking about hypotheticals here.
It can be if you need an OOP D binding to a C/C++ library. E.g. QtD uses an autogenerator, GtkD as well.
All it takes is a function passing a size_t to that function rather than letting it use its default value, and you'll get problems on 64-bit systems if you made the line number an int. It really needs to be a size_t, even if you'll never have a value for it which is high enough to need it. - Jonathan M Davis
Mar 03 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Ok, well a quick search shows socket.d:132 needs fixing. Also there's
two versioned out enforces in object_.d which use int line = __LINE__.
Interestingly, earlier versions of Phobos used int a lot (I guess in
pre-64bit compatible D days). I'm also seeing int used in Derelict,
pspemu, plot2kill and dwt2.
Mar 03 2012
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, March 04, 2012 03:38:03 Andrej Mitrovic wrote:
 Ok, well a quick search shows socket.d:132 needs fixing. Also there's
 two versioned out enforces in object_.d which use int line = __LINE__.
 Interestingly, earlier versions of Phobos used int a lot (I guess in
 pre-64bit compatible D days). I'm also seeing int used in Derelict,
 pspemu, plot2kill and dwt2.
It's a common error to use int where size_t should be used. Using int when dealing with __LINE__ is just one case of that. It didn't start actually causing compilation errors until we go 64-bit dmd though. - Jonathan M Davis
Mar 03 2012