chelper: updated preprocess expression valuation - using macors values. Renamed the valuation function name.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4000 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
skalogryz
2015-03-06 14:08:16 +00:00
parent fdd08738ef
commit 115e6a7557
2 changed files with 49 additions and 16 deletions

View File

@ -3,7 +3,7 @@ unit cparserexp;
interface interface
uses uses
cparsertypes; SysUtils, cparsertypes;
type type
TIdentType = (itIdent, itIndex, itFuncCall, itField, itSubSel); TIdentType = (itIdent, itIndex, itFuncCall, itField, itSubSel);
@ -39,8 +39,8 @@ const
function ParseCExprEx(p: TTextParser): TExp; function ParseCExprEx(p: TTextParser): TExp;
function ValuateIntExp(exp: TExp; macros: TCMacroHandler): Integer; overload; function ValuatePreprocExp(exp: TExp; macros: TCMacroHandler): Integer; overload;
function ValuateIntExp(const exp: string; macros: TCMacroHandler): Integer; overload; function ValuatePreprocExp(const exp: string; macros: TCMacroHandler): Integer; overload;
function isCTypeCast(exp: TExp; tinfo: TCTypeInfo): Boolean; function isCTypeCast(exp: TExp; tinfo: TCTypeInfo): Boolean;
@ -96,7 +96,7 @@ begin
else Result:=false; else Result:=false;
end; end;
function level2(p: TTextParser; NameOnly: Boolean = false): TExp; function level2(p: TTextParser): TExp;
var var
exp : TExp; exp : TExp;
res : Boolean; res : Boolean;
@ -121,7 +121,7 @@ begin
exp:=e; exp:=e;
p.NextToken; p.NextToken;
if it in [itField, itSubSel] then if it in [itField, itSubSel] then
exp.right:=level2(p, true) exp.right:=level2(p)
else if it in [itFuncCall, itIndex] then begin else if it in [itFuncCall, itIndex] then begin
exp.inner:=ParseCExprEx(p); exp.inner:=ParseCExprEx(p);
if p.Token = CIdentClose[it] then if p.Token = CIdentClose[it] then
@ -454,7 +454,7 @@ begin
end; end;
function IntVal(exp: TExp; m: TCMacroHandler): Integer; function PreProcVal(exp: TExp; m: TCMacroHandler): Integer;
var var
code : Integer; code : Integer;
l, r : Integer; l, r : Integer;
@ -462,6 +462,7 @@ var
rt : TExp; rt : TExp;
nm : string; nm : string;
s : string; s : string;
v : string;
const const
IntRes : array [boolean] of integer = (0,1); IntRes : array [boolean] of integer = (0,1);
begin begin
@ -475,7 +476,7 @@ begin
if (nm='defined') and Assigned(m) then Result:=IntRes[ m.isMacroDefined(s)] if (nm='defined') and Assigned(m) then Result:=IntRes[ m.isMacroDefined(s)]
else Result:=0; else Result:=0;
end else if exp.dir = edPrefix then begin end else if exp.dir = edPrefix then begin
r:=IntVal(exp.right, m); r:=PreProcVal(exp.right, m);
//writeln('koko! ', PtrUInt(exp.right)); //writeln('koko! ', PtrUInt(exp.right));
if exp.op='!' then begin if exp.op='!' then begin
if r = 0 then Result:=1 if r = 0 then Result:=1
@ -483,8 +484,8 @@ begin
end; end;
// it should be // it should be
end else if exp.dir = edInfix then begin end else if exp.dir = edInfix then begin
l:=IntVal(exp.left,m); l:=PreProcVal(exp.left,m);
r:=IntVal(exp.right,m); r:=PreProcVal(exp.right,m);
if exp.op = '+' then Result:=l+r if exp.op = '+' then Result:=l+r
else if exp.op = '-' then Result:=l-r else if exp.op = '-' then Result:=l-r
else if exp.op = '/' then Result:=l div r else if exp.op = '/' then Result:=l div r
@ -505,16 +506,18 @@ begin
else if exp.op = '>' then Result:=IntRes[l > r] else if exp.op = '>' then Result:=IntRes[l > r]
else if exp.op = '<' then Result:=IntRes[l < r]; else if exp.op = '<' then Result:=IntRes[l < r];
end else begin end else begin
Val(exp.val, Result, code); v:=trim(exp.val);
if Assigned(m) and (m.isMacroDefined(v)) then v:=m.GetMacroReplaceStr(v);
Val(v, Result, code);
end; end;
end; end;
function ValuateIntExp(exp: TExp; macros: TCMacroHandler): Integer; function ValuatePreprocExp(exp: TExp; macros: TCMacroHandler): Integer;
begin begin
Result:=IntVal(Exp, macros); Result:=PreProcVal(Exp, macros);
end; end;
function ValuateIntExp(const exp: string; macros: TCMacroHandler): Integer; function ValuatePreprocExp(const exp: string; macros: TCMacroHandler): Integer;
var var
prs : TTextParser; prs : TTextParser;
expObj : TExp; expObj : TExp;
@ -526,7 +529,7 @@ begin
if prs.NextToken then begin if prs.NextToken then begin
expObj:=ParseCExprEx(prs); expObj:=ParseCExprEx(prs);
if Assigned(expObj) if Assigned(expObj)
then Result:=ValuateIntExp(expObj, macros) then Result:=ValuatePreprocExp(expObj, macros)
else Result:=0; else Result:=0;
end else end else
Result:=0; Result:=0;

View File

@ -84,6 +84,9 @@ type
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
function ParseMacro(const Parser: TTextParser; var MacroStr, ReplaceStr: AnsiString): Boolean; function ParseMacro(const Parser: TTextParser; var MacroStr, ReplaceStr: AnsiString): Boolean;
function GetMacroReplaceStr(const Macro: AnsiString): String;
function isMacroDefined(const Macro: AnsisTring): Boolean; function isMacroDefined(const Macro: AnsisTring): Boolean;
procedure AddSimpleMacro(const MacroStr, ReplaceStr: AnsiString); procedure AddSimpleMacro(const MacroStr, ReplaceStr: AnsiString);
@ -430,6 +433,7 @@ function PreprocessHeader(const s: string; entList: TList; macros: TCMacroHandle
procedure CPrepDefineToMacrosHandler(def: TCPrepDefine; mh: TCMacroHandler); procedure CPrepDefineToMacrosHandler(def: TCPrepDefine; mh: TCMacroHandler);
procedure DebugEnList(entlist: TList); procedure DebugEnList(entlist: TList);
procedure DebugMacros(macros: TCMacroHandler; showValues: Boolean = true);
procedure ParseDefine(const s: string; def: TCPrepDefine); procedure ParseDefine(const s: string; def: TCPrepDefine);
@ -1619,6 +1623,16 @@ begin
end; end;
end; end;
function TCMacroHandler.GetMacroReplaceStr(const Macro: AnsiString): String;
var
i: Integer;
begin
Result:='';
i := MacrosNames.IndexOf(Macro);
if i<0 then Exit;
Result:=TCMacroStruct(MacrosNames.Objects[i]).ReplaceText;
end;
{ TCMacroStruct } { TCMacroStruct }
constructor TCMacroStruct.Create; constructor TCMacroStruct.Create;
@ -2410,6 +2424,22 @@ begin
end; end;
end; end;
procedure DebugMacros(macros: TCMacroHandler; showValues: Boolean = true);
var
i : integer;
cm : TCMacroStruct;
begin
if not Assigned(macros) then Exit;
for i:=0 to macros.MacrosNames.Count-1 do begin
cm := TCMacroStruct(macros.MacrosNames.Objects[i]);
if (cm.ReplaceText<>'') and showValues then
writeln(cm.MacroName,' = ', cm.ReplaceText)
else
writeln(cm.MacroName);
end;
end;
function PreprocessHeader(const s: string; entList: TList; macros: TCMacroHandler; fs: TFileOffsets): string; function PreprocessHeader(const s: string; entList: TList; macros: TCMacroHandler; fs: TFileOffsets): string;
var var
isCondMet : Boolean; isCondMet : Boolean;
@ -2456,7 +2486,7 @@ var
isCondMet:=macros.isMacroDefined(dif._Cond); isCondMet:=macros.isMacroDefined(dif._Cond);
if (dif.IfOp='ifndef') then isCondMet:=not isCondMet; if (dif.IfOp='ifndef') then isCondMet:=not isCondMet;
end else if (dif.IfOp='if') or (dif.IfOp='elif') then begin end else if (dif.IfOp='if') or (dif.IfOp='elif') then begin
isCondMet:=ValuateIntExp(dif._Cond, macros)<>0; isCondMet:=ValuatePreprocExp(dif._Cond, macros)<>0;
end else end else
isCondMet:=false; isCondMet:=false;
@ -2482,7 +2512,7 @@ var
if (TCPrepIf(ent).IfOp='elif') then begin if (TCPrepIf(ent).IfOp='elif') then begin
if (lvl=0) then begin // same level if - check cond if (lvl=0) then begin // same level if - check cond
if not isCondMet then begin if not isCondMet then begin
if ValuateIntExp(TCPrepIf(ent)._Cond, macros)=1 then begin if ValuatePreprocExp(TCPrepIf(ent)._Cond, macros)=1 then begin
isCondMet:=true; isCondMet:=true;
stSub:=i+1; stSub:=i+1;
end; end;