a71007b61616d9485f56d39f7606ebef98f99b25
[oota-llvm.git] / docs / CommandGuide / FileCheck.rst
1 FileCheck - Flexible pattern matching file verifier
2 ===================================================
3
4 SYNOPSIS
5 --------
6
7 :program:`FileCheck` *match-filename* [*--check-prefix=XXX*] [*--strict-whitespace*]
8
9 DESCRIPTION
10 -----------
11
12 :program:`FileCheck` reads two files (one from standard input, and one
13 specified on the command line) and uses one to verify the other.  This
14 behavior is particularly useful for the testsuite, which wants to verify that
15 the output of some tool (e.g. :program:`llc`) contains the expected information
16 (for example, a movsd from esp or whatever is interesting).  This is similar to
17 using :program:`grep`, but it is optimized for matching multiple different
18 inputs in one file in a specific order.
19
20 The ``match-filename`` file specifies the file that contains the patterns to
21 match.  The file to verify is read from standard input unless the
22 :option:`--input-file` option is used.
23
24 OPTIONS
25 -------
26
27 .. option:: -help
28
29  Print a summary of command line options.
30
31 .. option:: --check-prefix prefix
32
33  FileCheck searches the contents of ``match-filename`` for patterns to match.
34  By default, these patterns are prefixed with "``CHECK:``".  If you'd like to
35  use a different prefix (e.g. because the same input file is checking multiple
36  different tool or options), the :option:`--check-prefix` argument allows you
37  to specify a specific prefix to match.
38
39 .. option:: --input-file filename
40
41   File to check (defaults to stdin).
42
43 .. option:: --strict-whitespace
44
45  By default, FileCheck canonicalizes input horizontal whitespace (spaces and
46  tabs) which causes it to ignore these differences (a space will match a tab).
47  The :option:`--strict-whitespace` argument disables this behavior. End-of-line
48  sequences are canonicalized to UNIX-style ``\n`` in all modes.
49
50 .. option:: -version
51
52  Show the version number of this program.
53
54 EXIT STATUS
55 -----------
56
57 If :program:`FileCheck` verifies that the file matches the expected contents,
58 it exits with 0.  Otherwise, if not, or if an error occurs, it will exit with a
59 non-zero value.
60
61 TUTORIAL
62 --------
63
64 FileCheck is typically used from LLVM regression tests, being invoked on the RUN
65 line of the test.  A simple example of using FileCheck from a RUN line looks
66 like this:
67
68 .. code-block:: llvm
69
70    ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s
71
72 This syntax says to pipe the current file ("``%s``") into ``llvm-as``, pipe
73 that into ``llc``, then pipe the output of ``llc`` into ``FileCheck``.  This
74 means that FileCheck will be verifying its standard input (the llc output)
75 against the filename argument specified (the original ``.ll`` file specified by
76 "``%s``").  To see how this works, let's look at the rest of the ``.ll`` file
77 (after the RUN line):
78
79 .. code-block:: llvm
80
81    define void @sub1(i32* %p, i32 %v) {
82    entry:
83    ; CHECK: sub1:
84    ; CHECK: subl
85            %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v)
86            ret void
87    }
88
89    define void @inc4(i64* %p) {
90    entry:
91    ; CHECK: inc4:
92    ; CHECK: incq
93            %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1)
94            ret void
95    }
96
97 Here you can see some "``CHECK:``" lines specified in comments.  Now you can
98 see how the file is piped into ``llvm-as``, then ``llc``, and the machine code
99 output is what we are verifying.  FileCheck checks the machine code output to
100 verify that it matches what the "``CHECK:``" lines specify.
101
102 The syntax of the "``CHECK:``" lines is very simple: they are fixed strings that
103 must occur in order.  FileCheck defaults to ignoring horizontal whitespace
104 differences (e.g. a space is allowed to match a tab) but otherwise, the contents
105 of the "``CHECK:``" line is required to match some thing in the test file exactly.
106
107 One nice thing about FileCheck (compared to grep) is that it allows merging
108 test cases together into logical groups.  For example, because the test above
109 is checking for the "``sub1:``" and "``inc4:``" labels, it will not match
110 unless there is a "``subl``" in between those labels.  If it existed somewhere
111 else in the file, that would not count: "``grep subl``" matches if "``subl``"
112 exists anywhere in the file.
113
114 The FileCheck -check-prefix option
115 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116
117 The FileCheck :option:`-check-prefix` option allows multiple test
118 configurations to be driven from one `.ll` file.  This is useful in many
119 circumstances, for example, testing different architectural variants with
120 :program:`llc`.  Here's a simple example:
121
122 .. code-block:: llvm
123
124    ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \
125    ; RUN:              | FileCheck %s -check-prefix=X32
126    ; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \
127    ; RUN:              | FileCheck %s -check-prefix=X64
128
129    define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind {
130            %tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1
131            ret <4 x i32> %tmp1
132    ; X32: pinsrd_1:
133    ; X32:    pinsrd $1, 4(%esp), %xmm0
134
135    ; X64: pinsrd_1:
136    ; X64:    pinsrd $1, %edi, %xmm0
137    }
138
139 In this case, we're testing that we get the expected code generation with
140 both 32-bit and 64-bit code generation.
141
142 The "CHECK-NEXT:" directive
143 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
144
145 Sometimes you want to match lines and would like to verify that matches
146 happen on exactly consecutive lines with no other lines in between them.  In
147 this case, you can use "``CHECK:``" and "``CHECK-NEXT:``" directives to specify
148 this.  If you specified a custom check prefix, just use "``<PREFIX>-NEXT:``".
149 For example, something like this works as you'd expect:
150
151 .. code-block:: llvm
152
153    define void @t2(<2 x double>* %r, <2 x double>* %A, double %B) {
154         %tmp3 = load <2 x double>* %A, align 16
155         %tmp7 = insertelement <2 x double> undef, double %B, i32 0
156         %tmp9 = shufflevector <2 x double> %tmp3,
157                                <2 x double> %tmp7,
158                                <2 x i32> < i32 0, i32 2 >
159         store <2 x double> %tmp9, <2 x double>* %r, align 16
160         ret void
161
162    ; CHECK:          t2:
163    ; CHECK:             movl    8(%esp), %eax
164    ; CHECK-NEXT:        movapd  (%eax), %xmm0
165    ; CHECK-NEXT:        movhpd  12(%esp), %xmm0
166    ; CHECK-NEXT:        movl    4(%esp), %eax
167    ; CHECK-NEXT:        movapd  %xmm0, (%eax)
168    ; CHECK-NEXT:        ret
169    }
170
171 "``CHECK-NEXT:``" directives reject the input unless there is exactly one
172 newline between it and the previous directive.  A "``CHECK-NEXT:``" cannot be
173 the first directive in a file.
174
175 The "CHECK-NOT:" directive
176 ~~~~~~~~~~~~~~~~~~~~~~~~~~
177
178 The "``CHECK-NOT:``" directive is used to verify that a string doesn't occur
179 between two matches (or before the first match, or after the last match).  For
180 example, to verify that a load is removed by a transformation, a test like this
181 can be used:
182
183 .. code-block:: llvm
184
185    define i8 @coerce_offset0(i32 %V, i32* %P) {
186      store i32 %V, i32* %P
187
188      %P2 = bitcast i32* %P to i8*
189      %P3 = getelementptr i8* %P2, i32 2
190
191      %A = load i8* %P3
192      ret i8 %A
193    ; CHECK: @coerce_offset0
194    ; CHECK-NOT: load
195    ; CHECK: ret i8
196    }
197
198 The "CHECK-DAG:" directive
199 ~~~~~~~~~~~~~~~~~~~~~~~~~~
200
201 If it's necessary to match strings that don't occur in a strictly sequential
202 order, "``CHECK-DAG:``" could be used to verify them between two matches (or
203 before the first match, or after the last match). For example, clang emits
204 vtable globals in reverse order. Using ``CHECK-DAG:``, we can keep the checks
205 in the natural order:
206
207 .. code-block:: c++
208
209     // RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
210
211     struct Foo { virtual void method(); };
212     Foo f;  // emit vtable
213     // CHECK-DAG: @_ZTV3Foo =
214
215     struct Bar { virtual void method(); };
216     Bar b;
217     // CHECK-DAG: @_ZTV3Bar =
218
219 ``CHECK-NOT:`` directives could be mixed with ``CHECK-DAG:`` directives to
220 exclude strings between the surrounding ``CHECK-DAG:`` directives. As a result,
221 the surrounding ``CHECK-DAG:`` directives cannot be reordered, i.e. all
222 occurrences matching ``CHECK-DAG:`` before ``CHECK-NOT:`` must not fall behind
223 occurrences matching ``CHECK-DAG:`` after ``CHECK-NOT:``. For example,
224
225 .. code-block:: llvm
226
227    ; CHECK-DAG: BEFORE
228    ; CHECK-NOT: NOT
229    ; CHECK-DAG: AFTER
230
231 This case will reject input strings where ``BEFORE`` occurs after ``AFTER``.
232
233 With captured variables, ``CHECK-DAG:`` is able to match valid topological
234 orderings of a DAG with edges from the definition of a variable to its use.
235 It's useful, e.g., when your test cases need to match different output
236 sequences from the instruction scheduler. For example,
237
238 .. code-block:: llvm
239
240    ; CHECK-DAG: add [[REG1:r[0-9]+]], r1, r2
241    ; CHECK-DAG: add [[REG2:r[0-9]+]], r3, r4
242    ; CHECK:     mul r5, [[REG1]], [[REG2]]
243
244 In this case, any order of that two ``add`` instructions will be allowed.
245
246 If you are defining `and` using variables in the same ``CHECK-DAG:`` block,
247 be aware that the definition rule can match `after` its use.
248
249 So, for instance, the code below will pass:
250
251 .. code-block:: llvm
252
253   ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
254   ; CHECK-DAG: vmov.32 [[REG2]][1]
255   vmov.32 d0[1]
256   vmov.32 d0[0]
257
258 While this other code, will not:
259
260 .. code-block:: llvm
261
262   ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
263   ; CHECK-DAG: vmov.32 [[REG2]][1]
264   vmov.32 d1[1]
265   vmov.32 d0[0]
266
267 While this can be very useful, it's also dangerous, because in the case of
268 register sequence, you must have a strong order (read before write, copy before
269 use, etc). If the definition your test is looking for doesn't match (because
270 of a bug in the compiler), it may match further away from the use, and mask
271 real bugs away.
272
273 In those cases, to enforce the order, use a non-DAG directive between DAG-blocks.
274
275 The "CHECK-LABEL:" directive
276 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277
278 Sometimes in a file containing multiple tests divided into logical blocks, one
279 or more ``CHECK:`` directives may inadvertently succeed by matching lines in a
280 later block. While an error will usually eventually be generated, the check
281 flagged as causing the error may not actually bear any relationship to the
282 actual source of the problem.
283
284 In order to produce better error messages in these cases, the "``CHECK-LABEL:``"
285 directive can be used. It is treated identically to a normal ``CHECK``
286 directive except that FileCheck makes an additional assumption that a line
287 matched by the directive cannot also be matched by any other check present in
288 ``match-filename``; this is intended to be used for lines containing labels or
289 other unique identifiers. Conceptually, the presence of ``CHECK-LABEL`` divides
290 the input stream into separate blocks, each of which is processed independently,
291 preventing a ``CHECK:`` directive in one block matching a line in another block.
292 For example,
293
294 .. code-block:: llvm
295
296   define %struct.C* @C_ctor_base(%struct.C* %this, i32 %x) {
297   entry:
298   ; CHECK-LABEL: C_ctor_base:
299   ; CHECK: mov [[SAVETHIS:r[0-9]+]], r0
300   ; CHECK: bl A_ctor_base
301   ; CHECK: mov r0, [[SAVETHIS]]
302     %0 = bitcast %struct.C* %this to %struct.A*
303     %call = tail call %struct.A* @A_ctor_base(%struct.A* %0)
304     %1 = bitcast %struct.C* %this to %struct.B*
305     %call2 = tail call %struct.B* @B_ctor_base(%struct.B* %1, i32 %x)
306     ret %struct.C* %this
307   }
308
309   define %struct.D* @D_ctor_base(%struct.D* %this, i32 %x) {
310   entry:
311   ; CHECK-LABEL: D_ctor_base:
312
313 The use of ``CHECK-LABEL:`` directives in this case ensures that the three
314 ``CHECK:`` directives only accept lines corresponding to the body of the
315 ``@C_ctor_base`` function, even if the patterns match lines found later in
316 the file. Furthermore, if one of these three ``CHECK:`` directives fail,
317 FileCheck will recover by continuing to the next block, allowing multiple test
318 failures to be detected in a single invocation.
319
320 There is no requirement that ``CHECK-LABEL:`` directives contain strings that
321 correspond to actual syntactic labels in a source or output language: they must
322 simply uniquely match a single line in the file being verified.
323
324 ``CHECK-LABEL:`` directives cannot contain variable definitions or uses.
325
326 FileCheck Pattern Matching Syntax
327 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328
329 The "``CHECK:``" and "``CHECK-NOT:``" directives both take a pattern to match.
330 For most uses of FileCheck, fixed string matching is perfectly sufficient.  For
331 some things, a more flexible form of matching is desired.  To support this,
332 FileCheck allows you to specify regular expressions in matching strings,
333 surrounded by double braces: ``{{yourregex}}``.  Because we want to use fixed
334 string matching for a majority of what we do, FileCheck has been designed to
335 support mixing and matching fixed string matching with regular expressions.
336 This allows you to write things like this:
337
338 .. code-block:: llvm
339
340    ; CHECK: movhpd      {{[0-9]+}}(%esp), {{%xmm[0-7]}}
341
342 In this case, any offset from the ESP register will be allowed, and any xmm
343 register will be allowed.
344
345 Because regular expressions are enclosed with double braces, they are
346 visually distinct, and you don't need to use escape characters within the double
347 braces like you would in C.  In the rare case that you want to match double
348 braces explicitly from the input, you can use something ugly like
349 ``{{[{][{]}}`` as your pattern.
350
351 FileCheck Variables
352 ~~~~~~~~~~~~~~~~~~~
353
354 It is often useful to match a pattern and then verify that it occurs again
355 later in the file.  For codegen tests, this can be useful to allow any register,
356 but verify that that register is used consistently later.  To do this,
357 :program:`FileCheck` allows named variables to be defined and substituted into
358 patterns.  Here is a simple example:
359
360 .. code-block:: llvm
361
362    ; CHECK: test5:
363    ; CHECK:    notw     [[REGISTER:%[a-z]+]]
364    ; CHECK:    andw     {{.*}}[[REGISTER]]
365
366 The first check line matches a regex ``%[a-z]+`` and captures it into the
367 variable ``REGISTER``.  The second line verifies that whatever is in
368 ``REGISTER`` occurs later in the file after an "``andw``".  :program:`FileCheck`
369 variable references are always contained in ``[[ ]]`` pairs, and their names can
370 be formed with the regex ``[a-zA-Z][a-zA-Z0-9]*``.  If a colon follows the name,
371 then it is a definition of the variable; otherwise, it is a use.
372
373 :program:`FileCheck` variables can be defined multiple times, and uses always
374 get the latest value.  Variables can also be used later on the same line they
375 were defined on. For example:
376
377 .. code-block:: llvm
378
379     ; CHECK: op [[REG:r[0-9]+]], [[REG]]
380
381 Can be useful if you want the operands of ``op`` to be the same register,
382 and don't care exactly which register it is.
383
384 FileCheck Expressions
385 ~~~~~~~~~~~~~~~~~~~~~
386
387 Sometimes there's a need to verify output which refers line numbers of the
388 match file, e.g. when testing compiler diagnostics.  This introduces a certain
389 fragility of the match file structure, as "``CHECK:``" lines contain absolute
390 line numbers in the same file, which have to be updated whenever line numbers
391 change due to text addition or deletion.
392
393 To support this case, FileCheck allows using ``[[@LINE]]``,
394 ``[[@LINE+<offset>]]``, ``[[@LINE-<offset>]]`` expressions in patterns. These
395 expressions expand to a number of the line where a pattern is located (with an
396 optional integer offset).
397
398 This way match patterns can be put near the relevant test lines and include
399 relative line number references, for example:
400
401 .. code-block:: c++
402
403    // CHECK: test.cpp:[[@LINE+4]]:6: error: expected ';' after top level declarator
404    // CHECK-NEXT: {{^int a}}
405    // CHECK-NEXT: {{^     \^}}
406    // CHECK-NEXT: {{^     ;}}
407    int a
408