h2pas: fixed commenting complex macros

git-svn-id: trunk@14535 -
This commit is contained in:
mattias 2008-03-15 14:00:25 +00:00
parent 1d92eaff9e
commit 3f7eaa4892
2 changed files with 37 additions and 10 deletions

View File

@ -98,7 +98,8 @@ begin
AtomStart:=Position;
repeat
ReadRawNextCAtom(Source,Position,AtomStart);
until (Position>Len) or (Source[Position] in [#10,#13]);
until (AtomStart>Len) or (Source[AtomStart] in [#10,#13]);
Position:=AtomStart;
{$IFDEF RangeChecking}{$R+}{$UNDEF RangeChecking}{$ENDIF}
end;
@ -321,7 +322,8 @@ begin
if Position<=Len then begin
c2:=Source[Position];
// test for double char operators :=, +=, -=, /=, *=, !=, ==, <=, >=, ^^, ::
if ((c2='=') and (IsEqualOperatorStartChar[c1]))
if ((c1=#13) and (c2=#10))
or ((c2='=') and IsEqualOperatorStartChar[c1])
or ((c1='=') and (c2='='))
or ((c1='!') and (c2='='))
or ((c1=':') and (c2=':'))

View File

@ -4639,18 +4639,23 @@ var
// h2pas has problems with
// - backslash + newline
// - whole functions { }
var
p: LongInt;
AtomStart: integer;
begin
while (StartPos<EndPos) do begin
if Src[StartPos]='{' then begin
p:=StartPos;
repeat
ReadRawNextCAtom(Src,p,AtomStart);
if (AtomStart>=EndPos) or (AtomStart>length(Src)) then break;
if Src[AtomStart]='{' then begin
// this macro is a whole function => too complex
exit(true);
end;
if (Src[StartPos] in [#10,#13]) then begin
if (Src[AtomStart] in [#10,#13]) then begin
// this macro uses multiple lines => too complex
exit(true);
end;
inc(StartPos);
end;
until false;
Result:=false;
end;
@ -4679,6 +4684,28 @@ var
Changed:=true;
end;
procedure CommentDefine(StartPos, EndPos: integer);
begin
// replace sub comments
while (StartPos<EndPos-1) do begin
if (Src[StartPos]='/') and (Src[StartPos+1]='*') then begin
// sub comment found -> disable
// IMPORTANT: replacement must be the same size to keep the positions
Replace(StartPos,StartPos+1,'(');
end;
if (Src[StartPos]='*') and (Src[StartPos+1]='/') then begin
// sub comment found -> disable
// IMPORTANT: replacement must be the same size to keep the positions
Replace(StartPos+1,StartPos+2,')');
end;
inc(StartPos);
end;
// IMPORTANT: insert in reverse order
Replace(EndPos,EndPos,'*/');
Replace(StartPos,StartPos,'/*');
end;
var
DefineStart: LongInt;
DefineEnd: LongInt;
@ -4705,9 +4732,7 @@ begin
DefineEnd:=p;
if DefineIsTooComplex(ValueStart,DefineEnd) then begin
DebugLn(['TCommentComplexCMacros.Execute commenting macro "',dbgstr(copy(Src,DefineStart,DefineEnd-DefineStart)),'"']);
// IMPORTANT: insert in reverse order
Replace(DefineEnd,DefineEnd,'*/');
Replace(DefineStart,DefineStart,'/*');
CommentDefine(DefineStart,DefineEnd);
end;
end;
end;