Converter: Check the number of params when replacing funcs. Prevents replacing a variable named Ptr to Pointer(nil).

git-svn-id: trunk@28293 -
This commit is contained in:
juha 2010-11-17 12:19:23 +00:00
parent 6ac13121b9
commit a6110a1e8b

View File

@ -614,6 +614,7 @@ end;
function TConvDelphiCodeTool.ReplaceFuncsInSource: boolean; function TConvDelphiCodeTool.ReplaceFuncsInSource: boolean;
// Replace the function names and parameters in source. // Replace the function names and parameters in source.
var var
// Replacement parameter positions, will be converted to integers.
ParamList: TStringList; ParamList: TStringList;
BodyEnd: Integer; // End of function body. BodyEnd: Integer; // End of function body.
@ -627,7 +628,7 @@ var
Result:=1; Result:=1;
ParamBeg:=Pos('(', aStr); ParamBeg:=Pos('(', aStr);
if ParamBeg>0 then begin if ParamBeg>0 then begin
ParamEnd:=Pos(')', aStr); ParamEnd:=PosEx(')', aStr, ParamBeg+1);
if ParamEnd=0 then if ParamEnd=0 then
raise EDelphiConverterError.Create('")" is missing from replacement function.'); raise EDelphiConverterError.Create('")" is missing from replacement function.');
s:=Copy(aStr, ParamBeg+1, ParamEnd-ParamBeg-1); s:=Copy(aStr, ParamBeg+1, ParamEnd-ParamBeg-1);
@ -639,6 +640,7 @@ var
function CollectParams(aParams: TStringList): string; function CollectParams(aParams: TStringList): string;
// Collect parameters from original call. Construct and return a new parameter list. // Collect parameters from original call. Construct and return a new parameter list.
// aParams - parameters from the original function call.
var var
Param: String; Param: String;
ParamPos: Integer; // Position of parameter in the original call. ParamPos: Integer; // Position of parameter in the original call.
@ -686,7 +688,7 @@ var
var var
FuncInfo: TFuncReplacement; FuncInfo: TFuncReplacement;
PossibleCommPos: Integer; // Start looking for comments here. PossibleCommentPos: Integer; // Start looking for comments here.
i: Integer; i: Integer;
s, NewFunc, NewParamStr, Comment: String; s, NewFunc, NewParamStr, Comment: String;
begin begin
@ -697,9 +699,12 @@ begin
for i:=fFuncsToReplace.Count-1 downto 0 do begin for i:=fFuncsToReplace.Count-1 downto 0 do begin
FuncInfo:=TFuncReplacement(fFuncsToReplace[i]); FuncInfo:=TFuncReplacement(fFuncsToReplace[i]);
BodyEnd:=-1; BodyEnd:=-1;
PossibleCommPos:=ParseReplacementParams(FuncInfo.ReplFunc); // Update ParamList.
PossibleCommentPos:=ParseReplacementParams(FuncInfo.ReplFunc);
// Replace only if the params match somehow, so eg. a variable is not replaced.
if (FuncInfo.Params.Count>0) or (ParamList.Count=0) then begin
NewParamStr:=CollectParams(FuncInfo.Params); NewParamStr:=CollectParams(FuncInfo.Params);
Comment:=GetComment(FuncInfo.ReplFunc, PossibleCommPos); Comment:=GetComment(FuncInfo.ReplFunc, PossibleCommentPos);
// Separate function body // Separate function body
if BodyEnd=-1 then if BodyEnd=-1 then
BodyEnd:=Length(FuncInfo.ReplFunc); BodyEnd:=Length(FuncInfo.ReplFunc);
@ -716,6 +721,7 @@ begin
IDEMessagesWindow.AddMsg('Replaced call '+s, '', -1); IDEMessagesWindow.AddMsg('Replaced call '+s, '', -1);
IDEMessagesWindow.AddMsg(' with '+NewFunc, '', -1); IDEMessagesWindow.AddMsg(' with '+NewFunc, '', -1);
end; end;
end;
finally finally
ParamList.Free; ParamList.Free;
end; end;