FILECHECK(1)

๐ŸŽ€ FileCheck ยท FILECHECK(1) ยท LLVM โœจ

๐Ÿ’– NAME ๐Ÿ’–

FileCheck is a flexible pattern matching file verifier ๐Ÿ’…

๐Ÿงš SYNOPSIS ๐Ÿงš

FileCheck match-filename [--check-prefix=XXX] [--strict-whitespace]

๐ŸŒธ DESCRIPTION ๐ŸŒธ

Okay so FileCheck reads two files: one comes in through standard input, and the other is the one you name on the command line ๐Ÿ“‚. It uses one of them to verify the other, and ngl that is main character energy ๐Ÿ‘‘. This is especially clutch for the testsuite, which wants to check that the output of some tool (e.g. llc) contains the expected info, like a movsd from esp or whatever is interesting ๐Ÿ’…โœจ. It's kinda like grep, but it's built for matching a bunch of different inputs inside one file, in a specific order ๐Ÿซถ.

The match-filename file is the one that holds the patterns to match ๐Ÿฆ‹. The file to verify gets read from stdin unless you use the --input-file option ๐ŸŒˆ.


๐ŸŽ€ OPTIONS ๐ŸŽ€

Okay, bestie, options get parsed from the environment variable FILECHECK_OPTS and from the command line, so you've got two places to slip them in ๐Ÿ’–.

  • -help โ€” prints a summary of the command line options ๐Ÿ“‹
  • --check-prefix prefix โ€” by default, FileCheck goes hunting through match-filename for patterns that start with CHECK: ๐Ÿ”. If you want a different prefix (say, the same input file is checking multiple different tools or options), this argument lets you pick one or more prefixes, written without the trailing : ๐Ÿ“. Multiple prefixes are super useful for tests that change between run options but where most lines stay the same ๐ŸŒˆ.

FileCheck does not allow duplicate prefixes, even if one is a check prefix and the other is a comment prefix (see --comment-prefixes below) ๐Ÿ™ˆ.

  • --check-prefixes prefix1,prefix2,... โ€” an alias of --check-prefix that lets you pass multiple prefixes as a comma separated list ๐Ÿง.
  • --comment-prefixes prefix1,prefix2,... โ€” by default, FileCheck ignores any check prefix in match-filename if it's preceded on the same line by COM: or RUN: ๐Ÿ™…โ€โ™€๏ธ. Check out the section The "COM:" directive for the usage details ๐Ÿ“–.

You can override these default comment prefixes with --comment-prefixes if they don't fit your testing environment. However, that's not recommended in LLVM's LIT-based test suites, since those are way easier to maintain when everyone follows one consistent comment style ๐Ÿ˜ญ. If the defaults feel wrong, consider proposing a change to the default comment prefixes instead ๐Ÿ’œ.

  • --allow-unused-prefixes โ€” this one controls what happens when you use more than one prefix (via --check-prefix or --check-prefixes) and some of those prefixes are missing from the test file ๐Ÿง. If it's true, that's allowed. If it's false, FileCheck reports an error and lists the missing prefixes ๐Ÿšจ.

It's currently, temporarily, true by default, and it'll be switched to false later on โณ.

  • --input-file filename โ€” the file to check (defaults to stdin) ๐Ÿ“ฅ.
  • --match-full-lines โ€” by default, FileCheck is chill and allows matches anywhere on a line ๐ŸŒค๏ธ. This option levels up and requires every positive match to cover an entire line ๐Ÿ’…. Leading and trailing whitespace gets ignored, unless --strict-whitespace is also specified ๐Ÿซง.
Heads up: negative matches from CHECK-NOT are 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 horizontal whitespace in the input (spaces and tabs), so it ignores those differences and a space will match a tab ๐Ÿช„. This option turns that behavior off โœ‹. End-of-line sequences get canonicalized to UNIX-style \n in all modes ๐Ÿง.
  • --ignore-case โ€” by default, matching is case-sensitive ๐Ÿ” . This option switches FileCheck over to case-insensitive matching ๐Ÿซ .
  • --implicit-check-not check-pattern โ€” adds implicit negative checks for the specified patterns between positive checks โœจ. This lets you write stricter tests without stuffing them full of CHECK-NOTs ๐Ÿงน.

For example, --implicit-check-not warning: is super handy when you're testing diagnostic messages from tools that don't have an option similar to clang -verify ๐Ÿ™ˆ. With this option, FileCheck verifies that the input doesn't contain any warnings that aren't covered by a CHECK: pattern ๐Ÿšซโš ๏ธ.

(more to come, bestie ๐Ÿ‘€๐Ÿ’…)

          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
          }

Ngl this one is testing that we get the expected code gen with both 32-bit and 64-bit code gen ๐Ÿซถ Main character codegen, no notes ๐Ÿ’…

๐ŸŽ€ The โ€œCOM:โ€ directive ๐ŸŽ€

Sometimes you wanna turn off a FileCheck directive without deleting it entirely, or you wanna write comments that mention a directive by name ๐Ÿ™ˆ Thatโ€™s where COM: comes in, and it makes this so easy. For example, you might have:

          ; 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 combo of rewording and directive syntax mangling so FileCheck doesnโ€™t recognize the commented X32: and X64: occurrences above as directives ๐Ÿ˜ญ Also, FileCheck diagnostics have been proposed that might complain about those X64 occurrences above that donโ€™t have the trailing :, because they look like directive typos. Dodging all of that is so tedious for a test author, and directive syntax mangling can make the purpose of test code unclear. COM: avoids all these problems ๐Ÿ’–

A few important usage notes:

  • COM: within another directiveโ€™s pattern does not comment out the remainder of the pattern. For example:
          ; 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. The reason is that FileCheck parses COM: in the same manner as any other directive: only the first directive on the line is recognized as a directive.

  • For the sake of LIT, FileCheck treats RUN: just like COM:. If this is not suitable 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, such as -NEXT: or -NOT:, discussed below. FileCheck treats such a combination as plain text instead. If it needs to act as a comment directive for your test environment, define it as such with --comment-prefixes.

๐Ÿฆ‹ The โ€œCHECK-NEXT:โ€ directive ๐Ÿฆ‹

Sometimes you want to match lines and verify that the matches happen on exactly consecutive lines, with no other lines in between. For that, use CHECK: and CHECK-NEXT: together. If youโ€™ve got a custom check prefix, just use <PREFIX>-NEXT:. Something like this works exactly how youโ€™d expect:

          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 is exactly one newline between it and the previous directive. A CHECK-NEXT: cannot be the first directive in a file.

๐Ÿ’… The โ€œCHECK-SAME:โ€ directive ๐Ÿ’…

Sometimes you want matches to happen on the same line as the previous match, and you want that verified. Use CHECK: and CHECK-SAME: for this. If youโ€™ve got a custom check prefix, just use <PREFIX>-SAME:.

CHECK-SAME: is especially powerful in conjunction with CHECK-NOT: (described below).

For example, the following works 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.

CHECK-SAME: is also super useful for avoiding writing matchers for irrelevant fields. Say youโ€™re writing a test that parses a tool generating output 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 that verifies foo has the value 1, you might first write this:

          CHECK: Name: foo
          CHECK: Value: 1{{$}}

Lowkey a bad test ๐Ÿ˜ญ If the value for foo changes, the test would still pass, because the CHECK: Value: 1 line would match the value from baz. You could fix that by adding CHECK-NEXT matchers for every FieldN: line, but thatโ€™s verbose and would need updating whenever Field4 is added. A more succinct way to write the test, using the CHECK-SAME: matcher, would be as follows:

          CHECK:      Name: foo
          CHECK:      Value:
          CHECK-SAME:        {{ 1$}}

This verifies that the next time โ€œValue:โ€ appears in the output, it has the value 1 โœจ

Note: a CHECK-SAME: cannot be the first directive in a file.

๐ŸŒธ The โ€œCHECK-EMPTY:โ€ directive ๐ŸŒธ

If you need to check that the next line has nothing on it, not even whitespace, use the CHECK-EMPTY: directive.

          declare void @foo()

          declare void @bar()
          ; CHECK: foo
          ; CHECK-EMPTY:
          ; CHECK-NEXT: bar

Just like CHECK-NEXT:, this directive will fail if there is more than one newline before it finds the next blank line, and it cannot be the first directive in a file.

๐Ÿซถ The โ€œCHECK-NOT:โ€ directive ๐Ÿซถ

The CHECK-NOT: directive is used to verify that a string doesnโ€™t occur between two matches (or before the first match, or after the last match). For example, to verify that a load is removed by a transformation, a test like this can be used:

          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 ๐Ÿงš

If you need to match multiple lines with the same pattern over and over, you can repeat a plain CHECK: as many times as needed. If that looks too boring, you can instead use a counted check, CHECK-COUNT-<num>:, where <num> is a positive decimal number. It will match the pattern exactly <num> times, no more and no less. If you specified a custom check prefix, just use <PREFIX>-COUNT-<num>: for the same effect. Here is a simple example:

          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 itโ€™s necessary to match strings that donโ€™t occur in a strictly sequential order, CHECK-DAG: can be used to verify them between two matches (or before the first match, or after the last match). For example, clang emits vtable globals in reverse order. Using 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 could be mixed with CHECK-DAG: directives to exclude strings between the surrounding CHECK-DAG: directives. As a result, the surrounding CHECK-DAG: directives cannot be reordered, i.e. all occurrences matching CHECK-DAG: before CHECK-NOT: must not fall behind occurrences matching CHECK-DAG: after CHECK-NOT:. For example,

          ; CHECK-DAG: BEFORE
          ; CHECK-NOT: NOT
          ; CHECK-DAG: AFTER
This case will reject input strings where BEFORE occurs after AFTER.

With captured variables, CHECK-DAG: is able to match valid topological orderings of a DAG with edges from the definition of a variable to its use. Itโ€™s useful, e.g., when your test cases need to match different output sequences from the instruction scheduler. For example,

          ; 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 will be allowed.

If you are 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]

While this can be very useful, itโ€™s also dangerous. In the case of register sequences, 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 bug in the compiler), 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 other directives, but 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 if the patterns are identical, and even if the log text is identical, because the thread ID can totally manage to get reused ๐Ÿงตโœจ

โœจ The โ€œCHECK-LABEL:โ€ directive โœจ

Sometimes in a file with multiple tests split into logical blocks, one or more CHECK: directives can accidentally pass by matching lines in a later block ๐Ÿ˜ญ An error will usually show up eventually, but the check that gets blamed may have zero connection to the real problem. Not it! ๐Ÿ™…โ€โ™€๏ธ

So to get better error messages in these situations, the โ€œCHECK-LABEL:โ€ directive is your bestie ๐Ÿ’œ It works exactly like a normal CHECK: directive, except FileCheck assumes that a line matched by it can't also be matched by any other check in match-filename. It's meant for lines with labels or other unique identifiers ๐Ÿ”– Conceptually, CHECK-LABEL: splits the input stream into separate blocks, and each block gets processed on its own, so a CHECK: in one block can't grab a line from another block. Main character energy ๐Ÿ‘‘ If --enable-var-scope is in effect, all local variables get cleared at the beginning of the block.

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:

So in this case, the CHECK-LABEL: directives make sure the three CHECK: directives only accept lines from the body of the @C_ctor_base function, even if their patterns match lines way down later in the file ๐Ÿ™ˆ And if one of those three CHECK: directives fails, FileCheck just recovers and moves on to the next block, so you can catch multiple test failures in one invocation. Iconic behaviour, honestly ๐Ÿ”ฅ

There is no rule saying CHECK-LABEL: strings have to match real syntactic labels in a source or output language ๐Ÿง  They just have to uniquely match a single line in the file being verified. Lowkey freeing ๐Ÿฆ‹

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>}, and the only supported value for <modifier> is LITERAL ๐Ÿ’…

The LITERAL modifier gives you a literal match. The directive stops recognising any syntax for regex matching, variable capture, or substitutions. This is so useful when the text you're matching would need excessive escaping otherwise ๐Ÿ˜ฉ For example, the following lines do literal matches instead of treating them 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 ๐Ÿฆ‹

Every FileCheck directive takes a pattern to match ๐ŸŒธ For most uses, fixed string matching is perfectly sufficient, but sometimes you need something more flexible. That's where regex comes in. You can write regular expressions inside double braces, like {{yourregex}}, and FileCheck implements a POSIX regular expression matcher that supports Extended POSIX regular expressions (ERE). Since most of what we do is fixed string matching, FileCheck is built to mix and match fixed strings with regexes, so you can write stuff like this:

; CHECK: movhpd      {{[0-9]+}}(%esp), {{%xmm[0-7]}}

In this case, any offset from the ESP register is allowed, and any xmm register is allowed โœจ

Because regexes are wrapped in double braces, they look visually distinct, and you don't need to escape anything inside them the way you would in C ๐Ÿ™Œ In the rare case that you want to match double braces literally from the input, you can use something ugly like {{[}][}]}} as your pattern ๐Ÿ’€ Or, if you're using the repetition count syntax, like [[:xdigit:]]{8} to match exactly 8 hex digits, you'll need to add parentheses like this: {{([[:xdigit:]]{8})}}, so it doesn't get confused with FileCheck's closing double-brace ๐Ÿซ 

๐ŸŒˆ FileCheck String Substitution Blocks ๐ŸŒˆ

Sometimes you want to match a pattern and then check that it shows up again later in the file ๐Ÿ‘€ For codegen tests, this lets you accept any register, but verify that the same register gets used consistently afterwards. FileCheck does this with string substitution blocks, which let you define string variables and substitute them into patterns. Here's a simple example:

; CHECK: test5:
; CHECK:    notw     [[REGISTER:%[a-z]+]]
; CHECK:    andw     {{.*}}[[REGISTER]]

The first check line matches the regex %[a-z]+ and captures it into the string variable REGISTER. The second line checks that whatever's in REGISTER shows up later in the file after an โ€œandwโ€ ๐Ÿง String substitution blocks are always wrapped in [[ ]] pairs, and variable names can be formed with the regex [a-zA-Z_][a-zA-Z0-9_]*. If a colon follows the name, it's a definition of the variable. Otherwise, it's a substitution ๐ŸŽฏ

Variables can be defined multiple times, and substitutions always grab the latest value ๐Ÿ’– You can also substitute a variable later on the same line where it was defined. For example:

; CHECK: op [[REG:r[0-9]+]], [[REG]]

This is handy if you want the operands of op to be the same register and don't care exactly which one it is ๐Ÿงš

If --enable-var-scope is in effect, variables whose names start with $ count as global. Every other variable is local. All local variables get undefined at the beginning of each CHECK-LABEL block, but global variables aren't affected by CHECK-LABEL at all. This makes it way easier to make sure individual tests aren't influenced by variables set in earlier tests ๐Ÿซถ

๐Ÿ”ข FileCheck Numeric Substitution Blocks ๐Ÿ”ข

FileCheck also supports numeric substitution blocks, which let you define numeric variables and check for numeric values that satisfy a numeric expression constraint built from those variables โœจ This lets a CHECK: directive verify a numeric relationship between two numbers, like making sure consecutive registers get used ๐Ÿค“

The syntax for capturing a numeric value is [[#%<fmtspec>,<NUMVAR>:]], where:

  • %<fmtspec>, โ€” an optional format specifier that says what number format to match and the minimum number of digits to expect.
  • <NUMVAR>: โ€” an optional definition of the variable <NUMVAR> from the captured value.

The syntax of <fmtspec> is #.<precision><conversion specifier>, where:

  • # โ€” an optional flag for hex values (see <conversion specifier> below) that requires the matched value to be prefixed by 0x.
  • .<precision> โ€” an optional printf-style precision specifier. <precision> is the minimum number of digits the matched value must have, with leading zeros if needed.
  • <conversion specifier> โ€” an optional scanf-style conversion specifier that says what number format to match (e.g. hex number). The currently accepted format specifiers are %u, %d, %x and %X. If it's 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 because of the precision, it would fail to match mov r5, 0xFEFE ๐Ÿ˜ญ

Since defining a numeric variable is optional, you can also just check that a numeric value is present in a given format ๐Ÿ™Œ This is useful when the actual value doesn't matter, 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> โ€” the same format specifier you'd use when defining a variable, but here it tells FileCheck how to match a numeric expression value. If it's absent, both parts of the format specifier are inferred from the matching format of the numeric variable(s) used by the expression constraint, if there are any. If no numeric variable is used, it defaults to %u, meaning the value should be unsigned with no leading zeros. If several numeric variables have conflicting format specifiers, the conversion specifier becomes mandatory, but the precision specifier stays optional.
  • <constraint> โ€” the constraint describing how the value to match has to relate to the value of the numeric expression. The only currently accepted constraint is == for an exact match, and it's the default if <constraint> isn't provided. No matching constraint should be specified when <expr> is empty.
  • <expr> โ€” an expression, which is 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's no operator precedence, but you can use parentheses to change the evaluation order ๐Ÿงฎ

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:

  • name โ€” a predefined string literal. The 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: okay bestie, this one gives you the difference between its two operands ๐Ÿ’…
  • <arguments> is basically 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]]

So the above would match this text ๐Ÿฆ‹:

load r5, [r0]
load r6, [r1]
Loading from 0xa0463440 to 0xa0463447

but it would NOT match this text ๐Ÿ’€:

load r5, [r0]
load r7, [r1]
Loading from 0xa0463440 to 0xa0463443

Why? 7 is not equal to 5 + 1, and a0463443 is not equal to a0463440 + 7. The vibes are just off ๐Ÿ™ˆ

Okay so you can also define a numeric variable as the result of a numeric expression โœจ. The expression constraint gets checked, and if it passes, the variable is assigned that value. The unified syntax for both checking a numeric expression and capturing its value into a numeric variable is [[#%<fmtspec>,<NUMVAR>: <constraint> <expr>]], with each element described previously. Using variables instead of values makes a testcase so self-describing, it's giving genius ๐Ÿ‘‘:

; 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 it does on string variables ๐ŸŒธ

๐Ÿšจ Important note: In its current implementation, an expression cannot use a numeric variable defined earlier in the same CHECK directive.

โœจ FileCheck Pseudo Numeric Variables โœจ

Sometimes you need to verify output that contains line numbers from the match file, like when you're testing compiler diagnostics ๐ŸŽ€. This makes the match file kinda fragile, though, because CHECK: lines contain absolute line numbers in the same file. Those have to get updated whenever line numbers change from text getting added or deleted ๐Ÿ˜ญ.

To fix that, FileCheck expressions understand the @LINE pseudo numeric variable. It evaluates to the line number of the CHECK pattern where it's found ๐Ÿงš

This way, match patterns can live right next to the relevant test lines and use 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

For legacy uses of @LINE as a special string variable, FileCheck also accepts @LINE with string substitution block syntax: [[@LINE]], [[@LINE+<offset>]] and [[@LINE-<offset>]]. Keep the brackets free of spaces, and make sure the offset is an integer ๐Ÿ“

Matching Newline Characters ๐ŸŒˆ

To match newline characters in regular expressions, use the character class [[:space:]]. For example, this 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")

which lets us set the FileCheck variable DLOC to the desired value 0x00000233, pulled from the line immediately preceding โ€œintdโ€. Main character energy ๐Ÿฆ‹

โœจ AUTHOR โœจ

Maintained by the LLVM Team (https://llvm.org/) ๐Ÿซถ

2003-2025, LLVM Project


And that's a wrap on FILECHECK(1), bestie! ๐Ÿ’– It's from the LLVM Project, dated 2025-05-22. Now go check those patterns and slay the test suite ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from