From 526775b4c670b3e26709b1f85fbcc522c79139fa Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Wed, 10 Jul 2019 12:49:28 +0000 Subject: [PATCH] [FileCheck] Simplify numeric variable interface Summary: This patch simplifies 2 aspects in the FileCheckNumericVariable code. First, setValue() method is turned into a void function since being called only on undefined variable is an invariant and is now asserted rather than returned. This remove the assert from the callers. Second, clearValue() method is also turned into a void function since the only caller does not check its return value since it may be trying to clear the value of variable that is already cleared without this being noteworthy. Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson, rnk Subscribers: JonChesterfield, rogfer01, hfinkel, kristina, rnk, tra, arichardson, grimar, dblaikie, probinson, llvm-commits, hiraditya Tags: #llvm Differential Revision: https://reviews.llvm.org/D64231 llvm-svn: 365249 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365625 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/FileCheck.h | 12 ++++++------ lib/Support/FileCheck.cpp | 14 +++++--------- unittests/Support/FileCheckTest.cpp | 17 ++++++----------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/include/llvm/Support/FileCheck.h b/include/llvm/Support/FileCheck.h index dea68ba482c..b3a8433b54e 100644 --- a/include/llvm/Support/FileCheck.h +++ b/include/llvm/Support/FileCheck.h @@ -69,13 +69,13 @@ public: /// \returns this variable's value. Optional getValue() const { return Value; } - /// Sets value of this numeric variable if not defined. \returns whether the - /// variable was already defined. - bool setValue(uint64_t Value); + /// Sets value of this numeric variable, if undefined. Triggers an assertion + /// failure if the variable is actually defined. + void setValue(uint64_t Value); - /// Clears value of this numeric variable. \returns whether the variable was - /// already undefined. - bool clearValue(); + /// Clears value of this numeric variable, regardless of whether it is + /// currently defined or not. + void clearValue(); /// \returns the line number where this variable is defined. size_t getDefLineNumber() { return DefLineNumber; } diff --git a/lib/Support/FileCheck.cpp b/lib/Support/FileCheck.cpp index 93a3f0513fc..5ec126f934e 100644 --- a/lib/Support/FileCheck.cpp +++ b/lib/Support/FileCheck.cpp @@ -24,18 +24,15 @@ using namespace llvm; -bool FileCheckNumericVariable::setValue(uint64_t NewValue) { - if (Value) - return true; +void FileCheckNumericVariable::setValue(uint64_t NewValue) { + assert(!Value && "Overwriting numeric variable's value is not allowed"); Value = NewValue; - return false; } -bool FileCheckNumericVariable::clearValue() { +void FileCheckNumericVariable::clearValue() { if (!Value) - return true; + return; Value = None; - return false; } Expected FileCheckExpression::eval() const { @@ -625,8 +622,7 @@ Expected FileCheckPattern::match(StringRef Buffer, size_t &MatchLen, if (MatchedValue.getAsInteger(10, Val)) return FileCheckErrorDiagnostic::get(SM, MatchedValue, "Unable to represent numeric value"); - if (DefinedNumericVariable->setValue(Val)) - llvm_unreachable("Numeric variable redefined"); + DefinedNumericVariable->setValue(Val); } // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after diff --git a/unittests/Support/FileCheckTest.cpp b/unittests/Support/FileCheckTest.cpp index 58550c7b7bd..848ab15116b 100644 --- a/unittests/Support/FileCheckTest.cpp +++ b/unittests/Support/FileCheckTest.cpp @@ -15,28 +15,23 @@ namespace { class FileCheckTest : public ::testing::Test {}; TEST_F(FileCheckTest, NumericVariable) { - // Undefined variable: getValue and clearValue fails, setValue works. + // Undefined variable: getValue fails, setValue does not trigger assert. FileCheckNumericVariable FooVar = FileCheckNumericVariable(1, "FOO"); EXPECT_EQ("FOO", FooVar.getName()); llvm::Optional Value = FooVar.getValue(); EXPECT_FALSE(Value); - EXPECT_TRUE(FooVar.clearValue()); - EXPECT_FALSE(FooVar.setValue(42)); + FooVar.clearValue(); + FooVar.setValue(42); - // Defined variable: getValue returns value set, setValue fails. - Value = FooVar.getValue(); - EXPECT_TRUE(Value); - EXPECT_EQ(42U, *Value); - EXPECT_TRUE(FooVar.setValue(43)); + // Defined variable: getValue returns value set. Value = FooVar.getValue(); EXPECT_TRUE(Value); EXPECT_EQ(42U, *Value); - // Clearing variable: getValue fails, clearValue again fails. - EXPECT_FALSE(FooVar.clearValue()); + // Clearing variable: getValue fails. + FooVar.clearValue(); Value = FooVar.getValue(); EXPECT_FALSE(Value); - EXPECT_TRUE(FooVar.clearValue()); } uint64_t doAdd(uint64_t OpL, uint64_t OpR) { return OpL + OpR; } -- 2.40.0