18f4a8a63SDaniel DunbarFileCheck - Flexible pattern matching file verifier 28f4a8a63SDaniel Dunbar=================================================== 38f4a8a63SDaniel Dunbar 4a056684cSJames Henderson.. program:: FileCheck 5a056684cSJames Henderson 68f4a8a63SDaniel DunbarSYNOPSIS 78f4a8a63SDaniel Dunbar-------- 88f4a8a63SDaniel Dunbar 9f589e241SDmitri Gribenko:program:`FileCheck` *match-filename* [*--check-prefix=XXX*] [*--strict-whitespace*] 108f4a8a63SDaniel Dunbar 118f4a8a63SDaniel DunbarDESCRIPTION 128f4a8a63SDaniel Dunbar----------- 138f4a8a63SDaniel Dunbar 14f589e241SDmitri Gribenko:program:`FileCheck` reads two files (one from standard input, and one 15f589e241SDmitri Gribenkospecified on the command line) and uses one to verify the other. This 16f589e241SDmitri Gribenkobehavior is particularly useful for the testsuite, which wants to verify that 17f589e241SDmitri Gribenkothe output of some tool (e.g. :program:`llc`) contains the expected information 18f589e241SDmitri Gribenko(for example, a movsd from esp or whatever is interesting). This is similar to 19f589e241SDmitri Gribenkousing :program:`grep`, but it is optimized for matching multiple different 20f589e241SDmitri Gribenkoinputs in one file in a specific order. 218f4a8a63SDaniel Dunbar 22f589e241SDmitri GribenkoThe ``match-filename`` file specifies the file that contains the patterns to 23a6e877faSStephen Linmatch. The file to verify is read from standard input unless the 24a6e877faSStephen Lin:option:`--input-file` option is used. 258f4a8a63SDaniel Dunbar 268f4a8a63SDaniel DunbarOPTIONS 278f4a8a63SDaniel Dunbar------- 288f4a8a63SDaniel Dunbar 2924994d77SJoel E. DennyOptions are parsed from the environment variable ``FILECHECK_OPTS`` 3024994d77SJoel E. Dennyand from the command line. 3124994d77SJoel E. Denny 32f589e241SDmitri Gribenko.. option:: -help 338f4a8a63SDaniel Dunbar 348f4a8a63SDaniel Dunbar Print a summary of command line options. 358f4a8a63SDaniel Dunbar 36f589e241SDmitri Gribenko.. option:: --check-prefix prefix 378f4a8a63SDaniel Dunbar 3813df4626SMatt Arsenault FileCheck searches the contents of ``match-filename`` for patterns to 3913df4626SMatt Arsenault match. By default, these patterns are prefixed with "``CHECK:``". 4013df4626SMatt Arsenault If you'd like to use a different prefix (e.g. because the same input 4113df4626SMatt Arsenault file is checking multiple different tool or options), the 42a1fd1882SJoel E. Denny :option:`--check-prefix` argument allows you to specify (without the trailing 43a1fd1882SJoel E. Denny "``:``") one or more prefixes to match. Multiple prefixes are useful for tests 44a1fd1882SJoel E. Denny which might change for different run options, but most lines remain the same. 45a1fd1882SJoel E. Denny 46a1fd1882SJoel E. Denny FileCheck does not permit duplicate prefixes, even if one is a check prefix 47a1fd1882SJoel E. Denny and one is a comment prefix (see :option:`--comment-prefixes` below). 488f4a8a63SDaniel Dunbar 49ad875c2cSDaniel Sanders.. option:: --check-prefixes prefix1,prefix2,... 50ad875c2cSDaniel Sanders 51ad875c2cSDaniel Sanders An alias of :option:`--check-prefix` that allows multiple prefixes to be 52ad875c2cSDaniel Sanders specified as a comma separated list. 53ad875c2cSDaniel Sanders 54a1fd1882SJoel E. Denny.. option:: --comment-prefixes prefix1,prefix2,... 55a1fd1882SJoel E. Denny 56a1fd1882SJoel E. Denny By default, FileCheck ignores any occurrence in ``match-filename`` of any check 57a1fd1882SJoel E. Denny prefix if it is preceded on the same line by "``COM:``" or "``RUN:``". See the 58a1fd1882SJoel E. Denny section `The "COM:" directive`_ for usage details. 59a1fd1882SJoel E. Denny 60a1fd1882SJoel E. Denny These default comment prefixes can be overridden by 61a1fd1882SJoel E. Denny :option:`--comment-prefixes` if they are not appropriate for your testing 62a1fd1882SJoel E. Denny environment. However, doing so is not recommended in LLVM's LIT-based test 63a1fd1882SJoel E. Denny suites, which should be easier to maintain if they all follow a consistent 64a1fd1882SJoel E. Denny comment style. In that case, consider proposing a change to the default 65a1fd1882SJoel E. Denny comment prefixes instead. 66a1fd1882SJoel E. Denny 6722113341SMircea Trofin.. option:: --allow-unused-prefixes 6822113341SMircea Trofin 6922113341SMircea Trofin This option controls the behavior when using more than one prefix as specified 7022113341SMircea Trofin by :option:`--check-prefix` or :option:`--check-prefixes`, and some of these 7122113341SMircea Trofin prefixes are missing in the test file. If true, this is allowed, if false, 72*24b98520SDiana Picus FileCheck will report an error, listing the missing prefixes. The default value 73*24b98520SDiana Picus is false. 7422113341SMircea Trofin 75f589e241SDmitri Gribenko.. option:: --input-file filename 768a7e80f6SEli Bendersky 778a7e80f6SEli Bendersky File to check (defaults to stdin). 788a7e80f6SEli Bendersky 7985913ccaSJames Y Knight.. option:: --match-full-lines 8085913ccaSJames Y Knight 8185913ccaSJames Y Knight By default, FileCheck allows matches of anywhere on a line. This 8285913ccaSJames Y Knight option will require all positive matches to cover an entire 8385913ccaSJames Y Knight line. Leading and trailing whitespace is ignored, unless 8485913ccaSJames Y Knight :option:`--strict-whitespace` is also specified. (Note: negative 8585913ccaSJames Y Knight matches from ``CHECK-NOT`` are not affected by this option!) 8685913ccaSJames Y Knight 8785913ccaSJames Y Knight Passing this option is equivalent to inserting ``{{^ *}}`` or 8885913ccaSJames Y Knight ``{{^}}`` before, and ``{{ *$}}`` or ``{{$}}`` after every positive 8985913ccaSJames Y Knight check pattern. 9085913ccaSJames Y Knight 91f589e241SDmitri Gribenko.. option:: --strict-whitespace 928f4a8a63SDaniel Dunbar 938f4a8a63SDaniel Dunbar By default, FileCheck canonicalizes input horizontal whitespace (spaces and 948f4a8a63SDaniel Dunbar tabs) which causes it to ignore these differences (a space will match a tab). 955ea04c38SGuy Benyei The :option:`--strict-whitespace` argument disables this behavior. End-of-line 96b6bfbad2SSean Silva sequences are canonicalized to UNIX-style ``\n`` in all modes. 978f4a8a63SDaniel Dunbar 985b5b2fd2SKai Nacke.. option:: --ignore-case 995b5b2fd2SKai Nacke 1005b5b2fd2SKai Nacke By default, FileCheck uses case-sensitive matching. This option causes 1015b5b2fd2SKai Nacke FileCheck to use case-insensitive matching. 1025b5b2fd2SKai Nacke 10356ccdbbdSAlexander Kornienko.. option:: --implicit-check-not check-pattern 10456ccdbbdSAlexander Kornienko 10556ccdbbdSAlexander Kornienko Adds implicit negative checks for the specified patterns between positive 10656ccdbbdSAlexander Kornienko checks. The option allows writing stricter tests without stuffing them with 107a762a137SDan Liew ``CHECK-NOT``\ s. 10856ccdbbdSAlexander Kornienko 10956ccdbbdSAlexander Kornienko For example, "``--implicit-check-not warning:``" can be useful when testing 11056ccdbbdSAlexander Kornienko diagnostic messages from tools that don't have an option similar to ``clang 11156ccdbbdSAlexander Kornienko -verify``. With this option FileCheck will verify that input does not contain 11256ccdbbdSAlexander Kornienko warnings not covered by any ``CHECK:`` patterns. 11356ccdbbdSAlexander Kornienko 1146dda6ff0SJoel E. Denny.. option:: --dump-input <value> 1153c5d267eSJoel E. Denny 1163c5d267eSJoel E. Denny Dump input to stderr, adding annotations representing currently enabled 1176dda6ff0SJoel E. Denny diagnostics. When there are multiple occurrences of this option, the 1186dda6ff0SJoel E. Denny ``<value>`` that appears earliest in the list below has precedence. The 1196dda6ff0SJoel E. Denny default is ``fail``. 1206dda6ff0SJoel E. Denny 1216dda6ff0SJoel E. Denny * ``help`` - Explain input dump and quit 1226dda6ff0SJoel E. Denny * ``always`` - Always dump input 1236dda6ff0SJoel E. Denny * ``fail`` - Dump input on failure 1246dda6ff0SJoel E. Denny * ``never`` - Never dump input 1256dda6ff0SJoel E. Denny 1266dda6ff0SJoel E. Denny.. option:: --dump-input-context <N> 1276dda6ff0SJoel E. Denny 1286dda6ff0SJoel E. Denny In the dump requested by ``--dump-input``, print ``<N>`` input lines before 1296dda6ff0SJoel E. Denny and ``<N>`` input lines after any lines specified by ``--dump-input-filter``. 1306dda6ff0SJoel E. Denny When there are multiple occurrences of this option, the largest specified 1316dda6ff0SJoel E. Denny ``<N>`` has precedence. The default is 5. 1326dda6ff0SJoel E. Denny 1336dda6ff0SJoel E. Denny.. option:: --dump-input-filter <value> 1346dda6ff0SJoel E. Denny 1356dda6ff0SJoel E. Denny In the dump requested by ``--dump-input``, print only input lines of kind 1366dda6ff0SJoel E. Denny ``<value>`` plus any context specified by ``--dump-input-context``. When 1376dda6ff0SJoel E. Denny there are multiple occurrences of this option, the ``<value>`` that appears 1386dda6ff0SJoel E. Denny earliest in the list below has precedence. The default is ``error`` when 1396dda6ff0SJoel E. Denny ``--dump-input=fail``, and it's ``all`` when ``--dump-input=always``. 1406dda6ff0SJoel E. Denny 1416dda6ff0SJoel E. Denny * ``all`` - All input lines 1426dda6ff0SJoel E. Denny * ``annotation-full`` - Input lines with annotations 1436dda6ff0SJoel E. Denny * ``annotation`` - Input lines with starting points of annotations 1446dda6ff0SJoel E. Denny * ``error`` - Input lines with starting points of error annotations 145346dfbe2SGeorge Karpenkov 146f55e72a5SArtem Belevich.. option:: --enable-var-scope 147f55e72a5SArtem Belevich 148f55e72a5SArtem Belevich Enables scope for regex variables. 149f55e72a5SArtem Belevich 150f55e72a5SArtem Belevich Variables with names that start with ``$`` are considered global and 151f55e72a5SArtem Belevich remain set throughout the file. 152f55e72a5SArtem Belevich 153f55e72a5SArtem Belevich All other variables get undefined after each encountered ``CHECK-LABEL``. 154f55e72a5SArtem Belevich 15546e1fd61SAlexander Richardson.. option:: -D<VAR=VALUE> 15646e1fd61SAlexander Richardson 157288ed91eSThomas Preud'homme Sets a filecheck pattern variable ``VAR`` with value ``VALUE`` that can be 158288ed91eSThomas Preud'homme used in ``CHECK:`` lines. 15946e1fd61SAlexander Richardson 1608e96697cSThomas Preud'homme.. option:: -D#<FMT>,<NUMVAR>=<NUMERIC EXPRESSION> 1617b4ecdd3SThomas Preud'homme 1628e96697cSThomas Preud'homme Sets a filecheck numeric variable ``NUMVAR`` of matching format ``FMT`` to 1638e96697cSThomas Preud'homme the result of evaluating ``<NUMERIC EXPRESSION>`` that can be used in 1648e96697cSThomas Preud'homme ``CHECK:`` lines. See section 1654cd9b853SThomas Preud'homme ``FileCheck Numeric Variables and Expressions`` for details on supported 1664cd9b853SThomas Preud'homme numeric expressions. 1677b4ecdd3SThomas Preud'homme 168f589e241SDmitri Gribenko.. option:: -version 1698f4a8a63SDaniel Dunbar 1708f4a8a63SDaniel Dunbar Show the version number of this program. 1718f4a8a63SDaniel Dunbar 172dc5ba317SJoel E. Denny.. option:: -v 173dc5ba317SJoel E. Denny 1746dda6ff0SJoel E. Denny Print good directive pattern matches. However, if ``-dump-input=fail`` or 1756dda6ff0SJoel E. Denny ``-dump-input=always``, add those matches as input annotations instead. 176dc5ba317SJoel E. Denny 177dc5ba317SJoel E. Denny.. option:: -vv 178dc5ba317SJoel E. Denny 179dc5ba317SJoel E. Denny Print information helpful in diagnosing internal FileCheck issues, such as 180dc5ba317SJoel E. Denny discarded overlapping ``CHECK-DAG:`` matches, implicit EOF pattern matches, 181dc5ba317SJoel E. Denny and ``CHECK-NOT:`` patterns that do not have matches. Implies ``-v``. 1826dda6ff0SJoel E. Denny However, if ``-dump-input=fail`` or ``-dump-input=always``, just add that 183352695c3SJoel E. Denny information as input annotations instead. 184dc5ba317SJoel E. Denny 185bcf5b441SJoel E. Denny.. option:: --allow-deprecated-dag-overlap 186bcf5b441SJoel E. Denny 187bcf5b441SJoel E. Denny Enable overlapping among matches in a group of consecutive ``CHECK-DAG:`` 188bcf5b441SJoel E. Denny directives. This option is deprecated and is only provided for convenience 189bcf5b441SJoel E. Denny as old tests are migrated to the new non-overlapping ``CHECK-DAG:`` 190bcf5b441SJoel E. Denny implementation. 191bcf5b441SJoel E. Denny 192d6c00edfSSameer Arora.. option:: --allow-empty 193d6c00edfSSameer Arora 194d6c00edfSSameer Arora Allow checking empty input. By default, empty input is rejected. 195d6c00edfSSameer Arora 1963e66509fSJoel E. Denny.. option:: --color 1973e66509fSJoel E. Denny 1983e66509fSJoel E. Denny Use colors in output (autodetected by default). 1993e66509fSJoel E. Denny 2008f4a8a63SDaniel DunbarEXIT STATUS 2018f4a8a63SDaniel Dunbar----------- 2028f4a8a63SDaniel Dunbar 203f589e241SDmitri GribenkoIf :program:`FileCheck` verifies that the file matches the expected contents, 204f589e241SDmitri Gribenkoit exits with 0. Otherwise, if not, or if an error occurs, it will exit with a 205f589e241SDmitri Gribenkonon-zero value. 2068f4a8a63SDaniel Dunbar 2078f4a8a63SDaniel DunbarTUTORIAL 2088f4a8a63SDaniel Dunbar-------- 2098f4a8a63SDaniel Dunbar 2108f4a8a63SDaniel DunbarFileCheck is typically used from LLVM regression tests, being invoked on the RUN 2118f4a8a63SDaniel Dunbarline of the test. A simple example of using FileCheck from a RUN line looks 2128f4a8a63SDaniel Dunbarlike this: 2138f4a8a63SDaniel Dunbar 214a99fa5b0SDmitri Gribenko.. code-block:: llvm 2158f4a8a63SDaniel Dunbar 2168f4a8a63SDaniel Dunbar ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s 2178f4a8a63SDaniel Dunbar 218a72e9f01SDmitri GribenkoThis syntax says to pipe the current file ("``%s``") into ``llvm-as``, pipe 219a72e9f01SDmitri Gribenkothat into ``llc``, then pipe the output of ``llc`` into ``FileCheck``. This 220a72e9f01SDmitri Gribenkomeans that FileCheck will be verifying its standard input (the llc output) 221a72e9f01SDmitri Gribenkoagainst the filename argument specified (the original ``.ll`` file specified by 222a72e9f01SDmitri Gribenko"``%s``"). To see how this works, let's look at the rest of the ``.ll`` file 223a72e9f01SDmitri Gribenko(after the RUN line): 2248f4a8a63SDaniel Dunbar 225a99fa5b0SDmitri Gribenko.. code-block:: llvm 2268f4a8a63SDaniel Dunbar 2278f4a8a63SDaniel Dunbar define void @sub1(i32* %p, i32 %v) { 2288f4a8a63SDaniel Dunbar entry: 2298f4a8a63SDaniel Dunbar ; CHECK: sub1: 2308f4a8a63SDaniel Dunbar ; CHECK: subl 2318f4a8a63SDaniel Dunbar %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v) 2328f4a8a63SDaniel Dunbar ret void 2338f4a8a63SDaniel Dunbar } 2348f4a8a63SDaniel Dunbar 2358f4a8a63SDaniel Dunbar define void @inc4(i64* %p) { 2368f4a8a63SDaniel Dunbar entry: 2378f4a8a63SDaniel Dunbar ; CHECK: inc4: 2388f4a8a63SDaniel Dunbar ; CHECK: incq 2398f4a8a63SDaniel Dunbar %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1) 2408f4a8a63SDaniel Dunbar ret void 2418f4a8a63SDaniel Dunbar } 2428f4a8a63SDaniel Dunbar 243a72e9f01SDmitri GribenkoHere you can see some "``CHECK:``" lines specified in comments. Now you can 244a72e9f01SDmitri Gribenkosee how the file is piped into ``llvm-as``, then ``llc``, and the machine code 245a72e9f01SDmitri Gribenkooutput is what we are verifying. FileCheck checks the machine code output to 246a72e9f01SDmitri Gribenkoverify that it matches what the "``CHECK:``" lines specify. 2478f4a8a63SDaniel Dunbar 248a72e9f01SDmitri GribenkoThe syntax of the "``CHECK:``" lines is very simple: they are fixed strings that 2498f4a8a63SDaniel Dunbarmust occur in order. FileCheck defaults to ignoring horizontal whitespace 2508f4a8a63SDaniel Dunbardifferences (e.g. a space is allowed to match a tab) but otherwise, the contents 251a72e9f01SDmitri Gribenkoof the "``CHECK:``" line is required to match some thing in the test file exactly. 2528f4a8a63SDaniel Dunbar 2538f4a8a63SDaniel DunbarOne nice thing about FileCheck (compared to grep) is that it allows merging 2548f4a8a63SDaniel Dunbartest cases together into logical groups. For example, because the test above 255a72e9f01SDmitri Gribenkois checking for the "``sub1:``" and "``inc4:``" labels, it will not match 256a72e9f01SDmitri Gribenkounless there is a "``subl``" in between those labels. If it existed somewhere 257a72e9f01SDmitri Gribenkoelse in the file, that would not count: "``grep subl``" matches if "``subl``" 258a72e9f01SDmitri Gribenkoexists anywhere in the file. 2598f4a8a63SDaniel Dunbar 2608f4a8a63SDaniel DunbarThe FileCheck -check-prefix option 2618f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2628f4a8a63SDaniel Dunbar 263124f2593SRenato GolinThe FileCheck `-check-prefix` option allows multiple test 264f589e241SDmitri Gribenkoconfigurations to be driven from one `.ll` file. This is useful in many 265f589e241SDmitri Gribenkocircumstances, for example, testing different architectural variants with 266f589e241SDmitri Gribenko:program:`llc`. Here's a simple example: 2678f4a8a63SDaniel Dunbar 268a99fa5b0SDmitri Gribenko.. code-block:: llvm 2698f4a8a63SDaniel Dunbar 2708f4a8a63SDaniel Dunbar ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \ 27119408a76SDmitri Gribenko ; RUN: | FileCheck %s -check-prefix=X32 2728f4a8a63SDaniel Dunbar ; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \ 27319408a76SDmitri Gribenko ; RUN: | FileCheck %s -check-prefix=X64 2748f4a8a63SDaniel Dunbar 2758f4a8a63SDaniel Dunbar define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind { 2768f4a8a63SDaniel Dunbar %tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1 2778f4a8a63SDaniel Dunbar ret <4 x i32> %tmp1 2788f4a8a63SDaniel Dunbar ; X32: pinsrd_1: 2798f4a8a63SDaniel Dunbar ; X32: pinsrd $1, 4(%esp), %xmm0 2808f4a8a63SDaniel Dunbar 2818f4a8a63SDaniel Dunbar ; X64: pinsrd_1: 2828f4a8a63SDaniel Dunbar ; X64: pinsrd $1, %edi, %xmm0 2838f4a8a63SDaniel Dunbar } 2848f4a8a63SDaniel Dunbar 2858f4a8a63SDaniel DunbarIn this case, we're testing that we get the expected code generation with 2868f4a8a63SDaniel Dunbarboth 32-bit and 64-bit code generation. 2878f4a8a63SDaniel Dunbar 288a1fd1882SJoel E. DennyThe "COM:" directive 289a1fd1882SJoel E. Denny~~~~~~~~~~~~~~~~~~~~ 290a1fd1882SJoel E. Denny 291a1fd1882SJoel E. DennySometimes you want to disable a FileCheck directive without removing it 292a1fd1882SJoel E. Dennyentirely, or you want to write comments that mention a directive by name. The 293a1fd1882SJoel E. Denny"``COM:``" directive makes it easy to do this. For example, you might have: 294a1fd1882SJoel E. Denny 295a1fd1882SJoel E. Denny.. code-block:: llvm 296a1fd1882SJoel E. Denny 297a1fd1882SJoel E. Denny ; X32: pinsrd_1: 298a1fd1882SJoel E. Denny ; X32: pinsrd $1, 4(%esp), %xmm0 299a1fd1882SJoel E. Denny 300a1fd1882SJoel E. Denny ; COM: FIXME: X64 isn't working correctly yet for this part of codegen, but 301a1fd1882SJoel E. Denny ; COM: X64 will have something similar to X32: 302a1fd1882SJoel E. Denny ; COM: 303a1fd1882SJoel E. Denny ; COM: X64: pinsrd_1: 304a1fd1882SJoel E. Denny ; COM: X64: pinsrd $1, %edi, %xmm0 305a1fd1882SJoel E. Denny 306a1fd1882SJoel E. DennyWithout "``COM:``", you would need to use some combination of rewording and 307a1fd1882SJoel E. Dennydirective syntax mangling to prevent FileCheck from recognizing the commented 308a1fd1882SJoel E. Dennyoccurrences of "``X32:``" and "``X64:``" above as directives. Moreover, 309a1fd1882SJoel E. DennyFileCheck diagnostics have been proposed that might complain about the above 310a1fd1882SJoel E. Dennyoccurrences of "``X64``" that don't have the trailing "``:``" because they look 311a1fd1882SJoel E. Dennylike directive typos. Dodging all these problems can be tedious for a test 312a1fd1882SJoel E. Dennyauthor, and directive syntax mangling can make the purpose of test code unclear. 313a1fd1882SJoel E. Denny"``COM:``" avoids all these problems. 314a1fd1882SJoel E. Denny 315a1fd1882SJoel E. DennyA few important usage notes: 316a1fd1882SJoel E. Denny 317a1fd1882SJoel E. Denny* "``COM:``" within another directive's pattern does *not* comment out the 318a1fd1882SJoel E. Denny remainder of the pattern. For example: 319a1fd1882SJoel E. Denny 320a1fd1882SJoel E. Denny .. code-block:: llvm 321a1fd1882SJoel E. Denny 322a1fd1882SJoel E. Denny ; X32: pinsrd $1, 4(%esp), %xmm0 COM: This is part of the X32 pattern! 323a1fd1882SJoel E. Denny 324a1fd1882SJoel E. Denny If you need to temporarily comment out part of a directive's pattern, move it 325a1fd1882SJoel E. Denny to another line. The reason is that FileCheck parses "``COM:``" in the same 326a1fd1882SJoel E. Denny manner as any other directive: only the first directive on the line is 327a1fd1882SJoel E. Denny recognized as a directive. 328a1fd1882SJoel E. Denny 329a1fd1882SJoel E. Denny* For the sake of LIT, FileCheck treats "``RUN:``" just like "``COM:``". If this 330a1fd1882SJoel E. Denny is not suitable for your test environment, see :option:`--comment-prefixes`. 331a1fd1882SJoel E. Denny 332a1fd1882SJoel E. Denny* FileCheck does not recognize "``COM``", "``RUN``", or any user-defined comment 333a1fd1882SJoel E. Denny prefix as a comment directive if it's combined with one of the usual check 334a1fd1882SJoel E. Denny directive suffixes, such as "``-NEXT:``" or "``-NOT:``", discussed below. 335a1fd1882SJoel E. Denny FileCheck treats such a combination as plain text instead. If it needs to act 336a1fd1882SJoel E. Denny as a comment directive for your test environment, define it as such with 337a1fd1882SJoel E. Denny :option:`--comment-prefixes`. 338a1fd1882SJoel E. Denny 3398f4a8a63SDaniel DunbarThe "CHECK-NEXT:" directive 3408f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3418f4a8a63SDaniel Dunbar 3428f4a8a63SDaniel DunbarSometimes you want to match lines and would like to verify that matches 3438f4a8a63SDaniel Dunbarhappen on exactly consecutive lines with no other lines in between them. In 344a72e9f01SDmitri Gribenkothis case, you can use "``CHECK:``" and "``CHECK-NEXT:``" directives to specify 345a72e9f01SDmitri Gribenkothis. If you specified a custom check prefix, just use "``<PREFIX>-NEXT:``". 346a72e9f01SDmitri GribenkoFor example, something like this works as you'd expect: 3478f4a8a63SDaniel Dunbar 348a99fa5b0SDmitri Gribenko.. code-block:: llvm 3498f4a8a63SDaniel Dunbar 35019408a76SDmitri Gribenko define void @t2(<2 x double>* %r, <2 x double>* %A, double %B) { 35119408a76SDmitri Gribenko %tmp3 = load <2 x double>* %A, align 16 35219408a76SDmitri Gribenko %tmp7 = insertelement <2 x double> undef, double %B, i32 0 35319408a76SDmitri Gribenko %tmp9 = shufflevector <2 x double> %tmp3, 35419408a76SDmitri Gribenko <2 x double> %tmp7, 35519408a76SDmitri Gribenko <2 x i32> < i32 0, i32 2 > 35619408a76SDmitri Gribenko store <2 x double> %tmp9, <2 x double>* %r, align 16 3578f4a8a63SDaniel Dunbar ret void 3588f4a8a63SDaniel Dunbar 3598f4a8a63SDaniel Dunbar ; CHECK: t2: 3608f4a8a63SDaniel Dunbar ; CHECK: movl 8(%esp), %eax 3618f4a8a63SDaniel Dunbar ; CHECK-NEXT: movapd (%eax), %xmm0 3628f4a8a63SDaniel Dunbar ; CHECK-NEXT: movhpd 12(%esp), %xmm0 3638f4a8a63SDaniel Dunbar ; CHECK-NEXT: movl 4(%esp), %eax 3648f4a8a63SDaniel Dunbar ; CHECK-NEXT: movapd %xmm0, (%eax) 3658f4a8a63SDaniel Dunbar ; CHECK-NEXT: ret 3668f4a8a63SDaniel Dunbar } 3678f4a8a63SDaniel Dunbar 368a72e9f01SDmitri Gribenko"``CHECK-NEXT:``" directives reject the input unless there is exactly one 3692fef6b6aSEli Benderskynewline between it and the previous directive. A "``CHECK-NEXT:``" cannot be 370a72e9f01SDmitri Gribenkothe first directive in a file. 3718f4a8a63SDaniel Dunbar 372cffbbe92SDuncan P. N. Exon SmithThe "CHECK-SAME:" directive 373cffbbe92SDuncan P. N. Exon Smith~~~~~~~~~~~~~~~~~~~~~~~~~~~ 374cffbbe92SDuncan P. N. Exon Smith 375cffbbe92SDuncan P. N. Exon SmithSometimes you want to match lines and would like to verify that matches happen 376cffbbe92SDuncan P. N. Exon Smithon the same line as the previous match. In this case, you can use "``CHECK:``" 377cffbbe92SDuncan P. N. Exon Smithand "``CHECK-SAME:``" directives to specify this. If you specified a custom 378cffbbe92SDuncan P. N. Exon Smithcheck prefix, just use "``<PREFIX>-SAME:``". 379cffbbe92SDuncan P. N. Exon Smith 380cffbbe92SDuncan P. N. Exon Smith"``CHECK-SAME:``" is particularly powerful in conjunction with "``CHECK-NOT:``" 381cffbbe92SDuncan P. N. Exon Smith(described below). 382cffbbe92SDuncan P. N. Exon Smith 383cffbbe92SDuncan P. N. Exon SmithFor example, the following works like you'd expect: 384cffbbe92SDuncan P. N. Exon Smith 385cffbbe92SDuncan P. N. Exon Smith.. code-block:: llvm 386cffbbe92SDuncan P. N. Exon Smith 387a9308c49SDuncan P. N. Exon Smith !0 = !DILocation(line: 5, scope: !1, inlinedAt: !2) 388cffbbe92SDuncan P. N. Exon Smith 389a9308c49SDuncan P. N. Exon Smith ; CHECK: !DILocation(line: 5, 390cffbbe92SDuncan P. N. Exon Smith ; CHECK-NOT: column: 391cffbbe92SDuncan P. N. Exon Smith ; CHECK-SAME: scope: ![[SCOPE:[0-9]+]] 392cffbbe92SDuncan P. N. Exon Smith 393cffbbe92SDuncan P. N. Exon Smith"``CHECK-SAME:``" directives reject the input if there are any newlines between 3944963ca46SJordan Rupprechtit and the previous directive. 3954963ca46SJordan Rupprecht 3964963ca46SJordan Rupprecht"``CHECK-SAME:``" is also useful to avoid writing matchers for irrelevant 3974963ca46SJordan Rupprechtfields. For example, suppose you're writing a test which parses a tool that 3984963ca46SJordan Rupprechtgenerates output like this: 3994963ca46SJordan Rupprecht 4004963ca46SJordan Rupprecht.. code-block:: text 4014963ca46SJordan Rupprecht 4024963ca46SJordan Rupprecht Name: foo 4034963ca46SJordan Rupprecht Field1: ... 4044963ca46SJordan Rupprecht Field2: ... 4054963ca46SJordan Rupprecht Field3: ... 4064963ca46SJordan Rupprecht Value: 1 4074963ca46SJordan Rupprecht 4084963ca46SJordan Rupprecht Name: bar 4094963ca46SJordan Rupprecht Field1: ... 4104963ca46SJordan Rupprecht Field2: ... 4114963ca46SJordan Rupprecht Field3: ... 4124963ca46SJordan Rupprecht Value: 2 4134963ca46SJordan Rupprecht 4144963ca46SJordan Rupprecht Name: baz 4154963ca46SJordan Rupprecht Field1: ... 4164963ca46SJordan Rupprecht Field2: ... 4174963ca46SJordan Rupprecht Field3: ... 4184963ca46SJordan Rupprecht Value: 1 4194963ca46SJordan Rupprecht 4204963ca46SJordan RupprechtTo write a test that verifies ``foo`` has the value ``1``, you might first 4214963ca46SJordan Rupprechtwrite this: 4224963ca46SJordan Rupprecht 4234963ca46SJordan Rupprecht.. code-block:: text 4244963ca46SJordan Rupprecht 4254963ca46SJordan Rupprecht CHECK: Name: foo 4264963ca46SJordan Rupprecht CHECK: Value: 1{{$}} 4274963ca46SJordan Rupprecht 4284963ca46SJordan RupprechtHowever, this would be a bad test: if the value for ``foo`` changes, the test 4294963ca46SJordan Rupprechtwould still pass because the "``CHECK: Value: 1``" line would match the value 4304963ca46SJordan Rupprechtfrom ``baz``. To fix this, you could add ``CHECK-NEXT`` matchers for every 4314963ca46SJordan Rupprecht``FieldN:`` line, but that would be verbose, and need to be updated when 432c28473feSSylvestre Ledru``Field4`` is added. A more succinct way to write the test using the 4334963ca46SJordan Rupprecht"``CHECK-SAME:``" matcher would be as follows: 4344963ca46SJordan Rupprecht 4354963ca46SJordan Rupprecht.. code-block:: text 4364963ca46SJordan Rupprecht 4374963ca46SJordan Rupprecht CHECK: Name: foo 4384963ca46SJordan Rupprecht CHECK: Value: 4394963ca46SJordan Rupprecht CHECK-SAME: {{ 1$}} 4404963ca46SJordan Rupprecht 44170b39506SYang ZhihuiThis verifies that the *next* time "``Value:``" appears in the output, it has 4424963ca46SJordan Rupprechtthe value ``1``. 4434963ca46SJordan Rupprecht 4444963ca46SJordan RupprechtNote: a "``CHECK-SAME:``" cannot be the first directive in a file. 445cffbbe92SDuncan P. N. Exon Smith 4465507f668SJames HendersonThe "CHECK-EMPTY:" directive 447c307b003SJames Henderson~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4485507f668SJames Henderson 4495507f668SJames HendersonIf you need to check that the next line has nothing on it, not even whitespace, 4505507f668SJames Hendersonyou can use the "``CHECK-EMPTY:``" directive. 4515507f668SJames Henderson 4525507f668SJames Henderson.. code-block:: llvm 4535507f668SJames Henderson 454ef705b7dSChandler Carruth declare void @foo() 4555507f668SJames Henderson 456ef705b7dSChandler Carruth declare void @bar() 4575507f668SJames Henderson ; CHECK: foo 4585507f668SJames Henderson ; CHECK-EMPTY: 4595507f668SJames Henderson ; CHECK-NEXT: bar 4605507f668SJames Henderson 4615507f668SJames HendersonJust like "``CHECK-NEXT:``" the directive will fail if there is more than one 4625507f668SJames Hendersonnewline before it finds the next blank line, and it cannot be the first 4635507f668SJames Hendersondirective in a file. 4645507f668SJames Henderson 4658f4a8a63SDaniel DunbarThe "CHECK-NOT:" directive 4668f4a8a63SDaniel Dunbar~~~~~~~~~~~~~~~~~~~~~~~~~~ 4678f4a8a63SDaniel Dunbar 468a72e9f01SDmitri GribenkoThe "``CHECK-NOT:``" directive is used to verify that a string doesn't occur 4698f4a8a63SDaniel Dunbarbetween two matches (or before the first match, or after the last match). For 4708f4a8a63SDaniel Dunbarexample, to verify that a load is removed by a transformation, a test like this 4718f4a8a63SDaniel Dunbarcan be used: 4728f4a8a63SDaniel Dunbar 473a99fa5b0SDmitri Gribenko.. code-block:: llvm 4748f4a8a63SDaniel Dunbar 4758f4a8a63SDaniel Dunbar define i8 @coerce_offset0(i32 %V, i32* %P) { 4768f4a8a63SDaniel Dunbar store i32 %V, i32* %P 4778f4a8a63SDaniel Dunbar 4788f4a8a63SDaniel Dunbar %P2 = bitcast i32* %P to i8* 4798f4a8a63SDaniel Dunbar %P3 = getelementptr i8* %P2, i32 2 4808f4a8a63SDaniel Dunbar 4818f4a8a63SDaniel Dunbar %A = load i8* %P3 4828f4a8a63SDaniel Dunbar ret i8 %A 4838f4a8a63SDaniel Dunbar ; CHECK: @coerce_offset0 4848f4a8a63SDaniel Dunbar ; CHECK-NOT: load 4858f4a8a63SDaniel Dunbar ; CHECK: ret i8 4868f4a8a63SDaniel Dunbar } 4878f4a8a63SDaniel Dunbar 4886c9e19b5SFedor SergeevThe "CHECK-COUNT:" directive 4895bf0c155SFedor Sergeev~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4906c9e19b5SFedor Sergeev 4916c9e19b5SFedor SergeevIf you need to match multiple lines with the same pattern over and over again 4926c9e19b5SFedor Sergeevyou can repeat a plain ``CHECK:`` as many times as needed. If that looks too 4936c9e19b5SFedor Sergeevboring you can instead use a counted check "``CHECK-COUNT-<num>:``", where 4946c9e19b5SFedor Sergeev``<num>`` is a positive decimal number. It will match the pattern exactly 4956c9e19b5SFedor Sergeev``<num>`` times, no more and no less. If you specified a custom check prefix, 4966c9e19b5SFedor Sergeevjust use "``<PREFIX>-COUNT-<num>:``" for the same effect. 4976c9e19b5SFedor SergeevHere is a simple example: 4986c9e19b5SFedor Sergeev 4998b078126SFedor Sergeev.. code-block:: text 5006c9e19b5SFedor Sergeev 5016c9e19b5SFedor Sergeev Loop at depth 1 5026c9e19b5SFedor Sergeev Loop at depth 1 5036c9e19b5SFedor Sergeev Loop at depth 1 5046c9e19b5SFedor Sergeev Loop at depth 1 5056c9e19b5SFedor Sergeev Loop at depth 2 5066c9e19b5SFedor Sergeev Loop at depth 3 5076c9e19b5SFedor Sergeev 5086c9e19b5SFedor Sergeev ; CHECK-COUNT-6: Loop at depth {{[0-9]+}} 5096c9e19b5SFedor Sergeev ; CHECK-NOT: Loop at depth {{[0-9]+}} 5106c9e19b5SFedor Sergeev 51191a1b2c9SMichael LiaoThe "CHECK-DAG:" directive 51291a1b2c9SMichael Liao~~~~~~~~~~~~~~~~~~~~~~~~~~ 51391a1b2c9SMichael Liao 51491a1b2c9SMichael LiaoIf it's necessary to match strings that don't occur in a strictly sequential 51591a1b2c9SMichael Liaoorder, "``CHECK-DAG:``" could be used to verify them between two matches (or 51691a1b2c9SMichael Liaobefore the first match, or after the last match). For example, clang emits 51791a1b2c9SMichael Liaovtable globals in reverse order. Using ``CHECK-DAG:``, we can keep the checks 51891a1b2c9SMichael Liaoin the natural order: 51991a1b2c9SMichael Liao 52091a1b2c9SMichael Liao.. code-block:: c++ 52191a1b2c9SMichael Liao 52291a1b2c9SMichael Liao // RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s 52391a1b2c9SMichael Liao 52491a1b2c9SMichael Liao struct Foo { virtual void method(); }; 52591a1b2c9SMichael Liao Foo f; // emit vtable 52691a1b2c9SMichael Liao // CHECK-DAG: @_ZTV3Foo = 52791a1b2c9SMichael Liao 52891a1b2c9SMichael Liao struct Bar { virtual void method(); }; 52991a1b2c9SMichael Liao Bar b; 53091a1b2c9SMichael Liao // CHECK-DAG: @_ZTV3Bar = 53191a1b2c9SMichael Liao 53258ab84a8SRenato Golin``CHECK-NOT:`` directives could be mixed with ``CHECK-DAG:`` directives to 53358ab84a8SRenato Golinexclude strings between the surrounding ``CHECK-DAG:`` directives. As a result, 53458ab84a8SRenato Golinthe surrounding ``CHECK-DAG:`` directives cannot be reordered, i.e. all 53558ab84a8SRenato Golinoccurrences matching ``CHECK-DAG:`` before ``CHECK-NOT:`` must not fall behind 53658ab84a8SRenato Golinoccurrences matching ``CHECK-DAG:`` after ``CHECK-NOT:``. For example, 53758ab84a8SRenato Golin 53858ab84a8SRenato Golin.. code-block:: llvm 53958ab84a8SRenato Golin 54058ab84a8SRenato Golin ; CHECK-DAG: BEFORE 54158ab84a8SRenato Golin ; CHECK-NOT: NOT 54258ab84a8SRenato Golin ; CHECK-DAG: AFTER 54358ab84a8SRenato Golin 54458ab84a8SRenato GolinThis case will reject input strings where ``BEFORE`` occurs after ``AFTER``. 54591a1b2c9SMichael Liao 54691a1b2c9SMichael LiaoWith captured variables, ``CHECK-DAG:`` is able to match valid topological 54791a1b2c9SMichael Liaoorderings of a DAG with edges from the definition of a variable to its use. 54891a1b2c9SMichael LiaoIt's useful, e.g., when your test cases need to match different output 54991a1b2c9SMichael Liaosequences from the instruction scheduler. For example, 55091a1b2c9SMichael Liao 55191a1b2c9SMichael Liao.. code-block:: llvm 55291a1b2c9SMichael Liao 55391a1b2c9SMichael Liao ; CHECK-DAG: add [[REG1:r[0-9]+]], r1, r2 55491a1b2c9SMichael Liao ; CHECK-DAG: add [[REG2:r[0-9]+]], r3, r4 55591a1b2c9SMichael Liao ; CHECK: mul r5, [[REG1]], [[REG2]] 55691a1b2c9SMichael Liao 55791a1b2c9SMichael LiaoIn this case, any order of that two ``add`` instructions will be allowed. 55891a1b2c9SMichael Liao 55958ab84a8SRenato GolinIf you are defining `and` using variables in the same ``CHECK-DAG:`` block, 56058ab84a8SRenato Golinbe aware that the definition rule can match `after` its use. 56158ab84a8SRenato Golin 56258ab84a8SRenato GolinSo, for instance, the code below will pass: 56391a1b2c9SMichael Liao 564124f2593SRenato Golin.. code-block:: text 56591a1b2c9SMichael Liao 56658ab84a8SRenato Golin ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0] 56758ab84a8SRenato Golin ; CHECK-DAG: vmov.32 [[REG2]][1] 56858ab84a8SRenato Golin vmov.32 d0[1] 56958ab84a8SRenato Golin vmov.32 d0[0] 57091a1b2c9SMichael Liao 57158ab84a8SRenato GolinWhile this other code, will not: 57258ab84a8SRenato Golin 573124f2593SRenato Golin.. code-block:: text 57458ab84a8SRenato Golin 57558ab84a8SRenato Golin ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0] 57658ab84a8SRenato Golin ; CHECK-DAG: vmov.32 [[REG2]][1] 57758ab84a8SRenato Golin vmov.32 d1[1] 57858ab84a8SRenato Golin vmov.32 d0[0] 57958ab84a8SRenato Golin 58058ab84a8SRenato GolinWhile this can be very useful, it's also dangerous, because in the case of 58158ab84a8SRenato Golinregister sequence, you must have a strong order (read before write, copy before 58258ab84a8SRenato Golinuse, etc). If the definition your test is looking for doesn't match (because 58358ab84a8SRenato Golinof a bug in the compiler), it may match further away from the use, and mask 58458ab84a8SRenato Golinreal bugs away. 58558ab84a8SRenato Golin 58658ab84a8SRenato GolinIn those cases, to enforce the order, use a non-DAG directive between DAG-blocks. 58791a1b2c9SMichael Liao 588bcf5b441SJoel E. DennyA ``CHECK-DAG:`` directive skips matches that overlap the matches of any 589bcf5b441SJoel E. Dennypreceding ``CHECK-DAG:`` directives in the same ``CHECK-DAG:`` block. Not only 590bcf5b441SJoel E. Dennyis this non-overlapping behavior consistent with other directives, but it's 591bcf5b441SJoel E. Dennyalso necessary to handle sets of non-unique strings or patterns. For example, 592bcf5b441SJoel E. Dennythe following directives look for unordered log entries for two tasks in a 593bcf5b441SJoel E. Dennyparallel program, such as the OpenMP runtime: 594bcf5b441SJoel E. Denny 595bcf5b441SJoel E. Denny.. code-block:: text 596bcf5b441SJoel E. Denny 597bcf5b441SJoel E. Denny // CHECK-DAG: [[THREAD_ID:[0-9]+]]: task_begin 598bcf5b441SJoel E. Denny // CHECK-DAG: [[THREAD_ID]]: task_end 599bcf5b441SJoel E. Denny // 600bcf5b441SJoel E. Denny // CHECK-DAG: [[THREAD_ID:[0-9]+]]: task_begin 601bcf5b441SJoel E. Denny // CHECK-DAG: [[THREAD_ID]]: task_end 602bcf5b441SJoel E. Denny 603bcf5b441SJoel E. DennyThe second pair of directives is guaranteed not to match the same log entries 604bcf5b441SJoel E. Dennyas the first pair even though the patterns are identical and even if the text 605bcf5b441SJoel E. Dennyof the log entries is identical because the thread ID manages to be reused. 606bcf5b441SJoel E. Denny 607f8bd2e5bSStephen LinThe "CHECK-LABEL:" directive 608c02da467SBill Wendling~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 609f8bd2e5bSStephen Lin 610f8bd2e5bSStephen LinSometimes in a file containing multiple tests divided into logical blocks, one 611f8bd2e5bSStephen Linor more ``CHECK:`` directives may inadvertently succeed by matching lines in a 612f8bd2e5bSStephen Linlater block. While an error will usually eventually be generated, the check 613f8bd2e5bSStephen Linflagged as causing the error may not actually bear any relationship to the 614f8bd2e5bSStephen Linactual source of the problem. 615f8bd2e5bSStephen Lin 616f8bd2e5bSStephen LinIn order to produce better error messages in these cases, the "``CHECK-LABEL:``" 617f8bd2e5bSStephen Lindirective can be used. It is treated identically to a normal ``CHECK`` 618b946407cSStephen Lindirective except that FileCheck makes an additional assumption that a line 619b946407cSStephen Linmatched by the directive cannot also be matched by any other check present in 620b946407cSStephen Lin``match-filename``; this is intended to be used for lines containing labels or 621b946407cSStephen Linother unique identifiers. Conceptually, the presence of ``CHECK-LABEL`` divides 622b946407cSStephen Linthe input stream into separate blocks, each of which is processed independently, 623b946407cSStephen Linpreventing a ``CHECK:`` directive in one block matching a line in another block. 624f55e72a5SArtem BelevichIf ``--enable-var-scope`` is in effect, all local variables are cleared at the 625f55e72a5SArtem Belevichbeginning of the block. 626f55e72a5SArtem Belevich 627b946407cSStephen LinFor example, 628f8bd2e5bSStephen Lin 629f8bd2e5bSStephen Lin.. code-block:: llvm 630f8bd2e5bSStephen Lin 631f8bd2e5bSStephen Lin define %struct.C* @C_ctor_base(%struct.C* %this, i32 %x) { 632f8bd2e5bSStephen Lin entry: 633f8bd2e5bSStephen Lin ; CHECK-LABEL: C_ctor_base: 634f8bd2e5bSStephen Lin ; CHECK: mov [[SAVETHIS:r[0-9]+]], r0 635f8bd2e5bSStephen Lin ; CHECK: bl A_ctor_base 636f8bd2e5bSStephen Lin ; CHECK: mov r0, [[SAVETHIS]] 637f8bd2e5bSStephen Lin %0 = bitcast %struct.C* %this to %struct.A* 638f8bd2e5bSStephen Lin %call = tail call %struct.A* @A_ctor_base(%struct.A* %0) 639f8bd2e5bSStephen Lin %1 = bitcast %struct.C* %this to %struct.B* 640f8bd2e5bSStephen Lin %call2 = tail call %struct.B* @B_ctor_base(%struct.B* %1, i32 %x) 641f8bd2e5bSStephen Lin ret %struct.C* %this 642f8bd2e5bSStephen Lin } 643f8bd2e5bSStephen Lin 644f8bd2e5bSStephen Lin define %struct.D* @D_ctor_base(%struct.D* %this, i32 %x) { 645f8bd2e5bSStephen Lin entry: 646f8bd2e5bSStephen Lin ; CHECK-LABEL: D_ctor_base: 647f8bd2e5bSStephen Lin 648f8bd2e5bSStephen LinThe use of ``CHECK-LABEL:`` directives in this case ensures that the three 649f8bd2e5bSStephen Lin``CHECK:`` directives only accept lines corresponding to the body of the 650f8bd2e5bSStephen Lin``@C_ctor_base`` function, even if the patterns match lines found later in 651b946407cSStephen Linthe file. Furthermore, if one of these three ``CHECK:`` directives fail, 652b946407cSStephen LinFileCheck will recover by continuing to the next block, allowing multiple test 653b946407cSStephen Linfailures to be detected in a single invocation. 654f8bd2e5bSStephen Lin 655f8bd2e5bSStephen LinThere is no requirement that ``CHECK-LABEL:`` directives contain strings that 656f8bd2e5bSStephen Lincorrespond to actual syntactic labels in a source or output language: they must 657f8bd2e5bSStephen Linsimply uniquely match a single line in the file being verified. 658f8bd2e5bSStephen Lin 659f8bd2e5bSStephen Lin``CHECK-LABEL:`` directives cannot contain variable definitions or uses. 660f8bd2e5bSStephen Lin 66144f399ccSJacques PienaarDirective modifiers 66244f399ccSJacques Pienaar~~~~~~~~~~~~~~~~~~~ 66344f399ccSJacques Pienaar 66444f399ccSJacques PienaarA directive modifier can be append to a directive by following the directive 66544f399ccSJacques Pienaarwith ``{<modifier>}`` where the only supported value for ``<modifier>`` is 66644f399ccSJacques Pienaar``LITERAL``. 66744f399ccSJacques Pienaar 66844f399ccSJacques PienaarThe ``LITERAL`` directive modifier can be used to perform a literal match. The 66944f399ccSJacques Pienaarmodifier results in the directive not recognizing any syntax to perform regex 67044f399ccSJacques Pienaarmatching, variable capture or any substitutions. This is useful when the text 67144f399ccSJacques Pienaarto match would require excessive escaping otherwise. For example, the 67244f399ccSJacques Pienaarfollowing will perform literal matches rather than considering these as 67344f399ccSJacques Pienaarregular expressions: 67444f399ccSJacques Pienaar 67544f399ccSJacques Pienaar.. code-block:: text 67644f399ccSJacques Pienaar 67744f399ccSJacques Pienaar Input: [[[10, 20]], [[30, 40]]] 67844f399ccSJacques Pienaar Output %r10: [[10, 20]] 67944f399ccSJacques Pienaar Output %r10: [[30, 40]] 68044f399ccSJacques Pienaar 68144f399ccSJacques Pienaar ; CHECK{LITERAL}: [[[10, 20]], [[30, 40]]] 68244f399ccSJacques Pienaar ; CHECK-DAG{LITERAL}: [[30, 40]] 68344f399ccSJacques Pienaar ; CHECK-DAG{LITERAL}: [[10, 20]] 68444f399ccSJacques Pienaar 6851a944d27SThomas Preud'hommeFileCheck Regex Matching Syntax 6861a944d27SThomas Preud'homme~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 6878f4a8a63SDaniel Dunbar 688282b3d3fSPaul RobinsonAll FileCheck directives take a pattern to match. 689a72e9f01SDmitri GribenkoFor most uses of FileCheck, fixed string matching is perfectly sufficient. For 690a72e9f01SDmitri Gribenkosome things, a more flexible form of matching is desired. To support this, 691a72e9f01SDmitri GribenkoFileCheck allows you to specify regular expressions in matching strings, 6929a26a7ecSSjoerd Meijersurrounded by double braces: ``{{yourregex}}``. FileCheck implements a POSIX 6939a26a7ecSSjoerd Meijerregular expression matcher; it supports Extended POSIX regular expressions 6949a26a7ecSSjoerd Meijer(ERE). Because we want to use fixed string matching for a majority of what we 6959a26a7ecSSjoerd Meijerdo, FileCheck has been designed to support mixing and matching fixed string 6969a26a7ecSSjoerd Meijermatching with regular expressions. This allows you to write things like this: 6978f4a8a63SDaniel Dunbar 698a99fa5b0SDmitri Gribenko.. code-block:: llvm 6998f4a8a63SDaniel Dunbar 7008f4a8a63SDaniel Dunbar ; CHECK: movhpd {{[0-9]+}}(%esp), {{%xmm[0-7]}} 7018f4a8a63SDaniel Dunbar 7028f4a8a63SDaniel DunbarIn this case, any offset from the ESP register will be allowed, and any xmm 7038f4a8a63SDaniel Dunbarregister will be allowed. 7048f4a8a63SDaniel Dunbar 7058f4a8a63SDaniel DunbarBecause regular expressions are enclosed with double braces, they are 7068f4a8a63SDaniel Dunbarvisually distinct, and you don't need to use escape characters within the double 7078f4a8a63SDaniel Dunbarbraces like you would in C. In the rare case that you want to match double 7088f4a8a63SDaniel Dunbarbraces explicitly from the input, you can use something ugly like 70989683e9dSPaul Robinson``{{[}][}]}}`` as your pattern. Or if you are using the repetition count 71089683e9dSPaul Robinsonsyntax, for example ``[[:xdigit:]]{8}`` to match exactly 8 hex digits, you 71189683e9dSPaul Robinsonwould need to add parentheses like this ``{{([[:xdigit:]]{8})}}`` to avoid 71289683e9dSPaul Robinsonconfusion with FileCheck's closing double-brace. 7138f4a8a63SDaniel Dunbar 7141a944d27SThomas Preud'hommeFileCheck String Substitution Blocks 7151a944d27SThomas Preud'homme~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7168f4a8a63SDaniel Dunbar 7178f4a8a63SDaniel DunbarIt is often useful to match a pattern and then verify that it occurs again 7181a944d27SThomas Preud'hommelater in the file. For codegen tests, this can be useful to allow any 7191a944d27SThomas Preud'hommeregister, but verify that that register is used consistently later. To do 7201a944d27SThomas Preud'hommethis, :program:`FileCheck` supports string substitution blocks that allow 7211a944d27SThomas Preud'hommestring variables to be defined and substituted into patterns. Here is a simple 7221a944d27SThomas Preud'hommeexample: 7238f4a8a63SDaniel Dunbar 724a99fa5b0SDmitri Gribenko.. code-block:: llvm 7258f4a8a63SDaniel Dunbar 7268f4a8a63SDaniel Dunbar ; CHECK: test5: 7278f4a8a63SDaniel Dunbar ; CHECK: notw [[REGISTER:%[a-z]+]] 728fd7469c9SChad Rosier ; CHECK: andw {{.*}}[[REGISTER]] 7298f4a8a63SDaniel Dunbar 730a72e9f01SDmitri GribenkoThe first check line matches a regex ``%[a-z]+`` and captures it into the 7311a944d27SThomas Preud'hommestring variable ``REGISTER``. The second line verifies that whatever is in 7324ca99ba6SEli Bendersky``REGISTER`` occurs later in the file after an "``andw``". :program:`FileCheck` 7331a944d27SThomas Preud'hommestring substitution blocks are always contained in ``[[ ]]`` pairs, and string 7341a944d27SThomas Preud'hommevariable names can be formed with the regex ``[a-zA-Z_][a-zA-Z0-9_]*``. If a 7351a944d27SThomas Preud'hommecolon follows the name, then it is a definition of the variable; otherwise, it 7361a944d27SThomas Preud'hommeis a substitution. 7378f4a8a63SDaniel Dunbar 7381a944d27SThomas Preud'homme:program:`FileCheck` variables can be defined multiple times, and substitutions 7391a944d27SThomas Preud'hommealways get the latest value. Variables can also be substituted later on the 7401a944d27SThomas Preud'hommesame line they were defined on. For example: 7414ca99ba6SEli Bendersky 7424ca99ba6SEli Bendersky.. code-block:: llvm 7434ca99ba6SEli Bendersky 7444ca99ba6SEli Bendersky ; CHECK: op [[REG:r[0-9]+]], [[REG]] 7454ca99ba6SEli Bendersky 7464ca99ba6SEli BenderskyCan be useful if you want the operands of ``op`` to be the same register, 7474ca99ba6SEli Benderskyand don't care exactly which register it is. 748a72e9f01SDmitri Gribenko 749f55e72a5SArtem BelevichIf ``--enable-var-scope`` is in effect, variables with names that 750f55e72a5SArtem Belevichstart with ``$`` are considered to be global. All others variables are 751f55e72a5SArtem Belevichlocal. All local variables get undefined at the beginning of each 752f55e72a5SArtem BelevichCHECK-LABEL block. Global variables are not affected by CHECK-LABEL. 753f55e72a5SArtem BelevichThis makes it easier to ensure that individual tests are not affected 754f55e72a5SArtem Belevichby variables set in preceding tests. 755f55e72a5SArtem Belevich 7561a944d27SThomas Preud'hommeFileCheck Numeric Substitution Blocks 7571a944d27SThomas Preud'homme~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7587b4ecdd3SThomas Preud'homme 7591a944d27SThomas Preud'homme:program:`FileCheck` also supports numeric substitution blocks that allow 76071d3f227SThomas Preud'hommedefining numeric variables and checking for numeric values that satisfy a 76171d3f227SThomas Preud'hommenumeric expression constraint based on those variables via a numeric 76271d3f227SThomas Preud'hommesubstitution. This allows ``CHECK:`` directives to verify a numeric relation 76371d3f227SThomas Preud'hommebetween two numbers, such as the need for consecutive registers to be used. 7647b4ecdd3SThomas Preud'homme 765998709b7SThomas Preud'hommeThe syntax to capture a numeric value is 766998709b7SThomas Preud'homme``[[#%<fmtspec>,<NUMVAR>:]]`` where: 7678e96697cSThomas Preud'homme 768998709b7SThomas Preud'homme* ``%<fmtspec>,`` is an optional format specifier to indicate what number 769998709b7SThomas Preud'homme format to match and the minimum number of digits to expect. 7708e96697cSThomas Preud'homme 771998709b7SThomas Preud'homme* ``<NUMVAR>:`` is an optional definition of variable ``<NUMVAR>`` from the 772998709b7SThomas Preud'homme captured value. 773998709b7SThomas Preud'homme 774f9e2a62cSThomas Preud'hommeThe syntax of ``<fmtspec>`` is: ``#.<precision><conversion specifier>`` where: 775998709b7SThomas Preud'homme 776f9e2a62cSThomas Preud'homme* ``#`` is an optional flag available for hex values (see 777f9e2a62cSThomas Preud'homme ``<conversion specifier>`` below) which requires the value matched to be 778f9e2a62cSThomas Preud'homme prefixed by ``0x``. 779998709b7SThomas Preud'homme* ``.<precision>`` is an optional printf-style precision specifier in which 780998709b7SThomas Preud'homme ``<precision>`` indicates the minimum number of digits that the value matched 781998709b7SThomas Preud'homme must have, expecting leading zeros if needed. 782998709b7SThomas Preud'homme 783998709b7SThomas Preud'homme* ``<conversion specifier>`` is an optional scanf-style conversion specifier 784998709b7SThomas Preud'homme to indicate what number format to match (e.g. hex number). Currently 785998709b7SThomas Preud'homme accepted format specifiers are ``%u``, ``%d``, ``%x`` and ``%X``. If absent, 786998709b7SThomas Preud'homme the format specifier defaults to ``%u``. 787998709b7SThomas Preud'homme 7887b4ecdd3SThomas Preud'homme 78971d3f227SThomas Preud'hommeFor example: 79071d3f227SThomas Preud'homme 79171d3f227SThomas Preud'homme.. code-block:: llvm 79271d3f227SThomas Preud'homme 793998709b7SThomas Preud'homme ; CHECK: mov r[[#REG:]], 0x[[#%.8X,ADDR:]] 79471d3f227SThomas Preud'homme 795998709b7SThomas Preud'hommewould match ``mov r5, 0x0000FEFE`` and set ``REG`` to the value ``5`` and 796998709b7SThomas Preud'homme``ADDR`` to the value ``0xFEFE``. Note that due to the precision it would fail 797998709b7SThomas Preud'hommeto match ``mov r5, 0xFEFE``. 798998709b7SThomas Preud'homme 799998709b7SThomas Preud'hommeAs a result of the numeric variable definition being optional, it is possible 800998709b7SThomas Preud'hommeto only check that a numeric value is present in a given format. This can be 801998709b7SThomas Preud'hommeuseful when the value itself is not useful, for instance: 802998709b7SThomas Preud'homme 803998709b7SThomas Preud'homme.. code-block:: gas 804998709b7SThomas Preud'homme 805998709b7SThomas Preud'homme ; CHECK-NOT: mov r0, r[[#]] 806998709b7SThomas Preud'homme 807998709b7SThomas Preud'hommeto check that a value is synthesized rather than moved around. 808998709b7SThomas Preud'homme 80971d3f227SThomas Preud'homme 81047934c7cSThomas Preud'hommeThe syntax of a numeric substitution is 811998709b7SThomas Preud'homme``[[#%<fmtspec>, <constraint> <expr>]]`` where: 8128e96697cSThomas Preud'homme 813998709b7SThomas Preud'homme* ``<fmtspec>`` is the same format specifier as for defining a variable but 814998709b7SThomas Preud'homme in this context indicating how a numeric expression value should be matched 815998709b7SThomas Preud'homme against. If absent, both components of the format specifier are inferred from 816998709b7SThomas Preud'homme the matching format of the numeric variable(s) used by the expression 817998709b7SThomas Preud'homme constraint if any, and defaults to ``%u`` if no numeric variable is used, 818998709b7SThomas Preud'homme denoting that the value should be unsigned with no leading zeros. In case of 819998709b7SThomas Preud'homme conflict between format specifiers of several numeric variables, the 820998709b7SThomas Preud'homme conversion specifier becomes mandatory but the precision specifier remains 821998709b7SThomas Preud'homme optional. 8228e96697cSThomas Preud'homme 82347934c7cSThomas Preud'homme* ``<constraint>`` is the constraint describing how the value to match must 82447934c7cSThomas Preud'homme relate to the value of the numeric expression. The only currently accepted 82547934c7cSThomas Preud'homme constraint is ``==`` for an exact match and is the default if 82647934c7cSThomas Preud'homme ``<constraint>`` is not provided. No matching constraint must be specified 82747934c7cSThomas Preud'homme when the ``<expr>`` is empty. 82847934c7cSThomas Preud'homme 8298e96697cSThomas Preud'homme* ``<expr>`` is an expression. An expression is in turn recursively defined 8308e96697cSThomas Preud'homme as: 83171d3f227SThomas Preud'homme 8322a7f5204SThomas Preud'homme * a numeric operand, or 8332a7f5204SThomas Preud'homme * an expression followed by an operator and a numeric operand. 8347b4ecdd3SThomas Preud'homme 8358fd22703SPaul Walker A numeric operand is a previously defined numeric variable, an integer 8368fd22703SPaul Walker literal, or a function. Spaces are accepted before, after and between any of 8378fd22703SPaul Walker these elements. Numeric operands have 64-bit precision. Overflow and underflow 838003ea142SNico Weber are rejected. There is no support for operator precedence, but parentheses 8398fd22703SPaul Walker can be used to change the evaluation order. 8408fd22703SPaul Walker 8418fd22703SPaul WalkerThe supported operators are: 8428fd22703SPaul Walker 8438fd22703SPaul Walker * ``+`` - Returns the sum of its two operands. 8448fd22703SPaul Walker * ``-`` - Returns the difference of its two operands. 8458fd22703SPaul Walker 8468fd22703SPaul WalkerThe syntax of a function call is ``<name>(<arguments>)`` where: 8478fd22703SPaul Walker 8488fd22703SPaul Walker* ``name`` is a predefined string literal. Accepted values are: 8498fd22703SPaul Walker 8508fd22703SPaul Walker * add - Returns the sum of its two operands. 85195db1e7fSPaul Walker * div - Returns the quotient of its two operands. 8528fd22703SPaul Walker * max - Returns the largest of its two operands. 8538fd22703SPaul Walker * min - Returns the smallest of its two operands. 85495db1e7fSPaul Walker * mul - Returns the product of its two operands. 8558fd22703SPaul Walker * sub - Returns the difference of its two operands. 8568fd22703SPaul Walker 857003ea142SNico Weber* ``<arguments>`` is a comma separated list of expressions. 8581a944d27SThomas Preud'homme 8597b4ecdd3SThomas Preud'hommeFor example: 8607b4ecdd3SThomas Preud'homme 8617b4ecdd3SThomas Preud'homme.. code-block:: llvm 8627b4ecdd3SThomas Preud'homme 86371d3f227SThomas Preud'homme ; CHECK: load r[[#REG:]], [r0] 86471d3f227SThomas Preud'homme ; CHECK: load r[[#REG+1]], [r1] 8650b85ea85SThomas Preud'homme ; CHECK: Loading from 0x[[#%x,ADDR:]] 8660b85ea85SThomas Preud'homme ; CHECK-SAME: to 0x[[#ADDR + 7]] 8677b4ecdd3SThomas Preud'homme 86871d3f227SThomas Preud'hommeThe above example would match the text: 8697b4ecdd3SThomas Preud'homme 8705c15dbafSThomas Preud'homme.. code-block:: gas 8717b4ecdd3SThomas Preud'homme 87271d3f227SThomas Preud'homme load r5, [r0] 87371d3f227SThomas Preud'homme load r6, [r1] 8748e96697cSThomas Preud'homme Loading from 0xa0463440 to 0xa0463447 8757b4ecdd3SThomas Preud'homme 87671d3f227SThomas Preud'hommebut would not match the text: 8777b4ecdd3SThomas Preud'homme 8785c15dbafSThomas Preud'homme.. code-block:: gas 8797b4ecdd3SThomas Preud'homme 88071d3f227SThomas Preud'homme load r5, [r0] 88171d3f227SThomas Preud'homme load r7, [r1] 8828e96697cSThomas Preud'homme Loading from 0xa0463440 to 0xa0463443 8837b4ecdd3SThomas Preud'homme 8848e96697cSThomas Preud'hommeDue to ``7`` being unequal to ``5 + 1`` and ``a0463443`` being unequal to 8858e96697cSThomas Preud'homme``a0463440 + 7``. 8867b4ecdd3SThomas Preud'homme 8874cd9b853SThomas Preud'homme 8884cd9b853SThomas Preud'hommeA numeric variable can also be defined to the result of a numeric expression, 88947934c7cSThomas Preud'hommein which case the numeric expression constraint is checked and if verified the 890998709b7SThomas Preud'hommevariable is assigned to the value. The unified syntax for both checking a 891998709b7SThomas Preud'hommenumeric expression and capturing its value into a numeric variable is thus 89247934c7cSThomas Preud'homme``[[#%<fmtspec>,<NUMVAR>: <constraint> <expr>]]`` with each element as 89347934c7cSThomas Preud'hommedescribed previously. One can use this syntax to make a testcase more 89447934c7cSThomas Preud'hommeself-describing by using variables instead of values: 8958e96697cSThomas Preud'homme 8968e96697cSThomas Preud'homme.. code-block:: gas 8978e96697cSThomas Preud'homme 8988e96697cSThomas Preud'homme ; CHECK: mov r[[#REG_OFFSET:]], 0x[[#%X,FIELD_OFFSET:12]] 8998e96697cSThomas Preud'homme ; CHECK-NEXT: load r[[#]], [r[[#REG_BASE:]], r[[#REG_OFFSET]]] 9008e96697cSThomas Preud'homme 9018e96697cSThomas Preud'hommewhich would match: 9028e96697cSThomas Preud'homme 9038e96697cSThomas Preud'homme.. code-block:: gas 9048e96697cSThomas Preud'homme 9058e96697cSThomas Preud'homme mov r4, 0xC 9068e96697cSThomas Preud'homme load r6, [r5, r4] 9074cd9b853SThomas Preud'homme 9087b4ecdd3SThomas Preud'hommeThe ``--enable-var-scope`` option has the same effect on numeric variables as 9091a944d27SThomas Preud'hommeon string variables. 9107b4ecdd3SThomas Preud'homme 911a2ef1ba3SThomas Preud'hommeImportant note: In its current implementation, an expression cannot use a 912a291b950SThomas Preud'hommenumeric variable defined earlier in the same CHECK directive. 91371d3f227SThomas Preud'homme 9147b4ecdd3SThomas Preud'hommeFileCheck Pseudo Numeric Variables 9157b4ecdd3SThomas Preud'homme~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 91692987fb3SAlexander Kornienko 917288ed91eSThomas Preud'hommeSometimes there's a need to verify output that contains line numbers of the 918f589e241SDmitri Gribenkomatch file, e.g. when testing compiler diagnostics. This introduces a certain 919f589e241SDmitri Gribenkofragility of the match file structure, as "``CHECK:``" lines contain absolute 920f589e241SDmitri Gribenkoline numbers in the same file, which have to be updated whenever line numbers 921f589e241SDmitri Gribenkochange due to text addition or deletion. 92292987fb3SAlexander Kornienko 923a2ef1ba3SThomas Preud'hommeTo support this case, FileCheck expressions understand the ``@LINE`` pseudo 924a2ef1ba3SThomas Preud'hommenumeric variable which evaluates to the line number of the CHECK pattern where 925a2ef1ba3SThomas Preud'hommeit is found. 92692987fb3SAlexander Kornienko 92792987fb3SAlexander KornienkoThis way match patterns can be put near the relevant test lines and include 92892987fb3SAlexander Kornienkorelative line number references, for example: 92992987fb3SAlexander Kornienko 93092987fb3SAlexander Kornienko.. code-block:: c++ 93192987fb3SAlexander Kornienko 932288ed91eSThomas Preud'homme // CHECK: test.cpp:[[# @LINE + 4]]:6: error: expected ';' after top level declarator 93392987fb3SAlexander Kornienko // CHECK-NEXT: {{^int a}} 93492987fb3SAlexander Kornienko // CHECK-NEXT: {{^ \^}} 93592987fb3SAlexander Kornienko // CHECK-NEXT: {{^ ;}} 93692987fb3SAlexander Kornienko int a 93792987fb3SAlexander Kornienko 9381a944d27SThomas Preud'hommeTo support legacy uses of ``@LINE`` as a special string variable, 9391a944d27SThomas Preud'homme:program:`FileCheck` also accepts the following uses of ``@LINE`` with string 9401a944d27SThomas Preud'hommesubstitution block syntax: ``[[@LINE]]``, ``[[@LINE+<offset>]]`` and 941288ed91eSThomas Preud'homme``[[@LINE-<offset>]]`` without any spaces inside the brackets and where 942288ed91eSThomas Preud'homme``offset`` is an integer. 943288ed91eSThomas Preud'homme 9440b4509e9SWolfgang PiebMatching Newline Characters 9450b4509e9SWolfgang Pieb~~~~~~~~~~~~~~~~~~~~~~~~~~~ 9460b4509e9SWolfgang Pieb 9470b4509e9SWolfgang PiebTo match newline characters in regular expressions the character class 9480b4509e9SWolfgang Pieb``[[:space:]]`` can be used. For example, the following pattern: 9490b4509e9SWolfgang Pieb 9500b4509e9SWolfgang Pieb.. code-block:: c++ 9510b4509e9SWolfgang Pieb 9520b4509e9SWolfgang Pieb // CHECK: DW_AT_location [DW_FORM_sec_offset] ([[DLOC:0x[0-9a-f]+]]){{[[:space:]].*}}"intd" 9530b4509e9SWolfgang Pieb 9540b4509e9SWolfgang Piebmatches output of the form (from llvm-dwarfdump): 9550b4509e9SWolfgang Pieb 956124f2593SRenato Golin.. code-block:: text 9570b4509e9SWolfgang Pieb 9580b4509e9SWolfgang Pieb DW_AT_location [DW_FORM_sec_offset] (0x00000233) 9590b4509e9SWolfgang Pieb DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000c9] = "intd") 9600b4509e9SWolfgang Pieb 9610b4509e9SWolfgang Piebletting us set the :program:`FileCheck` variable ``DLOC`` to the desired value 9620b4509e9SWolfgang Pieb``0x00000233``, extracted from the line immediately preceding "``intd``". 963