18f4a8a63SDaniel DunbarFileCheck - Flexible pattern matching file verifier
28f4a8a63SDaniel Dunbar===================================================
38f4a8a63SDaniel Dunbar
48f4a8a63SDaniel DunbarSYNOPSIS
58f4a8a63SDaniel Dunbar--------
68f4a8a63SDaniel Dunbar
7f589e241SDmitri Gribenko:program:`FileCheck` *match-filename* [*--check-prefix=XXX*] [*--strict-whitespace*]
88f4a8a63SDaniel Dunbar
98f4a8a63SDaniel DunbarDESCRIPTION
108f4a8a63SDaniel Dunbar-----------
118f4a8a63SDaniel Dunbar
12f589e241SDmitri Gribenko:program:`FileCheck` reads two files (one from standard input, and one
13f589e241SDmitri Gribenkospecified on the command line) and uses one to verify the other.  This
14f589e241SDmitri Gribenkobehavior is particularly useful for the testsuite, which wants to verify that
15f589e241SDmitri Gribenkothe output of some tool (e.g. :program:`llc`) contains the expected information
16f589e241SDmitri Gribenko(for example, a movsd from esp or whatever is interesting).  This is similar to
17f589e241SDmitri Gribenkousing :program:`grep`, but it is optimized for matching multiple different
18f589e241SDmitri Gribenkoinputs in one file in a specific order.
198f4a8a63SDaniel Dunbar
20f589e241SDmitri GribenkoThe ``match-filename`` file specifies the file that contains the patterns to
21a6e877faSStephen Linmatch.  The file to verify is read from standard input unless the
22a6e877faSStephen Lin:option:`--input-file` option is used.
238f4a8a63SDaniel Dunbar
248f4a8a63SDaniel DunbarOPTIONS
258f4a8a63SDaniel Dunbar-------
268f4a8a63SDaniel Dunbar
27f589e241SDmitri Gribenko.. option:: -help
288f4a8a63SDaniel Dunbar
298f4a8a63SDaniel Dunbar Print a summary of command line options.
308f4a8a63SDaniel Dunbar
31f589e241SDmitri Gribenko.. option:: --check-prefix prefix
328f4a8a63SDaniel Dunbar
3313df4626SMatt Arsenault FileCheck searches the contents of ``match-filename`` for patterns to
3413df4626SMatt Arsenault match.  By default, these patterns are prefixed with "``CHECK:``".
3513df4626SMatt Arsenault If you'd like to use a different prefix (e.g. because the same input
3613df4626SMatt Arsenault file is checking multiple different tool or options), the
3713df4626SMatt Arsenault :option:`--check-prefix` argument allows you to specify one or more
3813df4626SMatt Arsenault prefixes to match. Multiple prefixes are useful for tests which might
3913df4626SMatt Arsenault change for different run options, but most lines remain the same.
408f4a8a63SDaniel Dunbar
41f589e241SDmitri Gribenko.. option:: --input-file filename
428a7e80f6SEli Bendersky
438a7e80f6SEli Bendersky  File to check (defaults to stdin).
448a7e80f6SEli Bendersky
45f589e241SDmitri Gribenko.. option:: --strict-whitespace
468f4a8a63SDaniel Dunbar
478f4a8a63SDaniel Dunbar By default, FileCheck canonicalizes input horizontal whitespace (spaces and
488f4a8a63SDaniel Dunbar tabs) which causes it to ignore these differences (a space will match a tab).
495ea04c38SGuy Benyei The :option:`--strict-whitespace` argument disables this behavior. End-of-line
50b6bfbad2SSean Silva sequences are canonicalized to UNIX-style ``\n`` in all modes.
518f4a8a63SDaniel Dunbar
52*56ccdbbdSAlexander Kornienko.. option:: --implicit-check-not check-pattern
53*56ccdbbdSAlexander Kornienko
54*56ccdbbdSAlexander Kornienko  Adds implicit negative checks for the specified patterns between positive
55*56ccdbbdSAlexander Kornienko  checks. The option allows writing stricter tests without stuffing them with
56*56ccdbbdSAlexander Kornienko  ``CHECK-NOT``s.
57*56ccdbbdSAlexander Kornienko
58*56ccdbbdSAlexander Kornienko  For example, "``--implicit-check-not warning:``" can be useful when testing
59*56ccdbbdSAlexander Kornienko  diagnostic messages from tools that don't have an option similar to ``clang
60*56ccdbbdSAlexander Kornienko  -verify``. With this option FileCheck will verify that input does not contain
61*56ccdbbdSAlexander Kornienko  warnings not covered by any ``CHECK:`` patterns.
62*56ccdbbdSAlexander Kornienko
63f589e241SDmitri Gribenko.. option:: -version
648f4a8a63SDaniel Dunbar
658f4a8a63SDaniel Dunbar Show the version number of this program.
668f4a8a63SDaniel Dunbar
678f4a8a63SDaniel DunbarEXIT STATUS
688f4a8a63SDaniel Dunbar-----------
698f4a8a63SDaniel Dunbar
70f589e241SDmitri GribenkoIf :program:`FileCheck` verifies that the file matches the expected contents,
71f589e241SDmitri Gribenkoit exits with 0.  Otherwise, if not, or if an error occurs, it will exit with a
72f589e241SDmitri Gribenkonon-zero value.
738f4a8a63SDaniel Dunbar
748f4a8a63SDaniel DunbarTUTORIAL
758f4a8a63SDaniel Dunbar--------
768f4a8a63SDaniel Dunbar
778f4a8a63SDaniel DunbarFileCheck is typically used from LLVM regression tests, being invoked on the RUN
788f4a8a63SDaniel Dunbarline of the test.  A simple example of using FileCheck from a RUN line looks
798f4a8a63SDaniel Dunbarlike this:
808f4a8a63SDaniel Dunbar
81a99fa5b0SDmitri Gribenko.. code-block:: llvm
828f4a8a63SDaniel Dunbar
838f4a8a63SDaniel Dunbar   ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s
848f4a8a63SDaniel Dunbar
85a72e9f01SDmitri GribenkoThis syntax says to pipe the current file ("``%s``") into ``llvm-as``, pipe
86a72e9f01SDmitri Gribenkothat into ``llc``, then pipe the output of ``llc`` into ``FileCheck``.  This
87a72e9f01SDmitri Gribenkomeans that FileCheck will be verifying its standard input (the llc output)
88a72e9f01SDmitri Gribenkoagainst the filename argument specified (the original ``.ll`` file specified by
89a72e9f01SDmitri Gribenko"``%s``").  To see how this works, let's look at the rest of the ``.ll`` file
90a72e9f01SDmitri Gribenko(after the RUN line):
918f4a8a63SDaniel Dunbar
92a99fa5b0SDmitri Gribenko.. code-block:: llvm
938f4a8a63SDaniel Dunbar
948f4a8a63SDaniel Dunbar   define void @sub1(i32* %p, i32 %v) {
958f4a8a63SDaniel Dunbar   entry:
968f4a8a63SDaniel Dunbar   ; CHECK: sub1:
978f4a8a63SDaniel Dunbar   ; CHECK: subl
988f4a8a63SDaniel Dunbar           %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v)
998f4a8a63SDaniel Dunbar           ret void
1008f4a8a63SDaniel Dunbar   }
1018f4a8a63SDaniel Dunbar
1028f4a8a63SDaniel Dunbar   define void @inc4(i64* %p) {
1038f4a8a63SDaniel Dunbar   entry:
1048f4a8a63SDaniel Dunbar   ; CHECK: inc4:
1058f4a8a63SDaniel Dunbar   ; CHECK: incq
1068f4a8a63SDaniel Dunbar           %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1)
1078f4a8a63SDaniel Dunbar           ret void
1088f4a8a63SDaniel Dunbar   }
1098f4a8a63SDaniel Dunbar
110a72e9f01SDmitri GribenkoHere you can see some "``CHECK:``" lines specified in comments.  Now you can
111a72e9f01SDmitri Gribenkosee how the file is piped into ``llvm-as``, then ``llc``, and the machine code
112a72e9f01SDmitri Gribenkooutput is what we are verifying.  FileCheck checks the machine code output to
113a72e9f01SDmitri Gribenkoverify that it matches what the "``CHECK:``" lines specify.
1148f4a8a63SDaniel Dunbar
115a72e9f01SDmitri GribenkoThe syntax of the "``CHECK:``" lines is very simple: they are fixed strings that
1168f4a8a63SDaniel Dunbarmust occur in order.  FileCheck defaults to ignoring horizontal whitespace
1178f4a8a63SDaniel Dunbardifferences (e.g. a space is allowed to match a tab) but otherwise, the contents
118a72e9f01SDmitri Gribenkoof the "``CHECK:``" line is required to match some thing in the test file exactly.
1198f4a8a63SDaniel Dunbar
1208f4a8a63SDaniel DunbarOne nice thing about FileCheck (compared to grep) is that it allows merging
1218f4a8a63SDaniel Dunbartest cases together into logical groups.  For example, because the test above
122a72e9f01SDmitri Gribenkois checking for the "``sub1:``" and "``inc4:``" labels, it will not match
123a72e9f01SDmitri Gribenkounless there is a "``subl``" in between those labels.  If it existed somewhere
124a72e9f01SDmitri Gribenkoelse in the file, that would not count: "``grep subl``" matches if "``subl``"
125a72e9f01SDmitri Gribenkoexists anywhere in the file.
1268f4a8a63SDaniel Dunbar
1278f4a8a63SDaniel DunbarThe FileCheck -check-prefix option
1288f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1298f4a8a63SDaniel Dunbar
130f589e241SDmitri GribenkoThe FileCheck :option:`-check-prefix` option allows multiple test
131f589e241SDmitri Gribenkoconfigurations to be driven from one `.ll` file.  This is useful in many
132f589e241SDmitri Gribenkocircumstances, for example, testing different architectural variants with
133f589e241SDmitri Gribenko:program:`llc`.  Here's a simple example:
1348f4a8a63SDaniel Dunbar
135a99fa5b0SDmitri Gribenko.. code-block:: llvm
1368f4a8a63SDaniel Dunbar
1378f4a8a63SDaniel Dunbar   ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \
13819408a76SDmitri Gribenko   ; RUN:              | FileCheck %s -check-prefix=X32
1398f4a8a63SDaniel Dunbar   ; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \
14019408a76SDmitri Gribenko   ; RUN:              | FileCheck %s -check-prefix=X64
1418f4a8a63SDaniel Dunbar
1428f4a8a63SDaniel Dunbar   define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind {
1438f4a8a63SDaniel Dunbar           %tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1
1448f4a8a63SDaniel Dunbar           ret <4 x i32> %tmp1
1458f4a8a63SDaniel Dunbar   ; X32: pinsrd_1:
1468f4a8a63SDaniel Dunbar   ; X32:    pinsrd $1, 4(%esp), %xmm0
1478f4a8a63SDaniel Dunbar
1488f4a8a63SDaniel Dunbar   ; X64: pinsrd_1:
1498f4a8a63SDaniel Dunbar   ; X64:    pinsrd $1, %edi, %xmm0
1508f4a8a63SDaniel Dunbar   }
1518f4a8a63SDaniel Dunbar
1528f4a8a63SDaniel DunbarIn this case, we're testing that we get the expected code generation with
1538f4a8a63SDaniel Dunbarboth 32-bit and 64-bit code generation.
1548f4a8a63SDaniel Dunbar
1558f4a8a63SDaniel DunbarThe "CHECK-NEXT:" directive
1568f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~~
1578f4a8a63SDaniel Dunbar
1588f4a8a63SDaniel DunbarSometimes you want to match lines and would like to verify that matches
1598f4a8a63SDaniel Dunbarhappen on exactly consecutive lines with no other lines in between them.  In
160a72e9f01SDmitri Gribenkothis case, you can use "``CHECK:``" and "``CHECK-NEXT:``" directives to specify
161a72e9f01SDmitri Gribenkothis.  If you specified a custom check prefix, just use "``<PREFIX>-NEXT:``".
162a72e9f01SDmitri GribenkoFor example, something like this works as you'd expect:
1638f4a8a63SDaniel Dunbar
164a99fa5b0SDmitri Gribenko.. code-block:: llvm
1658f4a8a63SDaniel Dunbar
16619408a76SDmitri Gribenko   define void @t2(<2 x double>* %r, <2 x double>* %A, double %B) {
16719408a76SDmitri Gribenko 	%tmp3 = load <2 x double>* %A, align 16
16819408a76SDmitri Gribenko 	%tmp7 = insertelement <2 x double> undef, double %B, i32 0
16919408a76SDmitri Gribenko 	%tmp9 = shufflevector <2 x double> %tmp3,
17019408a76SDmitri Gribenko                               <2 x double> %tmp7,
17119408a76SDmitri Gribenko                               <2 x i32> < i32 0, i32 2 >
17219408a76SDmitri Gribenko 	store <2 x double> %tmp9, <2 x double>* %r, align 16
1738f4a8a63SDaniel Dunbar 	ret void
1748f4a8a63SDaniel Dunbar
1758f4a8a63SDaniel Dunbar   ; CHECK:          t2:
1768f4a8a63SDaniel Dunbar   ; CHECK: 	        movl	8(%esp), %eax
1778f4a8a63SDaniel Dunbar   ; CHECK-NEXT: 	movapd	(%eax), %xmm0
1788f4a8a63SDaniel Dunbar   ; CHECK-NEXT: 	movhpd	12(%esp), %xmm0
1798f4a8a63SDaniel Dunbar   ; CHECK-NEXT: 	movl	4(%esp), %eax
1808f4a8a63SDaniel Dunbar   ; CHECK-NEXT: 	movapd	%xmm0, (%eax)
1818f4a8a63SDaniel Dunbar   ; CHECK-NEXT: 	ret
1828f4a8a63SDaniel Dunbar   }
1838f4a8a63SDaniel Dunbar
184a72e9f01SDmitri Gribenko"``CHECK-NEXT:``" directives reject the input unless there is exactly one
1852fef6b6aSEli Benderskynewline between it and the previous directive.  A "``CHECK-NEXT:``" cannot be
186a72e9f01SDmitri Gribenkothe first directive in a file.
1878f4a8a63SDaniel Dunbar
1888f4a8a63SDaniel DunbarThe "CHECK-NOT:" directive
1898f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~
1908f4a8a63SDaniel Dunbar
191a72e9f01SDmitri GribenkoThe "``CHECK-NOT:``" directive is used to verify that a string doesn't occur
1928f4a8a63SDaniel Dunbarbetween two matches (or before the first match, or after the last match).  For
1938f4a8a63SDaniel Dunbarexample, to verify that a load is removed by a transformation, a test like this
1948f4a8a63SDaniel Dunbarcan be used:
1958f4a8a63SDaniel Dunbar
196a99fa5b0SDmitri Gribenko.. code-block:: llvm
1978f4a8a63SDaniel Dunbar
1988f4a8a63SDaniel Dunbar   define i8 @coerce_offset0(i32 %V, i32* %P) {
1998f4a8a63SDaniel Dunbar     store i32 %V, i32* %P
2008f4a8a63SDaniel Dunbar
2018f4a8a63SDaniel Dunbar     %P2 = bitcast i32* %P to i8*
2028f4a8a63SDaniel Dunbar     %P3 = getelementptr i8* %P2, i32 2
2038f4a8a63SDaniel Dunbar
2048f4a8a63SDaniel Dunbar     %A = load i8* %P3
2058f4a8a63SDaniel Dunbar     ret i8 %A
2068f4a8a63SDaniel Dunbar   ; CHECK: @coerce_offset0
2078f4a8a63SDaniel Dunbar   ; CHECK-NOT: load
2088f4a8a63SDaniel Dunbar   ; CHECK: ret i8
2098f4a8a63SDaniel Dunbar   }
2108f4a8a63SDaniel Dunbar
21191a1b2c9SMichael LiaoThe "CHECK-DAG:" directive
21291a1b2c9SMichael Liao~~~~~~~~~~~~~~~~~~~~~~~~~~
21391a1b2c9SMichael Liao
21491a1b2c9SMichael LiaoIf it's necessary to match strings that don't occur in a strictly sequential
21591a1b2c9SMichael Liaoorder, "``CHECK-DAG:``" could be used to verify them between two matches (or
21691a1b2c9SMichael Liaobefore the first match, or after the last match). For example, clang emits
21791a1b2c9SMichael Liaovtable globals in reverse order. Using ``CHECK-DAG:``, we can keep the checks
21891a1b2c9SMichael Liaoin the natural order:
21991a1b2c9SMichael Liao
22091a1b2c9SMichael Liao.. code-block:: c++
22191a1b2c9SMichael Liao
22291a1b2c9SMichael Liao    // RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
22391a1b2c9SMichael Liao
22491a1b2c9SMichael Liao    struct Foo { virtual void method(); };
22591a1b2c9SMichael Liao    Foo f;  // emit vtable
22691a1b2c9SMichael Liao    // CHECK-DAG: @_ZTV3Foo =
22791a1b2c9SMichael Liao
22891a1b2c9SMichael Liao    struct Bar { virtual void method(); };
22991a1b2c9SMichael Liao    Bar b;
23091a1b2c9SMichael Liao    // CHECK-DAG: @_ZTV3Bar =
23191a1b2c9SMichael Liao
23258ab84a8SRenato Golin``CHECK-NOT:`` directives could be mixed with ``CHECK-DAG:`` directives to
23358ab84a8SRenato Golinexclude strings between the surrounding ``CHECK-DAG:`` directives. As a result,
23458ab84a8SRenato Golinthe surrounding ``CHECK-DAG:`` directives cannot be reordered, i.e. all
23558ab84a8SRenato Golinoccurrences matching ``CHECK-DAG:`` before ``CHECK-NOT:`` must not fall behind
23658ab84a8SRenato Golinoccurrences matching ``CHECK-DAG:`` after ``CHECK-NOT:``. For example,
23758ab84a8SRenato Golin
23858ab84a8SRenato Golin.. code-block:: llvm
23958ab84a8SRenato Golin
24058ab84a8SRenato Golin   ; CHECK-DAG: BEFORE
24158ab84a8SRenato Golin   ; CHECK-NOT: NOT
24258ab84a8SRenato Golin   ; CHECK-DAG: AFTER
24358ab84a8SRenato Golin
24458ab84a8SRenato GolinThis case will reject input strings where ``BEFORE`` occurs after ``AFTER``.
24591a1b2c9SMichael Liao
24691a1b2c9SMichael LiaoWith captured variables, ``CHECK-DAG:`` is able to match valid topological
24791a1b2c9SMichael Liaoorderings of a DAG with edges from the definition of a variable to its use.
24891a1b2c9SMichael LiaoIt's useful, e.g., when your test cases need to match different output
24991a1b2c9SMichael Liaosequences from the instruction scheduler. For example,
25091a1b2c9SMichael Liao
25191a1b2c9SMichael Liao.. code-block:: llvm
25291a1b2c9SMichael Liao
25391a1b2c9SMichael Liao   ; CHECK-DAG: add [[REG1:r[0-9]+]], r1, r2
25491a1b2c9SMichael Liao   ; CHECK-DAG: add [[REG2:r[0-9]+]], r3, r4
25591a1b2c9SMichael Liao   ; CHECK:     mul r5, [[REG1]], [[REG2]]
25691a1b2c9SMichael Liao
25791a1b2c9SMichael LiaoIn this case, any order of that two ``add`` instructions will be allowed.
25891a1b2c9SMichael Liao
25958ab84a8SRenato GolinIf you are defining `and` using variables in the same ``CHECK-DAG:`` block,
26058ab84a8SRenato Golinbe aware that the definition rule can match `after` its use.
26158ab84a8SRenato Golin
26258ab84a8SRenato GolinSo, for instance, the code below will pass:
26391a1b2c9SMichael Liao
26491a1b2c9SMichael Liao.. code-block:: llvm
26591a1b2c9SMichael Liao
26658ab84a8SRenato Golin  ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
26758ab84a8SRenato Golin  ; CHECK-DAG: vmov.32 [[REG2]][1]
26858ab84a8SRenato Golin  vmov.32 d0[1]
26958ab84a8SRenato Golin  vmov.32 d0[0]
27091a1b2c9SMichael Liao
27158ab84a8SRenato GolinWhile this other code, will not:
27258ab84a8SRenato Golin
27358ab84a8SRenato Golin.. code-block:: llvm
27458ab84a8SRenato Golin
27558ab84a8SRenato Golin  ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0]
27658ab84a8SRenato Golin  ; CHECK-DAG: vmov.32 [[REG2]][1]
27758ab84a8SRenato Golin  vmov.32 d1[1]
27858ab84a8SRenato Golin  vmov.32 d0[0]
27958ab84a8SRenato Golin
28058ab84a8SRenato GolinWhile this can be very useful, it's also dangerous, because in the case of
28158ab84a8SRenato Golinregister sequence, you must have a strong order (read before write, copy before
28258ab84a8SRenato Golinuse, etc). If the definition your test is looking for doesn't match (because
28358ab84a8SRenato Golinof a bug in the compiler), it may match further away from the use, and mask
28458ab84a8SRenato Golinreal bugs away.
28558ab84a8SRenato Golin
28658ab84a8SRenato GolinIn those cases, to enforce the order, use a non-DAG directive between DAG-blocks.
28791a1b2c9SMichael Liao
288f8bd2e5bSStephen LinThe "CHECK-LABEL:" directive
289c02da467SBill Wendling~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290f8bd2e5bSStephen Lin
291f8bd2e5bSStephen LinSometimes in a file containing multiple tests divided into logical blocks, one
292f8bd2e5bSStephen Linor more ``CHECK:`` directives may inadvertently succeed by matching lines in a
293f8bd2e5bSStephen Linlater block. While an error will usually eventually be generated, the check
294f8bd2e5bSStephen Linflagged as causing the error may not actually bear any relationship to the
295f8bd2e5bSStephen Linactual source of the problem.
296f8bd2e5bSStephen Lin
297f8bd2e5bSStephen LinIn order to produce better error messages in these cases, the "``CHECK-LABEL:``"
298f8bd2e5bSStephen Lindirective can be used. It is treated identically to a normal ``CHECK``
299b946407cSStephen Lindirective except that FileCheck makes an additional assumption that a line
300b946407cSStephen Linmatched by the directive cannot also be matched by any other check present in
301b946407cSStephen Lin``match-filename``; this is intended to be used for lines containing labels or
302b946407cSStephen Linother unique identifiers. Conceptually, the presence of ``CHECK-LABEL`` divides
303b946407cSStephen Linthe input stream into separate blocks, each of which is processed independently,
304b946407cSStephen Linpreventing a ``CHECK:`` directive in one block matching a line in another block.
305b946407cSStephen LinFor example,
306f8bd2e5bSStephen Lin
307f8bd2e5bSStephen Lin.. code-block:: llvm
308f8bd2e5bSStephen Lin
309f8bd2e5bSStephen Lin  define %struct.C* @C_ctor_base(%struct.C* %this, i32 %x) {
310f8bd2e5bSStephen Lin  entry:
311f8bd2e5bSStephen Lin  ; CHECK-LABEL: C_ctor_base:
312f8bd2e5bSStephen Lin  ; CHECK: mov [[SAVETHIS:r[0-9]+]], r0
313f8bd2e5bSStephen Lin  ; CHECK: bl A_ctor_base
314f8bd2e5bSStephen Lin  ; CHECK: mov r0, [[SAVETHIS]]
315f8bd2e5bSStephen Lin    %0 = bitcast %struct.C* %this to %struct.A*
316f8bd2e5bSStephen Lin    %call = tail call %struct.A* @A_ctor_base(%struct.A* %0)
317f8bd2e5bSStephen Lin    %1 = bitcast %struct.C* %this to %struct.B*
318f8bd2e5bSStephen Lin    %call2 = tail call %struct.B* @B_ctor_base(%struct.B* %1, i32 %x)
319f8bd2e5bSStephen Lin    ret %struct.C* %this
320f8bd2e5bSStephen Lin  }
321f8bd2e5bSStephen Lin
322f8bd2e5bSStephen Lin  define %struct.D* @D_ctor_base(%struct.D* %this, i32 %x) {
323f8bd2e5bSStephen Lin  entry:
324f8bd2e5bSStephen Lin  ; CHECK-LABEL: D_ctor_base:
325f8bd2e5bSStephen Lin
326f8bd2e5bSStephen LinThe use of ``CHECK-LABEL:`` directives in this case ensures that the three
327f8bd2e5bSStephen Lin``CHECK:`` directives only accept lines corresponding to the body of the
328f8bd2e5bSStephen Lin``@C_ctor_base`` function, even if the patterns match lines found later in
329b946407cSStephen Linthe file. Furthermore, if one of these three ``CHECK:`` directives fail,
330b946407cSStephen LinFileCheck will recover by continuing to the next block, allowing multiple test
331b946407cSStephen Linfailures to be detected in a single invocation.
332f8bd2e5bSStephen Lin
333f8bd2e5bSStephen LinThere is no requirement that ``CHECK-LABEL:`` directives contain strings that
334f8bd2e5bSStephen Lincorrespond to actual syntactic labels in a source or output language: they must
335f8bd2e5bSStephen Linsimply uniquely match a single line in the file being verified.
336f8bd2e5bSStephen Lin
337f8bd2e5bSStephen Lin``CHECK-LABEL:`` directives cannot contain variable definitions or uses.
338f8bd2e5bSStephen Lin
3398f4a8a63SDaniel DunbarFileCheck Pattern Matching Syntax
3408f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3418f4a8a63SDaniel Dunbar
342a72e9f01SDmitri GribenkoThe "``CHECK:``" and "``CHECK-NOT:``" directives both take a pattern to match.
343a72e9f01SDmitri GribenkoFor most uses of FileCheck, fixed string matching is perfectly sufficient.  For
344a72e9f01SDmitri Gribenkosome things, a more flexible form of matching is desired.  To support this,
345a72e9f01SDmitri GribenkoFileCheck allows you to specify regular expressions in matching strings,
346a72e9f01SDmitri Gribenkosurrounded by double braces: ``{{yourregex}}``.  Because we want to use fixed
347a72e9f01SDmitri Gribenkostring matching for a majority of what we do, FileCheck has been designed to
348a72e9f01SDmitri Gribenkosupport mixing and matching fixed string matching with regular expressions.
349a72e9f01SDmitri GribenkoThis allows you to write things like this:
3508f4a8a63SDaniel Dunbar
351a99fa5b0SDmitri Gribenko.. code-block:: llvm
3528f4a8a63SDaniel Dunbar
3538f4a8a63SDaniel Dunbar   ; CHECK: movhpd	{{[0-9]+}}(%esp), {{%xmm[0-7]}}
3548f4a8a63SDaniel Dunbar
3558f4a8a63SDaniel DunbarIn this case, any offset from the ESP register will be allowed, and any xmm
3568f4a8a63SDaniel Dunbarregister will be allowed.
3578f4a8a63SDaniel Dunbar
3588f4a8a63SDaniel DunbarBecause regular expressions are enclosed with double braces, they are
3598f4a8a63SDaniel Dunbarvisually distinct, and you don't need to use escape characters within the double
3608f4a8a63SDaniel Dunbarbraces like you would in C.  In the rare case that you want to match double
3618f4a8a63SDaniel Dunbarbraces explicitly from the input, you can use something ugly like
362a72e9f01SDmitri Gribenko``{{[{][{]}}`` as your pattern.
3638f4a8a63SDaniel Dunbar
3648f4a8a63SDaniel DunbarFileCheck Variables
3658f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~
3668f4a8a63SDaniel Dunbar
3678f4a8a63SDaniel DunbarIt is often useful to match a pattern and then verify that it occurs again
3688f4a8a63SDaniel Dunbarlater in the file.  For codegen tests, this can be useful to allow any register,
3694ca99ba6SEli Benderskybut verify that that register is used consistently later.  To do this,
3704ca99ba6SEli Bendersky:program:`FileCheck` allows named variables to be defined and substituted into
3714ca99ba6SEli Benderskypatterns.  Here is a simple example:
3728f4a8a63SDaniel Dunbar
373a99fa5b0SDmitri Gribenko.. code-block:: llvm
3748f4a8a63SDaniel Dunbar
3758f4a8a63SDaniel Dunbar   ; CHECK: test5:
3768f4a8a63SDaniel Dunbar   ; CHECK:    notw	[[REGISTER:%[a-z]+]]
377fd7469c9SChad Rosier   ; CHECK:    andw	{{.*}}[[REGISTER]]
3788f4a8a63SDaniel Dunbar
379a72e9f01SDmitri GribenkoThe first check line matches a regex ``%[a-z]+`` and captures it into the
380a72e9f01SDmitri Gribenkovariable ``REGISTER``.  The second line verifies that whatever is in
3814ca99ba6SEli Bendersky``REGISTER`` occurs later in the file after an "``andw``".  :program:`FileCheck`
3824ca99ba6SEli Benderskyvariable references are always contained in ``[[ ]]`` pairs, and their names can
3834ca99ba6SEli Benderskybe formed with the regex ``[a-zA-Z][a-zA-Z0-9]*``.  If a colon follows the name,
384a72e9f01SDmitri Gribenkothen it is a definition of the variable; otherwise, it is a use.
3858f4a8a63SDaniel Dunbar
3864ca99ba6SEli Bendersky:program:`FileCheck` variables can be defined multiple times, and uses always
3874ca99ba6SEli Benderskyget the latest value.  Variables can also be used later on the same line they
3884ca99ba6SEli Benderskywere defined on. For example:
3894ca99ba6SEli Bendersky
3904ca99ba6SEli Bendersky.. code-block:: llvm
3914ca99ba6SEli Bendersky
3924ca99ba6SEli Bendersky    ; CHECK: op [[REG:r[0-9]+]], [[REG]]
3934ca99ba6SEli Bendersky
3944ca99ba6SEli BenderskyCan be useful if you want the operands of ``op`` to be the same register,
3954ca99ba6SEli Benderskyand don't care exactly which register it is.
396a72e9f01SDmitri Gribenko
39792987fb3SAlexander KornienkoFileCheck Expressions
39892987fb3SAlexander Kornienko~~~~~~~~~~~~~~~~~~~~~
39992987fb3SAlexander Kornienko
400f589e241SDmitri GribenkoSometimes there's a need to verify output which refers line numbers of the
401f589e241SDmitri Gribenkomatch file, e.g. when testing compiler diagnostics.  This introduces a certain
402f589e241SDmitri Gribenkofragility of the match file structure, as "``CHECK:``" lines contain absolute
403f589e241SDmitri Gribenkoline numbers in the same file, which have to be updated whenever line numbers
404f589e241SDmitri Gribenkochange due to text addition or deletion.
40592987fb3SAlexander Kornienko
40692987fb3SAlexander KornienkoTo support this case, FileCheck allows using ``[[@LINE]]``,
40792987fb3SAlexander Kornienko``[[@LINE+<offset>]]``, ``[[@LINE-<offset>]]`` expressions in patterns. These
40892987fb3SAlexander Kornienkoexpressions expand to a number of the line where a pattern is located (with an
40992987fb3SAlexander Kornienkooptional integer offset).
41092987fb3SAlexander Kornienko
41192987fb3SAlexander KornienkoThis way match patterns can be put near the relevant test lines and include
41292987fb3SAlexander Kornienkorelative line number references, for example:
41392987fb3SAlexander Kornienko
41492987fb3SAlexander Kornienko.. code-block:: c++
41592987fb3SAlexander Kornienko
41692987fb3SAlexander Kornienko   // CHECK: test.cpp:[[@LINE+4]]:6: error: expected ';' after top level declarator
41792987fb3SAlexander Kornienko   // CHECK-NEXT: {{^int a}}
41892987fb3SAlexander Kornienko   // CHECK-NEXT: {{^     \^}}
41992987fb3SAlexander Kornienko   // CHECK-NEXT: {{^     ;}}
42092987fb3SAlexander Kornienko   int a
42192987fb3SAlexander Kornienko
422