Recent

Author Topic: Undocumented compiler directive ?  (Read 5118 times)

440bx

  • Hero Member
  • *****
  • Posts: 4014
Undocumented compiler directive ?
« on: June 11, 2021, 09:58:39 am »
Just playing around, I came across the fact that {$ifend} seems to be an "undocumented" synonym of "$endif" as the following short program shows:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2.  
  3.  
  4. program CompilerDirectives;
  5.  
  6. const
  7.   AConst = 1;
  8.  
  9.  
  10. begin
  11.   { using endif                                                               }
  12.  
  13.   {$ifdef FPC }
  14.     {$if AConst = 3}
  15.       writeln('AConst = ', AConst);
  16.     {$endif}
  17.   {$endif}
  18.  
  19.   {$ifdef FPC}
  20.     {$if AConst = 1}
  21.       writeln('AConst = ONE');
  22.     {$endif}
  23.   {$endif}
  24.  
  25.  
  26.   { using ifend                                                               }
  27.  
  28.   {$ifdef FPC }
  29.     {$if AConst = 3}
  30.       writeln('AConst = ', AConst);
  31.     {$ifend}
  32.   {$ifend}
  33.  
  34.   {$ifdef FPC}
  35.     {$if AConst = 1}
  36.       writeln('AConst = ONE');
  37.     {$ifend}
  38.   {$ifend}
  39.  
  40.   readln;
  41. end.              
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Undocumented compiler directive ?
« Reply #1 on: June 11, 2021, 10:50:19 am »
AFAIK it is documented. It is also Delphi compatible.

EDIT
In the latest docs it is not documented. Seems a regression.
The preferred way to close $IF is $IFEND, not $ENDIF, but both can be used.
Order is:
{$IFDEF/$ENDIF} but NOT $IFEND
{$IF/$IFEND} but ALSO $ENDIF
But $ENDIF and $IFEND should be compatible if, and only if {$IF} is used, so plz file a bug against documentation.
« Last Edit: June 11, 2021, 11:01:03 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: Undocumented compiler directive ?
« Reply #2 on: June 11, 2021, 11:17:46 am »
There's something wrong here. Newly created Lazarus project using trunk:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes
  10.   { you can add units after this };
  11.  
  12. begin
  13. end.
  14.  

I'm pretty sure I've always used $endif, going back to Turbo Pascal and before. $ifend is the aberration.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Undocumented compiler directive ?
« Reply #3 on: June 11, 2021, 11:23:39 am »

I'm pretty sure I've always used $endif, going back to Turbo Pascal and before. $ifend is the aberration.

MarkMLl
Turbo pascal doesn't know $IF , Mark.... Only $IFDEF
Also at least D1 and D2 do not know about {$IF}
Defines with conditional expressions like using $IF defined() or declared() were introduced in a later delphi version.
« Last Edit: June 11, 2021, 11:27:50 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: Undocumented compiler directive ?
« Reply #4 on: June 11, 2021, 11:25:59 am »

I'm pretty sure I've always used $endif, going back to Turbo Pascal and before. $ifend is the aberration.

MarkMLl
Turbo pascal doesn't know $IF , Mark.... Only $IFDEF

Yes, and the standard way of closing conditionals is endif.

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

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Undocumented compiler directive ?
« Reply #5 on: June 11, 2021, 11:30:44 am »

I'm pretty sure I've always used $endif, going back to Turbo Pascal and before. $ifend is the aberration.

MarkMLl
Turbo pascal doesn't know $IF , Mark.... Only $IFDEF

Yes, and the standard way of closing conditionals is endif.

MarkMLl
NO it is $IF/$IFEND. I will search for the initial introduction of the feature.
It was done on purpose to resolve confusion with closing $IFDEF and add readability.
They actually introduced a bug by accepting $endif for $if.
« Last Edit: June 11, 2021, 11:48:03 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: Undocumented compiler directive ?
« Reply #6 on: June 11, 2021, 11:38:08 am »
NO it is $IF/$IFEND. I will search for the initial introduction of the feature.
It was done on purpose to resolve confusion and add readability.
They actually introduced a bug by accepting $endif for $if.

If it's /strictly/ for $if then that's fine, but other combinations should result in errors to reduce potential confusion.

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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Undocumented compiler directive ?
« Reply #7 on: June 11, 2021, 11:43:22 am »
Yes, and the standard way of closing conditionals is endif.

Not all conditionals, buf $IFDEF ones (which were the only ones at the time). That's (probably) why they chose a new word ($IFEND) to close the then new $IFs. Though IMHO it wasn't a specially good idea ... :-\

Then, as always happens, they relaxed the sintax and you could mix-and-match however you wanted so instead of a unique, clean construct you now (still?) can find any odd combination, leading to the confussion they were trying to avoid in the first place >:D

ETA: Forgot to add; in case anyone's looking for it, $IF was introduced in Delphi 6 (w/out $IFEND, IIRC; I think that was added in D7)
« Last Edit: June 11, 2021, 11:50:49 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Undocumented compiler directive ?
« Reply #8 on: June 11, 2021, 11:50:08 am »
They did not relax the syntax, but IMHO introduced a bug in their parser....
I am not the only one of that opinion. Has been discussed in the past.
I think Lucamar is right about the versions. It also explains why $ENDIF can still be parsed because of backwards compatibility reasons between D7 and D6. That is a valid reason, although very unfortunate.
« Last Edit: June 11, 2021, 11:54:42 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Undocumented compiler directive ?
« Reply #9 on: June 11, 2021, 11:51:27 am »
If it's /strictly/ for $if then that's fine, but other combinations should result in errors to reduce potential confusion.

MarkMLl
I totally agree.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Undocumented compiler directive ?
« Reply #10 on: June 11, 2021, 12:12:09 pm »
I think Lucamar is right about the versions. It also explains why $ENDIF can still be parsed because of backwards compatibility reasons between D7 and D6. That is a valid reason, although very unfortunate.

Note that one rationale for $ifend was precisely backwards compatibility, so you could compile source containing $if...$elseif...$ifend with, say, Delphi 5 by surrounding the block with e.g. {$IFDEF D6UP}...{$ENDIF}, so I might be wrong and $ifend might have been introduced in D6 along with $if.

I jumped straight from D5 to D7 so my knowledge of D6 is rather sketchy :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11446
  • FPC developer.
Re: Undocumented compiler directive ?
« Reply #11 on: June 11, 2021, 12:33:11 pm »
In some later version (XE3?) Delphi allowed  endif as ending for $IF.

For backwards compatibility there is a switch:    {$LEGACYIFEND ON}

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: Undocumented compiler directive ?
« Reply #12 on: June 11, 2021, 01:27:13 pm »
In some later version (XE3?) Delphi allowed  endif as ending for $IF.

For backwards compatibility there is a switch:    {$LEGACYIFEND ON}
XE4 http://docwiki.embarcadero.com/RADStudio/Sydney/en/Legacy_IFEND_(Delphi)
and
http://docwiki.embarcadero.com/RADStudio/Sydney/en/IFEND_directive_(Delphi)
I find that rather disturbing.
Ratio is that $IF greatly differs from $IFDEF, which, to my mind, warrants a different closing.
Anyway, my code always uses  $IFEND for $IF blocks and $ENDIF for $IFDEF/$IFOPT blocks.
Reason is simply readability: if I encounter a $IFEND I know there should be a $IF somewhere up. If I encounter a $ENDIF I know I should look for a $IFDEF/$IFOPT.
It is really a pity that got lost. Well, no quality control anyway overthere...
Introducing that "legacy" define is a gotspe  >:D >:D
Looks to me a parser rewriite and document a bug as a feature under false pretences.
As I wrote: I am not the only one who noticed this.

And I also remember that for $IFOPT a $OPTEND/$ENDOPT was suggested  8-)
« Last Edit: June 11, 2021, 01:53:50 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: Undocumented compiler directive ?
« Reply #13 on: June 11, 2021, 03:31:26 pm »
Just playing around, I came across the fact that {$ifend} seems to be an "undocumented" synonym of "$endif" as the following short program shows:

The $IFEND-directive was introduced by Delphi together with the $IF-directive and one needed to close a $IF with a $IFEND while a $IFDEF continued to be closed with a $ENDIF. Later Delphi versions (XE3 or so) relaxed this so that $ENDIF can be used with $IF as well.

FPC on the other hand got support for $IF rather soon and never had the restriction of $IF requiring $IFEND (or for that matter $IFDEF requiring $ENDIF), cause $IFEND is simply an alias to $ENDIF. $ENDIF is considered the main terminator of a conditional clause with $IFEND flying under the radar and existing mostly for Delphi compatibility which might explain why it was never documented.

So if you want, you can raise a bug report about $IFEND missing from the documentation, but in the end it's merely an alias for $ENDIF.

440bx

  • Hero Member
  • *****
  • Posts: 4014
Re: Undocumented compiler directive ?
« Reply #14 on: June 11, 2021, 04:16:36 pm »
So if you want, you can raise a bug report about $IFEND missing from the documentation, but in the end it's merely an alias for $ENDIF.
I'll file a bug report just because I believe its existence should be mentioned even though I can see that it is a rather unimportant element of the preprocessor.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018