diff --git a/components/codetools/examples/fixfilenames.pas b/components/codetools/examples/fixfilenames.pas index 07fc20c572..62df9e1856 100644 --- a/components/codetools/examples/fixfilenames.pas +++ b/components/codetools/examples/fixfilenames.pas @@ -39,6 +39,8 @@ var Code: TCodeBuffer; Filename: String; MissingUnits: TStrings; + ReplaceUnits: TStringList; + MissingIncludeFilesCodeXYPos: TFPList; begin // setup the Options Options:=TCodeToolsOptions.Create; @@ -72,8 +74,20 @@ begin raise Exception.Create('unable to read '+Filename); // fix the filenames in the include directives - if not CodeToolBoss.FixIncludeFilenames(Code,true) then + MissingIncludeFilesCodeXYPos:=nil; + if not CodeToolBoss.FixIncludeFilenames(Code,true, + MissingIncludeFilesCodeXYPos) + then raise Exception.Create('unable to fix include filesnames in '+Filename+' '+CodeToolBoss.ErrorMessage); + CodeToolBoss.FreeListOfPCodeXYPosition(MissingIncludeFilesCodeXYPos); + + // replace some unit names + ReplaceUnits:=TStringList.Create; + ReplaceUnits.Add('biglettersunit=SysUtils'); + ReplaceUnits.Free; + + writeln('=================================================================='); + writeln(Code.Source); // fix the unitnames in the uses section MissingUnits:=nil; @@ -84,9 +98,12 @@ begin writeln('MissingUnits=',MissingUnits.Text); if not CodeToolBoss.CommentUnitsInUsesSections(Code,MissingUnits) then raise Exception.Create('unable to comment units in uses section in '+Filename+' '+CodeToolBoss.ErrorMessage); + MissingUnits.Free; end; - + writeln('=================================================================='); writeln(Code.Source); + + Options.Free; end. diff --git a/components/codetools/examples/scanexamples/brokenfilenames.pas b/components/codetools/examples/scanexamples/brokenfilenames.pas index 15be199b27..b019003eab 100644 --- a/components/codetools/examples/scanexamples/brokenfilenames.pas +++ b/components/codetools/examples/scanexamples/brokenfilenames.pas @@ -10,7 +10,12 @@ uses biglettersunit, // must be fixed to BigLettersUnit biglettersunit in 'biglettersunit.pas',// -> BigLettersUnit.pas biglettersunit in '..\ScanExamples\biglettersunit.pas',// -> ../scanexamples/BigLettersUnit - NonExistingUnit1, NonExistingUnit2, SysUtils, NonExistingUnit3; + NonExistingUnit1, NonExistingUnit2, SysUtils, NonExistingUnit3, + {$IFDEF FPC} + NonExistingUnit4 + {$ELSE} + NonExistingUnit5 + {$ENDIF}; {$I BROKENincfiles.inc}// must be fixed to brokenincfiles.inc {$I ../ScanExamples/BROKENincfiles.inc}// must be fixed to ../scanexamples/brokenincfiles.inc diff --git a/components/codetools/stdcodetools.pas b/components/codetools/stdcodetools.pas index cc767c0cdc..ca4f49bab4 100644 --- a/components/codetools/stdcodetools.pas +++ b/components/codetools/stdcodetools.pas @@ -1000,36 +1000,39 @@ function TStandardCodeTool.CommentUnitsInUsesSections(MissingUnits: TStrings; procedure Comment(CommentStartPos, CommentEndPos: integer); var i: LongInt; + CurStartPos: LongInt; + CommentNeeded: Boolean; + CurEndPos: LongInt; begin if CommentStartPos>=CommentEndPos then RaiseException('TStandardCodeTool Comment'); - // try curly brackets {} + // comment with curly brackets {} i:=CommentStartPos; - while (i'}') do inc(i); - if i=CommentEndPos then begin - SourceChangeCache.Replace(gtNone,gtNone,CommentStartPos,CommentStartPos,'{'); - SourceChangeCache.Replace(gtNone,gtNone,CommentEndPos,CommentEndPos,'}'); - //debugln('Comment {} "',copy(Src,CommentStartPos,CommentEndPos-CommentStartPos)); - exit; - end; - // try (* *) - i:=CommentStartPos; - while (i'*') or (Src[i+1]<>')')) do inc(i); - if i=CommentEndPos-1 then begin - SourceChangeCache.Replace(gtNone,gtNone,CommentStartPos,CommentStartPos,'(*'); - SourceChangeCache.Replace(gtNone,gtNone,CommentEndPos,CommentEndPos,'*)'); - //debugln('Comment (**) "',copy(Src,CommentStartPos,CommentEndPos-CommentStartPos)); - exit; - end; - // do it with // comments - SourceChangeCache.Replace(gtNone,gtNone,CommentStartPos,CommentStartPos,'//'); - SourceChangeCache.Replace(gtNone,gtNewLine,CommentEndPos,CommentEndPos,' '); - //debugln('Comment // "',copy(Src,CommentStartPos,CommentEndPos-CommentStartPos)); - for i:=CommentStartPos+1 to CommentEndPos-1 do - if (Src[i-1] in [#10,#13]) and (not (Src[i] in [#10,#13])) then begin - SourceChangeCache.Replace(gtNone,gtNone,i,i,'//'); + CurStartPos:=CommentStartPos; + CurEndPos:=CurStartPos; + CommentNeeded:=false; + repeat + if (Src[i]='{') or (i>=CommentEndPos) then begin + // the area contains a comment -> comment in front + if CommentNeeded then begin + SourceChangeCache.Replace(gtNone,gtNone,CurStartPos,CurStartPos,'{'); + SourceChangeCache.Replace(gtNone,gtNone,CurEndPos,CurEndPos,'}'); + //DebugLn('Comment "',copy(Src,CurStartPos,i-CurStartPos),'"'); + CommentNeeded:=false; + end; + if i>=CommentEndPos then break; + // skip comment + i:=FindCommentEnd(Src,i,Scanner.NestedComments); + end else if not IsSpaceChar[Src[i]] then begin + if not CommentNeeded then begin + CurStartPos:=i; + CommentNeeded:=true; + end; + CurEndPos:=i+1; end; + inc(i); + until false; end; function CommentUnitsInUsesSection(UsesNode: TCodeTreeNode): boolean;