From 70494494c1e381792638eeb350660ca7e8a442c0 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 18 Jul 2019 13:39:04 +0000 Subject: [PATCH] [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 --- llvm/include/llvm/Support/FileCheck.h | 7 +++---- llvm/lib/Support/FileCheck.cpp | 16 +-------------- llvm/test/FileCheck/line-count.txt | 24 ++++++++++++++-------- llvm/test/FileCheck/numeric-expression.txt | 8 +++++++- llvm/unittests/Support/FileCheckTest.cpp | 3 ++- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/llvm/include/llvm/Support/FileCheck.h b/llvm/include/llvm/Support/FileCheck.h index 0cd25a71a3b3..69345266f2ef 100644 --- a/llvm/include/llvm/Support/FileCheck.h +++ b/llvm/include/llvm/Support/FileCheck.h @@ -115,13 +115,12 @@ public: /// \returns this variable's value. Optional getValue() const { return Value; } - /// Sets value of this numeric variable, if undefined. Triggers an assertion - /// failure if the variable is actually defined. - void setValue(uint64_t Value); + /// Sets value of this numeric variable to \p NewValue. + void setValue(uint64_t NewValue) { Value = NewValue; } /// Clears value of this numeric variable, regardless of whether it is /// currently defined or not. - void clearValue(); + void clearValue() { Value = None; } /// \returns the line number where this variable is defined, if any, or None /// if defined before input is parsed. diff --git a/llvm/lib/Support/FileCheck.cpp b/llvm/lib/Support/FileCheck.cpp index e0f17787bdf8..44e394f5b9d4 100644 --- a/llvm/lib/Support/FileCheck.cpp +++ b/llvm/lib/Support/FileCheck.cpp @@ -24,17 +24,6 @@ using namespace llvm; -void FileCheckNumericVariable::setValue(uint64_t NewValue) { - assert(!Value && "Overwriting numeric variable's value is not allowed"); - Value = NewValue; -} - -void FileCheckNumericVariable::clearValue() { - if (!Value) - return; - Value = None; -} - Expected FileCheckNumericVariableUse::eval() const { Optional Value = NumericVariable->getValue(); if (Value) @@ -631,10 +620,8 @@ Expected FileCheckPattern::match(StringRef Buffer, size_t &MatchLen, for (const auto &Substitution : Substitutions) { // Substitute and check for failure (e.g. use of undefined variable). Expected Value = Substitution->getResult(); - if (!Value) { - Context->LineVariable->clearValue(); + if (!Value) return Value.takeError(); - } // Plop it into the regex at the adjusted offset. TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset, @@ -644,7 +631,6 @@ Expected FileCheckPattern::match(StringRef Buffer, size_t &MatchLen, // Match the newly constructed regex. RegExToMatch = TmpStr; - Context->LineVariable->clearValue(); } SmallVector MatchInfo; diff --git a/llvm/test/FileCheck/line-count.txt b/llvm/test/FileCheck/line-count.txt index 0c7be7ebc99b..9f95ae530054 100644 --- a/llvm/test/FileCheck/line-count.txt +++ b/llvm/test/FileCheck/line-count.txt @@ -55,12 +55,18 @@ 55 BAD11: [[@LINE-1x]] 56 ERR11: line-count.txt:[[#@LINE-1]]:20: error: unexpected characters at end of expression 'x' 57 -58 CHECK: [[#@LINE]] CHECK -59 CHECK: [[# @LINE]] CHECK -60 CHECK: [[# @LINE ]] CHECK -61 -62 CHECK: [[#@LINE-1]] -63 CHECK: [[# @LINE-1]] CHECK -64 CHECK: [[# @LINE -1]] CHECK -65 CHECK: [[# @LINE - 1]] CHECK -66 CHECK: [[# @LINE - 1 ]] CHECK +; RUN: not FileCheck -check-prefix BAD12 -input-file %s %s 2>&1 \ +; RUN: | FileCheck -check-prefix ERR12 %s +60 +61 BAD12: [[#@LINE-1]] NOT HERE +62 ERR12: note: with "@LINE-1" equal to "60" +63 +64 CHECK: [[#@LINE]] CHECK +65 CHECK: [[# @LINE]] CHECK +66 CHECK: [[# @LINE ]] CHECK +67 +68 CHECK: [[#@LINE-1]] +69 CHECK: [[# @LINE-1]] CHECK +70 CHECK: [[# @LINE -1]] CHECK +71 CHECK: [[# @LINE - 1]] CHECK +72 CHECK: [[# @LINE - 1 ]] CHECK diff --git a/llvm/test/FileCheck/numeric-expression.txt b/llvm/test/FileCheck/numeric-expression.txt index 3ff7519e5119..29abc385650d 100644 --- a/llvm/test/FileCheck/numeric-expression.txt +++ b/llvm/test/FileCheck/numeric-expression.txt @@ -4,7 +4,7 @@ RUN: FileCheck --input-file %s %s ; Numeric variable definition without spaces. DEF NO SPC -11 +10 CHECK-LABEL: DEF NO SPC CHECK-NEXT: [[#VAR1:]] @@ -18,6 +18,12 @@ CHECK-NEXT: [[# VAR1a:]] CHECK-NEXT: [[# VAR1b :]] CHECK-NEXT: [[# VAR1c : ]] +; Numeric variable redefinition. +REDEF NO SPC +11 +CHECK-LABEL: REDEF +CHECK-NEXT: [[#VAR1:]] + ; Numeric expressions using variables defined on other lines without spaces. USE NO SPC 11 diff --git a/llvm/unittests/Support/FileCheckTest.cpp b/llvm/unittests/Support/FileCheckTest.cpp index 2275d7229997..2b7b2707be87 100644 --- a/llvm/unittests/Support/FileCheckTest.cpp +++ b/llvm/unittests/Support/FileCheckTest.cpp @@ -55,7 +55,7 @@ static void expectUndefError(const Twine &ExpectedUndefVarName, Error Err) { TEST_F(FileCheckTest, NumericVariable) { // Undefined variable: getValue and eval fail, error returned by eval holds - // the name of the undefined variable and setValue does not trigger assert. + // the name of the undefined variable. FileCheckNumericVariable FooVar = FileCheckNumericVariable("FOO", 1); EXPECT_EQ("FOO", FooVar.getName()); FileCheckNumericVariableUse FooVarUse = @@ -64,6 +64,7 @@ TEST_F(FileCheckTest, NumericVariable) { Expected EvalResult = FooVarUse.eval(); EXPECT_FALSE(EvalResult); expectUndefError("FOO", EvalResult.takeError()); + FooVar.setValue(42); // Defined variable: getValue and eval return value set. -- GitLab