[llvm] r247214 - Add makeArrayRef() overload for ArrayRef input (no-op/identity) NFC

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 17:05:05 PDT 2015


Author: mehdi_amini
Date: Wed Sep  9 19:05:04 2015
New Revision: 247214

URL: http://llvm.org/viewvc/llvm-project?rev=247214&view=rev
Log:
Add makeArrayRef() overload for ArrayRef input (no-op/identity) NFC

The purpose is to allow templated wrapper to work with either
ArrayRef or any convertible operation:

template<typename Container>
void wrapper(const Container &Arr) {
  impl(makeArrayRef(Arr));
}

with Container being a std::vector, a SmallVector, or an ArrayRef.

From: Mehdi Amini <mehdi.amini at apple.com>

Modified:
    llvm/trunk/include/llvm/ADT/ArrayRef.h
    llvm/trunk/unittests/ADT/ArrayRefTest.cpp

Modified: llvm/trunk/include/llvm/ADT/ArrayRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ArrayRef.h?rev=247214&r1=247213&r2=247214&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/ArrayRef.h (original)
+++ llvm/trunk/include/llvm/ADT/ArrayRef.h Wed Sep  9 19:05:04 2015
@@ -337,6 +337,16 @@ namespace llvm {
     return Vec;
   }
 
+  /// Construct an ArrayRef from an ArrayRef (no-op) (const)
+  template <typename T> ArrayRef<T> makeArrayRef(const ArrayRef<T> &Vec) {
+    return Vec;
+  }
+
+  /// Construct an ArrayRef from an ArrayRef (no-op)
+  template <typename T> ArrayRef<T> &makeArrayRef(ArrayRef<T> &Vec) {
+    return Vec;
+  }
+
   /// Construct an ArrayRef from a C array.
   template<typename T, size_t N>
   ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {

Modified: llvm/trunk/unittests/ADT/ArrayRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=247214&r1=247213&r2=247214&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ArrayRefTest.cpp Wed Sep  9 19:05:04 2015
@@ -124,4 +124,20 @@ TEST(ArrayRefTest, InitializerList) {
   ArgTest12({1, 2});
 }
 
+// Test that makeArrayRef works on ArrayRef (no-op)
+TEST(ArrayRefTest, makeArrayRef) {
+  static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  // No copy expected for non-const ArrayRef (true no-op)
+  ArrayRef<int> AR1(A1);
+  ArrayRef<int> &AR1Ref = makeArrayRef(AR1);
+  EXPECT_EQ(&AR1, &AR1Ref);
+
+  // A copy is expected for non-const ArrayRef (thin copy)
+  const ArrayRef<int> AR2(A1);
+  const ArrayRef<int> &AR2Ref = makeArrayRef(AR2);
+  EXPECT_NE(&AR2Ref, &AR2);
+  EXPECT_TRUE(AR2.equals(AR2Ref));
+}
+
 } // end anonymous namespace




More information about the llvm-commits mailing list