Skip to content

Commit 6594c1f

Browse files
authored
Merge pull request #935 from boriel-basic/fix/wrong_filename_in_errors
fix: wrong filename in errors
2 parents fad7d14 + a6e8075 commit 6594c1f

7 files changed

Lines changed: 42 additions & 21 deletions

File tree

src/api/check.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def check_type_is_explicit(lineno: int, id_: str, type_):
9191
errmsg.syntax_error_undeclared_type(lineno, id_)
9292

9393

94-
def check_call_arguments(lineno: int, id_: str, args):
94+
def check_call_arguments(lineno: int, id_: str, args, filename: str):
9595
"""Check arguments against function signature.
9696
9797
Checks every argument in a function call against a function.
@@ -109,14 +109,14 @@ def check_call_arguments(lineno: int, id_: str, args):
109109
param_names = {x.name for x in entry.ref.params}
110110
for arg in args:
111111
if arg.name is not None and arg.name not in param_names:
112-
errmsg.error(lineno, f"Unexpected argument '{arg.name}'", fname=entry.filename)
112+
errmsg.error(lineno, f"Unexpected argument '{arg.name}'", fname=filename)
113113
return False
114114

115115
last_arg_name = None
116116
for arg, param in zip(args, entry.ref.params):
117117
if last_arg_name is not None and arg.name is None:
118118
errmsg.error(
119-
lineno, f"Positional argument cannot go after keyword argument '{last_arg_name}'", fname=entry.filename
119+
lineno, f"Positional argument cannot go after keyword argument '{last_arg_name}'", fname=filename
120120
)
121121
return False
122122

@@ -139,15 +139,13 @@ def check_call_arguments(lineno: int, id_: str, args):
139139

140140
for arg in args:
141141
if arg.name is None:
142-
errmsg.error(lineno, f"Too many arguments for Function '{id_}'", fname=entry.filename)
142+
errmsg.error(lineno, f"Too many arguments for Function '{id_}'", fname=filename)
143143
return False
144144

145145
if len(named_args) != len(entry.ref.params):
146146
c = "s" if len(entry.ref.params) != 1 else ""
147147
errmsg.error(
148-
lineno,
149-
f"Function '{id_}' takes {len(entry.ref.params)} parameter{c}, not {len(args)}",
150-
fname=entry.filename,
148+
lineno, f"Function '{id_}' takes {len(entry.ref.params)} parameter{c}, not {len(args)}", fname=filename
151149
)
152150
return False
153151

@@ -195,8 +193,8 @@ def check_pending_calls():
195193
result = True
196194

197195
# Check for functions defined after calls (parameters, etc)
198-
for id_, params, lineno in global_.FUNCTION_CALLS:
199-
result = result and check_call_arguments(lineno, id_, params)
196+
for call in global_.FUNCTION_CALLS:
197+
result = result and check_call_arguments(call.lineno, call.entry.original_name, call.args, call.filename)
200198

201199
return result
202200

src/api/global_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from src.api.opcodestemps import OpcodesTemps
1515

1616
if TYPE_CHECKING:
17+
from src.symbols.call import SymbolCALL
1718
from src.symbols.id_ import SymbolID
1819

1920

@@ -90,7 +91,7 @@ class LoopInfo(NamedTuple):
9091
# Function calls pending to check
9192
# Each scope pushes (prepends) an empty list
9293
# ----------------------------------------------------------------------
93-
FUNCTION_CALLS: list[SymbolID] = []
94+
FUNCTION_CALLS: list[SymbolCALL] = []
9495

9596
# ----------------------------------------------------------------------
9697
# Function level entry ID in which scope we are in. If the list

src/symbols/call.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def __init__(self, entry: SymbolID, arglist: Iterable[SymbolARGUMENT], lineno: i
4040
super().__init__()
4141
self.entry = entry
4242
self.args = arglist # Func. call / array access
43-
self.lineno = lineno
44-
self.filename = filename
43+
self.lineno: int = lineno
44+
self.filename: str = filename
4545

4646
ref = entry.ref
4747
if isinstance(ref, FuncRef):
@@ -95,17 +95,13 @@ def make_node(cls, id_: str, params, lineno: int, filename: str) -> Optional["Sy
9595
return None
9696

9797
if entry.declared and not entry.forwarded:
98-
check.check_call_arguments(lineno, id_, params)
98+
check.check_call_arguments(lineno, id_, params, filename)
9999
else: # All functions goes to global scope by default
100100
if entry.token != "FUNCTION":
101101
entry = entry.to_function(lineno)
102102
gl.SYMBOL_TABLE.move_to_global_scope(id_)
103-
gl.FUNCTION_CALLS.append(
104-
(
105-
id_,
106-
params,
107-
lineno,
108-
)
109-
)
103+
result = cls(entry, params, lineno, filename)
104+
gl.FUNCTION_CALLS.append(result)
105+
return result
110106

111107
return cls(entry, params, lineno, filename)

src/symbols/id_/_id.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SymbolID(SymbolIdABC):
3333

3434
__slots__ = (
3535
"name",
36+
"original_name",
3637
"filename",
3738
"lineno",
3839
"mangled",
@@ -58,7 +59,8 @@ def __init__(
5859
super().__init__(name=name, lineno=lineno, filename=filename, type_=type_, class_=class_)
5960
assert class_ in (CLASS.const, CLASS.label, CLASS.var, CLASS.unknown)
6061

61-
self.name = name
62+
self.name = name # This value will be modified later removing the trailing sigil ($) if used.
63+
self.original_name = name # This value will always contain the original name, preserving the sigil if used
6264
self.filename = global_.FILENAME if filename is None else filename # In which file was first used
6365
self.lineno = lineno # In which line was first used
6466
self.mangled = f"{global_.MANGLE_CHR}{name}" # This value will be overridden later
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#line 1 "file2.bas"
2+
#line 1 "file1.bas"
3+
sub foo(a as ubyte, b as ubyte)
4+
print a, ", ", b
5+
end sub
6+
7+
#line 2 "file2.bas"
8+
9+
10+
foo(42)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#line 1 "file2.bas"
2+
#line 1 "file1.bas"
3+
sub foo(a as ubyte, b as ubyte)
4+
print a, ", ", b
5+
end sub
6+
7+
#line 2 "file2.bas"
8+
9+
10+
foo(42, 43, 44)

tests/functional/cmdline/test_errmsg.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ ND.Controls.bas:4: error: Invalid argument 'dirData'
159159
ND.Controls.bas:2: warning: [W150] Variable 'dirData' is never used
160160
>>> process_file('arch/zx48k/bad_fname_err4.bas', ['-S', '-q'])
161161
ND.Controls.bas:2: error: sub 'Controls_LABEL' declared but not implemented
162+
>>> process_file('arch/zx48k/bad_fname_err5.bas', ['-S', '-q'])
163+
file2.bas:4: error: Function 'foo' takes 2 parameters, not 1
164+
>>> process_file('arch/zx48k/bad_fname_err6.bas', ['-S', '-q'])
165+
file2.bas:4: error: Too many arguments for Function 'foo'
162166

163167
# DO LOOP type errors
164168
>>> process_file('arch/zx48k/do_crash.bas', ['-S', '-q'])

0 commit comments

Comments
 (0)