Recent

Author Topic: likely bug in comment/directive parsing  (Read 1928 times)

440bx

  • Hero Member
  • *****
  • Posts: 6088
likely bug in comment/directive parsing
« on: November 26, 2025, 01:35:57 am »
Hello,

Consider the following code:
Code: Pascal  [Select][+][-]
  1. {$MODESWITCH NESTEDCOMMENTS OFF }
  2.  
  3. program _Sizeof;
  4.  
  5.  
  6. type
  7.   ANYSIZE_ARRAY = 0..0;
  8.  
  9. type
  10.   { _BOOT_ENTRY                                                               }
  11.  
  12.   PBOOT_ENTRY = ^TBOOT_ENTRY;
  13.   TBOOT_ENTRY = packed record
  14.     Version              : DWORD;
  15.     Length               : DWORD;
  16.     Id                   : DWORD;
  17.     Attributes           : DWORD;
  18.     FriendlyNameOffset   : DWORD;
  19.     BootFilePathOffset   : DWORD;
  20.     OsOptionsLength      : DWORD;
  21.  
  22.     OsOptions            : packed array[ANYSIZE_ARRAY]  { OsOptionsLength     }
  23.                                      of byte;
  24.   end;
  25.  
  26.   { unterminated comment - CLOSING BRACE MISSING
  27.  
  28.   {$if sizeof(TBOOT_ENTRY) <> $1d}
  29.     {$FATAL sizeof TBOOT_ENTRY does not equal $1d}
  30.   {$endif}
  31.  
  32.  
  33. begin
  34.   writeln('sizeof TBOOT_ENTRY: ', sizeof(TBOOT_ENTRY));
  35.  
  36.   readln;
  37. end.    
  38.  
There is a closing brace missing in line 26.

When attempting to compile the program, the compiler emits the message "sizeof TBOOT_ENTRY does not equal $1d" indicating that the preprocessor found the $FATAL message in spite of it being in a regular comment (the unclosed comment.)

Additionally, in spite of the NESTEDCOMMENTS OFF directive, there is no message/warning/error about the nested comment.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12116
  • Debugger - SynEdit - and more
    • wiki
Re: likely bug in comment/directive parsing
« Reply #1 on: November 26, 2025, 02:06:29 am »
There is a closing brace missing in line 26.

When attempting to compile the program, the compiler emits the message "sizeof TBOOT_ENTRY does not equal $1d" indicating that the preprocessor found the $FATAL message in spite of it being in a regular comment (the unclosed comment.)

Additionally, in spite of the NESTEDCOMMENTS OFF directive, there is no message/warning/error about the nested comment.

It's late, but...

If nested comments are OFF, then the comment starting in line 26 ends in line 28 (at the end of "{$IF....").
The extra opening "{" does not open a nested comment, so a simple single level comment keeps going....

Then of course when the comment ends at eol line 28, the {$FATAL on line 29 is seen?

Khrys

  • Sr. Member
  • ****
  • Posts: 391
Re: likely bug in comment/directive parsing
« Reply #2 on: November 26, 2025, 06:44:38 am »
Additionally, in spite of the NESTEDCOMMENTS OFF directive, there is no message/warning/error about the nested comment.

Like already @Martin_fr explained, this is to be expected -  {$modeswitch nestedcomments off}  turns off support for nested comments at the parser level (as opposed to still parsing them & generating fatal errors).
Turning on nested comments also behaves exactly as expected - the  {$if}  directive is assumed to be a nested comment (and is reported as a warning) while the comment continues until EOF.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: likely bug in comment/directive parsing
« Reply #3 on: November 26, 2025, 10:50:43 am »
It is more like the editor at hand (Lazarus?) confuses you by syntax highlighting code that is in effect a comment so should be greyed/ comment styled when nested comments are off.
This also happens - or does not happen! - with the officially documented comment style {$if 0}/{$ifend}

Basically it is a shortcoming of the IDE. The compiler handles it correctly.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

440bx

  • Hero Member
  • *****
  • Posts: 6088
Re: likely bug in comment/directive parsing
« Reply #4 on: November 26, 2025, 11:11:50 am »
I understand the thinking but, I find it rather dubious.   The opening brace is not a closing brace (and should not be thought of as one) therefore it cannot be considered as closing the unclosed comment.

The absence of a closing brace creates a syntactically incorrect construction the compiler should simply reject.

The compiler has been specifically told that there should not be nested comments yet, it ignores the nested comment, i.e, the sequence of two opening braces, which should result in a compiler error.

NESTEDCOMMENTS OFF, that's just a xmas ornament!.  It tells the compiler that a second opening brace acts as a implied closing brace.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 7523
Re: likely bug in comment/directive parsing
« Reply #5 on: November 26, 2025, 11:21:24 am »
@440ex
Off topic:
 Are you sure the last field is not supposed to be 0 size content?

Jamie
The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 1568
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: likely bug in comment/directive parsing
« Reply #7 on: November 26, 2025, 12:13:34 pm »
@440bx
The compiler won't even see it....Why should it parse commented code?
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

440bx

  • Hero Member
  • *****
  • Posts: 6088
Re: likely bug in comment/directive parsing
« Reply #8 on: November 26, 2025, 01:04:38 pm »
I consider FPC's entire handling of block comments to be erroneous but, I realize it's not going to change regardless of the resulting atrocities.

Obviously, the preprocessor which is part of the scanner parses some comments.  Since the scanner is part of the compiler, it is a fact that the compiler (a specific part of it) sees it.

I'm done with this.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12116
  • Debugger - SynEdit - and more
    • wiki
Re: likely bug in comment/directive parsing
« Reply #9 on: November 26, 2025, 01:15:44 pm »
The compiler has been specifically told that there should not be nested comments yet, it ignores the nested comment, i.e, the sequence of two opening braces, which should result in a compiler error.

NESTEDCOMMENTS OFF, that's just a xmas ornament!.  It tells the compiler that a second opening brace acts as a implied closing brace.

NESTEDCOMMENTS OFF does not mean to give an error on nested comments.

It means not to "know about the possibility of nesting" (and then consequently, they don't exist, so they can not give an error).

Code: Pascal  [Select][+][-]
  1. {$MODESWITCH NESTEDCOMMENTS OFF }
  2.  
  3.  { start a comment
  4.   ....
  5.  { this is just another commented char. It has no meaning at all.
  6.   ....
  7.   }  // this closed the comment
  8.  
  9.  
  10.   // < this starts a comment // < this does not start a nested comment
  11.  

Inside a comment, any further "{" is not seen as start of a (nested) comment. Its no different than any other char (except the closing "}").



440bx

  • Hero Member
  • *****
  • Posts: 6088
Re: likely bug in comment/directive parsing
« Reply #10 on: November 26, 2025, 01:31:36 pm »
The open brace in Pascal is a special character that indicates the start of a block comment. 

Whenever an open brace is found, the compiler should interpret that as an attempt of opening a comment (that's what the open brace is for), therefore an open brace found inside a comment should be seen as an attempt of creating a nested comment, which is something not supported (at least not in standard Pascal.)

The attempt of supporting nested comments is a mistake with highly undesirable consequences.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: likely bug in comment/directive parsing
« Reply #11 on: November 26, 2025, 01:58:23 pm »
@440bx

THAT is something I wholly agree with.
Rip it out, nobody uses it or relies on it anyway.

I also never found any use for it, looks more like a hobby project.
Code: Pascal  [Select][+][-]
  1. {$but}
maybe?
« Last Edit: November 26, 2025, 02:04:15 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12116
  • Debugger - SynEdit - and more
    • wiki
Re: likely bug in comment/directive parsing
« Reply #12 on: November 26, 2025, 02:23:36 pm »
The open brace in Pascal is a special character that indicates the start of a block comment. 

Whenever an open brace is found, the compiler should interpret that as an attempt of opening a comment (that's what the open brace is for), therefore an open brace found inside a comment should be seen as an attempt of creating a nested comment, which is something not supported (at least not in standard Pascal.)

The attempt of supporting nested comments is a mistake with highly undesirable consequences.

In a string too? ;) SCNR

But joke aside, it's a matter of definition.

$NESTEDCOMMENT is defined as making it (or making it NOT) such a special char.

What should
$NESTEDCOMMENT off
do, if not "making it a normal char inside comments"?

If it keeps it a special char, then when found inside a comment, it would be a nested comment. But that conflicts with having that turned off... So what then? An error? Well the mode for "an error" just does not exist.

440bx

  • Hero Member
  • *****
  • Posts: 6088
Re: likely bug in comment/directive parsing
« Reply #13 on: November 26, 2025, 03:19:56 pm »
The presence of a { inside a comment that was started with a { should _always_ be considered an error.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: likely bug in comment/directive parsing
« Reply #14 on: November 26, 2025, 03:57:41 pm »
Only with nestedcomments on.
I don't get it: the parser knows it need to find the closing. IT DOES!
It should not even see it if nestedcomments are off, so where is your problem?

But as I wrote: who the hell allowed nested comments anyway.

Petty project?
« Last Edit: November 26, 2025, 03:59:17 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018