Skip to content

Commit e2ded82

Browse files
committed
Overflow checks: add tests/warnings/const-overflow-select-const.bas
- test overflow warnings for SELECT CASE AS CONST - test the function of parser-decl-symbtype.bas:cConstIntExprRanged()
1 parent 012d2cc commit e2ded82

File tree

8 files changed

+11470
-0
lines changed

8 files changed

+11470
-0
lines changed

tests/warnings/const-overflow-select-const.bas

Lines changed: 1190 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/'
2+
3+
Checks to see if our expectation for warnings matches the results
4+
from the compiler. This help with the first time we add the test
5+
result because we can't diff new results with any previous results.
6+
7+
For this to work, run independantly of warnings ./test.sh
8+
This needs to search the unmodified output from the compiler.
9+
10+
Usage:
11+
12+
Generate the test source
13+
$ fbc gen-select-const.bas
14+
$ ./gen-select-const > const-overflow-select-const.bas
15+
16+
Generate the results of compiling the test source
17+
$ fbc -c const-overflow-select-const.bas > const-overflow-select-const.log
18+
19+
Generate the results of the check
20+
$ fbc chk-select-const.bas
21+
$ ./chk-select-const > chk-select-const.log
22+
23+
Line numbers in the result refer to line numbers in const-overflow-select-const.log
24+
25+
'/
26+
27+
open "const-overflow-select-const.log" for input as #1
28+
29+
dim x as string
30+
dim mode as integer = 0
31+
dim lineno as integer = 0
32+
33+
while eof(1) = 0
34+
lineno += 1
35+
line input #1, x
36+
if( left(x,4) = "----" ) then
37+
exit while
38+
end if
39+
wend
40+
41+
while eof(1) = 0
42+
lineno += 1
43+
line input #1, x
44+
if( mode = 0 ) then
45+
if left( x, 8 ) = "warning " then
46+
mode = 1
47+
else
48+
if instr( x, "warning" ) > 0 then
49+
print "line " & lineno & " - unexpected warning"
50+
end if
51+
end if
52+
else
53+
if instr( x, "warning" ) = 0 then
54+
print "line " & lineno & " - expected warning"
55+
end if
56+
mode = 0
57+
end if
58+
59+
wend
60+
61+
close #1
62+
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/'
2+
3+
Generates const-overflow-select-const.bas
4+
5+
This test checks the warnings that could be generated at compile
6+
time by SELECT CASE AS CONST. More specifically, it tests
7+
parser-decl-symbtype.bas:cConstIntExprRanged() function.
8+
9+
Usage:
10+
11+
Generate the test source:
12+
$ fbc gen-select-const.bas
13+
$ ./gen-select-const > const-overflow-select-const.bas
14+
15+
Generate the results of compiling the test source:
16+
$ fbc -c const-overflow-select-const.bas > const-overflow-select-const.log
17+
18+
To check first time run, use this helper tool:
19+
$ fbc chk-select-const.bas
20+
$ ./chk-select-const
21+
22+
Line numbers reported by ./chk-select-const refer to line numbers
23+
in const-overflow-select-const.log
24+
25+
Data type range and test value matrix
26+
B = boolean
27+
S = signed
28+
U = unsigned
29+
# = byte size of data
30+
X = value is in data types range
31+
32+
B S S S S U U U U
33+
1 1 2 4 8 1 2 4 8
34+
- - - - X - - - - -9223372036854775808
35+
- - - - X - - - - -4294967297
36+
- - - - X - - - - -4294967296
37+
- - - - X - - - - -2147483649
38+
- - - X X - - - - -2147483648
39+
- - - X X - - - - -65537
40+
- - - X X - - - - -65536
41+
- - - X X - - - - -32769
42+
- - X X X - - - - -32768
43+
- - X X X - - - - -257
44+
- - X X X - - - - -256
45+
- - X X X - - - - -129
46+
- X X X X - - - - -128
47+
X X X X X - - - - -1
48+
X X X X X X X X X 0
49+
- X X X X X X X X 1
50+
- X X X X X X X X 127
51+
- - X X X X X X X 128
52+
- - X X X X X X X 255
53+
- - X X X - X X X 256
54+
- - X X X - X X X 32767
55+
- - - X X - X X X 32768
56+
- - - X X - X X X 65535
57+
- - - X X - - X X 65536
58+
- - - X X - - X X 2147483647
59+
- - - - X - - X X 2147483648
60+
- - - - X - - X X 4294967295
61+
- - - - X - - - X 4294967296
62+
- - - - X - - - X 9223372036854775807
63+
- - - - - - - - X 9223372036854775808
64+
- - - - - - - - X 18446744073709551615
65+
66+
'/
67+
68+
type VALUEINFO
69+
index as integer
70+
value as zstring * 24
71+
over32bit as integer
72+
reserved as integer
73+
end type
74+
75+
dim values( -14 to 16 ) as VALUEINFO = _
76+
{ _
77+
( -14, "-9223372036854775808ll" , true , false ), _
78+
( -13, "-4294967297ll" , true , false ), _
79+
( -12, "-4294967296ll" , true , false ), _
80+
( -11, "-2147483649ll" , true , false ), _
81+
( -10, "-2147483648ll" , false, false ), _
82+
( -9, "-65537" , false, false ), _
83+
( -8, "-65536" , false, false ), _
84+
( -7, "-32769" , false, false ), _
85+
( -6, "-32768" , false, false ), _
86+
( -5, "-257" , false, false ), _
87+
( -4, "-256" , false, false ), _
88+
( -3, "-129" , false, false ), _
89+
( -2, "-128" , false, false ), _
90+
( -1, "-1" , false, false ), _
91+
( 0, "0" , false, false ), _
92+
( 1, "1" , false, false ), _
93+
( 2, "127" , false, false ), _
94+
( 3, "128" , false, false ), _
95+
( 4, "255" , false, false ), _
96+
( 5, "256" , false, false ), _
97+
( 6, "32767" , false, false ), _
98+
( 7, "32768" , false, false ), _
99+
( 8, "65535" , false, false ), _
100+
( 9, "65536" , false, false ), _
101+
( 10, "2147483647ull" , false, false ), _
102+
( 11, "2147483648ull" , false, false ), _
103+
( 12, "4294967295ull" , false, false ), _
104+
( 13, "4294967296ull" , true, false ), _
105+
( 14, "9223372036854775807ull" , true , false ), _
106+
( 15, "9223372036854775808ull" , true , false ), _
107+
( 16, "18446744073709551615ull", false, false ) _
108+
}
109+
110+
type TYPEINFO
111+
dtype as zstring * 12 '' data type name
112+
dsize as integer '' data type size
113+
conv as zstring * 12 '' casting function
114+
value_idx1 as integer '' first value in the data type's range
115+
value_idx2 as integer '' last value in the data type's range
116+
end type
117+
118+
dim types( 0 to ... ) as TYPEINFO = _
119+
{ _
120+
( "boolean" , 1, "cbool" , -1, 0 ), _
121+
( "byte" , 1, "cbyte" , -2, 2 ), _
122+
( "ubyte" , 1, "cubyte" , 0, 4 ), _
123+
( "short" , 2, "cshort" , -6, 6 ), _
124+
( "ushort" , 2, "cushort" , 0, 8 ), _
125+
( "long" , 4, "clng" , -10, 10 ), _
126+
( "ulong" , 4, "culng" , 0, 12 ), _
127+
( "longint" , 8, "clngint" , -14, 14 ), _
128+
( "ulongint", 8, "culngint", 0, 16 ) _
129+
}
130+
131+
dim x as string
132+
133+
/'
134+
chkwarn( T, C, V, W, E )
135+
136+
T = data type name
137+
C = conversion function
138+
V = value
139+
W = if true, warning is expected to follow CASE
140+
E = if true, warning is expected to follow END SELECT
141+
'/
142+
143+
print "'' Automatically generated by " + __FILE__ + ", " + __DATE_ISO__
144+
print
145+
print "#macro chkwarn( T, C, V, W, E )"
146+
print " scope"
147+
print " dim x as T"
148+
print " select case as const x"
149+
print " #print CASE C ( V )"
150+
print " #if( W )"
151+
print " #print warning in CASE expected: Overflow in constant conversion"
152+
print " #endif"
153+
print " case C ( V )"
154+
print " #ifndef __FB_64BIT__"
155+
print " #if( E )"
156+
print " #print warning in END SELECT expected: Overflow in constant conversion"
157+
print " #endif"
158+
print " #endif"
159+
print " end select"
160+
print " end scope"
161+
print "#endmacro"
162+
print
163+
164+
'' test if a constant value(value_idx) of type(case_idx)
165+
'' will fit in expression of type(select_idx)
166+
167+
'' SELECT CASE AS CONST
168+
for select_idx as integer = lbound(types) to ubound(types)
169+
170+
print "'' " & string( 75, "-" )
171+
print
172+
173+
print "#print ---- " & ucase(types(select_idx).dtype) & " ----"
174+
print
175+
176+
print "#print SELECT CASE AS CONST " & ucase( types(select_idx).dtype )
177+
print
178+
179+
'' CASE
180+
for case_idx as integer = lbound(types) to ubound(types)
181+
182+
'' VALUE
183+
for value_idx as integer = types(case_idx).value_idx1 to types(case_idx).value_idx2
184+
x = chr(9) & "chkwarn( "
185+
x &= types(select_idx).dtype
186+
x &= ", "
187+
x &= types(case_idx).conv
188+
x &= ", "
189+
x &= values(value_idx).value
190+
x &= ", "
191+
192+
'' 'value_idx' out of range for type 'select_idx'
193+
if( value_idx < types(select_idx).value_idx1 or value_idx > types(select_idx).value_idx2 ) then
194+
x &= "true"
195+
else
196+
x &= "false"
197+
end if
198+
199+
x &= ", "
200+
201+
'' if SELECT CASE AS CONST data type is <= 32 bits and
202+
'' if jump table bias is over 32 bits after cast to the
203+
'' SELECT CASE AS CONST data type, we expect a warning
204+
'' in the jump table subtraction expression
205+
206+
if( (types(select_idx).dsize <= 4) and (values(value_idx).over32bit <> false) ) then
207+
x &= "true"
208+
else
209+
x &= "false"
210+
end if
211+
x &= " )"
212+
213+
print x
214+
next
215+
print
216+
next
217+
218+
print
219+
220+
next

0 commit comments

Comments
 (0)