Skip to content

Commit 74a9527

Browse files
committed
fbc: basic-macros: fix regression and macro / define name handling
- see commit e1a25ee - previous to the basic-macros feature additions, #define's with parens must be invoked with parens and if used without parens, then can be allowed to be a different symbol in some contexts. - without this fix, existing fbc headers will be broken where built-in defines like 'rgb' and 'bit' are used for fields or parameters. #define D() 4321 '' following are OK since symbol is used with parens and so does not '' invoke the macro type T D as string rgb as ulong bit as ubyte end type declare sub s( byval D as single )
1 parent 762f54d commit 74a9527

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

src/compiler/pp-define.bas

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private function hLoadDefine _
386386
hasParens = true
387387
else
388388
'' not an error, macro can be passed as param to other macros
389-
if( pp.invoking > 0 ) then
389+
if( (pp.invoking > 0) or ((symbGetDefineFlags( s ) and FB_DEFINE_FLAGS_NEEDPARENS) <> 0 ) ) then
390390
exit function
391391
end if
392392
end if
@@ -781,7 +781,7 @@ private function hLoadDefineW _
781781
hasParens = true
782782
else
783783
'' not an error, macro can be passed as param to other macros
784-
if( pp.invoking > 0 ) then
784+
if( (pp.invoking > 0) or ((symbGetDefineFlags( s ) and FB_DEFINE_FLAGS_NEEDPARENS) <> 0 ) ) then
785785
exit function
786786
end if
787787
end if
@@ -1090,7 +1090,8 @@ private sub hReadDefineText _
10901090
byval sym as FBSYMBOL ptr, _
10911091
byval defname as zstring ptr, _
10921092
byval isargless as integer, _
1093-
byval ismultiline as integer _
1093+
byval ismultiline as integer, _
1094+
byval flags as integer _
10941095
)
10951096

10961097
dim as zstring ptr text = any
@@ -1109,7 +1110,7 @@ private sub hReadDefineText _
11091110
errReportEx( FB_ERRMSG_DUPDEFINITION, defname )
11101111
end if
11111112
else
1112-
symbAddDefine( defname, text, len( *text ), isargless )
1113+
symbAddDefine( defname, text, len( *text ), isargless, , flags )
11131114
end if
11141115

11151116
'' unicode..
@@ -1126,7 +1127,7 @@ private sub hReadDefineText _
11261127
errReportEx( FB_ERRMSG_DUPDEFINITION, defname )
11271128
end if
11281129
else
1129-
symbAddDefineW( defname, textw, len( *textw ), isargless )
1130+
symbAddDefineW( defname, textw, len( *textw ), isargless, , flags )
11301131
end if
11311132

11321133
end if
@@ -1298,7 +1299,7 @@ sub ppDefine( byval ismultiline as integer )
12981299

12991300
'' not a macro?
13001301
if( params = 0 ) then
1301-
hReadDefineText( sym, @defname, isargless, ismultiline )
1302+
hReadDefineText( sym, @defname, isargless, ismultiline, define_flags )
13021303
exit sub
13031304
end if
13041305

src/compiler/rtl-macro.bas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,9 @@ private sub hAddMacro( byval macdef as FB_RTL_MACRODEF ptr )
584584
'' for each parameter..
585585
dim as FB_DEFPARAM ptr paramhead, lastparam
586586
for i as integer = 0 to macdef->params-1
587+
'' if there are parameters, then must have parens
588+
'' TODO: if other optional flags are needed, then in macrodata()
589+
flags or= FB_DEFINE_FLAGS_NEEDPARENS
587590
lastparam = symbAddDefineParam( lastparam, macdef->paramTb(i) )
588591
if( paramhead = NULL ) then
589592
paramhead = lastparam

src/compiler/symb-define.bas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ sub symbDefineInit _
789789
lastparam = symbAddDefineParam( lastparam, macroTb(i).params(j) )
790790
next
791791

792+
'' TODO: if any macros are added that don't need params, then
793+
'' flags should be stored in macroTb
792794
var sym = symbAddDefineMacro( macroTb(i).name, NULL, macroTb(i).nparams, firstparam, macroTb(i).flags or FB_DEFINE_FLAGS_NEEDPARENS )
793795
sym->def.mprocz = macroTb(i).procz
794796
sym->def.mprocw = macroTb(i).procw

tests/pp/define-shadow.bas

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
' TEST_MODE : COMPILE_ONLY_OK
2+
3+
'' macro with parens is seen as macro only when using parens
4+
5+
#define D1() 1
6+
7+
type T1
8+
'' no expansion of D1() or built-ins
9+
D1 as double
10+
rgb as double
11+
bit as double
12+
end type
13+
14+
'' no expansion of D1()
15+
declare sub S1 _
16+
( _
17+
byval D1 as single, _
18+
byval rgb as double, _
19+
byval bit as double _
20+
)
21+
22+
'' expand D
23+
var X1 = D1()
24+
25+
26+
#define D2(arg1,arg2) 1
27+
28+
type T2
29+
'' no expansion of D2() or built-ins
30+
D2 as double
31+
rgb as double
32+
bit as double
33+
end type
34+
35+
'' no expansion of D2()
36+
declare sub S2 _
37+
( _
38+
byval D2 as single, _
39+
byval rgb as double, _
40+
byval bit as double _
41+
)
42+
43+
'' expand D
44+
var X2 = D2(,)

0 commit comments

Comments
 (0)