Recent

Author Topic: {$DEFINE} has lower priority than {$INCLUDE %FILE%}  (Read 4810 times)

zex2011

  • New Member
  • *
  • Posts: 17
{$DEFINE} has lower priority than {$INCLUDE %FILE%}
« on: October 18, 2011, 07:39:40 pm »
There are compiler directives {$INCLUDE %FILE%} and {$INCLUDE %LINE%}

They DON'T load files. Instead they insert [current file name] / [current line] at compile time. So you can make something like this:
DebugLn ('Error in file ' + {$INCLUDE %FILE%} + ' on line ' + {$INCLUDE %LINE%});

I wanted to make this shorter, to avoid repeating the same long string everywhere in the code. So I wrote a macro like this:
{$MACROS ON}
{$DEFINE ErrorInfo:='Error in file ' + {$INCLUDE %FILE%} + ' on line ' + {$INCLUDE %LINE%}}

DebugLn (ErrorInfo);  // this should write the current file and line number

But this doesn't work. The problem is that $INCLUDE executes first so it inserts a fixed file name / line number during the macro declaration. Wherever this macro is used, always the same (incorrect) data is displayed. Not the current line, but line where macro was first declared.

If anyone has experience with FPC macros I'd like to know if there's a way around this?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: {$DEFINE} has lower priority than {$INCLUDE %FILE%}
« Reply #1 on: October 18, 2011, 08:50:58 pm »
Probably it is not possible. I found little related question on fpc mailing list:
http://free-pascal-general.1045716.n5.nabble.com/advanced-macros-td2823767.html
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Murmandamus

  • New member
  • *
  • Posts: 8
Re: {$DEFINE} has lower priority than {$INCLUDE %FILE%}
« Reply #2 on: October 18, 2011, 10:19:20 pm »
It is a pre-processor feature, which means it is only evaluated once at compile time. You'd need a way to escape the {$INCLUDE x} variables so that they are re-processed when the macro is expanded.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12712
  • FPC developer.
Re: {$DEFINE} has lower priority than {$INCLUDE %FILE%}
« Reply #3 on: October 19, 2011, 11:53:35 am »
Free Pascal is not a preprocessor heavy language and not considering changing that.

If you have a need for very complex preprocessing, it is best to use an external preprocessor.

 

TinyPortal © 2005-2018