From 7987683c3912056b4c54bbb8c7b38665f1049798 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Fri, 14 Nov 2014 00:41:46 +0000 Subject: [PATCH] StringMap: Test and finish off supporting perfectly forwarded values in StringMap operations. Followup to r221946. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221958 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/StringMap.h | 9 +++++---- unittests/ADT/StringMapTest.cpp | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/llvm/ADT/StringMap.h b/include/llvm/ADT/StringMap.h index 17d5c0d6091..e1a08d9d7f9 100644 --- a/include/llvm/ADT/StringMap.h +++ b/include/llvm/ADT/StringMap.h @@ -170,9 +170,9 @@ public: /// Create - Create a StringMapEntry with normal malloc/free. template - static StringMapEntry *Create(StringRef Key, InitType InitVal) { + static StringMapEntry *Create(StringRef Key, InitType &&InitVal) { MallocAllocator A; - return Create(Key, A, std::move(InitVal)); + return Create(Key, A, std::forward(InitVal)); } static StringMapEntry *Create(StringRef Key) { @@ -367,8 +367,9 @@ public: /// exists, return it. Otherwise, default construct a value, insert it, and /// return. template - MapEntryTy &GetOrCreateValue(StringRef Key, InitTy Val) { - return *insert(std::make_pair(Key, std::move(Val))).first; + MapEntryTy &GetOrCreateValue(StringRef Key, InitTy &&Val) { + return *insert(std::pair( + Key, std::forward(Val))).first; } MapEntryTy &GetOrCreateValue(StringRef Key) { diff --git a/unittests/ADT/StringMapTest.cpp b/unittests/ADT/StringMapTest.cpp index 028375d7b4b..af9b6115f85 100644 --- a/unittests/ADT/StringMapTest.cpp +++ b/unittests/ADT/StringMapTest.cpp @@ -256,9 +256,15 @@ TEST_F(StringMapTest, NonDefaultConstructable) { ASSERT_EQ(iter->second.i, 123); } +struct Immovable { + Immovable() {} + Immovable(Immovable&&) LLVM_DELETED_FUNCTION; // will disable the other special members +}; + struct MoveOnly { int i; MoveOnly(int i) : i(i) {} + MoveOnly(const Immovable&) : i(0) {} MoveOnly(MoveOnly &&RHS) : i(RHS.i) {} MoveOnly &operator=(MoveOnly &&RHS) { i = RHS.i; @@ -270,7 +276,7 @@ private: MoveOnly &operator=(const MoveOnly &) LLVM_DELETED_FUNCTION; }; -TEST_F(StringMapTest, MoveOnlyKey) { +TEST_F(StringMapTest, MoveOnly) { StringMap t; t.GetOrCreateValue("Test", MoveOnly(42)); StringRef Key = "Test"; @@ -278,6 +284,14 @@ TEST_F(StringMapTest, MoveOnlyKey) { ->Destroy(); } +TEST_F(StringMapTest, CtorArg) { + StringMap t; + t.GetOrCreateValue("Test", Immovable()); + StringRef Key = "Test"; + StringMapEntry::Create(Key, Immovable()) + ->Destroy(); +} + TEST_F(StringMapTest, MoveConstruct) { StringMap A; A.GetOrCreateValue("x", 42); -- 2.34.1