Skip to content

Commit 907172f

Browse files
arichardsonpow2clk
authored andcommitted
Add a -D flag to FileCheck to define variables
Summary: This makes it very easy to test files that only differ in a constant value somewhere in the test case. Reviewers: jlebar, hfinkel, chandlerc, probinson Reviewed By: probinson Subscribers: probinson, llvm-commits Differential Revision: https://reviews.llvm.org/D39629 llvm-svn: 317572 (cherry picked from LLVM commit 46e1fd61021ff06966e672cb38cba0278b9d0b0d)
1 parent 0f4202f commit 907172f

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

docs/CommandGuide/FileCheck.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ OPTIONS
6060
-verify``. With this option FileCheck will verify that input does not contain
6161
warnings not covered by any ``CHECK:`` patterns.
6262

63+
.. option:: -D<VAR=VALUE>
64+
65+
Sets a filecheck variable ``VAR`` with value ``VALUE`` that can be used in
66+
``CHECK:`` lines.
67+
6368
.. option:: -version
6469

6570
Show the version number of this program.

test/FileCheck/defines.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; RUN: FileCheck -DVALUE=10 -input-file %s %s
2+
; RUN: not FileCheck -DVALUE=20 -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRMSG
3+
4+
Value = 10
5+
; CHECK: Value = [[VALUE]]
6+
7+
; ERRMSG: defines.txt:5:10: error: expected string not found in input
8+
; ERRMSG: defines.txt:1:1: note: with variable "VALUE" equal to "20"
9+
; ERRMSG: defines.txt:4:1: note: possible intended match here

utils/FileCheck/FileCheck.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ static cl::list<std::string> ImplicitCheckNot(
6969
"this pattern occur which are not matched by a positive pattern"),
7070
cl::value_desc("pattern"));
7171

72+
static cl::list<std::string>
73+
GlobalDefines("D", cl::Prefix,
74+
cl::desc("Define a variable to be used in capture patterns."),
75+
cl::value_desc("VAR=VALUE"));
76+
7277
static cl::opt<bool> AllowEmptyInput(
7378
"allow-empty", cl::init(false),
7479
cl::desc("Allow the input file to be empty. This is useful when making\n"
@@ -1359,6 +1364,9 @@ int main(int argc, char **argv) {
13591364
/// VariableTable - This holds all the current filecheck variables.
13601365
StringMap<StringRef> VariableTable;
13611366

1367+
for (const auto &Def : GlobalDefines)
1368+
VariableTable.insert(StringRef(Def).split('='));
1369+
13621370
bool hasError = false;
13631371

13641372
unsigned i = 0, j = 0, e = CheckStrings.size();

0 commit comments

Comments
 (0)