Skip to content

Commit

Permalink
Add a -D flag to FileCheck to define variables
Browse files Browse the repository at this point in the history
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317572 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
arichardson committed Nov 7, 2017
1 parent 1ed4428 commit 7c37aa1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/CommandGuide/FileCheck.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ OPTIONS

All other variables get undefined after each encountered ``CHECK-LABEL``.

.. option:: -D<VAR=VALUE>

Sets a filecheck variable ``VAR`` with value ``VALUE`` that can be used in
``CHECK:`` lines.

.. option:: -version

Show the version number of this program.
Expand Down
9 changes: 9 additions & 0 deletions test/FileCheck/defines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; RUN: FileCheck -DVALUE=10 -input-file %s %s
; RUN: not FileCheck -DVALUE=20 -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRMSG

Value = 10
; CHECK: Value = [[VALUE]]

; ERRMSG: defines.txt:5:10: error: expected string not found in input
; ERRMSG: defines.txt:1:1: note: with variable "VALUE" equal to "20"
; ERRMSG: defines.txt:4:1: note: possible intended match here
7 changes: 7 additions & 0 deletions utils/FileCheck/FileCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ static cl::list<std::string> ImplicitCheckNot(
"this pattern occur which are not matched by a positive pattern"),
cl::value_desc("pattern"));

static cl::list<std::string> GlobalDefines("D", cl::Prefix,
cl::desc("Define a variable to be used in capture patterns."),
cl::value_desc("VAR=VALUE"));

static cl::opt<bool> AllowEmptyInput(
"allow-empty", cl::init(false),
cl::desc("Allow the input file to be empty. This is useful when making\n"
Expand Down Expand Up @@ -1295,6 +1299,9 @@ bool CheckInput(SourceMgr &SM, StringRef Buffer,
/// VariableTable - This holds all the current filecheck variables.
StringMap<StringRef> VariableTable;

for (const auto& Def : GlobalDefines)
VariableTable.insert(StringRef(Def).split('='));

unsigned i = 0, j = 0, e = CheckStrings.size();
while (true) {
StringRef CheckRegion;
Expand Down

0 comments on commit 7c37aa1

Please sign in to comment.