From 1d9f747f932167f9df00a3ac122b87b9ae146cdf Mon Sep 17 00:00:00 2001 From: mattias Date: Wed, 22 Mar 2006 01:10:33 +0000 Subject: [PATCH] delphi package conversion: implemented converting package units git-svn-id: trunk@8978 - --- components/codetools/basiccodetools.pas | 3 ++- components/codetools/codetoolmanager.pas | 18 +++++++++++++++ components/codetools/finddeclarationtool.pas | 24 +++++++++++++------- components/codetools/pascalreadertool.pas | 10 +++++--- components/codetools/stdcodetools.pas | 14 +++++++----- converter/delphiproject2laz.pas | 17 ++++++-------- packager/packagedefs.pas | 1 + 7 files changed, 59 insertions(+), 28 deletions(-) diff --git a/components/codetools/basiccodetools.pas b/components/codetools/basiccodetools.pas index d9227c5448..a501e5a1a9 100644 --- a/components/codetools/basiccodetools.pas +++ b/components/codetools/basiccodetools.pas @@ -2222,7 +2222,8 @@ begin else Offset:=-1; if (Offset>0) then begin - if (CompareIdentifiers('i',@ASource[Result+Offset])=0) + if ((UpChars[ASource[Result+Offset]]='I') + and (ASource[Result+Offset+1]=' ')) or (CompareIdentifiers('include',@ASource[Result+Offset])=0) then begin CommentEndPos:=FindCommentEnd(ASource,Result,NestedComments); if ASource[Result]='{' then diff --git a/components/codetools/codetoolmanager.pas b/components/codetools/codetoolmanager.pas index b81a5dffc6..a59c267ba3 100644 --- a/components/codetools/codetoolmanager.pas +++ b/components/codetools/codetoolmanager.pas @@ -438,6 +438,8 @@ type FixCase: boolean = false): boolean; function FindDelphiProjectUnits(Code: TCodeBuffer; var FoundInUnits, MissingInUnits, NormalUnits: TStrings): boolean; + function FindDelphiPackageUnits(Code: TCodeBuffer; + var FoundInUnits, MissingInUnits, NormalUnits: TStrings): boolean; function CommentUnitsInUsesSections(Code: TCodeBuffer; MissingUnits: TStrings): boolean; function FindUnit(Code: TCodeBuffer; @@ -2898,6 +2900,22 @@ begin end; end; +function TCodeToolManager.FindDelphiPackageUnits(Code: TCodeBuffer; + var FoundInUnits, MissingInUnits, NormalUnits: TStrings): boolean; +begin + Result:=false; + {$IFDEF CTDEBUG} + DebugLn('TCodeToolManager.FindDelphiPackageUnits A ',Code.Filename); + {$ENDIF} + if not InitCurCodeTool(Code) then exit; + try + Result:=FCurCodeTool.FindDelphiProjectUnits(FoundInUnits, + MissingInUnits, NormalUnits,true); + except + on e: Exception do Result:=HandleException(e); + end; +end; + function TCodeToolManager.CommentUnitsInUsesSections(Code: TCodeBuffer; MissingUnits: TStrings): boolean; begin diff --git a/components/codetools/finddeclarationtool.pas b/components/codetools/finddeclarationtool.pas index 549bda0523..bb021e3dd7 100644 --- a/components/codetools/finddeclarationtool.pas +++ b/components/codetools/finddeclarationtool.pas @@ -696,7 +696,7 @@ type function FindDeclarationNodeInInterface(const Identifier: string; BuildTheTree: Boolean): TCodeTreeNode; - function FindMainUsesSection: TCodeTreeNode; + function FindMainUsesSection(UseContainsSection: boolean = false): TCodeTreeNode; function FindImplementationUsesSection: TCodeTreeNode; function FindUnitSource(const AnUnitName, @@ -1496,17 +1496,25 @@ begin Result:=BestNode; end; -function TFindDeclarationTool.FindMainUsesSection: TCodeTreeNode; +function TFindDeclarationTool.FindMainUsesSection(UseContainsSection: boolean + ): TCodeTreeNode; begin Result:=Tree.Root; if Result=nil then exit; - if Result.Desc=ctnUnit then begin - Result:=Result.NextBrother; - if Result=nil then exit; + if UseContainsSection then begin + if Result.Desc<>ctnPackage then exit(nil); + Result:=Result.FirstChild; + while (Result<>nil) and (Result.Desc<>ctnContainsSection) do + Result:=Result.NextBrother; + end else begin + if Result.Desc=ctnUnit then begin + Result:=Result.NextBrother; + if Result=nil then exit; + end; + Result:=Result.FirstChild; + if (Result=nil) then exit; + if (Result.Desc<>ctnUsesSection) then Result:=nil; end; - Result:=Result.FirstChild; - if (Result=nil) then exit; - if (Result.Desc<>ctnUsesSection) then Result:=nil; end; function TFindDeclarationTool.FindImplementationUsesSection: TCodeTreeNode; diff --git a/components/codetools/pascalreadertool.pas b/components/codetools/pascalreadertool.pas index ece1a4f7d8..835f4b634c 100644 --- a/components/codetools/pascalreadertool.pas +++ b/components/codetools/pascalreadertool.pas @@ -1529,20 +1529,24 @@ end; procedure TPascalReaderTool.MoveCursorToUsesStart(UsesNode: TCodeTreeNode); begin - if (UsesNode=nil) or (UsesNode.Desc<>ctnUsesSection) then + if (UsesNode=nil) + or ((UsesNode.Desc<>ctnUsesSection) and (UsesNode.Desc<>ctnContainsSection)) + then RaiseException('[TPascalParserTool.MoveCursorToUsesStart] ' +'internal error: invalid UsesNode'); // search backwards through the uses section MoveCursorToCleanPos(UsesNode.StartPos); ReadNextAtom; - if not UpAtomIs('USES') then + if (not UpAtomIs('USES')) and (not UpAtomIs('CONTAINS')) then RaiseExceptionFmt(ctsStrExpectedButAtomFound,['uses',GetAtom]); ReadNextAtom; end; procedure TPascalReaderTool.MoveCursorToUsesEnd(UsesNode: TCodeTreeNode); begin - if (UsesNode=nil) or (UsesNode.Desc<>ctnUsesSection) then + if (UsesNode=nil) + or ((UsesNode.Desc<>ctnUsesSection) and (UsesNode.Desc<>ctnContainsSection)) + then RaiseException('[TPascalParserTool.MoveCursorToUsesEnd] ' +'internal error: invalid UsesNode'); // search backwards through the uses section diff --git a/components/codetools/stdcodetools.pas b/components/codetools/stdcodetools.pas index 7222ce3c13..cc767c0cdc 100644 --- a/components/codetools/stdcodetools.pas +++ b/components/codetools/stdcodetools.pas @@ -106,7 +106,8 @@ type function FindUsedUnitFiles(var MainUsesSection, ImplementationUsesSection: TStrings): boolean; function FindDelphiProjectUnits(var FoundInUnits, MissingInUnits, - NormalUnits: TStrings): boolean; + NormalUnits: TStrings; + UseContainsSection: boolean = false): boolean; function UsesSectionToFilenames(UsesNode: TCodeTreeNode): TStrings; function UsesSectionToUnitnames(UsesNode: TCodeTreeNode): TStrings; function FindMissingUnits(var MissingUnits: TStrings; FixCase: boolean; @@ -402,7 +403,7 @@ begin if SectionNode.Desc in [ctnProgram, ctnInterface, ctnImplementation] then begin UsesNode:=SectionNode.FirstChild; - if (UsesNode.Desc=ctnUsesSection) + if (UsesNode<>nil) and (UsesNode.Desc=ctnUsesSection) and FindUnitInUsesSection(UsesNode,UpperUnitName,NamePos,InPos) then begin Result:=true; exit; @@ -781,7 +782,7 @@ end; function TStandardCodeTool.FindDelphiProjectUnits(var FoundInUnits, MissingInUnits, NormalUnits: TStrings): boolean; - Reads the main uses section backwards and tries to find each unit file having + Reads the main uses section and tries to find each unit file having an 'in' modifier. The associated objects in the list will be the found codebuffers. FoundInUnits returns the list of found 'in' unitnames plus TCodeBuffer @@ -792,7 +793,7 @@ end; plus the 'in' extension. ------------------------------------------------------------------------------} function TStandardCodeTool.FindDelphiProjectUnits(var FoundInUnits, - MissingInUnits, NormalUnits: TStrings): boolean; + MissingInUnits, NormalUnits: TStrings; UseContainsSection: boolean): boolean; var InAtom, UnitNameAtom: TAtomPosition; AnUnitName, AnUnitInFilename: string; @@ -803,9 +804,10 @@ begin FoundInUnits:=nil; MissingInUnits:=nil; NormalUnits:=nil; + DebugLn('TStandardCodeTool.FindDelphiProjectUnits UseContainsSection=',dbgs(UseContainsSection)); // find the uses sections BuildTree(false); - UsesNode:=FindMainUsesSection; + UsesNode:=FindMainUsesSection(UseContainsSection); if UsesNode=nil then exit; MoveCursorToUsesStart(UsesNode); FoundInUnits:=TStringList.Create; @@ -832,7 +834,7 @@ begin FoundInUnits.AddObject(AnUnitName+' in '+AnUnitInFilename,NewCode); end; end else begin - // the non 'in' units are 'Forms' or units added by the user + // the units without 'in' are 'Forms' or units added by the user NewCode:=FindUnitSource(AnUnitName,AnUnitInFilename,false); NormalUnits.AddObject(AnUnitName,NewCode); end; diff --git a/converter/delphiproject2laz.pas b/converter/delphiproject2laz.pas index 5d4a2491e6..c94e052a1e 100644 --- a/converter/delphiproject2laz.pas +++ b/converter/delphiproject2laz.pas @@ -470,16 +470,13 @@ begin NormalUnits:=nil; try debugln('FindAllDelphiPackageUnits gathering all units ...'); - NotImplementedDialog('FindAllDelphiPackageUnits: Reading .dpk file'); - exit(mrAbort); - - //if not CodeToolBoss.FindDelphiPackageUnits(DPKCode,FoundInUnits, - //MissingInUnits, NormalUnits) then - //begin - //LazarusIDE.DoJumpToCodeToolBossError; - //Result:=mrCancel; - //exit; - //end; + if not CodeToolBoss.FindDelphiPackageUnits(DPKCode,FoundInUnits, + MissingInUnits, NormalUnits) then + begin + LazarusIDE.DoJumpToCodeToolBossError; + Result:=mrCancel; + exit; + end; debugln('FindAllDelphiPackageUnits FoundInUnits=[',FoundInUnits.Text,']', ' MissingInUnits=[',MissingInUnits.Text,']', ' NormalUnits=[',NormalUnits.Text,']'); diff --git a/packager/packagedefs.pas b/packager/packagedefs.pas index 73f3b84579..2e5a508286 100644 --- a/packager/packagedefs.pas +++ b/packager/packagedefs.pas @@ -2753,6 +2753,7 @@ begin if i>=FileCount then continue; if not FileExistsCached(Files[i].Filename) then RemoveFile(Files[i]); + dec(i); end; end;