From: Mehdi Amini Date: Thu, 10 Sep 2015 00:05:04 +0000 (+0000) Subject: Add makeArrayRef() overload for ArrayRef input (no-op/identity) NFC X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=4806ac4a7e4cfb6d6f386248e48849114fca085d 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 void wrapper(const Container &Arr) { impl(makeArrayRef(Arr)); } with Container being a std::vector, a SmallVector, or an ArrayRef. From: Mehdi Amini git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247214 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/ArrayRef.h b/include/llvm/ADT/ArrayRef.h index c28fdffae73..aaf32ff020e 100644 --- a/include/llvm/ADT/ArrayRef.h +++ b/include/llvm/ADT/ArrayRef.h @@ -337,6 +337,16 @@ namespace llvm { return Vec; } + /// Construct an ArrayRef from an ArrayRef (no-op) (const) + template ArrayRef makeArrayRef(const ArrayRef &Vec) { + return Vec; + } + + /// Construct an ArrayRef from an ArrayRef (no-op) + template ArrayRef &makeArrayRef(ArrayRef &Vec) { + return Vec; + } + /// Construct an ArrayRef from a C array. template ArrayRef makeArrayRef(const T (&Arr)[N]) { diff --git a/unittests/ADT/ArrayRefTest.cpp b/unittests/ADT/ArrayRefTest.cpp index 3ab13a34829..6cbadd6bc22 100644 --- a/unittests/ADT/ArrayRefTest.cpp +++ b/unittests/ADT/ArrayRefTest.cpp @@ -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 AR1(A1); + ArrayRef &AR1Ref = makeArrayRef(AR1); + EXPECT_EQ(&AR1, &AR1Ref); + + // A copy is expected for non-const ArrayRef (thin copy) + const ArrayRef AR2(A1); + const ArrayRef &AR2Ref = makeArrayRef(AR2); + EXPECT_NE(&AR2Ref, &AR2); + EXPECT_TRUE(AR2.equals(AR2Ref)); +} + } // end anonymous namespace