llvm.org GIT mirror llvm / 526775b
[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 Thomas Preud'homme 3 months ago
3 changed file(s) with 22 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
6868 /// \returns this variable's value.
6969 Optional getValue() const { return Value; }
7070
71 /// Sets value of this numeric variable if not defined. \returns whether the
72 /// variable was already defined.
73 bool setValue(uint64_t Value);
74
75 /// Clears value of this numeric variable. \returns whether the variable was
76 /// already undefined.
77 bool clearValue();
71 /// Sets value of this numeric variable, if undefined. Triggers an assertion
72 /// failure if the variable is actually defined.
73 void setValue(uint64_t Value);
74
75 /// Clears value of this numeric variable, regardless of whether it is
76 /// currently defined or not.
77 void clearValue();
7878
7979 /// \returns the line number where this variable is defined.
8080 size_t getDefLineNumber() { return DefLineNumber; }
2323
2424 using namespace llvm;
2525
26 bool FileCheckNumericVariable::setValue(uint64_t NewValue) {
27 if (Value)
28 return true;
26 void FileCheckNumericVariable::setValue(uint64_t NewValue) {
27 assert(!Value && "Overwriting numeric variable's value is not allowed");
2928 Value = NewValue;
30 return false;
31 }
32
33 bool FileCheckNumericVariable::clearValue() {
29 }
30
31 void FileCheckNumericVariable::clearValue() {
3432 if (!Value)
35 return true;
33 return;
3634 Value = None;
37 return false;
3835 }
3936
4037 Expected FileCheckExpression::eval() const {
624621 if (MatchedValue.getAsInteger(10, Val))
625622 return FileCheckErrorDiagnostic::get(SM, MatchedValue,
626623 "Unable to represent numeric value");
627 if (DefinedNumericVariable->setValue(Val))
628 llvm_unreachable("Numeric variable redefined");
624 DefinedNumericVariable->setValue(Val);
629625 }
630626
631627 // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
1414 class FileCheckTest : public ::testing::Test {};
1515
1616 TEST_F(FileCheckTest, NumericVariable) {
17 // Undefined variable: getValue and clearValue fails, setValue works.
17 // Undefined variable: getValue fails, setValue does not trigger assert.
1818 FileCheckNumericVariable FooVar = FileCheckNumericVariable(1, "FOO");
1919 EXPECT_EQ("FOO", FooVar.getName());
2020 llvm::Optional Value = FooVar.getValue();
2121 EXPECT_FALSE(Value);
22 EXPECT_TRUE(FooVar.clearValue());
23 EXPECT_FALSE(FooVar.setValue(42));
24
25 // Defined variable: getValue returns value set, setValue fails.
22 FooVar.clearValue();
23 FooVar.setValue(42);
24
25 // Defined variable: getValue returns value set.
2626 Value = FooVar.getValue();
2727 EXPECT_TRUE(Value);
2828 EXPECT_EQ(42U, *Value);
29 EXPECT_TRUE(FooVar.setValue(43));
30 Value = FooVar.getValue();
31 EXPECT_TRUE(Value);
32 EXPECT_EQ(42U, *Value);
33
34 // Clearing variable: getValue fails, clearValue again fails.
35 EXPECT_FALSE(FooVar.clearValue());
29
30 // Clearing variable: getValue fails.
31 FooVar.clearValue();
3632 Value = FooVar.getValue();
3733 EXPECT_FALSE(Value);
38 EXPECT_TRUE(FooVar.clearValue());
3934 }
4035
4136 uint64_t doAdd(uint64_t OpL, uint64_t OpR) { return OpL + OpR; }