Przeglądaj źródła

fcl-passrc: fixed parsing comment in $IFDEF, $IFNDEF, issue #34711

git-svn-id: trunk@40582 -
Mattias Gaertner 6 lat temu
rodzic
commit
65fdc04dc3

+ 23 - 6
packages/fcl-passrc/src/pscanner.pp

@@ -749,6 +749,7 @@ type
     procedure SetReadOnlyModeSwitches(const AValue: TModeSwitches);
     procedure SetReadOnlyModeSwitches(const AValue: TModeSwitches);
     procedure SetReadOnlyValueSwitches(const AValue: TValueSwitches);
     procedure SetReadOnlyValueSwitches(const AValue: TValueSwitches);
   protected
   protected
+    function ReadIdentifier(const AParam: string): string;
     function FetchLine: boolean;
     function FetchLine: boolean;
     procedure AddFile(aFilename: string); virtual;
     procedure AddFile(aFilename: string); virtual;
     function GetMacroName(const Param: String): String;
     function GetMacroName(const Param: String): String;
@@ -3457,13 +3458,16 @@ begin
 end;
 end;
 
 
 procedure TPascalScanner.HandleIFDEF(const AParam: String);
 procedure TPascalScanner.HandleIFDEF(const AParam: String);
+var
+  aName: String;
 begin
 begin
   PushSkipMode;
   PushSkipMode;
   if PPIsSkipping then
   if PPIsSkipping then
     PPSkipMode := ppSkipAll
     PPSkipMode := ppSkipAll
   else
   else
     begin
     begin
-    if IsDefined(AParam) then
+    aName:=ReadIdentifier(AParam);
+    if IsDefined(aName) then
       PPSkipMode := ppSkipElseBranch
       PPSkipMode := ppSkipElseBranch
     else
     else
       begin
       begin
@@ -3472,20 +3476,23 @@ begin
       end;
       end;
     If LogEvent(sleConditionals) then
     If LogEvent(sleConditionals) then
       if PPSkipMode=ppSkipElseBranch then
       if PPSkipMode=ppSkipElseBranch then
-        DoLog(mtInfo,nLogIFDefAccepted,sLogIFDefAccepted,[AParam])
+        DoLog(mtInfo,nLogIFDefAccepted,sLogIFDefAccepted,[aName])
       else
       else
-        DoLog(mtInfo,nLogIFDefRejected,sLogIFDefRejected,[AParam]);
+        DoLog(mtInfo,nLogIFDefRejected,sLogIFDefRejected,[aName]);
     end;
     end;
 end;
 end;
 
 
 procedure TPascalScanner.HandleIFNDEF(const AParam: String);
 procedure TPascalScanner.HandleIFNDEF(const AParam: String);
+var
+  aName: String;
 begin
 begin
   PushSkipMode;
   PushSkipMode;
   if PPIsSkipping then
   if PPIsSkipping then
     PPSkipMode := ppSkipAll
     PPSkipMode := ppSkipAll
   else
   else
     begin
     begin
-    if IsDefined(AParam) then
+    aName:=ReadIdentifier(AParam);
+    if IsDefined(aName) then
       begin
       begin
       PPSkipMode := ppSkipIfBranch;
       PPSkipMode := ppSkipIfBranch;
       PPIsSkipping := true;
       PPIsSkipping := true;
@@ -3494,9 +3501,9 @@ begin
       PPSkipMode := ppSkipElseBranch;
       PPSkipMode := ppSkipElseBranch;
     If LogEvent(sleConditionals) then
     If LogEvent(sleConditionals) then
       if PPSkipMode=ppSkipElseBranch then
       if PPSkipMode=ppSkipElseBranch then
-        DoLog(mtInfo,nLogIFNDefAccepted,sLogIFNDefAccepted,[AParam])
+        DoLog(mtInfo,nLogIFNDefAccepted,sLogIFNDefAccepted,[aName])
       else
       else
-        DoLog(mtInfo,nLogIFNDefRejected,sLogIFNDefRejected,[AParam]);
+        DoLog(mtInfo,nLogIFNDefRejected,sLogIFNDefRejected,[aName]);
     end;
     end;
 end;
 end;
 
 
@@ -4682,6 +4689,16 @@ begin
   FReadOnlyValueSwitches:=AValue;
   FReadOnlyValueSwitches:=AValue;
 end;
 end;
 
 
+function TPascalScanner.ReadIdentifier(const AParam: string): string;
+var
+  p, l: Integer;
+begin
+  p:=1;
+  l:=length(AParam);
+  while (p<=l) and (AParam[p] in IdentChars) do inc(p);
+  Result:=LeftStr(AParam,p-1);
+end;
+
 function TPascalScanner.FetchLine: boolean;
 function TPascalScanner.FetchLine: boolean;
 begin
 begin
   if CurSourceFile.IsEOF then
   if CurSourceFile.IsEOF then

+ 1 - 1
packages/fcl-passrc/tests/tcscanner.pas

@@ -1404,7 +1404,7 @@ procedure TTestScanner.TestDefine2;
 
 
 begin
 begin
   FSCanner.Defines.Add('ALWAYS');
   FSCanner.Defines.Add('ALWAYS');
-  TestTokens([tkComment,tkWhitespace,tkOf,tkWhitespace,tkcomment],'{$IFDEF ALWAYS} of {$ENDIF}');
+  TestTokens([tkComment,tkWhitespace,tkOf,tkWhitespace,tkcomment],'{$IFDEF ALWAYS comment} of {$ENDIF}');
 end;
 end;
 
 
 procedure TTestScanner.TestDefine21;
 procedure TTestScanner.TestDefine21;