Recent

Author Topic: [Solved] Do functions or procedures have an identity?  (Read 1154 times)

loaded

  • Hero Member
  • *****
  • Posts: 824
[Solved] Do functions or procedures have an identity?
« on: September 22, 2021, 09:45:43 am »
Hi All,
I wanted to direct the errors that will occur in the sample code below to a procedure. If an error occurs, the message comes, but I want to learn from which procedure it comes from. I can write the procedure information manually in the message to be sent, this way it will work for me.
Code: Pascal  [Select][+][-]
  1. var
  2.   permisson_errors:Boolean=true;
  3.    .....
  4. procedure TForm_Main.P_action_errors(Const msg:String);
  5. begin
  6. showmessage(msg); // or add memo
  7. end;
  8. procedure Button_Example_Z_Data_Add;
  9. begin
  10.    ....
  11.            try
  12.             DataModule1.ZQuery_CRecord.Sql.Clear;
  13.                         .....
  14.             DataModule1.ZQuery_CRecord.execSQL;
  15.            except
  16.              on E: EDatabaseError do   if permisson_errors then P_action_errors( 'Database error: '+ E.ClassName + #13#10 + E.Message );
  17.              on E: Exception do       if permisson_errors then P_action_errors( 'Error: '+ E.ClassName + #13#10 + E.Message );
  18.            end;
  19.    ....
  20. end;             
  21.  
  22. procedure Button_Example_M_Data_Add;
  23. begin
  24.      ....
  25.            try
  26.             DataModule1.ZQuery_CRecord.Sql.Clear;
  27.                         .....
  28.             DataModule1.ZQuery_CRecord.execSQL;
  29.            except
  30.              on E: EDatabaseError do   if permisson_errors then P_action_errors( 'Database error: '+ E.ClassName + #13#10 + E.Message );
  31.              on E: Exception do       if permisson_errors then P_action_errors( 'Error: '+ E.ClassName + #13#10 + E.Message );
  32.            end;
  33.    ....
  34. end;
  35.  
But ; I wanted to ask in order to learn something new.
Do procedures have similar methods like self or this ?
Respects.
« Last Edit: September 22, 2021, 10:27:28 am by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Do functions or procedures have an identity?
« Reply #1 on: September 22, 2021, 10:13:14 am »
In compiled code, procedures only have an address.

But you can make the compiler generate a string for them by

Code: Pascal  [Select][+][-]
  1. raise exception.create('error:'+{$i %CURRENTROUTINE%});

See also https://www.freepascal.org/docs-html/prog/progsu41.html

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Do functions or procedures have an identity?
« Reply #2 on: September 22, 2021, 10:14:46 am »
Noted Marco's response but added since the conditionals in the example show how it can handle different compiler versions.

No, because procedures are simple containers for code and don't have methods. However FPC 3.2 and later has a directive which allows you to do this sort of thing:

Code: Pascal  [Select][+][-]
  1. {$undef HAS_CURRENTROUTINE    }
  2. {$if FPC_FULLVERSION > 030200 }         (* Requires FPC 2.2.4 minimum           *)
  3. {$define HAS_CURRENTROUTINE   }         (* Requires FPC 3.2.0 minimum           *)
  4. {$assertions                  }         (* Make sure name checks are operative  *)
  5. {$endif FPC_FULLVERSION       }
  6.  
  7. ...
  8.  
  9. {$ifdef HAS_CURRENTROUTINE }
  10.   Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
  11.                                                 {$I %CURRENTROUTINE%} + '()');
  12. {$endif HAS_CURRENTROUTINE }
  13.  

MarkMLl

MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Do functions or procedures have an identity?
« Reply #3 on: September 22, 2021, 10:15:30 am »
Not 100% sure, how to extract procedure name information from it (debugger does it some way - may be there is some predefined helper function for it), but you may try to use ExceptAddr.
In compiled code, procedures only have an address.

But you can make the compiler generate a string for them by

Code: Pascal  [Select][+][-]
  1. raise exception.create('error:'+{$i %CURRENTROUTINE%});

See also https://www.freepascal.org/docs-html/prog/progsu41.html
I also thought about this solution, but wouldn't it interfere with exceptions' own error messages?
« Last Edit: September 22, 2021, 10:18:09 am by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: Do functions or procedures have an identity?
« Reply #4 on: September 22, 2021, 10:27:14 am »
Code: Pascal  [Select][+][-]
  1. raise exception.create('error:'+{$i %CURRENTROUTINE%});
Yes, that's exactly what I wanted, it worked flawlessly.
marcov and MarkMLl and Mr.Madguy thank you very much for your answers.

This forum is a land of legends with great heroes :)
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Re: Do functions or procedures have an identity?
« Reply #5 on: September 22, 2021, 10:46:55 am »
Not 100% sure, how to extract procedure name information from it (debugger does it some way - may be there is some predefined helper function for it), but you may try to use ExceptAddr.

If it is compiled with debugging info. And the debugger has less of a speed criterium since by then the program is paused. Moreover debuginfo is not a fixed format and might vary with version.

People have abused debug info for such purposes (specially the people that used D7 only for decades), but I don't recommend it.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Do functions or procedures have an identity?
« Reply #6 on: September 22, 2021, 01:18:51 pm »
Noted Marco's response but added since the conditionals in the example show how it can handle different compiler versions.

No, because procedures are simple containers for code and don't have methods. However FPC 3.2 and later has a directive which allows you to do this sort of thing:

Code: Pascal  [Select][+][-]
  1. {$undef HAS_CURRENTROUTINE    }
  2. {$if FPC_FULLVERSION > 030200 }         (* Requires FPC 2.2.4 minimum           *)
  3. {$define HAS_CURRENTROUTINE   }         (* Requires FPC 3.2.0 minimum           *)
  4. {$assertions                  }         (* Make sure name checks are operative  *)
  5. {$endif FPC_FULLVERSION       }
  6.  
  7. ...
  8.  
  9. {$ifdef HAS_CURRENTROUTINE }
  10.   Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: bad name in ' +
  11.                                                 {$I %CURRENTROUTINE%} + '()');
  12. {$endif HAS_CURRENTROUTINE }
  13.  

I take it you mean FPC_FULLVERSION >= 030200 and not FPC_FULLVERSION > 030200? :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Do functions or procedures have an identity?
« Reply #7 on: September 22, 2021, 01:36:00 pm »
I take it you mean FPC_FULLVERSION >= 030200 and not FPC_FULLVERSION > 030200? :)

Noted and thanks for that... I can't remember why I selected that specific cutoff but it was probably due to the specific versions available to me... 3.2 was very likely unreleased at the time and I was testing against trunk.

However I've also found forum comment from myself that >= is appropriate, so I guess I'll be doing a bit of grepping presently :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018