Skip to content

Commit 5b3111c

Browse files
committed
Show warnings when counter variable in FOR loop cannot exceed the constant limit value
e.g. 'for i as ubyte = 0 to 255'
1 parent cd390f4 commit 5b3111c

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/compiler/error.bas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ declare function hMakeParamDesc _
8686
( /'FB_WARNINGMSG_RETURNTYPEMISMATCH '/ 0, @"Return type mismatch" ), _
8787
( /'FB_WARNINGMSG_CALLINGCONVMISMATCH '/ 0, @"Calling convention mismatch" ), _
8888
( /'FB_WARNINGMSG_ARGCNTMISMATCH '/ 0, @"Argument count mismatch" ), _
89-
( /'FB_WARNINGMSG_SUFFIXIGNORED '/ 1, @"Suffix ignored" ) _
89+
( /'FB_WARNINGMSG_SUFFIXIGNORED '/ 1, @"Suffix ignored" ), _
90+
( /'FB_WARNINGMSG_FORENDTOOBIG '/ 1, @"FOR counter variable is unable to exceed limit value" ) _
9091
}
9192

9293
dim shared errorMsgs( 1 to FB_ERRMSGS-1 ) as const zstring ptr => _

src/compiler/error.bi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ enum FB_WARNINGMSG
377377
FB_WARNINGMSG_CALLINGCONVMISMATCH
378378
FB_WARNINGMSG_ARGCNTMISMATCH
379379
FB_WARNINGMSG_SUFFIXIGNORED
380+
FB_WARNINGMSG_FORENDTOOBIG
380381

381382
FB_WARNINGMSGS
382383
end enum

src/compiler/parser-compound-for.bas

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,46 @@ sub cForStmtBegin( )
834834
hUdtFor( stk )
835835
end if
836836

837+
if( stk->for.end.sym = NULL and stk->for.ispos.sym = NULL ) then
838+
dim as integer toobig = 0
839+
if ( stk->for.ispos.value.i ) then
840+
select case as const typeGetSizeType( stk->for.cnt.dtype )
841+
case FB_SIZETYPE_INT8
842+
toobig = (stk->for.end.value.i >= 127)
843+
case FB_SIZETYPE_UINT8
844+
toobig = (stk->for.end.value.i >= 255)
845+
case FB_SIZETYPE_INT16
846+
toobig = (stk->for.end.value.i >= 32767)
847+
case FB_SIZETYPE_UINT16
848+
toobig = (stk->for.end.value.i >= 65535)
849+
case FB_SIZETYPE_INT32
850+
toobig = (stk->for.end.value.i >= 2147483647)
851+
case FB_SIZETYPE_UINT32
852+
toobig = (stk->for.end.value.i >= 4294967295)
853+
case FB_SIZETYPE_INT64
854+
toobig = (culngint(stk->for.end.value.i) >= 9223372036854775807)
855+
case FB_SIZETYPE_UINT64
856+
toobig = (culngint(stk->for.end.value.i) >= 18446744073709551615)
857+
end select
858+
else
859+
select case as const typeGetSizeType( stk->for.cnt.dtype )
860+
case FB_SIZETYPE_UINT8, FB_SIZETYPE_UINT16, FB_SIZETYPE_UINT32, FB_SIZETYPE_UINT64
861+
toobig = (stk->for.end.value.i <= 0)
862+
case FB_SIZETYPE_INT8
863+
toobig = (stk->for.end.value.i <= -128)
864+
case FB_SIZETYPE_INT16
865+
toobig = (stk->for.end.value.i >= -32768)
866+
case FB_SIZETYPE_INT32
867+
toobig = (stk->for.end.value.i >= -2147483648)
868+
case FB_SIZETYPE_INT64
869+
toobig = (stk->for.end.value.i >= -9223372036854775808)
870+
end select
871+
end if
872+
if( toobig ) then
873+
errReportWarn( FB_WARNINGMSG_FORENDTOOBIG )
874+
end if
875+
end if
876+
837877
'' if inic, endc and stepc are all constants,
838878
'' check if this branch is needed
839879
if( isconst = 3 ) then
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#macro testForLimits(T, L, U)
2+
for i as T = 0 to U
3+
next i
4+
for i as T = 0 to L step -1
5+
next i
6+
#endmacro
7+
8+
testForLimits(ubyte, 0, 255)
9+
testForLimits(ushort, 0, 65535)
10+
testForLimits(ulong, 0, 4294967295)
11+
testForLimits(ulongint, 0, 18446744073709551615)
12+
13+
testForLimits(byte, -128, 127)
14+
testForLimits(short, -32768, 32767)
15+
testForLimits(long, -2147483648, 2147483647)
16+
testForLimits(longint, -9223372036854775808, 9223372036854775807)

0 commit comments

Comments
 (0)