Skip to content

Commit afb4c16

Browse files
authored
Merge pull request #512 from boriel/bugfix/crash_on_bad_pragma
Fix crash on unknown pragma
2 parents 38b7fa5 + 09b4c29 commit afb4c16

5 files changed

Lines changed: 68 additions & 3 deletions

File tree

src/api/errmsg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ def warning_function_should_return_a_value(lineno: int, func_name: str, fname: O
192192
def warning_value_will_be_truncated(lineno: int, fname: Optional[str] = None):
193193
warning(lineno, "Value will be truncated", fname=fname)
194194

195+
196+
@register_warning('300')
197+
def warning_ignoring_unknown_pragma(lineno: int, pragma_name: str):
198+
warning(lineno, f"Ignoring unknown pragma '{pragma_name}'")
199+
195200
# endregion
196201

197202
# region [Syntax Errors]

src/zxbc/zxbparser.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import src.api.symboltable
4848
import src.api.config
4949
import src.api.utils
50+
import src.api.options
5051

5152
# Symbol Classes
5253
from src import symbols, arch
@@ -3190,19 +3191,28 @@ def p_preproc_line_pragma_option(p):
31903191
| _PRAGMA ID EQ STRING
31913192
| _PRAGMA ID EQ INTEGER
31923193
"""
3193-
setattr(OPTIONS, p[2], p[4])
3194+
try:
3195+
setattr(OPTIONS, p[2], p[4])
3196+
except src.api.options.UndefinedOptionError:
3197+
src.api.errmsg.warning_ignoring_unknown_pragma(p.lineno(2), p[2])
31943198

31953199

31963200
def p_preproc_pragma_push(p):
31973201
""" preproc_line : _PRAGMA _PUSH LP ID RP
31983202
"""
3199-
OPTIONS[p[4]].push()
3203+
try:
3204+
OPTIONS[p[4]].push()
3205+
except src.api.options.UndefinedOptionError:
3206+
src.api.errmsg.warning_ignoring_unknown_pragma(p.lineno(4), p[4])
32003207

32013208

32023209
def p_preproc_pragma_pop(p):
32033210
""" preproc_line : _PRAGMA _POP LP ID RP
32043211
"""
3205-
OPTIONS[p[4]].pop()
3212+
try:
3213+
OPTIONS[p[4]].pop()
3214+
except src.api.options.UndefinedOptionError:
3215+
src.api.errmsg.warning_ignoring_unknown_pragma(p.lineno(4), p[4])
32063216

32073217

32083218
# region INTERNAL FUNCTIONS

tests/functional/bad_pragma.asm

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
org 32768
2+
__START_PROGRAM:
3+
di
4+
push ix
5+
push iy
6+
exx
7+
push hl
8+
exx
9+
ld hl, 0
10+
add hl, sp
11+
ld (__CALL_BACK__), hl
12+
ei
13+
jp __MAIN_PROGRAM__
14+
__CALL_BACK__:
15+
DEFW 0
16+
ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
19+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
20+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
23+
ld hl, 0
24+
ld b, h
25+
ld c, l
26+
__END_PROGRAM:
27+
di
28+
ld hl, (__CALL_BACK__)
29+
ld sp, hl
30+
exx
31+
pop hl
32+
exx
33+
pop iy
34+
pop ix
35+
ei
36+
ret
37+
;; --- end of user code ---
38+
END

tests/functional/bad_pragma.bas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
#pragma BAD_PRAGMA = 1
3+
4+
#pragma PUSH(BAD_PRAGMA)
5+
6+
#pragma POP(BAD_PRAGMA)
7+
8+

tests/functional/test_errmsg.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ opt2_unused_var1.bas:2: warning: Variable 'a' is never used
196196
>>> process_file('dim_at_init_err.bas')
197197
dim_at_init_err.bas:3: error: Syntax Error. Unexpected token 'AT' <AT>
198198
dim_at_init_err.bas:4: error: Syntax Error. Unexpected token 'AT' <AT>
199+
>>> process_file('bad_pragma.bas')
200+
bad_pragma.bas:2: warning: Ignoring unknown pragma 'BAD_PRAGMA'
201+
bad_pragma.bas:4: warning: Ignoring unknown pragma 'BAD_PRAGMA'
202+
bad_pragma.bas:6: warning: Ignoring unknown pragma 'BAD_PRAGMA'
199203

200204
# Test warning silencing
201205
>>> process_file('mcleod3.bas', ['-S', '-q', '-O --expect-warnings=2'])

0 commit comments

Comments
 (0)