[llvm] f971e77 - ADT/ArrayRef: Add makeMutableArrayRef overloads
Nicolai Hähnle via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 9 01:00:06 PDT 2022
Author: Nicolai Hähnle
Date: 2022-06-09T09:59:50+02:00
New Revision: f971e77fb4ec12256f4486fe6174e67639f06661
URL: https://github.com/llvm/llvm-project/commit/f971e77fb4ec12256f4486fe6174e67639f06661
DIFF: https://github.com/llvm/llvm-project/commit/f971e77fb4ec12256f4486fe6174e67639f06661.diff
LOG: ADT/ArrayRef: Add makeMutableArrayRef overloads
Equivalent overloads already exist for makeArrayRef.
Differential Revision: https://reviews.llvm.org/D126421
Added:
Modified:
llvm/include/llvm/ADT/ArrayRef.h
llvm/unittests/ADT/ArrayRefTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h
index 9af4232414e57..ee35a5686fc48 100644
--- a/llvm/include/llvm/ADT/ArrayRef.h
+++ b/llvm/include/llvm/ADT/ArrayRef.h
@@ -540,6 +540,42 @@ namespace llvm {
return MutableArrayRef<T>(data, length);
}
+ /// Construct a MutableArrayRef from a SmallVector.
+ template <typename T>
+ MutableArrayRef<T> makeMutableArrayRef(SmallVectorImpl<T> &Vec) {
+ return Vec;
+ }
+
+ /// Construct a MutableArrayRef from a SmallVector.
+ template <typename T, unsigned N>
+ MutableArrayRef<T> makeMutableArrayRef(SmallVector<T, N> &Vec) {
+ return Vec;
+ }
+
+ /// Construct a MutableArrayRef from a std::vector.
+ template<typename T>
+ MutableArrayRef<T> makeMutableArrayRef(std::vector<T> &Vec) {
+ return Vec;
+ }
+
+ /// Construct a MutableArrayRef from a std::array.
+ template <typename T, std::size_t N>
+ MutableArrayRef<T> makeMutableArrayRef(std::array<T, N> &Arr) {
+ return Arr;
+ }
+
+ /// Construct a MutableArrayRef from a MutableArrayRef (no-op) (const)
+ template <typename T>
+ MutableArrayRef<T> makeMutableArrayRef(const MutableArrayRef<T> &Vec) {
+ return Vec;
+ }
+
+ /// Construct a MutableArrayRef from a C array.
+ template<typename T, size_t N>
+ MutableArrayRef<T> makeMutableArrayRef(T (&Arr)[N]) {
+ return MutableArrayRef<T>(Arr);
+ }
+
/// @}
/// @name ArrayRef Comparison Operators
/// @{
diff --git a/llvm/unittests/ADT/ArrayRefTest.cpp b/llvm/unittests/ADT/ArrayRefTest.cpp
index f3da4c675a85b..eded12d37bb7d 100644
--- a/llvm/unittests/ADT/ArrayRefTest.cpp
+++ b/llvm/unittests/ADT/ArrayRefTest.cpp
@@ -229,7 +229,6 @@ TEST(ArrayRefTest, EmptyInitializerList) {
EXPECT_TRUE(A.empty());
}
-// Test that makeArrayRef works on ArrayRef (no-op)
TEST(ArrayRefTest, makeArrayRef) {
static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
@@ -265,4 +264,43 @@ TEST(ArrayRefTest, makeArrayRefFromStdArray) {
static_assert(std::is_trivially_copyable<ArrayRef<int>>::value,
"trivially copyable");
+TEST(ArrayRefTest, makeMutableArrayRef) {
+ int A = 0;
+ auto AR = makeMutableArrayRef(A);
+ EXPECT_EQ(AR.data(), &A);
+ EXPECT_EQ(AR.size(), (size_t)1);
+
+ AR[0] = 1;
+ EXPECT_EQ(A, 1);
+
+ int B[] = {0, 1, 2, 3};
+ auto BR1 = makeMutableArrayRef(&B[0], 4);
+ auto BR2 = makeMutableArrayRef(B);
+ EXPECT_EQ(BR1.data(), &B[0]);
+ EXPECT_EQ(BR1.size(), (size_t)4);
+ EXPECT_EQ(BR2.data(), &B[0]);
+ EXPECT_EQ(BR2.size(), (size_t)4);
+
+ SmallVector<int> C1;
+ SmallVectorImpl<int> &C2 = C1;
+ C1.resize(5);
+ auto CR1 = makeMutableArrayRef(C1);
+ auto CR2 = makeMutableArrayRef(C2);
+ EXPECT_EQ(CR1.data(), C1.data());
+ EXPECT_EQ(CR1.size(), C1.size());
+ EXPECT_EQ(CR2.data(), C2.data());
+ EXPECT_EQ(CR2.size(), C2.size());
+
+ std::vector<int> D;
+ D.resize(5);
+ auto DR = makeMutableArrayRef(D);
+ EXPECT_EQ(DR.data(), D.data());
+ EXPECT_EQ(DR.size(), D.size());
+
+ std::array<int, 5> E;
+ auto ER = makeMutableArrayRef(E);
+ EXPECT_EQ(ER.data(), E.data());
+ EXPECT_EQ(ER.size(), E.size());
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list