Skip to content

Commit

Permalink
better error messages and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLydike committed Jul 3, 2024
1 parent 5a3e9c0 commit 970cdfe
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 22 deletions.
22 changes: 12 additions & 10 deletions filecheck/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Matcher:

opts: Options
file: FInput
operations: Iterator[CheckOp]
operations: Parser

ctx: Context = field(default_factory=Context)

Expand Down Expand Up @@ -72,10 +72,14 @@ def run(self) -> int:
self._pre_check(op)
function_table.get(op.name, self.fail_op)(op)
self._post_check(op)

# run the post-check one last time to make sure all NOT checks are taken care of.
self.file.pos = len(self.file.content) - 1
self._post_check(CheckOp("NOP", "", -1, []))
except CheckError as ex:
print(ex.message)
if op is not None:
op.print_source_repr(self.opts)
print(
f"{self.opts.match_filename}:{self.operations.line_no}: error: {ex.message}"
)
self.file.print_line_with_current_pos()

if ex.pattern:
Expand All @@ -91,8 +95,6 @@ def run(self) -> int:
print(" " * (ex.offset - 1) + "^")
return 1

# run the post-check one last time to make sure all NOT checks are taken care of.
self._post_check(CheckOp("NOP", "", -1, []))
if checks == 0:
print(
f"Error: No check strings found with prefix {self.opts.check_prefix}:"
Expand Down Expand Up @@ -133,10 +135,10 @@ def check_not(self, op: CheckOp, start: int | None):
pattern, _ = compile_uops(op, self.ctx.live_variables, self.opts)
if start is None:
start = 0
start = min(start, self.file.start_of_line())
end = max(start, self.file.start_of_line())
if self.file.find_between(pattern, start, end):
raise CheckError(f"Matched {op.check_line_repr()}")
if self.file.find_between(pattern, start, self.file.pos):
raise CheckError(
f"{self.opts.check_prefix}-NOT: excluded string found in input ('{op.arg}')"
)

def enqueue_not(self, op: CheckOp):
"""
Expand Down
21 changes: 12 additions & 9 deletions filecheck/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class Parser(Iterator[CheckOp]):
check_line_regexp: re.Pattern[str]
comment_line_regexp: re.Pattern[str]

_line: int = field(default=0)
line_no: int = field(default=0)
line: str = field(default=0)

@classmethod
def from_opts(cls, opts: Options):
Expand All @@ -75,7 +76,7 @@ def __next__(self) -> CheckOp:
returns CHECK.
"""
while True:
self._line += 1
self.line_no += 1
line = self.input.readline()
if line == "":
raise StopIteration()
Expand All @@ -98,7 +99,7 @@ def __next__(self) -> CheckOp:
if not arg:
raise ParseError(
f"found empty check string with prefix '{kind}:'",
self._line,
self.line_no,
match.start(3),
line,
)
Expand All @@ -118,19 +119,21 @@ def __next__(self) -> CheckOp:
if count == 0:
raise ParseError(
f"invalid count in -COUNT specification on prefix '{opts.check_prefix}' (count can't be 0)",
self._line,
self.line_no,
match.end(2),
line,
)
return CountOp(
"COUNT",
arg,
self._line,
self.line_no,
uops,
is_literal=literal is not None,
count=count,
)
return CheckOp(kind, arg, self._line, uops, is_literal=literal is not None)
return CheckOp(
kind, arg, self.line_no, uops, is_literal=literal is not None
)

def parse_args(self, arg: str, line: str) -> list[UOp]:
"""
Expand All @@ -149,7 +152,7 @@ def parse_args(self, arg: str, line: str) -> list[UOp]:
if not parts:
raise ParseError(
"Invalid substitution block, no ]]",
self._line,
self.line_no,
offset,
line,
)
Expand Down Expand Up @@ -181,15 +184,15 @@ def parse_args(self, arg: str, line: str) -> list[UOp]:
else:
raise ParseError(
f"Invalid substitution block, unknown format: {part}",
self._line,
self.line_no,
offset,
line,
)
elif part == "{{":
while not part.endswith("}}"):
if not parts:
raise ParseError(
"Invalid regex block, no }}", self._line, offset, line
"Invalid regex block, no }}", self.line_no, offset, line
)
part += parts.pop(0)

Expand Down
10 changes: 10 additions & 0 deletions tests/filecheck/checks/check-not/check-not-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: strip-comments.sh %s | exfail filecheck %s --comment-prefixes=DIAG,RUN | filecheck %s --check-prefix DIAG

test1
test2
test3
// CHECK: test1
// CHECK-NOT: test2
// CHECK: test3

// DIAG: check-not-1.test:8: error: CHECK-NOT: excluded string found in input ('test2')
8 changes: 8 additions & 0 deletions tests/filecheck/checks/check-not/check-not-2.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: strip-comments.sh %s | exfail filecheck %s --comment-prefixes=DIAG,RUN | filecheck %s --check-prefix DIAG

test2
test3
// CHECK-NOT: test2
// CHECK: test3

// DIAG: check-not-2.test:6: error: CHECK-NOT: excluded string found in input ('test2')
8 changes: 8 additions & 0 deletions tests/filecheck/checks/check-not/check-not-3.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: strip-comments.sh %s | exfail filecheck %s --comment-prefixes=DIAG,RUN | filecheck %s --check-prefix DIAG

test2
test3
// CHECK: test2
// CHECK-NOT: test3

// DIAG: check-not-3.test:9: error: CHECK-NOT: excluded string found in input ('test3')
6 changes: 6 additions & 0 deletions tests/filecheck/diagnostics/regex-1.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: strip-comments.sh %s | exfail filecheck %s | filecheck %s --check-prefix DIAG

// CHECK: {{unclosed
// DIAG: tests/filecheck/diagnostics/regex-1.test:3:11 Invalid regex block, no }}
// DIAG-NEXT{LITERAL}: // CHECK: {{unclosed
// DIAG-NEXT: {{ }}^
2 changes: 1 addition & 1 deletion tests/filecheck/diagnostics/substitution-1.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

// CHECK: [[malformedness
// DIAG: tests/filecheck/diagnostics/substitution-1.test:3:11 Invalid substitution block, no ]]
// DIAG-NEXT: // CHECK: [{{}}[malformedness
// DIAG-NEXT{LITERAL}: // CHECK: [[malformedness
// DIAG-NEXT: {{ }}^
4 changes: 2 additions & 2 deletions tests/filecheck/diagnostics/substitution-2.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: strip-comments.sh %s | exfail filecheck %s | filecheck %s --check-prefix DIAG

// CHECK: [[09]]
// DIAG: tests/filecheck/diagnostics/substitution-2.test:3:11 Invalid substitution block, unknown format: [{{}}[09]]
// DIAG-NEXT: // CHECK: [{{}}[09]]
// DIAG{LITERAL}: tests/filecheck/diagnostics/substitution-2.test:3:11 Invalid substitution block, unknown format: [[09]]
// DIAG-NEXT{LITERAL}: // CHECK: [[09]]
// DIAG-NEXT: {{ }}^

0 comments on commit 970cdfe

Please sign in to comment.