h2pas: added tool to add missing round brackets around macro constants

git-svn-id: trunk@14121 -
This commit is contained in:
mattias 2008-02-13 15:07:15 +00:00
parent ebfeea36ca
commit 40c644d22f
2 changed files with 55 additions and 2 deletions

View File

@ -114,6 +114,16 @@ type
end; end;
{ TAddMissingMacroBrackets (for C header files)
Add missing brackets around macro values }
TAddMissingMacroBrackets = class(TCustomTextConverterTool)
public
class function ClassDescription: string; override;
function Execute(aText: TIDETextConverter): TModalResult; override;
end;
{ TReplaceUnitFilenameWithUnitName - { TReplaceUnitFilenameWithUnitName -
Replace "unit filename;" with "unit name;" } Replace "unit filename;" with "unit name;" }
@ -336,7 +346,8 @@ type
phConvertFunctionTypesToPointers, // Convert function types to pointers phConvertFunctionTypesToPointers, // Convert function types to pointers
phConvertEnumsToTypeDef, // Convert anonymous enums to ypedef enums phConvertEnumsToTypeDef, // Convert anonymous enums to ypedef enums
phCommentComplexCMacros, // Comment macros too complex for hpas phCommentComplexCMacros, // Comment macros too complex for hpas
phCommentComplexCFunctions // Comment functions too complex for hpas phCommentComplexCFunctions, // Comment functions too complex for hpas
phAddMissingMacroBrackets // Add missing macro brackets
); );
TPreH2PasToolsOptions = set of TPreH2PasToolsOption; TPreH2PasToolsOptions = set of TPreH2PasToolsOption;
const const
@ -3721,6 +3732,8 @@ begin
TCommentComplexCMacros,Result) then exit; TCommentComplexCMacros,Result) then exit;
if not Run(phCommentComplexCFunctions, if not Run(phCommentComplexCFunctions,
TCommentComplexCFunctions,Result) then exit; TCommentComplexCFunctions,Result) then exit;
if not Run(phAddMissingMacroBrackets,
TAddMissingMacroBrackets,Result) then exit;
Result:=mrOk; Result:=mrOk;
end; end;
@ -4855,4 +4868,43 @@ begin
Result:=mrOk; Result:=mrOk;
end; end;
{ TAddMissingMacroBrackets }
class function TAddMissingMacroBrackets.ClassDescription: string;
begin
Result:='Add missing brackets around macro values';
end;
function TAddMissingMacroBrackets.Execute(aText: TIDETextConverter
): TModalResult;
var
Macro: String;
Lines: TStrings;
i: Integer;
Line: string;
Value: String;
begin
Result:=mrCancel;
if aText=nil then exit;
Lines:=aText.Strings;
i:=0;
while i<=Lines.Count-1 do begin
Line:=Lines[i];
// example: #define READ_CURRENT_IAC_LAP_RP_SIZE 2+3*MAX_IAC_LAP
if REMatches(Line,'^(#define\s+[a-zA-Z0-9_]+\s+)(.+)')
then begin
Macro:=REVar(1);
Value:=REVar(2);
if (Value<>'') and (Value[1]<>'(')
and (REMatches(Value,'[^a-zA-Z0-9_()]')) then begin
// macro needs values
Line:=Macro+'('+Value+')';
Lines[i]:=Line;
end;
end;
inc(i);
end;
Result:=mrOk;
end;
end. end.

View File

@ -218,7 +218,8 @@ begin
TextConverterToolClasses.RegisterClass(TConvertEnumsToTypeDef); TextConverterToolClasses.RegisterClass(TConvertEnumsToTypeDef);
TextConverterToolClasses.RegisterClass(TCommentComplexCMacros); TextConverterToolClasses.RegisterClass(TCommentComplexCMacros);
TextConverterToolClasses.RegisterClass(TCommentComplexCFunctions); TextConverterToolClasses.RegisterClass(TCommentComplexCFunctions);
TextConverterToolClasses.RegisterClass(TAddMissingMacroBrackets);
TextConverterToolClasses.RegisterClass(TPostH2PasTools); TextConverterToolClasses.RegisterClass(TPostH2PasTools);
TextConverterToolClasses.RegisterClass(TReplaceUnitFilenameWithUnitName); TextConverterToolClasses.RegisterClass(TReplaceUnitFilenameWithUnitName);
TextConverterToolClasses.RegisterClass(TRemoveDoubleSemicolons); TextConverterToolClasses.RegisterClass(TRemoveDoubleSemicolons);