Skip to content

Commit 7049449

Browse files
author
Thomas Preud'homme
committed
[FileCheck] Fix numeric variable redefinition
Summary: Commit r365249 changed usage of FileCheckNumericVariable to have one instance of that class per variable as opposed to one instance per definition of a given variable as was done before. However, it retained the safety check in setValue that it should only be called with the variable unset, even after r365625. However this causes assert failure when a non-pseudo variable is being redefined. And while redefinition of @line at each CHECK line work in the general case, it caused problem when a substitution failed (fixed in r365624) and still causes problem when a CHECK line does not match since @line's value is cleared after substitutions in match() happened but printSubstitutions also attempts a substitution. This commit solves the root of the problem by changing setValue to set a new value regardless of whether a value was set or not, thus fixing all the aforementioned issues. Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson, rnk Subscribers: hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, tra, rnk, kristina, hfinkel, rogfer01, JonChesterfield Tags: #llvm Differential Revision: https://reviews.llvm.org/D64882 llvm-svn: 366434
1 parent 0c49484 commit 7049449

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

llvm/include/llvm/Support/FileCheck.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,12 @@ class FileCheckNumericVariable {
115115
/// \returns this variable's value.
116116
Optional<uint64_t> getValue() const { return Value; }
117117

118-
/// Sets value of this numeric variable, if undefined. Triggers an assertion
119-
/// failure if the variable is actually defined.
120-
void setValue(uint64_t Value);
118+
/// Sets value of this numeric variable to \p NewValue.
119+
void setValue(uint64_t NewValue) { Value = NewValue; }
121120

122121
/// Clears value of this numeric variable, regardless of whether it is
123122
/// currently defined or not.
124-
void clearValue();
123+
void clearValue() { Value = None; }
125124

126125
/// \returns the line number where this variable is defined, if any, or None
127126
/// if defined before input is parsed.

llvm/lib/Support/FileCheck.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424

2525
using namespace llvm;
2626

27-
void FileCheckNumericVariable::setValue(uint64_t NewValue) {
28-
assert(!Value && "Overwriting numeric variable's value is not allowed");
29-
Value = NewValue;
30-
}
31-
32-
void FileCheckNumericVariable::clearValue() {
33-
if (!Value)
34-
return;
35-
Value = None;
36-
}
37-
3827
Expected<uint64_t> FileCheckNumericVariableUse::eval() const {
3928
Optional<uint64_t> Value = NumericVariable->getValue();
4029
if (Value)
@@ -631,10 +620,8 @@ Expected<size_t> FileCheckPattern::match(StringRef Buffer, size_t &MatchLen,
631620
for (const auto &Substitution : Substitutions) {
632621
// Substitute and check for failure (e.g. use of undefined variable).
633622
Expected<std::string> Value = Substitution->getResult();
634-
if (!Value) {
635-
Context->LineVariable->clearValue();
623+
if (!Value)
636624
return Value.takeError();
637-
}
638625

639626
// Plop it into the regex at the adjusted offset.
640627
TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset,
@@ -644,7 +631,6 @@ Expected<size_t> FileCheckPattern::match(StringRef Buffer, size_t &MatchLen,
644631

645632
// Match the newly constructed regex.
646633
RegExToMatch = TmpStr;
647-
Context->LineVariable->clearValue();
648634
}
649635

650636
SmallVector<StringRef, 4> MatchInfo;

llvm/test/FileCheck/line-count.txt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@
5555
55 BAD11: [[@LINE-1x]]
5656
56 ERR11: line-count.txt:[[#@LINE-1]]:20: error: unexpected characters at end of expression 'x'
5757
57
58-
58 CHECK: [[#@LINE]] CHECK
59-
59 CHECK: [[# @LINE]] CHECK
60-
60 CHECK: [[# @LINE ]] CHECK
61-
61
62-
62 CHECK: [[#@LINE-1]]
63-
63 CHECK: [[# @LINE-1]] CHECK
64-
64 CHECK: [[# @LINE -1]] CHECK
65-
65 CHECK: [[# @LINE - 1]] CHECK
66-
66 CHECK: [[# @LINE - 1 ]] CHECK
58+
; RUN: not FileCheck -check-prefix BAD12 -input-file %s %s 2>&1 \
59+
; RUN: | FileCheck -check-prefix ERR12 %s
60+
60
61+
61 BAD12: [[#@LINE-1]] NOT HERE
62+
62 ERR12: note: with "@LINE-1" equal to "60"
63+
63
64+
64 CHECK: [[#@LINE]] CHECK
65+
65 CHECK: [[# @LINE]] CHECK
66+
66 CHECK: [[# @LINE ]] CHECK
67+
67
68+
68 CHECK: [[#@LINE-1]]
69+
69 CHECK: [[# @LINE-1]] CHECK
70+
70 CHECK: [[# @LINE -1]] CHECK
71+
71 CHECK: [[# @LINE - 1]] CHECK
72+
72 CHECK: [[# @LINE - 1 ]] CHECK

llvm/test/FileCheck/numeric-expression.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN: FileCheck --input-file %s %s
44

55
; Numeric variable definition without spaces.
66
DEF NO SPC
7-
11
7+
10
88
CHECK-LABEL: DEF NO SPC
99
CHECK-NEXT: [[#VAR1:]]
1010

@@ -18,6 +18,12 @@ CHECK-NEXT: [[# VAR1a:]]
1818
CHECK-NEXT: [[# VAR1b :]]
1919
CHECK-NEXT: [[# VAR1c : ]]
2020

21+
; Numeric variable redefinition.
22+
REDEF NO SPC
23+
11
24+
CHECK-LABEL: REDEF
25+
CHECK-NEXT: [[#VAR1:]]
26+
2127
; Numeric expressions using variables defined on other lines without spaces.
2228
USE NO SPC
2329
11

llvm/unittests/Support/FileCheckTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void expectUndefError(const Twine &ExpectedUndefVarName, Error Err) {
5555

5656
TEST_F(FileCheckTest, NumericVariable) {
5757
// Undefined variable: getValue and eval fail, error returned by eval holds
58-
// the name of the undefined variable and setValue does not trigger assert.
58+
// the name of the undefined variable.
5959
FileCheckNumericVariable FooVar = FileCheckNumericVariable("FOO", 1);
6060
EXPECT_EQ("FOO", FooVar.getName());
6161
FileCheckNumericVariableUse FooVarUse =
@@ -64,6 +64,7 @@ TEST_F(FileCheckTest, NumericVariable) {
6464
Expected<uint64_t> EvalResult = FooVarUse.eval();
6565
EXPECT_FALSE(EvalResult);
6666
expectUndefError("FOO", EvalResult.takeError());
67+
6768
FooVar.setValue(42);
6869

6970
// Defined variable: getValue and eval return value set.

0 commit comments

Comments
 (0)