Jump to content
PeterPanettone

Compiler directives for line number?

Recommended Posts

It seems that Delphi does not have a built-in compiler directive or "magic word" equivalent to __LINE__ or $LINE for directly accessing the current source code line number, as seen in languages like C/C++.

Free Pascal (which shares compatibility with Delphi) does support special tokens like %LINE% and %FILE% within the {$INCLUDE} directive to insert the current line number or file name as text.

 

Is there a way to make Delphi support this (e.g., with custom pre-processors)?

Share this post


Link to post

GREAT! This function solves my inspecting problem:

function LN: string;
begin
  Result := IntToStr(JclDebug.GetLocationInfo(ReturnAddress).LineNumber);
end;

I now use this CnWizards Source Template:

 

image.thumb.png.c7b75ecacfdf18fd99bcbb1cf72fbe25.png

 

1. I press CTRL+SHIFT+# which inserts this code:   

CodeSite.Send('TForm1.Button1Click, Line Number: ' + LN + ': ', E);

2. I insert the desired expression I want to inspect, for example, Sender.ClassName   

CodeSite.Send('TForm1.Button1Click, Line Number: ' + LN + ': Sender.ClassName', Sender.ClassName);

3. And then, in the CodeSite Live Viewer at runtime, I get the desired inspection result:
   TForm1.Button1Click, Line Number: 40: Sender.ClassName = TButton

 

Happy inspecting!

Edited by PeterPanettone

Share this post


Link to post
2 hours ago, PeterPanettone said:

CodeSite.Send('TForm1.Button1Click, Line Number: ' + LN + ': Sender.ClassName', Sender.ClassName);

 

With the same function you can get also the procedure name ' TForm1.Button1Click', instead to write it like a constant:

GetLocationInfo(ReturnAddress).ProcedureName

Think to use a temporary record and call GetLocationInfo only one time.

  • Like 1

Share this post


Link to post
13 hours ago, DelphiUdIT said:

 

With the same function you can get also the procedure name ' TForm1.Button1Click', instead to write it like a constant:


GetLocationInfo(ReturnAddress).ProcedureName

Think to use a temporary record and call GetLocationInfo only one time.

Thanks for the hint. Currently, the procedure name is automatically inserted at design time by the CnWizards Source Template macro %CurrProcName%.

 

However, I plan to create a separate IDE plugin (along with other features) - if Thomas has not already picked up the idea - where a CodeSite editor replaces the CnWizards Source Template macro. That would make it very flexible and independent of the CodeSite statement location.

Edited by PeterPanettone

Share this post


Link to post

No need to use a record:

uses
 CodeSiteLogging,
 JCLDebug;

function _Loc: JclDebug.TJclLocationInfo;
begin
 Result := JclDebug.GetLocationInfo(ReturnAddress);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // 1. At design time, insert the CodeSite statement with CTRL+SHIFT+# by using this CnWizards Source Template:
  // CodeSite.Send(_Loc.ProcedureName + ' (Line#: ' + IntToStr(_Loc.LineNumber) + '): X', X);

  // 2. Then, replace X with the desired expression to get the Procedure Name and the current line Number at runtime:
  CodeSite.Send(_Loc.ProcedureName + ' (Line#: ' + IntToStr(_Loc.LineNumber) + '): Sender.ClassName', Sender.ClassName);

  // 3. The CodeSite result in the CodeSite Live Viewer rsults as: TForm1.Button1Click (Line#: 39): Sender.ClassName = TButton
end;

image.thumb.png.df232dc9d1550bffbd1f301e08831e88.png

Edited by PeterPanettone

Share this post


Link to post

Wich will result in multiple expensive calls to GetLocationInfo...

Share this post


Link to post
8 hours ago, dummzeuch said:

Wich will result in multiple expensive calls to GetLocationInfo...

My CPU can afford this 😉

 

However, I've simplified the CodeSite statement and avoided calling GetLocationInfo twice by using a with statement (without using a record):

function GetLocPrefix: string;
begin
  CodeSite.Send('GetLocPrefix: '); // this is called only once!

  // Directly get the location of the caller (Button1Click), skipping the need for _Loc here
  with JclDebug.GetLocationInfo(ReturnAddress) do
    Result := ProcedureName + ' (Line#: ' + IntToStr(LineNumber) + '): ';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // 1. At design time, insert the CodeSite statement with CTRL+SHIFT+# by using this CnWizards Source Template:
  // CodeSite.Send(GetLocPrefix + 'X', X);

  // 2. Then, replace X with the desired expression, then get the Procedure Name and the current line Number at runtime:
  CodeSite.Send(GetLocPrefix + 'Sender.ClassName', Sender.ClassName);

  // 3. The CodeSite result in the CodeSite Live Viewer rsults as: TForm1.Button1Click (Line#: 51): Sender.ClassName = TButton
end;

 

Edited by PeterPanettone

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×