From ac191520f119fedf2aca8b4f493100fa52208a4e Mon Sep 17 00:00:00 2001 From: mattias Date: Tue, 12 Feb 2008 17:02:51 +0000 Subject: [PATCH] h2pas: added tool to add units to the uses section git-svn-id: trunk@14114 - --- components/codetools/directivestree.pas | 7 +- components/h2pas/h2pasconvert.pas | 125 ++++++++++++++++++++++-- components/h2pas/h2pasdlg.pas | 1 + 3 files changed, 125 insertions(+), 8 deletions(-) diff --git a/components/codetools/directivestree.pas b/components/codetools/directivestree.pas index 92b3ddf216..ccc047e75a 100644 --- a/components/codetools/directivestree.pas +++ b/components/codetools/directivestree.pas @@ -1532,11 +1532,14 @@ begin FromPos:=Node.StartPos; if RemoveDisabledDirectives then begin - if Node.NextBrother.Desc=cdnEnd then begin + if (Node.NextBrother.Desc=cdnEnd) and (Node.Desc=cdnIf) then begin + // remove the whole IF..END block ToPos:=FindCommentEnd(Src,Node.NextBrother.StartPos,NestedComments); ToPos:=FindLineEndOrCodeAfterPosition(Src,ToPos,SrcLen+1,NestedComments); - end else + end else begin + // remove a sub block ToPos:=Node.NextBrother.StartPos; + end; if WithContent then begin // remove node source with content if (FromPos>1) and (Src[FromPos-1] in [#10,#13]) diff --git a/components/h2pas/h2pasconvert.pas b/components/h2pas/h2pasconvert.pas index a5655b7ee5..21508a60c3 100644 --- a/components/h2pas/h2pasconvert.pas +++ b/components/h2pas/h2pasconvert.pas @@ -310,6 +310,21 @@ type function Execute(aText: TIDETextConverter): TModalResult; override; end; + { TAddToUsesSection - add units to uses section } + + TAddToUsesSection = class(TCustomTextConverterTool) + private + FUseUnits: TStrings; + procedure SetUseUnits(const AValue: TStrings); + public + constructor Create(TheOwner: TComponent); override; + destructor Destroy; override; + class function ClassDescription: string; override; + function Execute(aText: TIDETextConverter): TModalResult; override; + published + property UseUnits: TStrings read FUseUnits write SetUseUnits; + end; + type { TPretH2PasTools - Combines the common tools. } @@ -360,7 +375,8 @@ type phFixAliasDefinitionsInUnit, // fix section type of alias definitions phReplaceConstFunctionsInUnit, // replace simple assignment functions with constants phReplaceTypeCastFunctionsInUnit, // replace simple type cast functions with types - phFixForwardDefinitions // fix forward definitions by reordering + phFixForwardDefinitions, // fix forward definitions by reordering + phAddUnitsToUsesSection // add units to uses section ); TPostH2PasToolsOptions = set of TPostH2PasToolsOption; const @@ -372,8 +388,10 @@ type FDefines: TStrings; FOptions: TPostH2PasToolsOptions; FUndefines: TStrings; + FUseUnits: TStrings; procedure SetDefines(const AValue: TStrings); procedure SetUndefines(const AValue: TStrings); + procedure SetUseUnits(const AValue: TStrings); public constructor Create(TheOwner: TComponent); override; destructor Destroy; override; @@ -382,6 +400,7 @@ type published property Undefines: TStrings read FUndefines write SetUndefines; property Defines: TStrings read FDefines write SetDefines; + property UseUnits: TStrings read FUseUnits write SetUseUnits; property Options: TPostH2PasToolsOptions read FOptions write FOptions default DefaultPostH2PasToolsOptions; end; @@ -3614,7 +3633,11 @@ begin +'phRemoveEmptyCMacrosTool - Remove empty C macros'#13 +'phReplaceEdgedBracketPairWithStar - Replace [] with *'#13 +'phReplace0PointerWithNULL - Replace macro values 0 pointer like (char *)0'#13 - +'phConvertFunctionTypesToPointers - Convert function types to pointers'#13; + +'phConvertFunctionTypesToPointers - Convert function types to pointers'#13 + +'phConvertEnumsToTypeDef - Convert anonymous enums to ypedef enums'#13 + +'phCommentComplexCMacros - Comment macros too complex for hpas'#13 + +'phCommentComplexCFunctions - Comment functions too complex for hpas'#13 + ; end; function TPreH2PasTools.Execute(aText: TIDETextConverter): TModalResult; @@ -3677,11 +3700,18 @@ begin FUndefines.Assign(AValue); end; +procedure TPostH2PasTools.SetUseUnits(const AValue: TStrings); +begin + if FUseUnits=AValue then exit; + FUseUnits.Assign(FUseUnits); +end; + constructor TPostH2PasTools.Create(TheOwner: TComponent); begin inherited Create(TheOwner); FDefines:=TStringList.Create; FUndefines:=TStringList.Create; + FUseUnits:=TStringList.Create; FOptions:=DefaultPostH2PasToolsOptions; end; @@ -3708,7 +3738,9 @@ begin +'phFixAliasDefinitionsInUnit - fix section type of alias definitions'#13 +'phReplaceConstFunctionsInUnit - replace simple assignment functions with constants'#13 +'phReplaceTypeCastFunctionsInUnit - replace simple type cast functions with types'#13 - +'phFixForwardDefinitions - fix forward definitions by reordering'#13; + +'phFixForwardDefinitions - fix forward definitions by reordering'#13 + +'phAddUnitsToUsesSection - add units to uses section'#13 + ; end; function TPostH2PasTools.Execute(aText: TIDETextConverter): TModalResult; @@ -3757,6 +3789,34 @@ function TPostH2PasTools.Execute(aText: TIDETextConverter): TModalResult; Result:=true; end; + function AddToUsesSection(var Changed: boolean; + var aResult: TModalResult): boolean; + var + i: Integer; + UnitName: string; + begin + aResult:=mrOk; + if not (phAddUnitsToUsesSection in Options) then exit; + DebugLn(['TPostH2PasTools.Execute.AddToUsesSection ']); + for i:=0 to FUseUnits.Count-1 do begin + UnitName:=FUseUnits[i]; + if (UnitName='') then continue; + if not IsValidIdent(UnitName) then + raise Exception.Create('TPostH2PasTools.Execute.AddToUsesSection invalid unitname "'+UnitName+'"'); + Changed:=true; + if not CodeToolBoss.AddUnitToMainUsesSection( + TCodeBuffer(aText.CodeBuffer),UnitName,'') + then begin + AssignCodeToolBossError; + DebugLn(['TPostH2PasTools.Execute.AddToUsesSection failed ',CodeToolBoss.ErrorMessage]); + aResult:=mrCancel; + exit(false); + end; + end; + aResult:=mrOk; + Result:=true; + end; + function ConvertSimpleFunctions(var Changed: boolean; var aResult: TModalResult): boolean; var @@ -3833,7 +3893,7 @@ begin TFixH2PasMissingIFDEFsInUnit,Result) then exit; // reduce compiler directives so that other tools can work with less double data if not ReduceCompilerDirectives(Changed,Result) then exit; - // remove h2pas redefinitions to data get unambiguous types + // remove h2pas redefinitions to get unambiguous types if not Run(phRemoveRedefinedPointerTypes, TRemoveRedefinedPointerTypes,Result) then exit; if not Run(phRemoveEmptyTypeVarConstSections, @@ -3845,7 +3905,7 @@ begin TFixArrayOfParameterType,Result) then exit; if not Run(phAddMissingPointerTypes, TAddMissingPointerTypes,Result) then exit; - // remove redefinitions, to data get unambiguous types + // remove redefinitions, to get unambiguous types if not Run(phRemoveRedefinitionsInUnit, TRemoveRedefinitionsInUnit,Result) then exit; @@ -3860,6 +3920,8 @@ begin // fix forward definitions if not Run(phFixForwardDefinitions, TFixForwardDefinitions,Result) then exit; + // add units to uses section + if not AddToUsesSection(Changed,Result) then exit; end; { TRemoveIncludeDirectives } @@ -4626,7 +4688,7 @@ var FuncEnd: LongInt; begin Result:=false; - DebugLn(['ReadFunction START "',copy(Src,AtomStart,p-AtomStart),'"']); + //DebugLn(['ReadFunction START "',copy(Src,AtomStart,p-AtomStart),'"']); // a C function works like this: // [modifiers, macros] type name(param list){ statements } // 'type' can be an identifier or identifier* or something with brackets @@ -4704,4 +4766,55 @@ begin Result:=mrOk; end; +{ TAddToUsesSection } + +procedure TAddToUsesSection.SetUseUnits(const AValue: TStrings); +begin + if FUseUnits=AValue then exit; + FUseUnits.Assign(AValue); +end; + +constructor TAddToUsesSection.Create(TheOwner: TComponent); +begin + inherited Create(TheOwner); + FUseUnits:=TStringList.Create; +end; + +destructor TAddToUsesSection.Destroy; +begin + FreeAndNil(FUseUnits); + inherited Destroy; +end; + +class function TAddToUsesSection.ClassDescription: string; +begin + Result:='Add units to uses section'; +end; + +function TAddToUsesSection.Execute(aText: TIDETextConverter): TModalResult; +var + UnitName: string; + i: Integer; +begin + Result:=mrCancel; + if (not FilenameIsPascalUnit(aText.Filename)) then begin + DebugLn(['TAddToUsesSection.Execute file is not pascal: ',aText.Filename]); + exit(mrOk);// ignore + end; + for i:=0 to FUseUnits.Count-1 do begin + UnitName:=FUseUnits[i]; + if (UnitName='') then continue; + if not IsValidIdent(UnitName) then + raise Exception.Create('TAddToUsesSection.Execute invalid unitname "'+UnitName+'"'); + if not CodeToolBoss.AddUnitToMainUsesSection( + TCodeBuffer(aText.CodeBuffer),UnitName,'') + then begin + AssignCodeToolBossError; + DebugLn(['TAddToUsesSection.Execute failed ',CodeToolBoss.ErrorMessage]); + exit; + end; + end; + Result:=mrOk; +end; + end. diff --git a/components/h2pas/h2pasdlg.pas b/components/h2pas/h2pasdlg.pas index e88ccdf0a5..470884374a 100644 --- a/components/h2pas/h2pasdlg.pas +++ b/components/h2pas/h2pasdlg.pas @@ -234,6 +234,7 @@ begin TextConverterToolClasses.RegisterClass(TReplaceConstFunctionsInUnit); TextConverterToolClasses.RegisterClass(TReplaceTypeCastFunctionsInUnit); TextConverterToolClasses.RegisterClass(TFixForwardDefinitions); + TextConverterToolClasses.RegisterClass(TAddToUsesSection); end; { TH2PasDialog }