Recent

Author Topic: unexpected behavior in FPC v3.2.2  (Read 930 times)

440bx

  • Hero Member
  • *****
  • Posts: 4901
unexpected behavior in FPC v3.2.2
« on: October 27, 2024, 08:47:02 am »
Hello,

Consider the following program:
Code: Pascal  [Select][+][-]
  1.  
  2. {$define (NoClosingParenthesis}   { no complaint about missing parenthesis    }
  3. {$define NoOpeningParenthesis)}   { no complaint about missing parenthesis    }
  4.  
  5.  
  6. program _DirectivesParentheses;
  7.  
  8. begin
  9. end.                  
Note that the compiler directives have mismatched parentheses, yet the compiler compiles that code without complaints.

Comments welcome.
« Last Edit: October 27, 2024, 10:55:11 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

dbannon

  • Hero Member
  • *****
  • Posts: 3195
    • tomboy-ng, a rewrite of the classic Tomboy
Re: unexpected behavior in FPC v3.2.2
« Reply #1 on: October 27, 2024, 10:23:12 am »
Its one or the other of -
  • You have discovered a new compiler directive.
  • The compiler does not check for matching Parenthesis inside a comment.

I think I prefer the latter.  A {$define } is a comment when you think about it, a comment left for the compiler rather than a human reader but still a comment.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MarkMLl

  • Hero Member
  • *****
  • Posts: 8129
Re: unexpected behavior in FPC v3.2.2
« Reply #2 on: October 27, 2024, 10:26:39 am »
Its one or the other of -
  • You have discovered a new compiler directive.
  • The compiler does not check for matching Parenthesis inside a comment.

I think I prefer the latter.  A {$define } is a comment when you think about it, a comment left for the compiler rather than a human reader but still a comment.

Davo

What is the behaviour of the -d option on the commandline (with appropriate shell quoting/escaping)?

What is the behaviour of {$ifdef with those mismatched parentheses?

Can't check myself, out in a few mins.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

440bx

  • Hero Member
  • *****
  • Posts: 4901
Re: unexpected behavior in FPC v3.2.2
« Reply #3 on: October 27, 2024, 10:53:01 am »
I think I prefer the latter.  A {$define } is a comment when you think about it, a comment left for the compiler rather than a human reader but still a comment.
It's a comment but one that must comply with some rules.  For instance:
Code: Pascal  [Select][+][-]
  1. {$ifdef (SomeName}  // note the missing closing parenthesis
does NOT compile because the compiler insists on having a closing parenthesis in that "comment".

It was a reasonable thought but, directives are comments that are held to some rules they must follow.  Parentheses must be matched because they cannot be part of the identifier, therefore a lone parenthesis is an extraneous character.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11997
  • FPC developer.
Re: unexpected behavior in FPC v3.2.2
« Reply #4 on: October 27, 2024, 10:53:46 am »
IIRC there is a "feature" inherited from turbo pascal that trailing tokens of a $define are ignored. That explains the second one.

dbannon

  • Hero Member
  • *****
  • Posts: 3195
    • tomboy-ng, a rewrite of the classic Tomboy
Re: unexpected behavior in FPC v3.2.2
« Reply #5 on: October 27, 2024, 11:27:45 am »
Yes Marcov, the trailing Parentheses seems to be ignored, $ifdef finds the identifier both with and without the Parentheses. But the leading Parentheses is a problem.

Code: Pascal  [Select][+][-]
  1. {$define (NoNothing}  
  2. {$define NoSomething)}  
  3.  
  4. program _DirectivesParentheses;
  5.  
  6. begin
  7. //      {$ifdef (NoNothing}             // This will not compile
  8. //      writeln('(NoNothing');
  9. //      {$endif}
  10.         {$ifdef NoNothing}             
  11.         writeln('NoNothing');    // This is NOT printed
  12.         {$endif}
  13.         {$ifdef NoSomething)}
  14.         writeln('NoSomething)');   // This IS printed
  15.         {$endif}
  16.         {$ifdef NoSomething}
  17.         writeln('NoSomething');   // This IS printed
  18.         {$endif}
  19. end.  
  20.  
   

You could argue that the way it is here could lead to one of those bugs where we sit and stare at the computer screen, getting angrier by the minute until we spot that leading Parentheses. But I can think of a other things I'd like to see fixed first !

Davo
« Last Edit: October 27, 2024, 11:29:19 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: unexpected behavior in FPC v3.2.2
« Reply #6 on: October 27, 2024, 10:56:15 pm »
Delphi 7 does not allow the first one, but allows the second one.

Bart

 

TinyPortal © 2005-2018