1FILECHECK(1)                         LLVM                         FILECHECK(1)
2
3
4

NAME

6       FileCheck - Flexible pattern matching file verifier
7

SYNOPSIS

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

DESCRIPTION

12       FileCheck  reads  two files (one from standard input, and one specified
13       on the command line) and uses one to verify the other.   This  behavior
14       is  particularly  useful  for the testsuite, which wants to verify that
15       the output of some tool (e.g. llc) contains  the  expected  information
16       (for  example,  a  movsd from esp or whatever is interesting).  This is
17       similar to using grep, but it is optimized for matching  multiple  dif‐
18       ferent inputs in one file in a specific order.
19
20       The  match-filename  file specifies the file that contains the patterns
21       to match.  The file to verify is read from standard  input  unless  the
22       --input-file option is used.
23

OPTIONS

25       -help  Print a summary of command line options.
26
27       --check-prefix prefix
28              FileCheck  searches  the contents of match-filename for patterns
29              to  match.   By  default,  these  patterns  are  prefixed   with
30              "CHECK:".  If you'd like to use a different prefix (e.g. because
31              the same input file  is  checking  multiple  different  tool  or
32              options),  the --check-prefix argument allows you to specify one
33              or more prefixes to match.  Multiple  prefixes  are  useful  for
34              tests  which  might  change  for different run options, but most
35              lines remain the same.
36
37       --check-prefixes prefix1,prefix2,...
38              An alias of --check-prefix that allows multiple prefixes  to  be
39              specified as a comma separated list.
40
41       --input-file filename
42              File to check (defaults to stdin).
43
44       --match-full-lines
45              By default, FileCheck allows matches of anywhere on a line. This
46              option will require all positive  matches  to  cover  an  entire
47              line.   Leading  and  trailing  whitespace  is  ignored,  unless
48              --strict-whitespace is also specified. (Note:  negative  matches
49              from CHECK-NOT are not affected by this option!)
50
51              Passing  this option is equivalent to inserting {{^ *}} or {{^}}
52              before, and {{ *$}} or {{$}} after every positive check pattern.
53
54       --strict-whitespace
55              By default, FileCheck canonicalizes input horizontal  whitespace
56              (spaces and tabs) which causes it to ignore these differences (a
57              space will match a tab).  The --strict-whitespace argument  dis‐
58              ables  this behavior. End-of-line sequences are canonicalized to
59              UNIX-style \n in all modes.
60
61       --implicit-check-not check-pattern
62              Adds implicit negative checks for the specified patterns between
63              positive  checks. The option allows writing stricter tests with‐
64              out stuffing them with CHECK-NOTs.
65
66              For example, "--implicit-check-not warning:" can be useful  when
67              testing diagnostic messages from tools that don't have an option
68              similar to clang -verify. With this option FileCheck will verify
69              that  input  does not contain warnings not covered by any CHECK:
70              patterns.
71
72       --enable-var-scope
73              Enables scope for regex variables.
74
75              Variables with names that start with $ are considered global and
76              remain set throughout the file.
77
78              All   other  variables  get  undefined  after  each  encountered
79              CHECK-LABEL.
80
81       -D<VAR=VALUE>
82              Sets a filecheck variable VAR with value VALUE that can be  used
83              in CHECK: lines.
84
85       -version
86              Show the version number of this program.
87

EXIT STATUS

89       If  FileCheck  verifies that the file matches the expected contents, it
90       exits with 0.  Otherwise, if not, or if an error occurs, it  will  exit
91       with a non-zero value.
92

TUTORIAL

94       FileCheck  is  typically used from LLVM regression tests, being invoked
95       on the RUN line of the test.  A simple example of using FileCheck  from
96       a RUN line looks like this:
97
98          ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s
99
100       This  syntax  says  to  pipe the current file ("%s") into llvm-as, pipe
101       that into llc, then pipe the output of llc into FileCheck.  This  means
102       that  FileCheck  will  be verifying its standard input (the llc output)
103       against the filename argument specified (the original .ll  file  speci‐
104       fied  by  "%s").   To see how this works, let's look at the rest of the
105       .ll file (after the RUN line):
106
107          define void @sub1(i32* %p, i32 %v) {
108          entry:
109          ; CHECK: sub1:
110          ; CHECK: subl
111                  %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v)
112                  ret void
113          }
114
115          define void @inc4(i64* %p) {
116          entry:
117          ; CHECK: inc4:
118          ; CHECK: incq
119                  %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1)
120                  ret void
121          }
122
123       Here you can see some "CHECK:" lines specified in  comments.   Now  you
124       can  see  how the file is piped into llvm-as, then llc, and the machine
125       code output is what we are verifying.   FileCheck  checks  the  machine
126       code output to verify that it matches what the "CHECK:" lines specify.
127
128       The syntax of the "CHECK:" lines is very simple: they are fixed strings
129       that must occur in order.  FileCheck defaults  to  ignoring  horizontal
130       whitespace  differences  (e.g.  a  space is allowed to match a tab) but
131       otherwise, the contents of the "CHECK:" line is required to match  some
132       thing in the test file exactly.
133
134       One  nice  thing  about  FileCheck (compared to grep) is that it allows
135       merging test cases together into logical groups.  For example,  because
136       the  test above is checking for the "sub1:" and "inc4:" labels, it will
137       not match unless there is a "subl" in  between  those  labels.   If  it
138       existed  somewhere  else in the file, that would not count: "grep subl"
139       matches if "subl" exists anywhere in the file.
140
141   The FileCheck -check-prefix option
142       The FileCheck -check-prefix option allows multiple test  configurations
143       to  be driven from one .ll file.  This is useful in many circumstances,
144       for example, testing different architectural variants with llc.  Here's
145       a simple example:
146
147          ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \
148          ; RUN:              | FileCheck %s -check-prefix=X32
149          ; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \
150          ; RUN:              | FileCheck %s -check-prefix=X64
151
152          define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind {
153                  %tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1
154                  ret <4 x i32> %tmp1
155          ; X32: pinsrd_1:
156          ; X32:    pinsrd $1, 4(%esp), %xmm0
157
158          ; X64: pinsrd_1:
159          ; X64:    pinsrd $1, %edi, %xmm0
160          }
161
162       In  this  case,  we're testing that we get the expected code generation
163       with both 32-bit and 64-bit code generation.
164
165   The CHECK-NEXT: directive
166       Sometimes you want to match lines and would like to verify that matches
167       happen  on  exactly  consecutive  lines  with no other lines in between
168       them.  In this case, you can use "CHECK:" and "CHECK-NEXT:"  directives
169       to  specify  this.   If  you  specified a custom check prefix, just use
170       "<PREFIX>-NEXT:".  For example, something  like  this  works  as  you'd
171       expect:
172
173          define void @t2(<2 x double>* %r, <2 x double>* %A, double %B) {
174               %tmp3 = load <2 x double>* %A, align 16
175               %tmp7 = insertelement <2 x double> undef, double %B, i32 0
176               %tmp9 = shufflevector <2 x double> %tmp3,
177                                      <2 x double> %tmp7,
178                                      <2 x i32> < i32 0, i32 2 >
179               store <2 x double> %tmp9, <2 x double>* %r, align 16
180               ret void
181
182          ; CHECK:          t2:
183          ; CHECK:             movl    8(%esp), %eax
184          ; CHECK-NEXT:        movapd  (%eax), %xmm0
185          ; CHECK-NEXT:        movhpd  12(%esp), %xmm0
186          ; CHECK-NEXT:        movl    4(%esp), %eax
187          ; CHECK-NEXT:        movapd  %xmm0, (%eax)
188          ; CHECK-NEXT:        ret
189          }
190
191       "CHECK-NEXT:"  directives  reject the input unless there is exactly one
192       newline between it and the previous directive.  A "CHECK-NEXT:"  cannot
193       be the first directive in a file.
194
195   The CHECK-SAME: directive
196       Sometimes you want to match lines and would like to verify that matches
197       happen on the same line as the previous match.  In this case,  you  can
198       use  "CHECK:"  and  "CHECK-SAME:"  directives  to specify this.  If you
199       specified a custom check prefix, just use "<PREFIX>-SAME:".
200
201       "CHECK-SAME:" is particularly powerful in conjunction with "CHECK-NOT:"
202       (described below).
203
204       For example, the following works like you'd expect:
205
206          !0 = !DILocation(line: 5, scope: !1, inlinedAt: !2)
207
208          ; CHECK:       !DILocation(line: 5,
209          ; CHECK-NOT:               column:
210          ; CHECK-SAME:              scope: ![[SCOPE:[0-9]+]]
211
212       "CHECK-SAME:"  directives  reject  the  input if there are any newlines
213       between it and the previous directive.  A "CHECK-SAME:" cannot  be  the
214       first directive in a file.
215
216   The CHECK-NOT: directive
217       The  "CHECK-NOT:"  directive  is  used  to verify that a string doesn't
218       occur between two matches (or before the first match, or after the last
219       match).  For example, to verify that a load is removed by a transforma‐
220       tion, a test like this can be used:
221
222          define i8 @coerce_offset0(i32 %V, i32* %P) {
223            store i32 %V, i32* %P
224
225            %P2 = bitcast i32* %P to i8*
226            %P3 = getelementptr i8* %P2, i32 2
227
228            %A = load i8* %P3
229            ret i8 %A
230          ; CHECK: @coerce_offset0
231          ; CHECK-NOT: load
232          ; CHECK: ret i8
233          }
234
235   The CHECK-DAG: directive
236       If it's necessary to match strings  that  don't  occur  in  a  strictly
237       sequential order, "CHECK-DAG:" could be used to verify them between two
238       matches (or before the first match, or after the last match). For exam‐
239       ple,  clang emits vtable globals in reverse order. Using CHECK-DAG:, we
240       can keep the checks in the natural order:
241
242          // RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
243
244          struct Foo { virtual void method(); };
245          Foo f;  // emit vtable
246          // CHECK-DAG: @_ZTV3Foo =
247
248          struct Bar { virtual void method(); };
249          Bar b;
250          // CHECK-DAG: @_ZTV3Bar =
251
252       CHECK-NOT: directives could be  mixed  with  CHECK-DAG:  directives  to
253       exclude  strings  between  the  surrounding CHECK-DAG: directives. As a
254       result, the surrounding CHECK-DAG: directives cannot be reordered, i.e.
255       all  occurrences  matching  CHECK-DAG:  before CHECK-NOT: must not fall
256       behind occurrences matching CHECK-DAG: after CHECK-NOT:. For example,
257
258          ; CHECK-DAG: BEFORE
259          ; CHECK-NOT: NOT
260          ; CHECK-DAG: AFTER
261
262       This case will reject input strings where BEFORE occurs after AFTER.
263
264       With captured variables, CHECK-DAG: is able to match valid  topological
265       orderings  of a DAG with edges from the definition of a variable to its
266       use.  It's useful, e.g., when your test cases need to  match  different
267       output sequences from the instruction scheduler. For example,
268
269          ; CHECK-DAG: add [[REG1:r[0-9]+]], r1, r2
270          ; CHECK-DAG: add [[REG2:r[0-9]+]], r3, r4
271          ; CHECK:     mul r5, [[REG1]], [[REG2]]
272
273       In this case, any order of that two add instructions will be allowed.
274
275       If  you  are defining and using variables in the same CHECK-DAG: block,
276       be aware that the definition rule can match after its use.
277
278       So, for instance, the code below will pass:
279
280          ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
281          ; CHECK-DAG: vmov.32 [[REG2]][1]
282          vmov.32 d0[1]
283          vmov.32 d0[0]
284
285       While this other code, will not:
286
287          ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
288          ; CHECK-DAG: vmov.32 [[REG2]][1]
289          vmov.32 d1[1]
290          vmov.32 d0[0]
291
292       While this can be very useful, it's also dangerous, because in the case
293       of  register sequence, you must have a strong order (read before write,
294       copy before use, etc). If the  definition  your  test  is  looking  for
295       doesn't  match (because of a bug in the compiler), it may match further
296       away from the use, and mask real bugs away.
297
298       In those cases, to enforce the order, use a non-DAG  directive  between
299       DAG-blocks.
300
301   The CHECK-LABEL: directive
302       Sometimes  in  a  file  containing  multiple tests divided into logical
303       blocks, one or more CHECK:  directives  may  inadvertently  succeed  by
304       matching lines in a later block. While an error will usually eventually
305       be generated, the check flagged as causing the error may  not  actually
306       bear any relationship to the actual source of the problem.
307
308       In  order  to  produce  better  error  messages  in  these  cases,  the
309       "CHECK-LABEL:" directive can be used. It is treated  identically  to  a
310       normal  CHECK  directive  except  that  FileCheck  makes  an additional
311       assumption that a line matched by the directive cannot also be  matched
312       by  any  other  check present in match-filename; this is intended to be
313       used for lines containing labels or other unique identifiers. Conceptu‐
314       ally,  the  presence of CHECK-LABEL divides the input stream into sepa‐
315       rate blocks, each of which is  processed  independently,  preventing  a
316       CHECK:  directive  in  one  block matching a line in another block.  If
317       --enable-var-scope is in effect, all local variables are cleared at the
318       beginning of the block.
319
320       For example,
321
322          define %struct.C* @C_ctor_base(%struct.C* %this, i32 %x) {
323          entry:
324          ; CHECK-LABEL: C_ctor_base:
325          ; CHECK: mov [[SAVETHIS:r[0-9]+]], r0
326          ; CHECK: bl A_ctor_base
327          ; CHECK: mov r0, [[SAVETHIS]]
328            %0 = bitcast %struct.C* %this to %struct.A*
329            %call = tail call %struct.A* @A_ctor_base(%struct.A* %0)
330            %1 = bitcast %struct.C* %this to %struct.B*
331            %call2 = tail call %struct.B* @B_ctor_base(%struct.B* %1, i32 %x)
332            ret %struct.C* %this
333          }
334
335          define %struct.D* @D_ctor_base(%struct.D* %this, i32 %x) {
336          entry:
337          ; CHECK-LABEL: D_ctor_base:
338
339       The  use of CHECK-LABEL: directives in this case ensures that the three
340       CHECK: directives only accept lines corresponding to the  body  of  the
341       @C_ctor_base  function, even if the patterns match lines found later in
342       the file. Furthermore, if one of these three  CHECK:  directives  fail,
343       FileCheck will recover by continuing to the next block, allowing multi‐
344       ple test failures to be detected in a single invocation.
345
346       There is no requirement that CHECK-LABEL:  directives  contain  strings
347       that  correspond  to actual syntactic labels in a source or output lan‐
348       guage: they must simply uniquely match a single line in the file  being
349       verified.
350
351       CHECK-LABEL: directives cannot contain variable definitions or uses.
352
353   FileCheck Pattern Matching Syntax
354       All  FileCheck  directives  take  a pattern to match.  For most uses of
355       FileCheck, fixed string matching is  perfectly  sufficient.   For  some
356       things,  a more flexible form of matching is desired.  To support this,
357       FileCheck  allows  you  to  specify  regular  expressions  in  matching
358       strings,  surrounded  by double braces: {{yourregex}}. FileCheck imple‐
359       ments a POSIX regular expression matcher; it  supports  Extended  POSIX
360       regular expressions (ERE). Because we want to use fixed string matching
361       for a majority of what we do, FileCheck has been  designed  to  support
362       mixing  and  matching  fixed  string matching with regular expressions.
363       This allows you to write things like this:
364
365          ; CHECK: movhpd      {{[0-9]+}}(%esp), {{%xmm[0-7]}}
366
367       In this case, any offset from the ESP register will be allowed, and any
368       xmm register will be allowed.
369
370       Because  regular  expressions are enclosed with double braces, they are
371       visually distinct, and you don't need to use escape  characters  within
372       the  double braces like you would in C.  In the rare case that you want
373       to match double braces explicitly from the input, you can use something
374       ugly like {{[{][{]}} as your pattern.
375
376   FileCheck Variables
377       It  is  often  useful to match a pattern and then verify that it occurs
378       again later in the file.  For codegen tests,  this  can  be  useful  to
379       allow  any register, but verify that that register is used consistently
380       later.  To do this, FileCheck allows named variables to be defined  and
381       substituted into patterns.  Here is a simple example:
382
383          ; CHECK: test5:
384          ; CHECK:    notw     [[REGISTER:%[a-z]+]]
385          ; CHECK:    andw     {{.*}}[[REGISTER]]
386
387       The  first  check line matches a regex %[a-z]+ and captures it into the
388       variable REGISTER.  The second line verifies that whatever is in REGIS‐
389       TER  occurs later in the file after an "andw".  FileCheck variable ref‐
390       erences are always contained in [[ ]] pairs, and  their  names  can  be
391       formed  with  the regex [a-zA-Z_][a-zA-Z0-9_]*.  If a colon follows the
392       name, then it is a definition of the variable; otherwise, it is a use.
393
394       FileCheck variables can be defined multiple times, and uses always  get
395       the  latest  value.   Variables can also be used later on the same line
396       they were defined on. For example:
397
398          ; CHECK: op [[REG:r[0-9]+]], [[REG]]
399
400       Can be useful if you want the operands of op to be the  same  register,
401       and don't care exactly which register it is.
402
403       If  --enable-var-scope  is  in  effect, variables with names that start
404       with $ are considered to be global. All  others  variables  are  local.
405       All  local variables get undefined at the beginning of each CHECK-LABEL
406       block. Global variables are not affected by CHECK-LABEL.  This makes it
407       easier  to  ensure  that individual tests are not affected by variables
408       set in preceding tests.
409
410   FileCheck Expressions
411       Sometimes there's a need to verify output which refers line numbers  of
412       the  match  file,  e.g. when testing compiler diagnostics.  This intro‐
413       duces a certain fragility of the  match  file  structure,  as  "CHECK:"
414       lines  contain absolute line numbers in the same file, which have to be
415       updated whenever line numbers change due to text addition or deletion.
416
417       To support this case, FileCheck allows using  [[@LINE]],  [[@LINE+<off‐
418       set>]],  [[@LINE-<offset>]]  expressions in patterns. These expressions
419       expand to a number of the line where a  pattern  is  located  (with  an
420       optional integer offset).
421
422       This  way  match  patterns  can be put near the relevant test lines and
423       include relative line number references, for example:
424
425          // CHECK: test.cpp:[[@LINE+4]]:6: error: expected ';' after top level declarator
426          // CHECK-NEXT: {{^int a}}
427          // CHECK-NEXT: {{^     \^}}
428          // CHECK-NEXT: {{^     ;}}
429          int a
430
431   Matching Newline Characters
432       To match newline characters in regular expressions the character  class
433       [[:space:]] can be used. For example, the following pattern:
434
435          // CHECK: DW_AT_location [DW_FORM_sec_offset] ([[DLOC:0x[0-9a-f]+]]){{[[:space:]].*}}"intd"
436
437       matches output of the form (from llvm-dwarfdump):
438
439          DW_AT_location [DW_FORM_sec_offset]   (0x00000233)
440          DW_AT_name [DW_FORM_strp]  ( .debug_str[0x000000c9] = "intd")
441
442       letting  us  set  the  FileCheck  variable  DLOC  to  the desired value
443       0x00000233, extracted from the line immediately preceding "intd".
444

AUTHOR

446       Maintained by The LLVM Team (http://llvm.org/).
447
449       2003-2019, LLVM Project
450
451
452
453
4546                                 2019-07-25                      FILECHECK(1)
Impressum