From 110712e59c0434857ac36fb5aa3618b9b41f01a0 Mon Sep 17 00:00:00 2001 From: mattias Date: Wed, 30 Jul 2008 06:58:20 +0000 Subject: [PATCH] codetools: h2pas: allowing in #if && || > < >= <= git-svn-id: trunk@15915 - --- components/codetools/ccodeparsertool.pas | 24 +++++++++++++++++++-- components/codetools/examples/h2pastest.lpr | 3 +++ components/codetools/h2pastool.pas | 12 ++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/components/codetools/ccodeparsertool.pas b/components/codetools/ccodeparsertool.pas index 4bab7b8054..cc4c47e977 100644 --- a/components/codetools/ccodeparsertool.pas +++ b/components/codetools/ccodeparsertool.pas @@ -235,6 +235,8 @@ type function ExtractDefine(DefineNode: TCodeTreeNode; out MacroName, MacroParamList, MacroValue: string): boolean; + function FindEnclosingIFNDEF: TCodeTreeNode;// finds the typical IFNDEF that encloses the whole header source + procedure Replace(FromPos, ToPos: integer; const NewSrc: string); procedure IncreaseChangeStep; @@ -360,9 +362,12 @@ function TCCodeParserTool.DirectiveToken: boolean; end else if AtomIsChar(')') then begin if BracketLevel=0 then RaiseException(') without ('); - inc(BracketLevel); - end else if AtomIsCharOfSet('!+-*/') or AtomIs('!=') or AtomIs('==') + dec(BracketLevel); + end else if AtomIsCharOfSet('!+-*/><') + or AtomIs('!=') or AtomIs('==') or AtomIs('<=') or AtomIs('>=') + or AtomIs('&&') or AtomIs('||') then begin + // valid operator end else if IsIdentChar[Src[AtomStart]] then begin if AtomIs('defined') then begin // read defined(macro) @@ -1863,6 +1868,21 @@ begin Result:=true; end; +function TCCodeParserTool.FindEnclosingIFNDEF: TCodeTreeNode; +{ Search for the typical enclosing IFNDEF of c header file: + - No code in front + - #IFNDEF NAME + - #DEFINE NAME + - ... + - #ENDIF + - No code behind +} +begin + Result:=Tree.Root; + // skip non code + WriteDebugReport; +end; + function TCCodeParserTool.GetAtom: string; begin Result:=copy(Src,AtomStart,SrcPos-AtomStart); diff --git a/components/codetools/examples/h2pastest.lpr b/components/codetools/examples/h2pastest.lpr index 6e656fac6b..bbab099551 100644 --- a/components/codetools/examples/h2pastest.lpr +++ b/components/codetools/examples/h2pastest.lpr @@ -82,6 +82,9 @@ begin CCode:=CodeToolBoss.LoadFile(Filename,false,false); if CCode=nil then raise Exception.Create('loading failed '+Filename); + Tool.FindEnclosingIFNDEF(CCode); + Halt; + if Src<>'' then Src:=Src+LineEnding; Src:=Src+CCode.Source; diff --git a/components/codetools/h2pastool.pas b/components/codetools/h2pastool.pas index a2a8194b99..cf4a123df3 100644 --- a/components/codetools/h2pastool.pas +++ b/components/codetools/h2pastool.pas @@ -246,6 +246,7 @@ type Macros: TAVLTree;// tree of TH2PMacroStats function Convert(CCode, PascalCode: TCodeBuffer): boolean; procedure BuildH2PTree(ParentNode: TH2PNode = nil; StartNode: TCodeTreeNode = nil); + function FindEnclosingIFNDEF(CCode: TCodeBuffer): TCodeTreeNode; procedure SimplifyDirectives; procedure WritePascal(PascalCode: TCodeBuffer); procedure WritePascalToStream(s: TStream); @@ -1945,7 +1946,7 @@ begin if CTool=nil then CTool:=TCCodeParserTool.Create; - // pare C header file + // parse C header file CTool.Parse(CCode); CTool.WriteDebugReport; @@ -2020,6 +2021,15 @@ begin end; end; +function TH2PasTool.FindEnclosingIFNDEF(CCode: TCodeBuffer): TCodeTreeNode; +begin + if CTool=nil then + CTool:=TCCodeParserTool.Create; + // parse C header file + CTool.Parse(CCode); + Result:=CTool.FindEnclosingIFNDEF; +end; + procedure TH2PasTool.SimplifyDirectives; (* Check and improve the following cases 1.a {$DEFINE Name} and Name is never used afterwards -> disable