h2pas: added tool to add units to the uses section

git-svn-id: trunk@14114 -
This commit is contained in:
mattias 2008-02-12 17:02:51 +00:00
parent 369132766d
commit ac191520f1
3 changed files with 125 additions and 8 deletions

View File

@ -1532,11 +1532,14 @@ begin
FromPos:=Node.StartPos; FromPos:=Node.StartPos;
if RemoveDisabledDirectives then begin 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:=FindCommentEnd(Src,Node.NextBrother.StartPos,NestedComments);
ToPos:=FindLineEndOrCodeAfterPosition(Src,ToPos,SrcLen+1,NestedComments); ToPos:=FindLineEndOrCodeAfterPosition(Src,ToPos,SrcLen+1,NestedComments);
end else end else begin
// remove a sub block
ToPos:=Node.NextBrother.StartPos; ToPos:=Node.NextBrother.StartPos;
end;
if WithContent then begin if WithContent then begin
// remove node source with content // remove node source with content
if (FromPos>1) and (Src[FromPos-1] in [#10,#13]) if (FromPos>1) and (Src[FromPos-1] in [#10,#13])

View File

@ -310,6 +310,21 @@ type
function Execute(aText: TIDETextConverter): TModalResult; override; function Execute(aText: TIDETextConverter): TModalResult; override;
end; 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 type
{ TPretH2PasTools - Combines the common tools. } { TPretH2PasTools - Combines the common tools. }
@ -360,7 +375,8 @@ type
phFixAliasDefinitionsInUnit, // fix section type of alias definitions phFixAliasDefinitionsInUnit, // fix section type of alias definitions
phReplaceConstFunctionsInUnit, // replace simple assignment functions with constants phReplaceConstFunctionsInUnit, // replace simple assignment functions with constants
phReplaceTypeCastFunctionsInUnit, // replace simple type cast functions with types 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; TPostH2PasToolsOptions = set of TPostH2PasToolsOption;
const const
@ -372,8 +388,10 @@ type
FDefines: TStrings; FDefines: TStrings;
FOptions: TPostH2PasToolsOptions; FOptions: TPostH2PasToolsOptions;
FUndefines: TStrings; FUndefines: TStrings;
FUseUnits: TStrings;
procedure SetDefines(const AValue: TStrings); procedure SetDefines(const AValue: TStrings);
procedure SetUndefines(const AValue: TStrings); procedure SetUndefines(const AValue: TStrings);
procedure SetUseUnits(const AValue: TStrings);
public public
constructor Create(TheOwner: TComponent); override; constructor Create(TheOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
@ -382,6 +400,7 @@ type
published published
property Undefines: TStrings read FUndefines write SetUndefines; property Undefines: TStrings read FUndefines write SetUndefines;
property Defines: TStrings read FDefines write SetDefines; property Defines: TStrings read FDefines write SetDefines;
property UseUnits: TStrings read FUseUnits write SetUseUnits;
property Options: TPostH2PasToolsOptions read FOptions write FOptions default DefaultPostH2PasToolsOptions; property Options: TPostH2PasToolsOptions read FOptions write FOptions default DefaultPostH2PasToolsOptions;
end; end;
@ -3614,7 +3633,11 @@ begin
+'phRemoveEmptyCMacrosTool - Remove empty C macros'#13 +'phRemoveEmptyCMacrosTool - Remove empty C macros'#13
+'phReplaceEdgedBracketPairWithStar - Replace [] with *'#13 +'phReplaceEdgedBracketPairWithStar - Replace [] with *'#13
+'phReplace0PointerWithNULL - Replace macro values 0 pointer like (char *)0'#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; end;
function TPreH2PasTools.Execute(aText: TIDETextConverter): TModalResult; function TPreH2PasTools.Execute(aText: TIDETextConverter): TModalResult;
@ -3677,11 +3700,18 @@ begin
FUndefines.Assign(AValue); FUndefines.Assign(AValue);
end; end;
procedure TPostH2PasTools.SetUseUnits(const AValue: TStrings);
begin
if FUseUnits=AValue then exit;
FUseUnits.Assign(FUseUnits);
end;
constructor TPostH2PasTools.Create(TheOwner: TComponent); constructor TPostH2PasTools.Create(TheOwner: TComponent);
begin begin
inherited Create(TheOwner); inherited Create(TheOwner);
FDefines:=TStringList.Create; FDefines:=TStringList.Create;
FUndefines:=TStringList.Create; FUndefines:=TStringList.Create;
FUseUnits:=TStringList.Create;
FOptions:=DefaultPostH2PasToolsOptions; FOptions:=DefaultPostH2PasToolsOptions;
end; end;
@ -3708,7 +3738,9 @@ begin
+'phFixAliasDefinitionsInUnit - fix section type of alias definitions'#13 +'phFixAliasDefinitionsInUnit - fix section type of alias definitions'#13
+'phReplaceConstFunctionsInUnit - replace simple assignment functions with constants'#13 +'phReplaceConstFunctionsInUnit - replace simple assignment functions with constants'#13
+'phReplaceTypeCastFunctionsInUnit - replace simple type cast functions with types'#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; end;
function TPostH2PasTools.Execute(aText: TIDETextConverter): TModalResult; function TPostH2PasTools.Execute(aText: TIDETextConverter): TModalResult;
@ -3757,6 +3789,34 @@ function TPostH2PasTools.Execute(aText: TIDETextConverter): TModalResult;
Result:=true; Result:=true;
end; 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; function ConvertSimpleFunctions(var Changed: boolean;
var aResult: TModalResult): boolean; var aResult: TModalResult): boolean;
var var
@ -3833,7 +3893,7 @@ begin
TFixH2PasMissingIFDEFsInUnit,Result) then exit; TFixH2PasMissingIFDEFsInUnit,Result) then exit;
// reduce compiler directives so that other tools can work with less double data // reduce compiler directives so that other tools can work with less double data
if not ReduceCompilerDirectives(Changed,Result) then exit; 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, if not Run(phRemoveRedefinedPointerTypes,
TRemoveRedefinedPointerTypes,Result) then exit; TRemoveRedefinedPointerTypes,Result) then exit;
if not Run(phRemoveEmptyTypeVarConstSections, if not Run(phRemoveEmptyTypeVarConstSections,
@ -3845,7 +3905,7 @@ begin
TFixArrayOfParameterType,Result) then exit; TFixArrayOfParameterType,Result) then exit;
if not Run(phAddMissingPointerTypes, if not Run(phAddMissingPointerTypes,
TAddMissingPointerTypes,Result) then exit; TAddMissingPointerTypes,Result) then exit;
// remove redefinitions, to data get unambiguous types // remove redefinitions, to get unambiguous types
if not Run(phRemoveRedefinitionsInUnit, if not Run(phRemoveRedefinitionsInUnit,
TRemoveRedefinitionsInUnit,Result) then exit; TRemoveRedefinitionsInUnit,Result) then exit;
@ -3860,6 +3920,8 @@ begin
// fix forward definitions // fix forward definitions
if not Run(phFixForwardDefinitions, if not Run(phFixForwardDefinitions,
TFixForwardDefinitions,Result) then exit; TFixForwardDefinitions,Result) then exit;
// add units to uses section
if not AddToUsesSection(Changed,Result) then exit;
end; end;
{ TRemoveIncludeDirectives } { TRemoveIncludeDirectives }
@ -4626,7 +4688,7 @@ var
FuncEnd: LongInt; FuncEnd: LongInt;
begin begin
Result:=false; Result:=false;
DebugLn(['ReadFunction START "',copy(Src,AtomStart,p-AtomStart),'"']); //DebugLn(['ReadFunction START "',copy(Src,AtomStart,p-AtomStart),'"']);
// a C function works like this: // a C function works like this:
// [modifiers, macros] type name(param list){ statements } // [modifiers, macros] type name(param list){ statements }
// 'type' can be an identifier or identifier* or something with brackets // 'type' can be an identifier or identifier* or something with brackets
@ -4704,4 +4766,55 @@ begin
Result:=mrOk; Result:=mrOk;
end; 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. end.

View File

@ -234,6 +234,7 @@ begin
TextConverterToolClasses.RegisterClass(TReplaceConstFunctionsInUnit); TextConverterToolClasses.RegisterClass(TReplaceConstFunctionsInUnit);
TextConverterToolClasses.RegisterClass(TReplaceTypeCastFunctionsInUnit); TextConverterToolClasses.RegisterClass(TReplaceTypeCastFunctionsInUnit);
TextConverterToolClasses.RegisterClass(TFixForwardDefinitions); TextConverterToolClasses.RegisterClass(TFixForwardDefinitions);
TextConverterToolClasses.RegisterClass(TAddToUsesSection);
end; end;
{ TH2PasDialog } { TH2PasDialog }