From b93d3dbc1fc963834d51168d34a76815d2906391 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Mon, 19 Jan 2015 03:25:33 +0000 Subject: [PATCH] [tinyptrvector] Add in a MutableArrayRef implicit conversion operator to complement the ArrayRef implicit conversion operator. llvm-svn: 226428 --- llvm/include/llvm/ADT/TinyPtrVector.h | 9 +++++++++ llvm/unittests/ADT/TinyPtrVectorTest.cpp | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/llvm/include/llvm/ADT/TinyPtrVector.h b/llvm/include/llvm/ADT/TinyPtrVector.h index bb31540c508b..f29608f3d3d1 100644 --- a/llvm/include/llvm/ADT/TinyPtrVector.h +++ b/llvm/include/llvm/ADT/TinyPtrVector.h @@ -116,6 +116,15 @@ public: return *Val.template get(); } + // implicit conversion operator to MutableArrayRef. + operator MutableArrayRef() { + if (Val.isNull()) + return None; + if (Val.template is()) + return *Val.getAddrOfPtr1(); + return *Val.template get(); + } + bool empty() const { // This vector can be empty if it contains no element, or if it // contains a pointer to an empty vector. diff --git a/llvm/unittests/ADT/TinyPtrVectorTest.cpp b/llvm/unittests/ADT/TinyPtrVectorTest.cpp index a402e8be9df6..294dfac0c633 100644 --- a/llvm/unittests/ADT/TinyPtrVectorTest.cpp +++ b/llvm/unittests/ADT/TinyPtrVectorTest.cpp @@ -438,3 +438,24 @@ TEST(TinyPtrVectorTest, ArrayRefCtorTest) { EXPECT_TRUE(V[i] == data[i]); } } + +TEST(TinyPtrVectorTest, MutableArrayRefTest) { + int data_array[128]; + std::vector data; + + for (unsigned i = 0, e = 128; i != e; ++i) { + data_array[i] = 324 - int(i); + data.push_back(&data_array[i]); + } + + TinyPtrVector V(data); + EXPECT_TRUE(V.size() == 128); + EXPECT_FALSE(V.empty()); + + MutableArrayRef mut_array = V; + for (unsigned i = 0, e = 128; i != e; ++i) { + EXPECT_TRUE(mut_array[i] == data[i]); + mut_array[i] = 324 + mut_array[i]; + EXPECT_TRUE(mut_array[i] == (324 + data[i])); + } +}