From 8e704b16f1f4f0723c93a6b3cd7dcb8331c53246 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sun, 27 Jul 2014 01:11:19 +0000 Subject: [PATCH] [ADT] Add a remarkbly useful little helper routine to ArrayRef for checking whether the ArrayRef is equal to an explicit list of arguments. This is particularly easy to implement even without variadic templates because ArrayRef happens to be homogeneously typed. As a consequence we can use a "clever" wrapper type and default arguments to capture in a single method many arguments as well as *how many* arguments the user specified. Thanks to Dave Blaikie for helping me pull together this little helper. Suggestions for how to improve or generalize it are of course welcome. I'll be using it immediately in my follow-up patch. =D git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@214041 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/ArrayRef.h | 55 ++++++++++++++++++++++++++++++++++ unittests/ADT/ArrayRefTest.cpp | 22 ++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/include/llvm/ADT/ArrayRef.h b/include/llvm/ADT/ArrayRef.h index 0fff505d8d0..44400d591da 100644 --- a/include/llvm/ADT/ArrayRef.h +++ b/include/llvm/ADT/ArrayRef.h @@ -11,6 +11,7 @@ #define LLVM_ADT_ARRAYREF_H #include "llvm/ADT/None.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include @@ -43,6 +44,19 @@ namespace llvm { /// The number of elements. size_type Length; + /// \brief A dummy "optional" type that is only created by implicit + /// conversion from a reference to T. + /// + /// This type must *only* be used in a function argument or as a copy of + /// a function argument, as otherwise it will hold a pointer to a temporary + /// past that temporaries' lifetime. + struct TRefOrNothing { + const T *TPtr; + + TRefOrNothing() : TPtr(nullptr) {} + TRefOrNothing(const T &TRef) : TPtr(&TRef) {} + }; + public: /// @name Constructors /// @{ @@ -175,6 +189,47 @@ namespace llvm { return std::vector(Data, Data+Length); } + /// @} + /// @{ + /// @name Convenience methods + + /// @brief Predicate for testing that the array equals the exact sequence of + /// arguments. + /// + /// Will return false if the size is not equal to the exact number of + /// arguments given or if the array elements don't equal the argument + /// elements in order. Currently supports up to 16 arguments, but can + /// easily be extended. + bool equals(TRefOrNothing Arg0 = TRefOrNothing(), + TRefOrNothing Arg1 = TRefOrNothing(), + TRefOrNothing Arg2 = TRefOrNothing(), + TRefOrNothing Arg3 = TRefOrNothing(), + TRefOrNothing Arg4 = TRefOrNothing(), + TRefOrNothing Arg5 = TRefOrNothing(), + TRefOrNothing Arg6 = TRefOrNothing(), + TRefOrNothing Arg7 = TRefOrNothing(), + TRefOrNothing Arg8 = TRefOrNothing(), + TRefOrNothing Arg9 = TRefOrNothing(), + TRefOrNothing Arg10 = TRefOrNothing(), + TRefOrNothing Arg11 = TRefOrNothing(), + TRefOrNothing Arg12 = TRefOrNothing(), + TRefOrNothing Arg13 = TRefOrNothing(), + TRefOrNothing Arg14 = TRefOrNothing(), + TRefOrNothing Arg15 = TRefOrNothing()) { + TRefOrNothing Args[] = {Arg0, Arg1, Arg2, Arg3, Arg4, Arg5, + Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, + Arg12, Arg13, Arg14, Arg15}; + if (size() > array_lengthof(Args)) + return false; + + for (unsigned i = 0, e = size(); i != e; ++i) + if (Args[i].TPtr == nullptr || (*this)[i] != *Args[i].TPtr) + return false; + + // Either the size is exactly as many args, or the next arg must be null. + return size() == array_lengthof(Args) || Args[size()].TPtr == nullptr; + } + /// @} }; diff --git a/unittests/ADT/ArrayRefTest.cpp b/unittests/ADT/ArrayRefTest.cpp index 293afc6ea37..83a012d0cb1 100644 --- a/unittests/ADT/ArrayRefTest.cpp +++ b/unittests/ADT/ArrayRefTest.cpp @@ -36,5 +36,27 @@ TEST(ArrayRefTest, DropBack) { EXPECT_TRUE(AR1.drop_back().equals(AR2)); } +TEST(ArrayRefTest, Equals) { + static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + ArrayRef AR1(A1); + EXPECT_TRUE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 8)); + EXPECT_FALSE(AR1.equals(8, 1, 2, 4, 5, 6, 6, 7)); + EXPECT_FALSE(AR1.equals(2, 4, 5, 6, 6, 7, 8, 1)); + EXPECT_FALSE(AR1.equals(0, 1, 2, 4, 5, 6, 6, 7)); + EXPECT_FALSE(AR1.equals(1, 2, 42, 4, 5, 6, 7, 8)); + EXPECT_FALSE(AR1.equals(42, 2, 3, 4, 5, 6, 7, 8)); + EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 42)); + EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7)); + EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 8, 9)); + + ArrayRef AR1a = AR1.drop_back(); + EXPECT_TRUE(AR1a.equals(1, 2, 3, 4, 5, 6, 7)); + EXPECT_FALSE(AR1a.equals(1, 2, 3, 4, 5, 6, 7, 8)); + + ArrayRef AR1b = AR1a.slice(2, 4); + EXPECT_TRUE(AR1b.equals(3, 4, 5, 6)); + EXPECT_FALSE(AR1b.equals(2, 3, 4, 5, 6)); + EXPECT_FALSE(AR1b.equals(3, 4, 5, 6, 7)); +} } // end anonymous namespace -- 2.34.1