Skip to content

Commit 82f3342

Browse files
committed
Merge remote-tracking branch 'jayrm/bugfix-994' into fbc-1.10
2 parents cda1b56 + 0ea2326 commit 82f3342

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Version 1.10.1
2424
- rtlib: emscripten: CURDIR and EXEPATH always returned ""
2525
- emscripten/fbc: -sASYNCIFY wasn't passed to emcc at link-time as needed by recent Emscripten
2626
- sf.net #993: ignore (parse) single line comments for macros invoked with optional parentheses instead of passing as an argument to macro
27-
- sf.net #994: Bad handling of macro argument evaluation - reset file pointers after evaluating arguments in __fb_query_symbol__, __fb_eval__, __fb_arg_extract__, __fb_iif__
27+
- sf.net #994: Bad handling of macro argument evaluation - reset file pointers to previous context after evaluating arguments in __fb_query_symbol__, __fb_eval__, __fb_arg_extract__, __fb_iif__, and releasing lexer context
2828

2929

3030
Version 1.10.0

src/compiler/fb.bas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ sub fbInit _
496496
hashInit( @env.inconcehash, FB_INITINCFILES, FALSE )
497497

498498
stackNew( @parser.stmt.stk, FB_INITSTMTSTACKNODES, len( FB_CMPSTMTSTK ), FALSE )
499-
lexInit( FALSE, FALSE )
499+
lexInit( LEX_TKCTX_CONTEXT_INIT )
500500
parserInit( )
501501
rtlInit( )
502502

@@ -1661,7 +1661,7 @@ sub fbIncludeFile(byval filename as zstring ptr, byval isonce as integer)
16611661
'' parse
16621662
lexPushCtx( )
16631663

1664-
lexInit( TRUE, FALSE )
1664+
lexInit( LEX_TKCTX_CONTEXT_INCLUDE )
16651665

16661666
cProgram()
16671667

src/compiler/lex.bas

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ sub lexPopCtx( )
6060
DWstrAllocate( lex.ctx->deftextw, 0 )
6161
end if
6262

63-
'' restore file pointer position if current context is different from previous
64-
if( (lex.ctx-1)->physfilepos > 0 ) then
65-
if( (lex.ctx-1)->physfilepos <> lex.ctx->physfilepos ) then
66-
seek #env.inf.num, lex.ctx->physfilepos
63+
64+
'' if it's an include file, don't bother restoring file position because
65+
'' we are going to be closing the file anyway
66+
if( lex.ctx->kind <> LEX_TKCTX_CONTEXT_INCLUDE ) then
67+
'' restore file pointer position if current context is different from previous
68+
if( (lex.ctx-1)->physfilepos > 0 ) then
69+
if( (lex.ctx-1)->physfilepos <> lex.ctx->physfilepos ) then
70+
seek #env.inf.num, (lex.ctx-1)->physfilepos
71+
end if
6772
end if
6873
end if
6974

@@ -75,14 +80,16 @@ end sub
7580
'':::::
7681
sub lexInit _
7782
( _
78-
byval isinclude as integer, _
79-
byval is_fb_eval as integer _
83+
byval ctx_kind as LEX_TKCTX_CONTEXT _
8084
)
8185

8286
dim as integer i
8387
dim as FBTOKEN ptr n
8488

85-
if( (env.includerec = 0) and (is_fb_eval = FALSE) ) then
89+
'' !!!TODO!!! - determine if env.includerec check can be removed
90+
91+
'' first time? make sure lex.ctx points to something
92+
if( (env.includerec = 0) and (ctx_kind = LEX_TKCTX_CONTEXT_INIT) ) then
8693
lex.ctx = @lex.ctxTB(0)
8794
end if
8895

@@ -108,12 +115,15 @@ sub lexInit _
108115
lex.ctx->lahdchar1 = UINVALID
109116
lex.ctx->lahdchar2 = UINVALID
110117

111-
lex.ctx->is_fb_eval = is_fb_eval
118+
lex.ctx->kind = ctx_kind
112119

113-
if( is_fb_eval ) then
120+
'' preprocessor evaluation?
121+
if( ctx_kind = LEX_TKCTX_CONTEXT_EVAL ) then
114122
lex.ctx->linenum = (lex.ctx-1)->linenum
115123
lex.ctx->reclevel = (lex.ctx-1)->reclevel
116124
lex.ctx->currmacro = (lex.ctx-1)->currmacro
125+
126+
'' else it is an include file or first time initialization
117127
else
118128
lex.ctx->linenum = 1
119129
lex.ctx->reclevel = 0
@@ -127,7 +137,7 @@ sub lexInit _
127137
lex.ctx->deflen = 0
128138

129139
if( env.inf.format = FBFILE_FORMAT_ASCII ) then
130-
lex.ctx->buffptr = iif( is_fb_eval, @lex.ctx->buff, NULL )
140+
lex.ctx->buffptr = iif( ctx_kind = LEX_TKCTX_CONTEXT_EVAL, @lex.ctx->buff, NULL )
131141
lex.ctx->defptr = NULL
132142
DZstrAllocate( lex.ctx->deftext, 0 )
133143
else
@@ -136,26 +146,30 @@ sub lexInit _
136146
DWstrAllocate( lex.ctx->deftextw, 0 )
137147
end if
138148

139-
''
140-
if( is_fb_eval ) then
149+
'' preprocessor evaluation?
150+
if( ctx_kind = LEX_TKCTX_CONTEXT_EVAL ) then
141151
lex.ctx->filepos = (lex.ctx-1)->filepos
142152
lex.ctx->lastfilepos = (lex.ctx-1)->lastfilepos
143153
lex.ctx->physfilepos = (lex.ctx-1)->physfilepos
154+
155+
'' else it is an include file or first time initialization
144156
else
145157
lex.ctx->filepos = 0
146158
lex.ctx->lastfilepos = 0
147159
lex.ctx->physfilepos = 0
148160
end if
149161

150162
'' only if it's not on an inc file
151-
if( (env.includerec = 0) or (is_fb_eval = TRUE) ) then
163+
'' !!!TODO!!! - determine if env.includerec check can be removed
164+
'' if( ctx_kind <> LEX_TKCTX_CONTEXT_INCLUDE ) then
165+
if( (env.includerec = 0) or (ctx_kind = LEX_TKCTX_CONTEXT_EVAL) ) then
152166
DZstrAllocate( lex.ctx->currline, 0 )
153167
lex.insidemacro = FALSE
154168
end if
155169

156170
lex.ctx->after_space = FALSE
157171

158-
if( (isinclude = FALSE) and (is_fb_eval = FALSE ) ) then
172+
if( ctx_kind = LEX_TKCTX_CONTEXT_INIT ) then
159173
ppInit( )
160174
end if
161175

@@ -2445,7 +2459,7 @@ function lexPeekCurrentLine _
24452459
'' get file contents around current token
24462460
old_p = seek( env.inf.num )
24472461

2448-
if( lex.ctx->is_fb_eval ) then
2462+
if( lex.ctx->kind = LEX_TKCTX_CONTEXT_EVAL ) then
24492463
p = (lex.ctx-1)->lastfilepos - 512
24502464
else
24512465
p = lex.ctx->lastfilepos - 512

src/compiler/lex.bi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ const FB_LEX_MAXK = 3
7777

7878
const LEX_MAXBUFFCHARS = 8192
7979

80+
enum LEX_TKCTX_CONTEXT
81+
LEX_TKCTX_CONTEXT_INIT = 0 '' initializing, first time only
82+
LEX_TKCTX_CONTEXT_INCLUDE = 1 '' include file
83+
LEX_TKCTX_CONTEXT_EVAL = 2 '' preprocessor evaluation
84+
end enum
85+
8086
type LEX_TKCTX
8187
tokenTB(0 to FB_LEX_MAXK) as FBTOKEN
8288
k as integer '' look ahead cnt (1..MAXK)
@@ -95,7 +101,7 @@ type LEX_TKCTX
95101
currmacro as FBSYMBOL ptr '' used to check macro recursion
96102

97103
kwdns as FBSYMBOL ptr '' used by the PP
98-
is_fb_eval as integer '' TRUE if inside an FB_EVAL
104+
kind as LEX_TKCTX_CONTEXT '' the kind of lexer context
99105

100106
'' last #define's text
101107
deflen as integer
@@ -159,8 +165,7 @@ end type
159165

160166
declare sub lexInit _
161167
( _
162-
byval isinclude as integer, _
163-
byval is_fb_eval as integer _
168+
byval ctx_kind as LEX_TKCTX_CONTEXT _
164169
)
165170

166171
declare sub lexEnd _

src/compiler/symb-define.bas

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ private function hMacro_EvalZ( byval arg as zstring ptr, byval errnum as integer
388388
'' - text to expand is to be loaded in LEX.CTX->DEFTEXT[W]
389389
'' - use the parser to build an AST for the literal result
390390

391-
lexPushCtx()
392-
lexInit( FALSE, TRUE )
391+
lexPushCtx( )
392+
lexInit( LEX_TKCTX_CONTEXT_EVAL )
393393

394394
'' prevent cExpression from writing to .pp.bas file
395395
lex.ctx->reclevel += 1
@@ -428,7 +428,7 @@ private function hMacro_EvalZ( byval arg as zstring ptr, byval errnum as integer
428428

429429
lex.ctx->reclevel -= 1
430430

431-
lexPopCtx()
431+
lexPopCtx( )
432432

433433
if( errmsg <> FB_ERRMSG_OK ) then
434434
errReportEx( errmsg, *arg )
@@ -464,8 +464,8 @@ private function hMacro_EvalW( byval arg as wstring ptr, byval errnum as integer
464464
'' - text to expand is to be loaded in LEX.CTX->DEFTEXT[W]
465465
'' - use the parser to build an AST for the literal result
466466

467-
lexPushCtx()
468-
lexInit( FALSE, TRUE )
467+
lexPushCtx( )
468+
lexInit( LEX_TKCTX_CONTEXT_EVAL )
469469

470470
'' prevent cExpression from writing to .pp.bas file
471471
lex.ctx->reclevel += 1
@@ -504,7 +504,7 @@ private function hMacro_EvalW( byval arg as wstring ptr, byval errnum as integer
504504

505505
lex.ctx->reclevel -= 1
506506

507-
lexPopCtx()
507+
lexPopCtx( )
508508

509509
if( errmsg <> FB_ERRMSG_OK ) then
510510
errReportEx( errmsg, *arg )
@@ -1058,8 +1058,8 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
10581058
var errmsg = FB_ERRMSG_OK
10591059

10601060
'' create a lightweight context push
1061-
lexPushCtx()
1062-
lexInit( FALSE, TRUE )
1061+
lexPushCtx( )
1062+
lexInit( LEX_TKCTX_CONTEXT_EVAL )
10631063

10641064
'' prevent cExpression from writing to .pp.bas file
10651065
lex.ctx->reclevel += 1
@@ -1153,7 +1153,10 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
11531153
sym = NULL
11541154

11551155
'' reset the current lexer context and refresh the text to parse.
1156-
lexInit( FALSE, TRUE )
1156+
'' !!!TODO!!! - probably more efficient with some kind of 'lexReinit()' function
1157+
lexPopCtx( )
1158+
lexPushCtx( )
1159+
lexInit( LEX_TKCTX_CONTEXT_EVAL )
11571160

11581161
hArgInsertArgA( sexpr )
11591162
hArgAppendLFCHAR()
@@ -1219,7 +1222,7 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
12191222

12201223
lex.ctx->reclevel -= 1
12211224

1222-
lexPopCtx()
1225+
lexPopCtx( )
12231226

12241227
else
12251228
'' NUMARG isn't a number

0 commit comments

Comments
 (0)