FILECHECK(1)
okay so bestie, welcome to the FILECHECK(1) man page from LLVM π β¨ weβre talking FileCheck, and itβs genuinely iconic in a βquietly runs half the test suiteβ way, so letβs get into it π«Ά
π¦ NAME π¦
FileCheck β Flexible pattern matching file verifier πΈ
β¨ SYNOPSIS β¨
FileCheck match-filename [βcheck-prefix=XXX] [βstrict-whitespace]
π DESCRIPTION π
okay so FileCheck reads two files β one from standard input, and one specified on the command line β and uses one to verify the other. this behavior is particularly useful for the testsuite, which wants to verify that the output of some tool (e.g. llc) contains the expected info (for example, a movsd from esp or whatever is interesting). itβs similar to using grep, but itβs optimized for matching multiple different inputs in one file in a specific order π
the match-filename file specifies the file that contains the patterns to match. the file to verify is read from standard input unless the --input-file option is used π
π₯ OPTIONS π₯
options are parsed from the environment variable FILECHECK_OPTS and from the command line β¨
-helpβ print a summary of command line options.--check-prefix prefixβ FileCheck searches the contents ofmatch-filenamefor patterns to match. by default, these patterns are prefixed with βCHECK:β. if youβd like to use a different prefix (e.g. because the same input file is checking multiple different tool or options), the--check-prefixargument lets you specify (without the trailing β:β) one or more prefixes to match. multiple prefixes are useful for tests which might change for different run options, but most lines remain the same. FileCheck does not permit duplicate prefixes, even if one is a check prefix and one is a comment prefix (see--comment-prefixesbelow).--check-prefixes prefix1,prefix2,...β an alias of--check-prefixthat allows multiple prefixes to be specified as a comma separated list.--comment-prefixes prefix1,prefix2,...β by default, FileCheck ignores any occurrence inmatch-filenameof any check prefix if it is preceded on the same line by βCOM:β or βRUN:β. see the section The βCOM:β directive for usage details. these default comment prefixes can be overridden by--comment-prefixesif they are not appropriate for your testing environment. however, doing so is not recommended in LLVMβs LIT-based test suites, which should be easier to maintain if they all follow a consistent comment style. in that case, consider proposing a change to the default comment prefixes instead π«Ά--allow-unused-prefixesβ this option controls the behavior when using more than one prefix as specified by--check-prefixor--check-prefixes, and some of these prefixes are missing in the test file. if true, this is allowed; if false, FileCheck will report an error, listing the missing prefixes. it is currently, temporarily,trueby default, and will be subsequently switched tofalse.--input-file filenameβ file to check (defaults to stdin).--match-full-linesβ by default, FileCheck allows matches of anywhere on a line. this option will require all positive matches to cover an entire line. leading and trailing whitespace is ignored, unless--strict-whitespaceis also specified. (note: negative matches fromCHECK-NOTare not affected by this option!) passing this option is equivalent to inserting{{^ *}}or{{^}}before, and{{ *$}}or{{$}}after every positive check pattern.--strict-whitespaceβ by default, FileCheck canonicalizes input horizontal whitespace (spaces and tabs) which causes it to ignore these differences (a space will match a tab). the--strict-whitespaceargument disables this behavior. end-of-line sequences are canonicalized to UNIX-style\nin all modes.--ignore-caseβ by default, FileCheck uses case-sensitive matching. this option causes FileCheck to use case-insensitive matching.--implicit-check-not check-patternβ adds implicit negative checks for the specified patterns between positive checks. the option allows writing stricter tests without stuffing them withCHECK-NOTs. for example, β--implicit-check-not warning:β can be useful when testing diagnostic messages from tools that donβt have an option similar toclang -verify. with this option FileCheck will verify that input does not contain warnings not covered by anyCHECK:patterns.--dump-input <value>β dump input to stderr, adding annotations representing currently enabled diagnostics. when there are multiple occurrences of this option, the<value>that appears earliest in the list below has precedence. the default isfail.helpβ explain input dump and quitalwaysβ always dump inputfailβ dump input on failureneverβ never dump input
--dump-input-context <N>β in the dump requested by--dump-input, print<N>input lines before and<N>input lines after any lines specified by--dump-input-filter. when there are multiple occurrences of this option, the largest specified<N>has precedence. the default is5.--dump-input-filter <value>β in the dump requested by--dump-input, print only input lines of kind<value>plus any context specified by--dump-input-context. when there are multiple occurrences of this option, the<value>that appears earliest in the list below has precedence. the default iserrorwhen--dump-input=fail, and itβsallwhen--dump-input=always.allβ all input linesannotation-fullβ input lines with annotationsannotationβ input lines with starting points of annotationserrorβ input lines with starting points of error annotations
--enable-var-scopeβ enables scope for regex variables. variables with names that start with$are considered global and remain set throughout the file. all other variables get undefined after each encounteredCHECK-LABEL.-D<VAR=VALUE>β sets a filecheck pattern variableVARwith valueVALUEthat can be used inCHECK:lines.-D#<FMT>,<NUMVAR>=<NUMERIC EXPRESSION>β sets a filecheck numeric variableNUMVARof matching formatFMTto the result of evaluating<NUMERIC EXPRESSION>that can be used inCHECK:lines. see section FileCheck Numeric Variables and Expressions for details on supported numeric expressions.-versionβ show the version number of this program.-vβ print good directive pattern matches. however, if-dump-input=failor-dump-input=always, add those matches as input annotations instead.-vvβ print information helpful in diagnosing internal FileCheck issues, such as discarded overlappingCHECK-DAG:matches, implicit EOF pattern matches, andCHECK-NOT:patterns that do not have matches. implies-v. however, if-dump-input=failor-dump-input=always, just add that information as input annotations instead.--allow-deprecated-dag-overlapβ enable overlapping among matches in a group of consecutiveCHECK-DAG:directives. this option is deprecated and is only provided for convenience as old tests are migrated to the new non-overlappingCHECK-DAG:implementation.--allow-emptyβ allow checking empty input. by default, empty input is rejected.--colorβ use colors in output (autodetected by default).
π EXIT STATUS π
if FileCheck verifies that the file matches the expected contents, it exits with 0 β¨ otherwise, if not, or if an error occurs, it will exit with a non-zero value π
π TUTORIAL π
FileCheck is typically used from LLVM regression tests, being invoked on the RUN line of the test. a simple example of using FileCheck from a RUN line looks like this:
; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s
this syntax says to pipe the current file (β%sβ) into llvm-as, pipe that into llc, then pipe the output of llc into FileCheck. this means that FileCheck will be verifying its standard input (the llc output) against the filename argument specified (the original .ll file specified by β%sβ). to see how this works, letβs look at the rest of the .ll file (after the RUN line):
define void @sub1(i32* %p, i32 %v) {
entry:
; CHECK: sub1:
; CHECK: subl
%0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v)
ret void
}
define void @inc4(i64* %p) {
entry:
; CHECK: inc4:
; CHECK: incq
%0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1)
ret void
}
here you can see some βCHECK:β lines specified in comments. now you can see how the file is piped into llvm-as, then llc, and the machine code output is what we are verifying. FileCheck checks the machine code output to verify that it matches what the βCHECK:β lines specify π
the syntax of the βCHECK:β lines is very simple: they are fixed strings that must occur in order. FileCheck defaults to ignoring horizontal whitespace differences (e.g. a space is allowed to match a tab) but otherwise, the contents of the βCHECK:β line is required to match some thing in the test file exactly.
one nice thing about FileCheck (compared to grep) is that it allows merging test cases together into logical groups. for example, because the test above is checking for the βsub1:β and βinc4:β labels, it will not match unless there is a βsublβ in between those labels. if it existed somewhere else in the file, that would not count: βgrep sublβ matches if βsublβ exists anywhere in the file π
π The FileCheck -check-prefix option π
the FileCheck -check-prefix option allows multiple test configurations to be driven from one .ll file. this is useful in many circumstances, for example, testing different architectural variants with llc. hereβs a simple example:
; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \
; RUN: | FileCheck %s -check-prefix=X32
; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \
; RUN: | FileCheck %s -check-prefix=X64
define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind {
%tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1
ret <4 x i32> %tmp1
; X32: pinsrd_1:
; X32: pinsrd $1, 4(%esp), %xmm0
; X64: pinsrd_1:
; X64: pinsrd $1, %edi, %xmm0
}
So here we're literally just vibe-checking that the codegen comes out right for both 32-bit and 64-bit codegen π«Ά two prefixes, one file, zero drama.
The "COM:" directive π¬
Okay so sometimes you wanna disable a FileCheck directive without fully deleting it, or you wanna leave a lil comment that mentions a directive by name. That's literally what "COM:" is for, bestie. Example time:
; X32: pinsrd_1:
; X32: pinsrd $1, 4(%esp), %xmm0
; COM: FIXME: X64 isn't working correctly yet for this part of codegen, but
; COM: X64 will have something similar to X32:
; COM:
; COM: X64: pinsrd_1:
; COM: X64: pinsrd $1, %edi, %xmm0
Without "COM:", you'd have to do some cursed combo of rewording + directive-syntax mangling just to stop FileCheck from reading those commented X32: and X64: occurrences as actual directives π And fr fr, there are proposed FileCheck diagnostics that might side-eye those X64 occurrences up there that don't have the trailing : β because they look like directive typos π dodging all of that is tedious for a test author, and mangling directive syntax makes your test code's whole purpose unclear. "COM:" makes all those problems poof β¨
A few important usage notes, and yes you need them:
- A "COM:" inside another directive's pattern does not comment out the rest of the pattern. Like:
; X32: pinsrd $1, 4(%esp), %xmm0 COM: This is part of the X32 pattern!
If you need to temporarily comment out part of a directive's pattern, move it to another line. Why? Because FileCheck parses "COM:" the exact same way as any other directive: only the first directive on the line counts as a directive. Periodt.
- For the sake of LIT, FileCheck treats "RUN:" just like "COM:". If that's not it for your test environment, see
--comment-prefixes. - FileCheck does not recognize "COM", "RUN", or any user-defined comment prefix as a comment directive if it's combined with one of the usual check directive suffixes, like "-NEXT:" or "-NOT:" (we get to those below, don't worry π
). FileCheck treats that combo as plain text instead. If it needs to act as a comment directive for your test environment, define it that way with
--comment-prefixes.
The "CHECK-NEXT:" directive β‘οΈ
Sometimes you wanna match lines and verify those matches land on exactly consecutive lines with nothing in between. That's the "CHECK:" + "CHECK-NEXT:" combo, bestie. Got a custom check prefix? Then just use "<PREFIX>-NEXT:". Check it:
define void @t2(<2 x double>* %r, <2 x double>* %A, double %B) {
%tmp3 = load <2 x double>* %A, align 16
%tmp7 = insertelement <2 x double> undef, double %B, i32 0
%tmp9 = shufflevector <2 x double> %tmp3,
<2 x double> %tmp7,
<2 x i32> < i32 0, i32 2 >
store <2 x double> %tmp9, <2 x double>* %r, align 16
ret void
; CHECK: t2:
; CHECK: movl 8(%esp), %eax
; CHECK-NEXT: movapd (%eax), %xmm0
; CHECK-NEXT: movhpd 12(%esp), %xmm0
; CHECK-NEXT: movl 4(%esp), %eax
; CHECK-NEXT: movapd %xmm0, (%eax)
; CHECK-NEXT: ret
}
"CHECK-NEXT:" directives reject the input unless there's exactly one newline between it and the previous directive. And no cap: a "CHECK-NEXT:" cannot be the first directive in a file. π ββοΈ
The "CHECK-SAME:" directive βοΈ
Sometimes you wanna match lines and verify the match happens on the same line as the previous match. That's "CHECK:" + "CHECK-SAME:" for you. Custom prefix? "<PREFIX>-SAME:". Easy.
"CHECK-SAME:" is highkey powerful when you pair it with "CHECK-NOT:" (described down below π).
Example that works exactly like you'd expect:
!0 = !DILocation(line: 5, scope: !1, inlinedAt: !2)
; CHECK: !DILocation(line: 5,
; CHECK-NOT: column:
; CHECK-SAME: scope: ![[SCOPE:[0-9]+]]
"CHECK-SAME:" directives reject the input if there are any newlines between it and the previous directive.
It's also iconic for skipping matchers on fields you literally do not care about. Say you're writing a test that parses a tool spitting out stuff like this:
Name: foo
Field1: ...
Field2: ...
Field3: ...
Value: 1
Name: bar
Field1: ...
Field2: ...
Field3: ...
Value: 2
Name: baz
Field1: ...
Field2: ...
Field3: ...
Value: 1
To write a test verifying foo has the value 1, you might first write this:
CHECK: Name: foo
CHECK: Value: 1{{$}}
...and that would be a bad test, bestie π if foo's value changes, the test still passes, because the "CHECK: Value: 1" line would happily match baz's value instead. To fix it you could add CHECK-NEXT matchers for every single FieldN: line, but that's verbose and needs updating the moment Field4 shows up. The more succinct way, using the "CHECK-SAME:" matcher, is:
CHECK: Name: foo
CHECK: Value:
CHECK-SAME: {{ 1$}}
That verifies the next time "Value:" shows up in the output, it's got the value 1. Slay. β¨
Note: a "CHECK-SAME:" cannot be the first directive in a file. Not negotiable.
The "CHECK-EMPTY:" directive π«₯
Need to check the next line has nothing on it, not even whitespace? "CHECK-EMPTY:" is your girl:
declare void @foo()
declare void @bar()
; CHECK: foo
; CHECK-EMPTY:
; CHECK-NEXT: bar
Just like "CHECK-NEXT:", this directive fails if there's more than one newline before it finds the next blank line, and it also cannot be the first directive in a file. Rules are rules π
The "CHECK-NOT:" directive π«
"CHECK-NOT:" is literally there to verify a string doesn't occur between two matches (or before the first match, or after the last match). Say you wanna verify a load gets removed by a transformation β a test like this does the trick:
define i8 @coerce_offset0(i32 %V, i32* %P) {
store i32 %V, i32* %P
%P2 = bitcast i32* %P to i8*
%P3 = getelementptr i8* %P2, i32 2
%A = load i8* %P3
ret i8 %A
; CHECK: @coerce_offset0
; CHECK-NOT: load
; CHECK: ret i8
}
The "CHECK-COUNT:" directive π’
Need to match multiple lines with the same pattern over and over? You can just repeat a plain CHECK: as many times as needed. But ngl that's boring β instead use a counted check "CHECK-COUNT-<num>:", where <num> is a positive decimal number. It matches the pattern exactly <num> times, no more and no less. Custom check prefix? "<PREFIX>-COUNT-<num>:" gives you the same effect. Simple example, here u go:
Loop at depth 1
Loop at depth 1
Loop at depth 1
Loop at depth 1
Loop at depth 2
Loop at depth 3
; CHECK-COUNT-6: Loop at depth {{[0-9]+}}
; CHECK-NOT: Loop at depth {{[0-9]+}}
The "CHECK-DAG:" directive πΏ
If you need to match strings that don't show up in a strictly sequential order, "CHECK-DAG:" lets you verify them between two matches (or before the first match, or after the last match). Example: clang emits vtable globals in reverse order, and with CHECK-DAG: we can keep the checks in the natural order π¦
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
struct Foo { virtual void method(); };
Foo f; // emit vtable
// CHECK-DAG: @_ZTV3Foo =
struct Bar { virtual void method(); };
Bar b;
// CHECK-DAG: @_ZTV3Bar =
CHECK-NOT: directives can be mixed with CHECK-DAG: directives to exclude strings between the surrounding CHECK-DAG: directives. And because of that, the surrounding CHECK-DAG: directives cannot be reordered β like, all occurrences matching CHECK-DAG: before CHECK-NOT: must not fall behind occurrences matching CHECK-DAG: after CHECK-NOT:. Example:
; CHECK-DAG: BEFORE
; CHECK-NOT: NOT
; CHECK-DAG: AFTER
This case rejects input strings where BEFORE occurs after AFTER. No cap, that's just how it is.
With captured variables, CHECK-DAG: can match valid topological orderings of a DAG with edges going from a variable's definition to its use. Super useful when your test cases need to match different output sequences from the instruction scheduler. Like:
; CHECK-DAG: add [[REG1:r[0-9]+]], r1, r2
; CHECK-DAG: add [[REG2:r[0-9]+]], r3, r4
; CHECK: mul r5, [[REG1]], [[REG2]]
In this case, any order of those two add instructions is allowed. Slay π
If you're defining and using variables in the same CHECK-DAG: block, be aware that the definition rule can match after its use π
So for instance, the code below will pass:
; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
; CHECK-DAG: vmov.32 [[REG2]][1]
vmov.32 d0[1]
vmov.32 d0[0]
While this other code will not:
; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
; CHECK-DAG: vmov.32 [[REG2]][1]
vmov.32 d1[1]
vmov.32 d0[0]
And while this can be very useful, it's also dangerous π because with a register sequence you must have a strong order (read before write, copy before use, etc.). If the definition your test is looking for doesn't match (because of a compiler bug, oop), it may match further away from the use β and mask real bugs away. π
In those cases, to enforce the order, use a non-DAG directive between DAG-blocks.
A CHECK-DAG: directive skips matches that overlap the matches of any preceding CHECK-DAG: directives in the same CHECK-DAG: block. Not only is this non-overlapping behavior consistent with the other directives, it's also necessary to handle sets of non-unique strings or patterns. For example, the following directives look for unordered log entries for two tasks in a parallel program, such as the OpenMP runtime:
// CHECK-DAG: [[THREAD_ID:[0-9]+]]: task_begin
// CHECK-DAG: [[THREAD_ID]]: task_end
//
// CHECK-DAG: [[THREAD_ID:[0-9]+]]: task_begin
// CHECK-DAG: [[THREAD_ID]]: task_end
okay so the second pair of directives is guaranteed not to match the same log entries as the first pair, even when the patterns are identical and even when the text of the log entries is identical, because the thread ID manages to be reused. no cap, thatβs the whole point. π
The βCHECK-LABEL:β directive π
Sometimes a file has multiple tests split into logical blocks, and one or more CHECK: directives may inadvertently succeed by matching lines in a later block. An error will usually eventually be generated, but the check flagged as causing the error may not actually bear any relationship to the actual source of the problem. messy, honestly. π
To get better error messages in these cases, use the CHECK-LABEL: directive. Itβs treated identically to a normal CHECK directive, except FileCheck makes an additional assumption: a line matched by the directive cannot also be matched by any other check present in match-filename. This is intended for lines containing labels or other unique identifiers. Conceptually, the presence of CHECK-LABEL divides the input stream into separate blocks, each processed independently, preventing a CHECK: directive in one block from matching a line in another block. If --enable-var-scope is in effect, all local variables are cleared at the beginning of the block. slay, boundaries. π
For example:
define %struct.C* @C_ctor_base(%struct.C* %this, i32 %x) {
entry:
; CHECK-LABEL: C_ctor_base:
; CHECK: mov [[SAVETHIS:r[0-9]+]], r0
; CHECK: bl A_ctor_base
; CHECK: mov r0, [[SAVETHIS]]
%0 = bitcast %struct.C* %this to %struct.A*
%call = tail call %struct.A* @A_ctor_base(%struct.A* %0)
%1 = bitcast %struct.C* %this to %struct.B*
%call2 = tail call %struct.B* @B_ctor_base(%struct.B* %1, i32 %x)
ret %struct.C* %this
}
define %struct.D* @D_ctor_base(%struct.D* %this, i32 %x) {
entry:
; CHECK-LABEL: D_ctor_base:
Using CHECK-LABEL: directives here ensures that the three CHECK: directives only accept lines corresponding to the body of the @C_ctor_base function, even if the patterns match lines found later in the file. iconic. Furthermore, if one of these three CHECK: directives fails, FileCheck will recover by continuing to the next block, allowing multiple test failures to be detected in a single invocation. multi-fail energy β¨
Thereβs no requirement that CHECK-LABEL: directives contain strings that correspond to actual syntactic labels in a source or output language: they must simply uniquely match a single line in the file being verified.
π¨ bestie, CHECK-LABEL: directives cannot contain variable definitions or uses.
Directive modifiers π
You can append a directive modifier to a directive by following the directive with {<modifier>}, where the only supported value for <modifier> is LITERAL. short list, easy win. π¦
The LITERAL directive modifier performs a literal match. The modifier results in the directive not recognizing any syntax to perform regex matching, variable capture or any substitutions. This is useful when the text to match would require excessive escaping otherwise. For example, the following will perform literal matches rather than considering these as regular expressions:
Input: [[[10, 20]], [[30, 40]]]
Output %r10: [[10, 20]]
Output %r10: [[30, 40]]
; CHECK{LITERAL}: [[[10, 20]], [[30, 40]]]
; CHECK-DAG{LITERAL}: [[30, 40]]
; CHECK-DAG{LITERAL}: [[10, 20]]
FileCheck Regex Matching Syntax π¦
All FileCheck directives take a pattern to match. For most uses of FileCheck, fixed string matching is perfectly sufficient. For some things, a more flexible form of matching is desired. To support this, FileCheck lets you specify regular expressions in matching strings, surrounded by double braces: {{yourregex}}. FileCheck implements a POSIX regular expression matcher; it supports Extended POSIX regular expressions (ERE). Because we want to use fixed string matching for a majority of what we do, FileCheck has been designed to support mixing and matching fixed string matching with regular expressions. This allows you to write things like this:
; CHECK: movhpd {{[0-9]+}}(%esp), {{%xmm[0-7]}}
In this case, any offset from the ESP register will be allowed, and any xmm register will be allowed. flexible but cute. π
Because regular expressions are enclosed with double braces, they are visually distinct, and you donβt need to use escape characters within the double braces like you would in C. In the rare case that you want to match double braces explicitly from the input, you can use something ugly like {{[}][}]}} as your pattern. Or if you are using the repetition count syntax, for example [[:xdigit:]]{8} to match exactly 8 hex digits, you would need to add parentheses like this {{([[:xdigit:]]{8})}} to avoid confusion with FileCheckβs closing double-brace. iykyk, brace chaos. π
FileCheck String Substitution Blocks π
It is often useful to match a pattern and then verify that it occurs again later in the file. For codegen tests, this can be useful to allow any register, but verify that that register is used consistently later. To do this, FileCheck supports string substitution blocks that allow string variables to be defined and substituted into patterns. Here is a simple example:
; CHECK: test5:
; CHECK: notw [[REGISTER:%[a-z]+]]
; CHECK: andw {{.*}}[[REGISTER]]
The first check line matches a regex %[a-z]+ and captures it into the string variable REGISTER. The second line verifies that whatever is in REGISTER occurs later in the file after an βandwβ. FileCheck string substitution blocks are always contained in [[ ]] pairs, and string variable names can be formed with the regex [a-zA-Z_][a-zA-Z0-9_]*. If a colon follows the name, then it is a definition of the variable; otherwise, it is a substitution. colon = definition, periodt. π
FileCheck variables can be defined multiple times, and substitutions always get the latest value. Variables can also be substituted later on the same line they were defined on. For example:
; CHECK: op [[REG:r[0-9]+]], [[REG]]
Can be useful if you want the operands of op to be the same register, and donβt care exactly which register it is. same-register representation β¨
If --enable-var-scope is in effect, variables with names that start with $ are considered to be global. All the other variables are local. All local variables get undefined at the beginning of each CHECK-LABEL block. Global variables are not affected by CHECK-LABEL. This makes it easier to ensure that individual tests are not affected by variables set in preceding tests. scope hygiene, iconic. π§
FileCheck Numeric Substitution Blocks π₯
FileCheck also supports numeric substitution blocks that allow defining numeric variables and checking for numeric values that satisfy a numeric expression constraint based on those variables via a numeric substitution. This allows CHECK: directives to verify a numeric relation between two numbers, such as the need for consecutive registers to be used. numbers getting the group-chat treatment. π
The syntax to capture a numeric value is [[#%<fmtspec>,<NUMVAR>:]] where:
%<fmtspec>,is an optional format specifier to indicate what number format to match and the minimum number of digits to expect.<NUMVAR>:is an optional definition of variable<NUMVAR>from the captured value.
The syntax of <fmtspec> is: #.<precision><conversion specifier> where:
#is an optional flag available for hex values (see<conversion specifier>below) which requires the value matched to be prefixed by0x..<precision>is an optional printf-style precision specifier in which<precision>indicates the minimum number of digits that the value matched must have, expecting leading zeros if needed.<conversion specifier>is an optional scanf-style conversion specifier to indicate what number format to match (e.g. hex number). Currently accepted format specifiers are%u,%d,%xand%X. If absent, the format specifier defaults to%u.
For example:
; CHECK: mov r[[#REG:]], 0x[[#%.8X,ADDR:]]
would match mov r5, 0x0000FEFE and set REG to the value 5 and ADDR to the value 0xFEFE. Note that due to the precision it would fail to match mov r5, 0xFEFE. precision said what it said. π
As a result of the numeric variable definition being optional, it is possible to only check that a numeric value is present in a given format. This can be useful when the value itself is not useful, for instance:
; CHECK-NOT: mov r0, r[[#]]
to check that a value is synthesized rather than moved around.
The syntax of a numeric substitution is [[#%<fmtspec>, <constraint> <expr>]] where:
<fmtspec>is the same format specifier as for defining a variable but in this context indicating how a numeric expression value should be matched against. If absent, both components of the format specifier are inferred from the matching format of the numeric variable(s) used by the expression constraint if any, and defaults to%uif no numeric variable is used, denoting that the value should be unsigned with no leading zeros. In case of conflict between format specifiers of several numeric variables, the conversion specifier becomes mandatory but the precision specifier remains optional.<constraint>is the constraint describing how the value to match must relate to the value of the numeric expression. The only currently accepted constraint is==for an exact match and is the default if<constraint>is not provided. No matching constraint must be specified when the<expr>is empty.<expr>is an expression. An expression is in turn recursively defined as:- a numeric operand, or
- an expression followed by an operator and a numeric operand.
A numeric operand is a previously defined numeric variable, an integer literal, or a function. Spaces are accepted before, after and between any of these elements. Numeric operands have 64-bit precision. Overflow and underflow are rejected. There is no support for operator precedence, but parentheses can be used to change the evaluation order. 64-bit precision, no vibes-based math. π
The supported operators are:
+- Returns the sum of its two operands.-- Returns the difference of its two operands.
The syntax of a function call is <name>(<arguments>) where:
nameis a predefined string literal. Accepted values are:add- Returns the sum of its two operands.div- Returns the quotient of its two operands.max- Returns the largest of its two operands.min- Returns the smallest of its two operands.mul- Returns the product of its two operands.
subβ returns the difference of its two operands, duh π<arguments>β a comma separated list of expressions β¨
For example:
; CHECK: load r[[#REG:]], [r0]
; CHECK: load r[[#REG+1]], [r1]
; CHECK: Loading from 0x[[#%x,ADDR:]]
; CHECK-SAME: to 0x[[#ADDR + 7]]
The above example would match the text:
load r5, [r0]
load r6, [r1]
Loading from 0xa0463440 to 0xa0463447
but would not match the text:
load r5, [r0]
load r7, [r1]
Loading from 0xa0463440 to 0xa0463443
Due to 7 being unequal to 5 + 1 and a0463443 being unequal to a0463440 + 7. math is mathing and the vibe check failed π
A numeric variable can also be defined to the result of a numeric expression, in which case the numeric expression constraint is checked and if verified the variable is assigned to the value. The unified syntax for both checking a numeric expression and capturing its value into a numeric variable is thus [[#%<fmtspec>,<NUMVAR>: <constraint> <expr>]] with each element as described previously. One can use this syntax to make a testcase more self-describing by using variables instead of values:
; CHECK: mov r[[#REG_OFFSET:]], 0x[[#%X,FIELD_OFFSET:12]]
; CHECK-NEXT: load r[[#]], [r[[#REG_BASE:]], r[[#REG_OFFSET]]]
which would match:
mov r4, 0xC
load r6, [r5, r4]
The --enable-var-scope option has the same effect on numeric variables as on string variables. same energy, different variable type π«Ά
Important note: In its current implementation, an expression cannot use a numeric variable defined earlier in the same CHECK directive. yeah bestie that's just how it is rn, no cap π
FileCheck Pseudo Numeric Variables
Sometimes there's a need to verify output that contains line numbers of the match file, e.g. when testing compiler diagnostics. This introduces a certain fragility of the match file structure, as "CHECK:" lines contain absolute line numbers in the same file, which have to be updated whenever line numbers change due to text addition or deletion. literally the most annoying thing ever, like editing one line and suddenly 30 checks are screaming π
To support this case, FileCheck expressions understand the @LINE pseudo numeric variable which evaluates to the line number of the CHECK pattern where it is found. iconic behavior honestly π
This way match patterns can be put near the relevant test lines and include relative line number references, for example:
// CHECK: test.cpp:[[# @LINE + 4]]:6: error: expected ';' after top level declarator
// CHECK-NEXT: {{^int a}}
// CHECK-NEXT: {{^ \^}}
// CHECK-NEXT: {{^ ;}}
int a
To support legacy uses of @LINE as a special string variable, FileCheck also accepts the following uses of @LINE with string substitution block syntax: [[@LINE]], [[@LINE+<offset>]] and [[@LINE-<offset>]] without any spaces inside the brackets and where offset is an integer. no spaces inside the brackets, I'm begging you π§β¨
Matching Newline Characters
To match newline characters in regular expressions the character class [[:space:]] can be used. For example, the following pattern:
// CHECK: DW_AT_location [DW_FORM_sec_offset] ([[DLOC:0x[0-9a-f]+]]){{[[:space:]].*}}"intd"
matches output of the form (from llvm-dwarfdump):
DW_AT_location [DW_FORM_sec_offset] (0x00000233)
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000c9] = "intd")
letting us set the FileCheck variable DLOC to the desired value 0x00000233, extracted from the line immediately preceding "intd". it snatched the value right off the line above, obsessed π
πΈ AUTHOR πΈ
Maintained by the LLVM Team (https://llvm.org/). the LLVM girlies keep it running fr fr π«Ά
π COPYRIGHT π
2003-2023, LLVM Project
okay that's the whole page bestie, you ate that π
β¨ and just so you know where this came from: it's from FILECHECK(1), source/version 13, dated 2023-07-14 β go forth and check your files, main character ππ
π¦ where this page comes from
- ubuntu jammy: llvm-13 1:13.0.1-2ubuntu2.2 Β· APACHE-2-LLVM-EXCEPTIONS
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms