Skip to content

Commit cda1b56

Browse files
committed
Merge remote-tracking branch 'jayrm/bugfix-994' into fbc-1.10
2 parents 5e02be7 + b86ed4e commit cda1b56

23 files changed

+318
-40
lines changed

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Version 1.10.1
1111
- release: add recipes for building fbc distros using gcc-11.2 and gcc-12.2 Mingw-w64 project toolchains
1212
- release: add recipe for building fbc distros using winlibs-gcc-11.2/3/4 toolchain
1313
- fbc: '-fbgfx' command line option to link against correct libfbgfx variant, for platforms (emscripten) where it's not automatic due to missing objinfo support
14+
- basic-macros: better support for zstring and wstring conversion in evaluation of compile time arguments in __fb_query_symbol__, __fb_arg_extract__, __fb_iif__
1415

1516
[fixed]
1617
- sf.net #982: Array descriptors emitted incorrectly in gcc backend
@@ -23,6 +24,7 @@ Version 1.10.1
2324
- rtlib: emscripten: CURDIR and EXEPATH always returned ""
2425
- emscripten/fbc: -sASYNCIFY wasn't passed to emcc at link-time as needed by recent Emscripten
2526
- 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__
2628

2729

2830
Version 1.10.0

src/compiler/lex-utf.bas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ end function
300300
private function hUTF16LEToUTF16LE( ) as integer static
301301

302302
if( get( #env.inf.num, , lex.ctx->buffw ) = 0 ) then
303-
function = cunsg(seek( env.inf.num ) - lex.ctx->filepos) \ len( ushort )
303+
lex.ctx->physfilepos = seek( env.inf.num )
304+
function = cunsg(lex.ctx->physfilepos - lex.ctx->filepos) \ len( ushort )
304305
else
305306
function = 0
306307
end if
@@ -600,7 +601,8 @@ end function
600601
private function hUTF32LEToUTF32LE( ) as integer static
601602

602603
if( get( #env.inf.num, , lex.ctx->buffw ) = 0 ) then
603-
function = cunsg(seek( env.inf.num ) - lex.ctx->filepos) \ sizeof( ulong )
604+
lex.ctx->physfilepos = seek( env.inf.num )
605+
function = cunsg(lex.ctx->physfilepos - lex.ctx->filepos) \ sizeof( ulong )
604606
else
605607
function = 0
606608
end if

src/compiler/lex.bas

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ 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
67+
end if
68+
end if
69+
6370
lex.ctx -= 1
6471

6572
end sub
@@ -133,9 +140,11 @@ sub lexInit _
133140
if( is_fb_eval ) then
134141
lex.ctx->filepos = (lex.ctx-1)->filepos
135142
lex.ctx->lastfilepos = (lex.ctx-1)->lastfilepos
143+
lex.ctx->physfilepos = (lex.ctx-1)->physfilepos
136144
else
137145
lex.ctx->filepos = 0
138146
lex.ctx->lastfilepos = 0
147+
lex.ctx->physfilepos = 0
139148
end if
140149

141150
'' only if it's not on an inc file
@@ -227,7 +236,8 @@ private function hReadChar _
227236
select case as const env.inf.format
228237
case FBFILE_FORMAT_ASCII
229238
if( get( #env.inf.num, , lex.ctx->buff ) = 0 ) then
230-
lex.ctx->bufflen = seek( env.inf.num ) - lex.ctx->filepos
239+
lex.ctx->physfilepos = seek( env.inf.num )
240+
lex.ctx->bufflen = lex.ctx->physfilepos - lex.ctx->filepos
231241
lex.ctx->buffptr = @lex.ctx->buff
232242
end if
233243

src/compiler/lex.bi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type LEX_TKCTX
129129

130130
filepos as integer
131131
lastfilepos as integer
132+
physfilepos as integer
132133

133134
currline as DZSTRING '' current line in text form
134135

src/compiler/symb-define.bas

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,57 @@ private function hMacro_getArgW( byval argtb as LEXPP_ARGTB ptr, byval num as in
317317

318318
end function
319319

320+
#define hIsTokenEndOfStream() ((lexGetToken() = FB_TK_EOL) orelse (lexGetToken() = FB_TK_EOF))
321+
322+
private sub hArgAppendLFCHAR( )
323+
'' Add an end of expression marker so that the parser
324+
'' doesn't read past the end of the expression text
325+
'' by appending an LFCHAR to the end of the expression
326+
'' It would be better to use the explicit EOF character,
327+
'' but we can't appened an extra NUL character to a zstring
328+
329+
'' ascii
330+
if( env.inf.format = FBFILE_FORMAT_ASCII ) then
331+
DZstrConcatAssign( lex.ctx->deftext, LFCHAR )
332+
lex.ctx->defptr = lex.ctx->deftext.data
333+
lex.ctx->deflen += len( LFCHAR )
334+
'' unicode
335+
else
336+
DWstrConcatAssignA( lex.ctx->deftextw, LFCHAR )
337+
lex.ctx->defptrw = lex.ctx->deftextw.data
338+
lex.ctx->deflen += len( LFCHAR )
339+
end if
340+
end sub
341+
342+
private sub hArgInsertArgA( byval arg as zstring ptr )
343+
'' ascii
344+
if( env.inf.format = FBFILE_FORMAT_ASCII ) then
345+
DZstrAssign( lex.ctx->deftext, *arg )
346+
lex.ctx->defptr = lex.ctx->deftext.data
347+
lex.ctx->deflen += len( *arg )
348+
'' unicode
349+
else
350+
DWstrAssignA( lex.ctx->deftextw, *arg )
351+
lex.ctx->defptrw = lex.ctx->deftextw.data
352+
lex.ctx->deflen += len( *arg )
353+
end if
354+
end sub
355+
356+
private sub hArgInsertArgW( byval arg as wstring ptr )
357+
'' ascii
358+
if( env.inf.format = FBFILE_FORMAT_ASCII ) then
359+
DZstrAssignW( lex.ctx->deftext, *arg )
360+
lex.ctx->defptr = lex.ctx->deftext.data
361+
lex.ctx->deflen += len( *arg )
362+
'' unicode
363+
else
364+
DWstrAssign( lex.ctx->deftextw, *arg )
365+
lex.ctx->defptrw = lex.ctx->deftextw.data
366+
lex.ctx->deflen += len( *arg )
367+
end if
368+
end sub
369+
370+
320371
private function hMacro_EvalZ( byval arg as zstring ptr, byval errnum as integer ptr ) as string
321372

322373
'' the expression should have already been handled in hLoadMacro|hLoadMacroW
@@ -343,19 +394,8 @@ private function hMacro_EvalZ( byval arg as zstring ptr, byval errnum as integer
343394
'' prevent cExpression from writing to .pp.bas file
344395
lex.ctx->reclevel += 1
345396

346-
DZstrAssign( lex.ctx->deftext, *arg )
347-
lex.ctx->defptr = lex.ctx->deftext.data
348-
lex.ctx->deflen += len( *arg )
349-
350-
'' Add an end of expression marker so that the parser
351-
'' doesn't read past the end of the expression text
352-
'' by appending an LFCHAR to the end of the expression
353-
'' It would be better to use the explicit EOF character,
354-
'' but we can't appened an extra NUL character to a zstring
355-
356-
DZstrConcatAssign( lex.ctx->deftext, LFCHAR )
357-
lex.ctx->defptr = lex.ctx->deftext.data
358-
lex.ctx->deflen += len( LFCHAR )
397+
hArgInsertArgA( arg )
398+
hArgAppendLFCHAR()
359399

360400
dim expr as ASTNODE ptr = cExpression( )
361401
var errmsg = FB_ERRMSG_OK
@@ -367,13 +407,13 @@ private function hMacro_EvalZ( byval arg as zstring ptr, byval errnum as integer
367407
DZStrAssign( res, astConstFlushToStr( expr ) )
368408

369409
'' any tokens still in the buffer? cExpression() should have used them all
370-
if( lexGetToken( ) <> FB_TK_EOL ) then
410+
if( not hIsTokenEndOfStream() ) then
371411
errmsg = FB_ERRMSG_SYNTAXERROR
372412
end if
373413
elseif( astIsConstant( expr ) ) then
374414
DZStrAssign( res, symbGetConstStrAsStr( expr->sym ) )
375415
'' any tokens still in the buffer? cExpression() should have used them all
376-
if( lexGetToken( ) <> FB_TK_EOL ) then
416+
if( not hIsTokenEndOfStream() ) then
377417
errmsg = FB_ERRMSG_SYNTAXERROR
378418
end if
379419
astDelTree( expr )
@@ -430,19 +470,8 @@ private function hMacro_EvalW( byval arg as wstring ptr, byval errnum as integer
430470
'' prevent cExpression from writing to .pp.bas file
431471
lex.ctx->reclevel += 1
432472

433-
DWstrAssign( lex.ctx->deftextw, *arg )
434-
lex.ctx->defptrw = lex.ctx->deftextw.data
435-
lex.ctx->deflen += len( *arg )
436-
437-
'' Add an end of expression marker so that the parser
438-
'' doesn't read past the end of the expression text
439-
'' by appending an LFCHAR to the end of the expression
440-
'' It would be better to use the explicit EOF character,
441-
'' but we can't appened an extra NUL character to a zstring
442-
443-
DWstrConcatAssign( lex.ctx->deftextw, LFCHAR )
444-
lex.ctx->defptrw = lex.ctx->deftextw.data
445-
lex.ctx->deflen += len( LFCHAR )
473+
hArgInsertArgW( arg )
474+
hArgAppendLFCHAR()
446475

447476
dim expr as ASTNODE ptr = cExpression( )
448477
var errmsg = FB_ERRMSG_OK
@@ -454,13 +483,13 @@ private function hMacro_EvalW( byval arg as wstring ptr, byval errnum as integer
454483
DWStrAssign( res, astConstFlushToWstr( expr ) )
455484

456485
'' any tokens still in the buffer? cExpression() should have used them all
457-
if( lexGetToken( ) <> FB_TK_EOL ) then
486+
if( not hIsTokenEndOfStream() ) then
458487
errmsg = FB_ERRMSG_SYNTAXERROR
459488
end if
460489
elseif( astIsConstant( expr ) ) then
461490
DWStrAssign( res, symbGetConstStrAsWstr( expr->sym ) )
462491
'' any tokens still in the buffer? cExpression() should have used them all
463-
if( lexGetToken( ) <> FB_TK_EOL ) then
492+
if( not hIsTokenEndOfStream() ) then
464493
errmsg = FB_ERRMSG_SYNTAXERROR
465494
end if
466495
astDelTree( expr )
@@ -1035,9 +1064,8 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
10351064
'' prevent cExpression from writing to .pp.bas file
10361065
lex.ctx->reclevel += 1
10371066

1038-
DZstrAssign( lex.ctx->deftext, *sexpr )
1039-
lex.ctx->defptr = lex.ctx->deftext.data
1040-
lex.ctx->deflen += len( *sexpr )
1067+
hArgInsertArgA( sexpr )
1068+
hArgAppendLFCHAR()
10411069

10421070
'' if filtervalue is zero then set the default methods to use for
10431071
'' look-up depending on what we are looking for
@@ -1071,6 +1099,9 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
10711099
FB_TKCLASS_QUIRKWD, FB_TKCLASS_OPERATOR
10721100

10731101
sym = cIdentifierOrUDTMember( )
1102+
if( sym = NULL ) then
1103+
retry = TRUE
1104+
end if
10741105
end select
10751106

10761107
'' for some symbols, we maybe want to reset and try a TYPEOF below
@@ -1114,7 +1145,7 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
11141145
'' because there is more, try as typeof.
11151146

11161147
'' not the end of 'sym'? retry as TYPEOF
1117-
if( lexGetToken( ) <> FB_TK_EOF ) then
1148+
if( not hIsTokenEndOfStream() ) then
11181149
retry = TRUE
11191150
end if
11201151

@@ -1124,9 +1155,8 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
11241155
'' reset the current lexer context and refresh the text to parse.
11251156
lexInit( FALSE, TRUE )
11261157

1127-
DZstrAssign( lex.ctx->deftext, *sexpr )
1128-
lex.ctx->defptr = lex.ctx->deftext.data
1129-
lex.ctx->deflen += len( *sexpr )
1158+
hArgInsertArgA( sexpr )
1159+
hArgAppendLFCHAR()
11301160
end if
11311161

11321162
end if
@@ -1181,6 +1211,12 @@ private function hDefQuerySymZ_cb( byval argtb as LEXPP_ARGTB ptr, byval errnum
11811211
res = str( -1 )
11821212
end select
11831213

1214+
if( *errnum <> FB_ERRMSG_OK ) then
1215+
errReport( *errnum )
1216+
'' error recovery: skip until next line (in the buffer)
1217+
hSkipUntil( FB_TK_EOL, TRUE )
1218+
end if
1219+
11841220
lex.ctx->reclevel -= 1
11851221

11861222
lexPopCtx()
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const fbc_FB_QUERY_SYMBOL_symbclass = 0
2+
#macro SomeLongishExpressionToHitTheBadSpot(longish_argument_name)
3+
longish_argument_name
4+
#endmacro
5+
'' Padding to fill the source file to 8Kb---------------------------------------
6+
'' Padding to fill the source file to 8Kb---------------------------------------
7+
'' Padding to fill the source file to 8Kb---------------------------------------
8+
'' Padding to fill the source file to 8Kb---------------------------------------
9+
'' Padding to fill the source file to 8Kb---------------------------------------
10+
'' Padding to fill the source file to 8Kb---------------------------------------
11+
'' Padding to fill the source file to 8Kb---------------------------------------
12+
'' Padding to fill the source file to 8Kb---------------------------------------
13+
'' Padding to fill the source file to 8Kb---------------------------------------
14+
'' Padding to fill the source file to 8Kb---------------------------------------
15+
'' Padding to fill the source file to 8Kb---------------------------------------
16+
'' Padding to fill the source file to 8Kb---------------------------------------
17+
'' Padding to fill the source file to 8Kb---------------------------------------
18+
'' Padding to fill the source file to 8Kb---------------------------------------
19+
'' Padding to fill the source file to 8Kb---------------------------------------
20+
'' Padding to fill the source file to 8Kb---------------------------------------
21+
'' Padding to fill the source file to 8Kb---------------------------------------
22+
'' Padding to fill the source file to 8Kb---------------------------------------
23+
'' Padding to fill the source file to 8Kb---------------------------------------
24+
'' Padding to fill the source file to 8Kb---------------------------------------
25+
'' Padding to fill the source file to 8Kb---------------------------------------
26+
'' Padding to fill the source file to 8Kb---------------------------------------
27+
'' Padding to fill the source file to 8Kb---------------------------------------
28+
'' Padding to fill the source file to 8Kb---------------------------------------
29+
'' Padding to fill the source file to 8Kb---------------------------------------
30+
'' Padding to fill the source file to 8Kb---------------------------------------
31+
'' Padding to fill the source file to 8Kb---------------------------------------
32+
'' Padding to fill the source file to 8Kb---------------------------------------
33+
'' Padding to fill the source file to 8Kb---------------------------------------
34+
'' Padding to fill the source file to 8Kb---------------------------------------
35+
'' Padding to fill the source file to 8Kb---------------------------------------
36+
'' Padding to fill the source file to 8Kb---------------------------------------
37+
'' Padding to fill the source file to 8Kb---------------------------------------
38+
'' Padding to fill the source file to 8Kb---------------------------------------
39+
'' Padding to fill the source file to 8Kb---------------------------------------
40+
'' Padding to fill the source file to 8Kb---------------------------------------
41+
'' Padding to fill the source file to 8Kb---------------------------------------
42+
'' Padding to fill the source file to 8Kb---------------------------------------
43+
'' Padding to fill the source file to 8Kb---------------------------------------
44+
'' Padding to fill the source file to 8Kb---------------------------------------
45+
'' Padding to fill the source file to 8Kb---------------------------------------
46+
'' Padding to fill the source file to 8Kb---------------------------------------
47+
'' Padding to fill the source file to 8Kb---------------------------------------
48+
'' Padding to fill the source file to 8Kb---------------------------------------
49+
'' Padding to fill the source file to 8Kb---------------------------------------
50+
'' Padding to fill the source file to 8Kb---------------------------------------
51+
'' Padding to fill the source file to 8Kb---------------------------------------
52+
'' Padding to fill the source file to 8Kb---------------------------------------
53+
'' Padding to fill the source file to 8Kb---------------------------------------
54+
'' Padding to fill the source file to 8Kb---------------------------------------
55+
'' Padding to fill the source file to 8Kb---------------------------------------
56+
'' Padding to fill the source file to 8Kb---------------------------------------
57+
'' Padding to fill the source file to 8Kb---------------------------------------
58+
'' Padding to fill the source file to 8Kb---------------------------------------
59+
'' Padding to fill the source file to 8Kb---------------------------------------
60+
'' Padding to fill the source file to 8Kb---------------------------------------
61+
'' Padding to fill the source file to 8Kb---------------------------------------
62+
'' Padding to fill the source file to 8Kb---------------------------------------
63+
'' Padding to fill the source file to 8Kb---------------------------------------
64+
'' Padding to fill the source file to 8Kb---------------------------------------
65+
'' Padding to fill the source file to 8Kb---------------------------------------
66+
'' Padding to fill the source file to 8Kb---------------------------------------
67+
'' Padding to fill the source file to 8Kb---------------------------------------
68+
'' Padding to fill the source file to 8Kb---------------------------------------
69+
'' Padding to fill the source file to 8Kb---------------------------------------
70+
'' Padding to fill the source file to 8Kb---------------------------------------
71+
'' Padding to fill the source file to 8Kb---------------------------------------
72+
'' Padding to fill the source file to 8Kb---------------------------------------
73+
'' Padding to fill the source file to 8Kb---------------------------------------
74+
'' Padding to fill the source file to 8Kb---------------------------------------
75+
'' Padding to fill the source file to 8Kb---------------------------------------
76+
'' Padding to fill the source file to 8Kb---------------------------------------
77+
'' Padding to fill the source file to 8Kb---------------------------------------
78+
'' Padding to fill the source file to 8Kb---------------------------------------
79+
'' Padding to fill the source file to 8Kb---------------------------------------
80+
'' Padding to fill the source file to 8Kb---------------------------------------
81+
'' Padding to fill the source file to 8Kb---------------------------------------
82+
'' Padding to fill the source file to 8Kb---------------------------------------
83+
'' Padding to fill the source file to 8Kb---------------------------------------
84+
'' Padding to fill the source file to 8Kb---------------------------------------
85+
'' Padding to fill the source file to 8Kb---------------------------------------
86+
'' Padding to fill the source file to 8Kb---------------------------------------
87+
'' Padding to fill the source file to 8Kb---------------------------------------
88+
'' Padding to fill the source file to 8Kb---------------------------------------
89+
'' Padding to fill the source file to 8Kb---------------------------------------
90+
'' Padding to fill the source file to 8Kb---------------------------------------
91+
'' Padding to fill the source file to 8Kb---------------------------------------
92+
'' Padding to fill the source file to 8Kb---------------------------------------
93+
'' Padding to fill the source file to 8Kb---------------------------------------
94+
'' Padding to fill the source file to 8Kb---------------------------------------
95+
'' Padding to fill the source file to 8Kb---------------------------------------
96+
'' Padding to fill the source file to 8Kb---------------------------------------
97+
'' Padding to fill the source file to 8Kb---------------------------------------
98+
'' That should be close enough
99+
print __FB_QUERY_SYMBOL__( _
100+
SomeLongishExpressionToHitTheBadSpot( fbc_FB_QUERY_SYMBOL_symbclass ), _
101+
SomeUndefinedSymbol )
102+
print "This is the last lines of the file - This is the last lines of the file"
103+
print "This is the last lines of the file - This is the last lines of the file"
104+
print "This is the last lines of the file - This is the last lines of the file"
105+
print "This is the last lines of the file - This is the last lines of the file"
106+
print "This is the last lines of the file - This is the last lines of the file"

0 commit comments

Comments
 (0)