From 259b1a4ca3ff8cefe819c67d8036412ebf195b1e 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. llvm-svn: 221958 --- llvm/include/llvm/ADT/StringMap.h | 9 +++++---- llvm/unittests/ADT/StringMapTest.cpp | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index 17d5c0d..e1a08d9 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/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/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index 028375d..af9b611 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/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.7.4