Skip to content

Commit 9120e85

Browse files
Ben MuddSLTozer
authored andcommitted
[Dexter] Associate parser errors with correct file (#66765)
Currently if Dexter encounters a parser error with a command, the resulting error message will refer to the most recently declared file (i.e. the source file it is testing) rather than the file containing the command itself. This patch fixes this so that parser errors point towards the correct location.
1 parent a292e7e commit 9120e85

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

cross-project-tests/debuginfo-tests/dexter/dex/command/ParseCommand.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import unittest
1414
from copy import copy
1515
from pathlib import PurePath
16-
from collections import defaultdict, OrderedDict
16+
from collections import defaultdict, OrderedDict, namedtuple
1717

1818
from dex.utils.Exceptions import CommandParseError, NonFloatValueInCommand
1919

@@ -83,7 +83,7 @@ def _merge_subcommands(command_name: str, valid_commands: dict) -> dict:
8383

8484

8585
def _build_command(
86-
command_type, labels, addresses, raw_text: str, path: str, lineno: str
86+
command_type, labels, addresses, raw_text: str, path, lineno: str
8787
) -> CommandBase:
8888
"""Build a command object from raw text.
8989
@@ -100,11 +100,13 @@ def label_to_line(label_name: str) -> int:
100100
line = labels.get(label_name, None)
101101
if line != None:
102102
return line
103-
raise format_unresolved_label_err(label_name, raw_text, path, lineno)
103+
raise format_unresolved_label_err(label_name, raw_text, path.base, lineno)
104104

105105
def get_address_object(address_name: str, offset: int = 0):
106106
if address_name not in addresses:
107-
raise format_undeclared_address_err(address_name, raw_text, path, lineno)
107+
raise format_undeclared_address_err(
108+
address_name, raw_text, path.base, lineno
109+
)
108110
return AddressExpression(address_name, offset)
109111

110112
valid_commands = _merge_subcommands(
@@ -120,7 +122,7 @@ def get_address_object(address_name: str, offset: int = 0):
120122
command = eval(raw_text, valid_commands)
121123
# pylint: enable=eval-used
122124
command.raw_text = raw_text
123-
command.path = path
125+
command.path = path.declared
124126
command.lineno = lineno
125127
return command
126128

@@ -267,7 +269,8 @@ def _find_all_commands_in_file(path, file_lines, valid_commands, source_root_dir
267269
labels = {} # dict of {name: line}.
268270
addresses = [] # list of addresses.
269271
address_resolutions = {}
270-
cmd_path = path
272+
CmdPath = namedtuple("cmd_path", "base declared")
273+
cmd_path = CmdPath(path, path)
271274
declared_files = set()
272275
commands = defaultdict(dict)
273276
paren_balance = 0
@@ -346,17 +349,16 @@ def _find_all_commands_in_file(path, file_lines, valid_commands, source_root_dir
346349
elif type(command) is DexDeclareAddress:
347350
add_address(addresses, command, path, cmd_point.get_lineno())
348351
elif type(command) is DexDeclareFile:
349-
cmd_path = command.declared_file
350-
if not os.path.isabs(cmd_path):
352+
declared_path = command.declared_file
353+
if not os.path.isabs(declared_path):
351354
source_dir = (
352355
source_root_dir
353356
if source_root_dir
354357
else os.path.dirname(path)
355358
)
356-
cmd_path = os.path.join(source_dir, cmd_path)
357-
# TODO: keep stored paths as PurePaths for 'longer'.
358-
cmd_path = str(PurePath(cmd_path))
359-
declared_files.add(cmd_path)
359+
declared_path = os.path.join(source_dir, declared_path)
360+
cmd_path = CmdPath(cmd_path.base, str(PurePath(declared_path)))
361+
declared_files.add(cmd_path.declared)
360362
elif type(command) is DexCommandLine and "DexCommandLine" in commands:
361363
msg = "More than one DexCommandLine in file"
362364
raise format_parse_err(msg, path, file_lines, err_point)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Purpose:
2+
// Check that Dexter command syntax errors associate with the line and file
3+
// they appeared in rather than the current declared file.
4+
//
5+
// RUN: %dexter_regression_test_build %s -o %t
6+
// RUN: not %dexter_base test --binary %t --debugger 'lldb' -v -- %s \
7+
// RUN: | FileCheck %s --implicit-check-not=FAIL-FILENAME-MATCH
8+
9+
// CHECK: err_syntax_dexdeclarefile.cpp(14): Undeclared address: 'not_been_declared'
10+
11+
int main() { return 0; }
12+
13+
// DexDeclareFile('FAIL-FILENAME-MATCH')
14+
// DexExpectWatchValue('example', address('not_been_declared'))

0 commit comments

Comments
 (0)