1FileCheck - Flexible pattern matching file verifier 2=================================================== 3 4.. program:: FileCheck 5 6SYNOPSIS 7-------- 8 9:program:`FileCheck` *match-filename* [*--check-prefix=XXX*] [*--strict-whitespace*] 10 11DESCRIPTION 12----------- 13 14:program:`FileCheck` reads two files (one from standard input, and one 15specified on the command line) and uses one to verify the other. This 16behavior is particularly useful for the testsuite, which wants to verify that 17the output of some tool (e.g. :program:`llc`) contains the expected information 18(for example, a movsd from esp or whatever is interesting). This is similar to 19using :program:`grep`, but it is optimized for matching multiple different 20inputs in one file in a specific order. 21 22The ``match-filename`` file specifies the file that contains the patterns to 23match. The file to verify is read from standard input unless the 24:option:`--input-file` option is used. 25 26OPTIONS 27------- 28 29Options are parsed from the environment variable ``FILECHECK_OPTS`` 30and from the command line. 31 32.. option:: -help 33 34 Print a summary of command line options. 35 36.. option:: --check-prefix prefix 37 38 FileCheck searches the contents of ``match-filename`` for patterns to 39 match. By default, these patterns are prefixed with "``CHECK:``". 40 If you'd like to use a different prefix (e.g. because the same input 41 file is checking multiple different tool or options), the 42 :option:`--check-prefix` argument allows you to specify (without the trailing 43 "``:``") one or more prefixes to match. Multiple prefixes are useful for tests 44 which might change for different run options, but most lines remain the same. 45 46 FileCheck does not permit duplicate prefixes, even if one is a check prefix 47 and one is a comment prefix (see :option:`--comment-prefixes` below). 48 49.. option:: --check-prefixes prefix1,prefix2,... 50 51 An alias of :option:`--check-prefix` that allows multiple prefixes to be 52 specified as a comma separated list. 53 54.. option:: --comment-prefixes prefix1,prefix2,... 55 56 By default, FileCheck ignores any occurrence in ``match-filename`` of any check 57 prefix if it is preceded on the same line by "``COM:``" or "``RUN:``". See the 58 section `The "COM:" directive`_ for usage details. 59 60 These default comment prefixes can be overridden by 61 :option:`--comment-prefixes` if they are not appropriate for your testing 62 environment. However, doing so is not recommended in LLVM's LIT-based test 63 suites, which should be easier to maintain if they all follow a consistent 64 comment style. In that case, consider proposing a change to the default 65 comment prefixes instead. 66 67.. option:: --input-file filename 68 69 File to check (defaults to stdin). 70 71.. option:: --match-full-lines 72 73 By default, FileCheck allows matches of anywhere on a line. This 74 option will require all positive matches to cover an entire 75 line. Leading and trailing whitespace is ignored, unless 76 :option:`--strict-whitespace` is also specified. (Note: negative 77 matches from ``CHECK-NOT`` are not affected by this option!) 78 79 Passing this option is equivalent to inserting ``{{^ *}}`` or 80 ``{{^}}`` before, and ``{{ *$}}`` or ``{{$}}`` after every positive 81 check pattern. 82 83.. option:: --strict-whitespace 84 85 By default, FileCheck canonicalizes input horizontal whitespace (spaces and 86 tabs) which causes it to ignore these differences (a space will match a tab). 87 The :option:`--strict-whitespace` argument disables this behavior. End-of-line 88 sequences are canonicalized to UNIX-style ``\n`` in all modes. 89 90.. option:: --ignore-case 91 92 By default, FileCheck uses case-sensitive matching. This option causes 93 FileCheck to use case-insensitive matching. 94 95.. option:: --implicit-check-not check-pattern 96 97 Adds implicit negative checks for the specified patterns between positive 98 checks. The option allows writing stricter tests without stuffing them with 99 ``CHECK-NOT``\ s. 100 101 For example, "``--implicit-check-not warning:``" can be useful when testing 102 diagnostic messages from tools that don't have an option similar to ``clang 103 -verify``. With this option FileCheck will verify that input does not contain 104 warnings not covered by any ``CHECK:`` patterns. 105 106.. option:: --dump-input <value> 107 108 Dump input to stderr, adding annotations representing currently enabled 109 diagnostics. When there are multiple occurrences of this option, the 110 ``<value>`` that appears earliest in the list below has precedence. The 111 default is ``fail``. 112 113 * ``help`` - Explain input dump and quit 114 * ``always`` - Always dump input 115 * ``fail`` - Dump input on failure 116 * ``never`` - Never dump input 117 118.. option:: --dump-input-context <N> 119 120 In the dump requested by ``--dump-input``, print ``<N>`` input lines before 121 and ``<N>`` input lines after any lines specified by ``--dump-input-filter``. 122 When there are multiple occurrences of this option, the largest specified 123 ``<N>`` has precedence. The default is 5. 124 125.. option:: --dump-input-filter <value> 126 127 In the dump requested by ``--dump-input``, print only input lines of kind 128 ``<value>`` plus any context specified by ``--dump-input-context``. When 129 there are multiple occurrences of this option, the ``<value>`` that appears 130 earliest in the list below has precedence. The default is ``error`` when 131 ``--dump-input=fail``, and it's ``all`` when ``--dump-input=always``. 132 133 * ``all`` - All input lines 134 * ``annotation-full`` - Input lines with annotations 135 * ``annotation`` - Input lines with starting points of annotations 136 * ``error`` - Input lines with starting points of error annotations 137 138.. option:: --enable-var-scope 139 140 Enables scope for regex variables. 141 142 Variables with names that start with ``$`` are considered global and 143 remain set throughout the file. 144 145 All other variables get undefined after each encountered ``CHECK-LABEL``. 146 147.. option:: -D<VAR=VALUE> 148 149 Sets a filecheck pattern variable ``VAR`` with value ``VALUE`` that can be 150 used in ``CHECK:`` lines. 151 152.. option:: -D#<FMT>,<NUMVAR>=<NUMERIC EXPRESSION> 153 154 Sets a filecheck numeric variable ``NUMVAR`` of matching format ``FMT`` to 155 the result of evaluating ``<NUMERIC EXPRESSION>`` that can be used in 156 ``CHECK:`` lines. See section 157 ``FileCheck Numeric Variables and Expressions`` for details on supported 158 numeric expressions. 159 160.. option:: -version 161 162 Show the version number of this program. 163 164.. option:: -v 165 166 Print good directive pattern matches. However, if ``-dump-input=fail`` or 167 ``-dump-input=always``, add those matches as input annotations instead. 168 169.. option:: -vv 170 171 Print information helpful in diagnosing internal FileCheck issues, such as 172 discarded overlapping ``CHECK-DAG:`` matches, implicit EOF pattern matches, 173 and ``CHECK-NOT:`` patterns that do not have matches. Implies ``-v``. 174 However, if ``-dump-input=fail`` or ``-dump-input=always``, just add that 175 information as input annotations instead. 176 177.. option:: --allow-deprecated-dag-overlap 178 179 Enable overlapping among matches in a group of consecutive ``CHECK-DAG:`` 180 directives. This option is deprecated and is only provided for convenience 181 as old tests are migrated to the new non-overlapping ``CHECK-DAG:`` 182 implementation. 183 184.. option:: --color 185 186 Use colors in output (autodetected by default). 187 188EXIT STATUS 189----------- 190 191If :program:`FileCheck` verifies that the file matches the expected contents, 192it exits with 0. Otherwise, if not, or if an error occurs, it will exit with a 193non-zero value. 194 195TUTORIAL 196-------- 197 198FileCheck is typically used from LLVM regression tests, being invoked on the RUN 199line of the test. A simple example of using FileCheck from a RUN line looks 200like this: 201 202.. code-block:: llvm 203 204 ; RUN: llvm-as < %s | llc -march=x86-64 | FileCheck %s 205 206This syntax says to pipe the current file ("``%s``") into ``llvm-as``, pipe 207that into ``llc``, then pipe the output of ``llc`` into ``FileCheck``. This 208means that FileCheck will be verifying its standard input (the llc output) 209against the filename argument specified (the original ``.ll`` file specified by 210"``%s``"). To see how this works, let's look at the rest of the ``.ll`` file 211(after the RUN line): 212 213.. code-block:: llvm 214 215 define void @sub1(i32* %p, i32 %v) { 216 entry: 217 ; CHECK: sub1: 218 ; CHECK: subl 219 %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v) 220 ret void 221 } 222 223 define void @inc4(i64* %p) { 224 entry: 225 ; CHECK: inc4: 226 ; CHECK: incq 227 %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1) 228 ret void 229 } 230 231Here you can see some "``CHECK:``" lines specified in comments. Now you can 232see how the file is piped into ``llvm-as``, then ``llc``, and the machine code 233output is what we are verifying. FileCheck checks the machine code output to 234verify that it matches what the "``CHECK:``" lines specify. 235 236The syntax of the "``CHECK:``" lines is very simple: they are fixed strings that 237must occur in order. FileCheck defaults to ignoring horizontal whitespace 238differences (e.g. a space is allowed to match a tab) but otherwise, the contents 239of the "``CHECK:``" line is required to match some thing in the test file exactly. 240 241One nice thing about FileCheck (compared to grep) is that it allows merging 242test cases together into logical groups. For example, because the test above 243is checking for the "``sub1:``" and "``inc4:``" labels, it will not match 244unless there is a "``subl``" in between those labels. If it existed somewhere 245else in the file, that would not count: "``grep subl``" matches if "``subl``" 246exists anywhere in the file. 247 248The FileCheck -check-prefix option 249~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 250 251The FileCheck `-check-prefix` option allows multiple test 252configurations to be driven from one `.ll` file. This is useful in many 253circumstances, for example, testing different architectural variants with 254:program:`llc`. Here's a simple example: 255 256.. code-block:: llvm 257 258 ; RUN: llvm-as < %s | llc -mtriple=i686-apple-darwin9 -mattr=sse41 \ 259 ; RUN: | FileCheck %s -check-prefix=X32 260 ; RUN: llvm-as < %s | llc -mtriple=x86_64-apple-darwin9 -mattr=sse41 \ 261 ; RUN: | FileCheck %s -check-prefix=X64 262 263 define <4 x i32> @pinsrd_1(i32 %s, <4 x i32> %tmp) nounwind { 264 %tmp1 = insertelement <4 x i32>; %tmp, i32 %s, i32 1 265 ret <4 x i32> %tmp1 266 ; X32: pinsrd_1: 267 ; X32: pinsrd $1, 4(%esp), %xmm0 268 269 ; X64: pinsrd_1: 270 ; X64: pinsrd $1, %edi, %xmm0 271 } 272 273In this case, we're testing that we get the expected code generation with 274both 32-bit and 64-bit code generation. 275 276The "COM:" directive 277~~~~~~~~~~~~~~~~~~~~ 278 279Sometimes you want to disable a FileCheck directive without removing it 280entirely, or you want to write comments that mention a directive by name. The 281"``COM:``" directive makes it easy to do this. For example, you might have: 282 283.. code-block:: llvm 284 285 ; X32: pinsrd_1: 286 ; X32: pinsrd $1, 4(%esp), %xmm0 287 288 ; COM: FIXME: X64 isn't working correctly yet for this part of codegen, but 289 ; COM: X64 will have something similar to X32: 290 ; COM: 291 ; COM: X64: pinsrd_1: 292 ; COM: X64: pinsrd $1, %edi, %xmm0 293 294Without "``COM:``", you would need to use some combination of rewording and 295directive syntax mangling to prevent FileCheck from recognizing the commented 296occurrences of "``X32:``" and "``X64:``" above as directives. Moreover, 297FileCheck diagnostics have been proposed that might complain about the above 298occurrences of "``X64``" that don't have the trailing "``:``" because they look 299like directive typos. Dodging all these problems can be tedious for a test 300author, and directive syntax mangling can make the purpose of test code unclear. 301"``COM:``" avoids all these problems. 302 303A few important usage notes: 304 305* "``COM:``" within another directive's pattern does *not* comment out the 306 remainder of the pattern. For example: 307 308 .. code-block:: llvm 309 310 ; X32: pinsrd $1, 4(%esp), %xmm0 COM: This is part of the X32 pattern! 311 312 If you need to temporarily comment out part of a directive's pattern, move it 313 to another line. The reason is that FileCheck parses "``COM:``" in the same 314 manner as any other directive: only the first directive on the line is 315 recognized as a directive. 316 317* For the sake of LIT, FileCheck treats "``RUN:``" just like "``COM:``". If this 318 is not suitable for your test environment, see :option:`--comment-prefixes`. 319 320* FileCheck does not recognize "``COM``", "``RUN``", or any user-defined comment 321 prefix as a comment directive if it's combined with one of the usual check 322 directive suffixes, such as "``-NEXT:``" or "``-NOT:``", discussed below. 323 FileCheck treats such a combination as plain text instead. If it needs to act 324 as a comment directive for your test environment, define it as such with 325 :option:`--comment-prefixes`. 326 327The "CHECK-NEXT:" directive 328~~~~~~~~~~~~~~~~~~~~~~~~~~~ 329 330Sometimes you want to match lines and would like to verify that matches 331happen on exactly consecutive lines with no other lines in between them. In 332this case, you can use "``CHECK:``" and "``CHECK-NEXT:``" directives to specify 333this. If you specified a custom check prefix, just use "``<PREFIX>-NEXT:``". 334For example, something like this works as you'd expect: 335 336.. code-block:: llvm 337 338 define void @t2(<2 x double>* %r, <2 x double>* %A, double %B) { 339 %tmp3 = load <2 x double>* %A, align 16 340 %tmp7 = insertelement <2 x double> undef, double %B, i32 0 341 %tmp9 = shufflevector <2 x double> %tmp3, 342 <2 x double> %tmp7, 343 <2 x i32> < i32 0, i32 2 > 344 store <2 x double> %tmp9, <2 x double>* %r, align 16 345 ret void 346 347 ; CHECK: t2: 348 ; CHECK: movl 8(%esp), %eax 349 ; CHECK-NEXT: movapd (%eax), %xmm0 350 ; CHECK-NEXT: movhpd 12(%esp), %xmm0 351 ; CHECK-NEXT: movl 4(%esp), %eax 352 ; CHECK-NEXT: movapd %xmm0, (%eax) 353 ; CHECK-NEXT: ret 354 } 355 356"``CHECK-NEXT:``" directives reject the input unless there is exactly one 357newline between it and the previous directive. A "``CHECK-NEXT:``" cannot be 358the first directive in a file. 359 360The "CHECK-SAME:" directive 361~~~~~~~~~~~~~~~~~~~~~~~~~~~ 362 363Sometimes you want to match lines and would like to verify that matches happen 364on the same line as the previous match. In this case, you can use "``CHECK:``" 365and "``CHECK-SAME:``" directives to specify this. If you specified a custom 366check prefix, just use "``<PREFIX>-SAME:``". 367 368"``CHECK-SAME:``" is particularly powerful in conjunction with "``CHECK-NOT:``" 369(described below). 370 371For example, the following works like you'd expect: 372 373.. code-block:: llvm 374 375 !0 = !DILocation(line: 5, scope: !1, inlinedAt: !2) 376 377 ; CHECK: !DILocation(line: 5, 378 ; CHECK-NOT: column: 379 ; CHECK-SAME: scope: ![[SCOPE:[0-9]+]] 380 381"``CHECK-SAME:``" directives reject the input if there are any newlines between 382it and the previous directive. A "``CHECK-SAME:``" cannot be the first 383directive in a file. 384 385The "CHECK-EMPTY:" directive 386~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 387 388If you need to check that the next line has nothing on it, not even whitespace, 389you can use the "``CHECK-EMPTY:``" directive. 390 391.. code-block:: llvm 392 393 declare void @foo() 394 395 declare void @bar() 396 ; CHECK: foo 397 ; CHECK-EMPTY: 398 ; CHECK-NEXT: bar 399 400Just like "``CHECK-NEXT:``" the directive will fail if there is more than one 401newline before it finds the next blank line, and it cannot be the first 402directive in a file. 403 404The "CHECK-NOT:" directive 405~~~~~~~~~~~~~~~~~~~~~~~~~~ 406 407The "``CHECK-NOT:``" directive is used to verify that a string doesn't occur 408between two matches (or before the first match, or after the last match). For 409example, to verify that a load is removed by a transformation, a test like this 410can be used: 411 412.. code-block:: llvm 413 414 define i8 @coerce_offset0(i32 %V, i32* %P) { 415 store i32 %V, i32* %P 416 417 %P2 = bitcast i32* %P to i8* 418 %P3 = getelementptr i8* %P2, i32 2 419 420 %A = load i8* %P3 421 ret i8 %A 422 ; CHECK: @coerce_offset0 423 ; CHECK-NOT: load 424 ; CHECK: ret i8 425 } 426 427The "CHECK-COUNT:" directive 428~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 429 430If you need to match multiple lines with the same pattern over and over again 431you can repeat a plain ``CHECK:`` as many times as needed. If that looks too 432boring you can instead use a counted check "``CHECK-COUNT-<num>:``", where 433``<num>`` is a positive decimal number. It will match the pattern exactly 434``<num>`` times, no more and no less. If you specified a custom check prefix, 435just use "``<PREFIX>-COUNT-<num>:``" for the same effect. 436Here is a simple example: 437 438.. code-block:: text 439 440 Loop at depth 1 441 Loop at depth 1 442 Loop at depth 1 443 Loop at depth 1 444 Loop at depth 2 445 Loop at depth 3 446 447 ; CHECK-COUNT-6: Loop at depth {{[0-9]+}} 448 ; CHECK-NOT: Loop at depth {{[0-9]+}} 449 450The "CHECK-DAG:" directive 451~~~~~~~~~~~~~~~~~~~~~~~~~~ 452 453If it's necessary to match strings that don't occur in a strictly sequential 454order, "``CHECK-DAG:``" could be used to verify them between two matches (or 455before the first match, or after the last match). For example, clang emits 456vtable globals in reverse order. Using ``CHECK-DAG:``, we can keep the checks 457in the natural order: 458 459.. code-block:: c++ 460 461 // RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s 462 463 struct Foo { virtual void method(); }; 464 Foo f; // emit vtable 465 // CHECK-DAG: @_ZTV3Foo = 466 467 struct Bar { virtual void method(); }; 468 Bar b; 469 // CHECK-DAG: @_ZTV3Bar = 470 471``CHECK-NOT:`` directives could be mixed with ``CHECK-DAG:`` directives to 472exclude strings between the surrounding ``CHECK-DAG:`` directives. As a result, 473the surrounding ``CHECK-DAG:`` directives cannot be reordered, i.e. all 474occurrences matching ``CHECK-DAG:`` before ``CHECK-NOT:`` must not fall behind 475occurrences matching ``CHECK-DAG:`` after ``CHECK-NOT:``. For example, 476 477.. code-block:: llvm 478 479 ; CHECK-DAG: BEFORE 480 ; CHECK-NOT: NOT 481 ; CHECK-DAG: AFTER 482 483This case will reject input strings where ``BEFORE`` occurs after ``AFTER``. 484 485With captured variables, ``CHECK-DAG:`` is able to match valid topological 486orderings of a DAG with edges from the definition of a variable to its use. 487It's useful, e.g., when your test cases need to match different output 488sequences from the instruction scheduler. For example, 489 490.. code-block:: llvm 491 492 ; CHECK-DAG: add [[REG1:r[0-9]+]], r1, r2 493 ; CHECK-DAG: add [[REG2:r[0-9]+]], r3, r4 494 ; CHECK: mul r5, [[REG1]], [[REG2]] 495 496In this case, any order of that two ``add`` instructions will be allowed. 497 498If you are defining `and` using variables in the same ``CHECK-DAG:`` block, 499be aware that the definition rule can match `after` its use. 500 501So, for instance, the code below will pass: 502 503.. code-block:: text 504 505 ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0] 506 ; CHECK-DAG: vmov.32 [[REG2]][1] 507 vmov.32 d0[1] 508 vmov.32 d0[0] 509 510While this other code, will not: 511 512.. code-block:: text 513 514 ; CHECK-DAG: vmov.32 [[REG2:d[0-9]+]][0] 515 ; CHECK-DAG: vmov.32 [[REG2]][1] 516 vmov.32 d1[1] 517 vmov.32 d0[0] 518 519While this can be very useful, it's also dangerous, because in the case of 520register sequence, you must have a strong order (read before write, copy before 521use, etc). If the definition your test is looking for doesn't match (because 522of a bug in the compiler), it may match further away from the use, and mask 523real bugs away. 524 525In those cases, to enforce the order, use a non-DAG directive between DAG-blocks. 526 527A ``CHECK-DAG:`` directive skips matches that overlap the matches of any 528preceding ``CHECK-DAG:`` directives in the same ``CHECK-DAG:`` block. Not only 529is this non-overlapping behavior consistent with other directives, but it's 530also necessary to handle sets of non-unique strings or patterns. For example, 531the following directives look for unordered log entries for two tasks in a 532parallel program, such as the OpenMP runtime: 533 534.. code-block:: text 535 536 // CHECK-DAG: [[THREAD_ID:[0-9]+]]: task_begin 537 // CHECK-DAG: [[THREAD_ID]]: task_end 538 // 539 // CHECK-DAG: [[THREAD_ID:[0-9]+]]: task_begin 540 // CHECK-DAG: [[THREAD_ID]]: task_end 541 542The second pair of directives is guaranteed not to match the same log entries 543as the first pair even though the patterns are identical and even if the text 544of the log entries is identical because the thread ID manages to be reused. 545 546The "CHECK-LABEL:" directive 547~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 548 549Sometimes in a file containing multiple tests divided into logical blocks, one 550or more ``CHECK:`` directives may inadvertently succeed by matching lines in a 551later block. While an error will usually eventually be generated, the check 552flagged as causing the error may not actually bear any relationship to the 553actual source of the problem. 554 555In order to produce better error messages in these cases, the "``CHECK-LABEL:``" 556directive can be used. It is treated identically to a normal ``CHECK`` 557directive except that FileCheck makes an additional assumption that a line 558matched by the directive cannot also be matched by any other check present in 559``match-filename``; this is intended to be used for lines containing labels or 560other unique identifiers. Conceptually, the presence of ``CHECK-LABEL`` divides 561the input stream into separate blocks, each of which is processed independently, 562preventing a ``CHECK:`` directive in one block matching a line in another block. 563If ``--enable-var-scope`` is in effect, all local variables are cleared at the 564beginning of the block. 565 566For example, 567 568.. code-block:: llvm 569 570 define %struct.C* @C_ctor_base(%struct.C* %this, i32 %x) { 571 entry: 572 ; CHECK-LABEL: C_ctor_base: 573 ; CHECK: mov [[SAVETHIS:r[0-9]+]], r0 574 ; CHECK: bl A_ctor_base 575 ; CHECK: mov r0, [[SAVETHIS]] 576 %0 = bitcast %struct.C* %this to %struct.A* 577 %call = tail call %struct.A* @A_ctor_base(%struct.A* %0) 578 %1 = bitcast %struct.C* %this to %struct.B* 579 %call2 = tail call %struct.B* @B_ctor_base(%struct.B* %1, i32 %x) 580 ret %struct.C* %this 581 } 582 583 define %struct.D* @D_ctor_base(%struct.D* %this, i32 %x) { 584 entry: 585 ; CHECK-LABEL: D_ctor_base: 586 587The use of ``CHECK-LABEL:`` directives in this case ensures that the three 588``CHECK:`` directives only accept lines corresponding to the body of the 589``@C_ctor_base`` function, even if the patterns match lines found later in 590the file. Furthermore, if one of these three ``CHECK:`` directives fail, 591FileCheck will recover by continuing to the next block, allowing multiple test 592failures to be detected in a single invocation. 593 594There is no requirement that ``CHECK-LABEL:`` directives contain strings that 595correspond to actual syntactic labels in a source or output language: they must 596simply uniquely match a single line in the file being verified. 597 598``CHECK-LABEL:`` directives cannot contain variable definitions or uses. 599 600FileCheck Regex Matching Syntax 601~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 602 603All FileCheck directives take a pattern to match. 604For most uses of FileCheck, fixed string matching is perfectly sufficient. For 605some things, a more flexible form of matching is desired. To support this, 606FileCheck allows you to specify regular expressions in matching strings, 607surrounded by double braces: ``{{yourregex}}``. FileCheck implements a POSIX 608regular expression matcher; it supports Extended POSIX regular expressions 609(ERE). Because we want to use fixed string matching for a majority of what we 610do, FileCheck has been designed to support mixing and matching fixed string 611matching with regular expressions. This allows you to write things like this: 612 613.. code-block:: llvm 614 615 ; CHECK: movhpd {{[0-9]+}}(%esp), {{%xmm[0-7]}} 616 617In this case, any offset from the ESP register will be allowed, and any xmm 618register will be allowed. 619 620Because regular expressions are enclosed with double braces, they are 621visually distinct, and you don't need to use escape characters within the double 622braces like you would in C. In the rare case that you want to match double 623braces explicitly from the input, you can use something ugly like 624``{{[}][}]}}`` as your pattern. Or if you are using the repetition count 625syntax, for example ``[[:xdigit:]]{8}`` to match exactly 8 hex digits, you 626would need to add parentheses like this ``{{([[:xdigit:]]{8})}}`` to avoid 627confusion with FileCheck's closing double-brace. 628 629FileCheck String Substitution Blocks 630~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 631 632It is often useful to match a pattern and then verify that it occurs again 633later in the file. For codegen tests, this can be useful to allow any 634register, but verify that that register is used consistently later. To do 635this, :program:`FileCheck` supports string substitution blocks that allow 636string variables to be defined and substituted into patterns. Here is a simple 637example: 638 639.. code-block:: llvm 640 641 ; CHECK: test5: 642 ; CHECK: notw [[REGISTER:%[a-z]+]] 643 ; CHECK: andw {{.*}}[[REGISTER]] 644 645The first check line matches a regex ``%[a-z]+`` and captures it into the 646string variable ``REGISTER``. The second line verifies that whatever is in 647``REGISTER`` occurs later in the file after an "``andw``". :program:`FileCheck` 648string substitution blocks are always contained in ``[[ ]]`` pairs, and string 649variable names can be formed with the regex ``[a-zA-Z_][a-zA-Z0-9_]*``. If a 650colon follows the name, then it is a definition of the variable; otherwise, it 651is a substitution. 652 653:program:`FileCheck` variables can be defined multiple times, and substitutions 654always get the latest value. Variables can also be substituted later on the 655same line they were defined on. For example: 656 657.. code-block:: llvm 658 659 ; CHECK: op [[REG:r[0-9]+]], [[REG]] 660 661Can be useful if you want the operands of ``op`` to be the same register, 662and don't care exactly which register it is. 663 664If ``--enable-var-scope`` is in effect, variables with names that 665start with ``$`` are considered to be global. All others variables are 666local. All local variables get undefined at the beginning of each 667CHECK-LABEL block. Global variables are not affected by CHECK-LABEL. 668This makes it easier to ensure that individual tests are not affected 669by variables set in preceding tests. 670 671FileCheck Numeric Substitution Blocks 672~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 673 674:program:`FileCheck` also supports numeric substitution blocks that allow 675defining numeric variables and checking for numeric values that satisfy a 676numeric expression constraint based on those variables via a numeric 677substitution. This allows ``CHECK:`` directives to verify a numeric relation 678between two numbers, such as the need for consecutive registers to be used. 679 680The syntax to define a numeric variable is ``[[#%<fmtspec>,<NUMVAR>:]]`` where: 681 682* ``%<fmtspec>`` is an optional scanf-style matching format specifier to 683 indicate what number format to match (e.g. hex number). Currently accepted 684 format specifiers are ``%u``, ``%d``, ``%x`` and ``%X``. If absent, the 685 format specifier defaults to ``%u``. 686 687* ``<NUMVAR>`` is the name of the numeric variable to define to the matching 688 value. 689 690For example: 691 692.. code-block:: llvm 693 694 ; CHECK: mov r[[#REG:]], 0x[[#%X,IMM:]] 695 696would match ``mov r5, 0xF0F0`` and set ``REG`` to the value ``5`` and ``IMM`` 697to the value ``0xF0F0``. 698 699The syntax of a numeric substitution is 700``[[#%<fmtspec>: <constraint> <expr>]]`` where: 701 702* ``%<fmtspec>`` is the same matching format specifier as for defining numeric 703 variables but acting as a printf-style format to indicate how a numeric 704 expression value should be matched against. If absent, the format specifier 705 is inferred from the matching format of the numeric variable(s) used by the 706 expression constraint if any, and defaults to ``%u`` if no numeric variable 707 is used. In case of conflict between matching formats of several numeric 708 variables the format specifier is mandatory. 709 710* ``<constraint>`` is the constraint describing how the value to match must 711 relate to the value of the numeric expression. The only currently accepted 712 constraint is ``==`` for an exact match and is the default if 713 ``<constraint>`` is not provided. No matching constraint must be specified 714 when the ``<expr>`` is empty. 715 716* ``<expr>`` is an expression. An expression is in turn recursively defined 717 as: 718 719 * a numeric operand, or 720 * an expression followed by an operator and a numeric operand. 721 722 A numeric operand is a previously defined numeric variable, an integer 723 literal, or a function. Spaces are accepted before, after and between any of 724 these elements. Numeric operands have 64-bit precision. Overflow and underflow 725 are rejected. There is no support for operator precedence, but parentheses 726 can be used to change the evaluation order. 727 728The supported operators are: 729 730 * ``+`` - Returns the sum of its two operands. 731 * ``-`` - Returns the difference of its two operands. 732 733The syntax of a function call is ``<name>(<arguments>)`` where: 734 735* ``name`` is a predefined string literal. Accepted values are: 736 737 * add - Returns the sum of its two operands. 738 * div - Returns the quotient of its two operands. 739 * max - Returns the largest of its two operands. 740 * min - Returns the smallest of its two operands. 741 * mul - Returns the product of its two operands. 742 * sub - Returns the difference of its two operands. 743 744* ``<arguments>`` is a comma separated list of expressions. 745 746For example: 747 748.. code-block:: llvm 749 750 ; CHECK: load r[[#REG:]], [r0] 751 ; CHECK: load r[[#REG+1]], [r1] 752 ; CHECK: Loading from 0x[[#%x,ADDR:]] 753 ; CHECK-SAME: to 0x[[#ADDR + 7]] 754 755The above example would match the text: 756 757.. code-block:: gas 758 759 load r5, [r0] 760 load r6, [r1] 761 Loading from 0xa0463440 to 0xa0463447 762 763but would not match the text: 764 765.. code-block:: gas 766 767 load r5, [r0] 768 load r7, [r1] 769 Loading from 0xa0463440 to 0xa0463443 770 771Due to ``7`` being unequal to ``5 + 1`` and ``a0463443`` being unequal to 772``a0463440 + 7``. 773 774The syntax also supports an empty expression, equivalent to writing {{[0-9]+}}, 775for cases where the input must contain a numeric value but the value itself 776does not matter: 777 778.. code-block:: gas 779 780 ; CHECK-NOT: mov r0, r[[#]] 781 782to check that a value is synthesized rather than moved around. 783 784A numeric variable can also be defined to the result of a numeric expression, 785in which case the numeric expression constraint is checked and if verified the 786variable is assigned to the value. The unified syntax for both defining numeric 787variables and checking a numeric expression is thus 788``[[#%<fmtspec>,<NUMVAR>: <constraint> <expr>]]`` with each element as 789described previously. One can use this syntax to make a testcase more 790self-describing by using variables instead of values: 791 792.. code-block:: gas 793 794 ; CHECK: mov r[[#REG_OFFSET:]], 0x[[#%X,FIELD_OFFSET:12]] 795 ; CHECK-NEXT: load r[[#]], [r[[#REG_BASE:]], r[[#REG_OFFSET]]] 796 797which would match: 798 799.. code-block:: gas 800 801 mov r4, 0xC 802 load r6, [r5, r4] 803 804The ``--enable-var-scope`` option has the same effect on numeric variables as 805on string variables. 806 807Important note: In its current implementation, an expression cannot use a 808numeric variable defined earlier in the same CHECK directive. 809 810FileCheck Pseudo Numeric Variables 811~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 812 813Sometimes there's a need to verify output that contains line numbers of the 814match file, e.g. when testing compiler diagnostics. This introduces a certain 815fragility of the match file structure, as "``CHECK:``" lines contain absolute 816line numbers in the same file, which have to be updated whenever line numbers 817change due to text addition or deletion. 818 819To support this case, FileCheck expressions understand the ``@LINE`` pseudo 820numeric variable which evaluates to the line number of the CHECK pattern where 821it is found. 822 823This way match patterns can be put near the relevant test lines and include 824relative line number references, for example: 825 826.. code-block:: c++ 827 828 // CHECK: test.cpp:[[# @LINE + 4]]:6: error: expected ';' after top level declarator 829 // CHECK-NEXT: {{^int a}} 830 // CHECK-NEXT: {{^ \^}} 831 // CHECK-NEXT: {{^ ;}} 832 int a 833 834To support legacy uses of ``@LINE`` as a special string variable, 835:program:`FileCheck` also accepts the following uses of ``@LINE`` with string 836substitution block syntax: ``[[@LINE]]``, ``[[@LINE+<offset>]]`` and 837``[[@LINE-<offset>]]`` without any spaces inside the brackets and where 838``offset`` is an integer. 839 840Matching Newline Characters 841~~~~~~~~~~~~~~~~~~~~~~~~~~~ 842 843To match newline characters in regular expressions the character class 844``[[:space:]]`` can be used. For example, the following pattern: 845 846.. code-block:: c++ 847 848 // CHECK: DW_AT_location [DW_FORM_sec_offset] ([[DLOC:0x[0-9a-f]+]]){{[[:space:]].*}}"intd" 849 850matches output of the form (from llvm-dwarfdump): 851 852.. code-block:: text 853 854 DW_AT_location [DW_FORM_sec_offset] (0x00000233) 855 DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000c9] = "intd") 856 857letting us set the :program:`FileCheck` variable ``DLOC`` to the desired value 858``0x00000233``, extracted from the line immediately preceding "``intd``". 859