Skip to content

Commit 257e6b4

Browse files
committed
Add support for relative line number expressions
For example: ``` // CHECK: file.c:[[# @line + 3]]:8: error ```
1 parent 8ecf200 commit 257e6b4

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

docs/05-check-commands.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,13 @@ If the empty line is removed, the test will fail:
175175
^
176176
$ echo $?
177177
1
178+
179+
Line number expression
180+
======================
181+
182+
It is often useful to check for a specific line number in your regular
183+
expression, relative to its location in the file. Hard-coding that number can
184+
make the test fragile -- rearranging, adding, or deleting lines requires
185+
changing the expression. To solve this, FileCheck supports a variable for the
186+
current line number, `[[# @LINE ]]`, as well as simple offsets from this
187+
variable, e.g. `[[# @LINE + 4 ]]` or `[[# @LINE - 2 ]]`.

filecheck/FileCheck.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,18 @@ def exit_handler(code):
317317
if check_expression[-1] != "$":
318318
check_expression = check_expression + "$"
319319

320+
# Replace line number expressions, e.g. `[[# @LINE + 3 ]]`
321+
line_var_match = re.search(
322+
r"\[\[# +@LINE *([+-])? *([0-9]+)? *\]\]", check_expression)
323+
while line_var_match is not None:
324+
offset = int(line_var_match.group(2) or 0)
325+
if line_var_match.group(1) == "-":
326+
offset = -offset
327+
check_expression = re.sub(
328+
r"\[\[# +@LINE *([+-])? *([0-9]+)? *\]\]", str(line_idx + offset + 1), check_expression)
329+
line_var_match = re.search(
330+
r"\[\[# +@LINE *([+-])? *([0-9]+)? *\]\]", check_expression)
331+
320332
check = Check(check_type=check_type,
321333
match_type=match_type,
322334
check_keyword=check_keyword,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
; RUN: gcc "%s" -o %S/line && %S/line | filecheck %s
3+
*/
4+
5+
#include <stdio.h>
6+
int main() {
7+
// CHECK: Hello from line [[# @LINE + 1 ]]
8+
printf("Hello from line %d\n", __LINE__);
9+
return 0;
10+
}

0 commit comments

Comments
 (0)