Recent

Author Topic: [SOLVED] Macro as a function call — editor selects wrong line  (Read 1646 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 5596
Re: Macro as a function call — editor selects wrong line
« Reply #30 on: September 21, 2022, 03:40:41 pm »
If you don't mind go back and read where I wrote that I don't need the line number, I need the editor to highlihg the line where the error occured. And you suggest that I write even more code, duplicate it wherever current macros are inserted, and the line was not highlighted and will not still be. Come on, that doesn't solve my problem.

You said "You cannot rely on %LINE% and %LINENUM% because they return data about the macro, not where the macro raised an exception. Even %CURRENTROUTINE% is not enough, because in a given routine a macro can be inserted in several places." which suggests that you hadn't read my suggestion. Now if you don't mind I've been trying to make constructive suggestions, showing you how it can potentially be solved (at least as required by your use of Assert()) even if this is clunky.

Don't expect any change to the macro system, and definitely don't expect any compiler hooks for a separate preprocessor: been raised, been rejected, and the developers' position is that macros aren't necessary since anything they can do generics can do better... whether or not I agree with that is irrelevant.

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

furious programming

  • Hero Member
  • *****
  • Posts: 620
Re: Macro as a function call — editor selects wrong line
« Reply #31 on: September 21, 2022, 04:15:45 pm »
@MarkMLl — I understand this. I don't want to force anyone to change the behavior of macros, but just solve my problem and adapt the IDE to my own requirements. You may find it ridiculous, but just what I want to achieve is going to work for me, for this particular project.



Ok guys, finally I did what I wanted to get. To have this syntax:

Code: Pascal  [Select][+][-]
  1. whenever {conditions to met} fail assertion;

I can define macros for the words whenever and fail (just like I did before), while the word assertion can be a normal inline routine calling System.Error(reAssertionFailed). It is so simple. And now when the runtime error is thrown, the call to System.Error(reAssertionFailed) is inlined, so the IDE will always highlight the line containing the call to the procedure assertion, so the line I needed to be highlighted. I'm an idiot and unfortunately I have wasted your time.

Solution looks like this:

Code: Pascal  [Select][+][-]
  1. // in the include file with my macros:
  2.  
  3. {$IFDEF GAME_BUILD_DEBUG}
  4.   {$DEFINE whenever := if}
  5.   {$DEFINE fail     := then}
  6. {$ELSE}
  7.   {$DEFINE whenever := //}
  8. {$ENDIF}
  9.  
  10.  
  11. // in the separate file:
  12.  
  13. {$PUSH}
  14. {$INLINE ON}
  15.  
  16. procedure Assertion(); inline;
  17. begin
  18.   System.Error(reAssertionFailed);
  19. end;
  20.  
  21. {$POP}
  22.  
  23.  
  24. // in any of the project source file:
  25.  
  26. procedure Foo(ADataSize: Integer);
  27. begin
  28.   whenever ADataSize <= 0 fail assertion; // Macros with custom assertion routine, only for "debug" builds.
  29.  
  30.   // other stuff
  31. end;

I am just wondering about one more thing. How it works that the contents of the System unit are always available, even if this unit is not added to the uses list. Is there a way to do something like this with custom unit, so it will be always visible to the compiler without adding it to the uses list?
« Last Edit: September 21, 2022, 04:27:32 pm by furious programming »
Lazarus 2.2.4 with FPC 3.2.2 (2022-05-15), Windows 10 — all 64-bit

Working solo on my own acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL2. Release expected in 2025.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5596
Re: Macro as a function call — editor selects wrong line
« Reply #32 on: September 21, 2022, 05:45:50 pm »
I'm an idiot and unfortunately I have wasted your time.

You're not, and you haven't: discussion like this is always useful. Just remember that you'll have to keep your implementation of assertion concise and simple, otherwise you're likely to find that the compiler will refuse to inline it... and that the definition of simple might be version/variant specific :-/

For what it's worth, I'm one of the people who feels that an improved macro system and/or a hook for an external preprocessor would be valuable. But we're not going to get either of those.

Quote
I am just wondering about one more thing. How it works that the contents of the System unit are always available, even if this unit is not added to the uses list. Is there a way to do something like this with custom unit, so it will be always visible to the compiler without adding it to the uses list?

Try

-Fa<x>[,y] (for a program) load units <x> and [y] before uses is parsed

Apart from that I don't think you can without customising the RTL to your requirements.

MarkMLl

p.s. Stating the obvious, just make you you check whether that inline does what you expect in all case.
« Last Edit: September 21, 2022, 06:06:39 pm by 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

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: Macro as a function call — editor selects wrong line
« Reply #33 on: September 22, 2022, 09:01:50 am »
I am just wondering about one more thing. How it works that the contents of the System unit are always available, even if this unit is not added to the uses list. Is there a way to do something like this with custom unit, so it will be always visible to the compiler without adding it to the uses list?

The System unit is always available, because the compiler makes sure that it is. Just like it automagically adds the ObjPas unit if you select mode ObjFPC or Delphi or various other base level units depending on the one or other setting.

And as MarkMLI wrote, you can use -Fa<name> to add a unit from the command line. This will then be added after the internal ones and before the uses-section.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5596
Re: Macro as a function call — editor selects wrong line
« Reply #34 on: September 22, 2022, 09:18:34 am »
One extra thing does occur to me. This is obviously incomplete but something like

Code: Pascal  [Select][+][-]
  1.     // in the include file with my macros:
  2.      
  3.     {$IFDEF GAME_BUILD_DEBUG}
  4.       {$DEFINE whenever := if}
  5.       {$DEFINE fail     := then}
  6.       {$DEFINE assertion     := assert}
  7.     {$ELSE}
  8.       {$DEFINE whenever := //}
  9.     {$ENDIF}
  10.      
  11.      
  12.     // in any of the project source file:
  13.      
  14.     procedure Foo(ADataSize: Integer);
  15.     begin
  16.       whenever ADataSize <= 0 fail assertion(); // Macros with custom assertion routine, only for "debug" builds.
  17.      
  18.       // other stuff
  19.     end;
  20.  

would probably work, since only the name is being substituted rather than the entire procedure call.

It's not, unfortunately, possible to do something like

Code: Pascal  [Select][+][-]
  1.       {$DEFINE assertion(     := assert(other_stuff_here }
  2. ...
  3.  
  4.       whenever ADataSize <= 0 fail assertion(); // Macros with custom assertion routine, only for "debug" builds.
  5.  

i.e. to use a macro to insert a leading parameter etc.

I'm not sure there's an easy way to make that be useful, I mention it for completeness and in case somebody else spots anything relevant.

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

furious programming

  • Hero Member
  • *****
  • Posts: 620
Re: Macro as a function call — editor selects wrong line
« Reply #35 on: September 22, 2022, 10:06:08 am »
Thanks for the suggestion, but it's not necessary, because I'm working solo on my project and I will always work solo. Also note that with these macros I intentionally call the Error function instead of Assert, because Assert in practice (when debugging within the IDE) works like Halt, i.e. it kills the process, instead of pauses debugging. I do not want this. I just want an error to pop up in the window and after it closes I need to move to the line that caused the error, so that I don't have to look for anything or analyze the logs.

What I gave earlier, i.e. a set of macros and a custom procedure called Assertion, does what I need in 100%, for all cases. And finally it works as it should, i.e. it reports the error and highlights the line where the error occurred and not terminating the program, so I can e.g. watch the variables, etc. 8)
« Last Edit: September 22, 2022, 10:08:25 am by furious programming »
Lazarus 2.2.4 with FPC 3.2.2 (2022-05-15), Windows 10 — all 64-bit

Working solo on my own acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL2. Release expected in 2025.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5596
Re: [SOLVED] Macro as a function call — editor selects wrong line
« Reply #36 on: September 22, 2022, 10:31:01 am »
Understood, but I was pointing out the extent to which your original attempt almost worked: if it hadn't tried to expand the entire statement the debugger would probably have behaved as you wanted.

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: Macro as a function call — editor selects wrong line
« Reply #37 on: September 22, 2022, 01:23:05 pm »
Also note that with these macros I intentionally call the Error function instead of Assert, because Assert in practice (when debugging within the IDE) works like Halt, i.e. it kills the process, instead of pauses debugging.

Do you use the SysUtils unit? Cause then Assert simply raises an ordinary exception. Otherwise the default behaviour is indeed a Halt(227) (plus some output). You can change that however by setting AssertErrorProc (which is what Assert uses internally) to some function of your choice.

furious programming

  • Hero Member
  • *****
  • Posts: 620
Re: [SOLVED] Macro as a function call — editor selects wrong line
« Reply #38 on: September 22, 2022, 04:43:00 pm »
Do you use the SysUtils unit?

No, I do not use any of the standard units other than System.

I tired with different approaches and functions. But the best and easiest was just to use custom error-function and some simple macros.
Lazarus 2.2.4 with FPC 3.2.2 (2022-05-15), Windows 10 — all 64-bit

Working solo on my own acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL2. Release expected in 2025.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: [SOLVED] Macro as a function call — editor selects wrong line
« Reply #39 on: September 23, 2022, 03:06:16 pm »
Do you use the SysUtils unit?

No, I do not use any of the standard units other than System.

I tired with different approaches and functions. But the best and easiest was just to use custom error-function and some simple macros.

You did read the remainder of my message? If you don't use the SysUtils unit, but want to use Assert you should use your own function for AssertErrorProc to avoid the Halt (you can for example simply call Error like you do now then).

 

TinyPortal © 2005-2018