DelphiAST icon indicating copy to clipboard operation
DelphiAST copied to clipboard

Multiple spaces after IFDEF is causing defect

Open sglienke opened this issue 7 years ago • 4 comments

Following code is a simplified test case for this defect - originally found when trying to parse System.pas from Delphi 10.2

unit Unit1;

interface

implementation

procedure Foo;
{$IFDEF  CPUX86}
begin
end;
{$ENDIF}

initialization

end.

I traced it down to TmwBasePasLex.GetDirectiveParam returning the string with a leading space character in this case.

sglienke avatar Sep 18 '18 14:09 sglienke

I fixed this by changing the before mentioned method as follows:

-  if FBuffer.Buf[TempRun] = ' ' then Inc(TempRun);
+  while FBuffer.Buf[TempRun] = ' ' do Inc(TempRun);
+  while FBuffer.Buf[EndPos - 1] = ' ' do Dec(EndPos);

Only the first added line is in fact necessary to make System.pas being parsed again but I added the second one to see if that fixes any trailing spaces.

However there are more defects like this. For example following code still fails because of the implementation of TmwBasePasLex.EvaluateConditionalExpression.

unit Unit1;

interface

implementation

procedure Foo;
{$IF defined( CPUX86 )}
begin
end;
{$ENDIF}

initialization

end.

I think this will also fail because of tabs which is why I am not proposing some pull request for this because my fixes are just incomplete.

sglienke avatar Sep 18 '18 14:09 sglienke

Why not just do:

while CharInSet(FBuffer.Buf[TempRun], whitespace) do Inc(TempRun);
while CharInSet(FBuffer.Buf[EndPos - 1], whitespace) do Dec(EndPos);

Where whitespace is [#32,#9,#10,#13] and whatever else happens to qualify.
Not sure about unicode issues though.

JBontes avatar Nov 02 '18 09:11 JBontes

Why not just do:

while CharInSet(FBuffer.Buf[TempRun], whitespace) do Inc(TempRun);
while CharInSet(FBuffer.Buf[EndPos - 1], whitespace) do Dec(EndPos);

Where whitespace is [#32,#9,#10,#13] and whatever else happens to qualify. Not sure about unicode issues though.

Simple, because IFDEF cannot span multiple lines. I mentioned tabs because I noticed but there are also not handled in some other places where they should be valid so I would not address that one in the context of this particular issue.

sglienke avatar Jan 25 '19 10:01 sglienke

@RomanYankovsky it look like Resolved. Maybe time to close the issue? :-)

bogdanpolak avatar Jun 08 '22 12:06 bogdanpolak