Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 03, 2012 Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
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...
|
March 03, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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.
|
March 03, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Le samedi 03 mars 2012 à 02:39 +0100, Adam D. Ruppe a écrit :
> 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.
I think __LINE__ is size_t not int
|
March 03, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | 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 |
March 03, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | On Saturday, March 03, 2012 13:21:05 bioinfornatics wrote:
> Le samedi 03 mars 2012 à 02:39 +0100, Adam D. Ruppe a écrit :
> > 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.
>
> I think __LINE__ is size_t not int
Yes, it's size_t.
- Jonathan M Davis
|
March 03, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
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. |
March 03, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
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.
|
March 04, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
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
|
March 04, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
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. |
March 04, 2012 Re: Evaluating __FILE__ and __LINE__ of caller? | ||||
---|---|---|---|---|
| ||||
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
|
Copyright © 1999-2021 by the D Language Foundation