Lazarus

Free Pascal => General => Topic started by: 440bx on July 06, 2018, 05:26:49 am

Title: is there a way of obtaining a function or procedure name string from FPC ?
Post by: 440bx on July 06, 2018, 05:26:49 am
Hello,

The C++ 11 standard defines a macro __func__ which yields the name of the enclosing function as a null terminated character string.   Using Pascal as an example, it does the following:

Code: Pascal  [Select][+][-]
  1. function myfunction(parameters here) : returntypehere;
  2. begin
  3.    writeln('Currently executing', __func__);
  4. end;

would output:  "Currently executing myfunction"

does FPC have a way of getting the name of the function (myfunction in this case,  i.e, __func__ equivalent) ?

This is particularly convenient when outputting error messages, spares the programmer from having to type (and possibly misspell) the name of the function in error messages and any other messages where the function name would be useful information to have.

Thank you for your help

Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: ASerge on July 06, 2018, 07:21:28 am
Only in future version. In trunk this work:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3. {$MODE OBJFPC}
  4.  
  5. procedure SomeName;
  6. begin
  7.   Writeln('Name is ', {$I %CURRENTROUTINE%});
  8. end;
  9.  
  10. procedure OtherName;
  11. begin
  12.   Writeln('Name is ', {$I %CURRENTROUTINE%});
  13. end;
  14.  
  15. begin
  16.   SomeName;
  17.   OtherName;
  18.   Readln;
  19. end.
Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: howardpc on July 06, 2018, 10:11:26 am
Code: Pascal  [Select][+][-]
  1. {$I %CURRENTROUTINE%}
works in FPC 3.1.1
Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: 440bx on July 06, 2018, 05:29:28 pm
Thank you for the replies, they are good news.

I am looking forward the next stable release of FPC that includes this feature.  It's quite convenient for error messages.

Now, I'm really curious, anyone have any idea when the next stable release that includes this feature might be coming out ?
Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: marcov on July 06, 2018, 05:48:48 pm
Now, I'm really curious, anyone have any idea when the next stable release that includes this feature might be coming out ?

Release process hasn't started yet, so minimum 6-9 months.
Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: 440bx on July 06, 2018, 06:49:02 pm
Release process hasn't started yet, so minimum 6-9 months.

6 months would be great, it would make for a nice Xmas present. :)  Thank you for the update.
Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: engkin on July 06, 2018, 09:14:52 pm
Release process hasn't started yet, so minimum 6-9 months.

6 months would be great, it would make for a nice Xmas present. :)

I thought you already had one.  :D
Title: Re: is there a way of obtaining a function or procedure name string from FPC ?
Post by: 440bx on July 06, 2018, 09:38:38 pm
Release process hasn't started yet, so minimum 6-9 months.

6 months would be great, it would make for a nice Xmas present. :)

I thought you already had one.  :D

yeah, I did... you gave me one already but... you know kids, they always want more ;)
TinyPortal © 2005-2018