Give APInt move semantics.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sun, 2 Mar 2014 20:56:28 +0000 (20:56 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sun, 2 Mar 2014 20:56:28 +0000 (20:56 +0000)
The interaction between defaulted operators and move elision isn't
totally obvious, add a unit test so it doesn't break unintentionally.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202662 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/APSInt.h
unittests/ADT/APSIntTest.cpp [new file with mode: 0644]
unittests/ADT/CMakeLists.txt

index ad035a7c30df477c6c39e5f892ca0031f9911999..053defff52345b374a5adf97d123fd2b5587d4eb 100644 (file)
@@ -30,18 +30,12 @@ public:
   explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
    : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
 
-  explicit APSInt(const APInt &I, bool isUnsigned = true)
-   : APInt(I), IsUnsigned(isUnsigned) {}
+  explicit APSInt(APInt I, bool isUnsigned = true)
+   : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
 
-  APSInt &operator=(const APSInt &RHS) {
-    APInt::operator=(RHS);
-    IsUnsigned = RHS.IsUnsigned;
-    return *this;
-  }
-
-  APSInt &operator=(const APInt &RHS) {
+  APSInt &operator=(APInt RHS) {
     // Retain our current sign.
-    APInt::operator=(RHS);
+    APInt::operator=(std::move(RHS));
     return *this;
   }
 
diff --git a/unittests/ADT/APSIntTest.cpp b/unittests/ADT/APSIntTest.cpp
new file mode 100644 (file)
index 0000000..eef9c8a
--- /dev/null
@@ -0,0 +1,44 @@
+//===- llvm/unittest/ADT/APSIntTest.cpp - APSInt unit tests ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/APSInt.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(APSIntTest, MoveTest) {
+  APSInt A(32, true);
+  EXPECT_TRUE(A.isUnsigned());
+
+  APSInt B(128, false);
+  A = B;
+  EXPECT_FALSE(A.isUnsigned());
+
+  APSInt C(B);
+  EXPECT_FALSE(C.isUnsigned());
+
+  APInt Wide(256, 0);
+  const uint64_t *Bits = Wide.getRawData();
+  APSInt D(std::move(Wide));
+  EXPECT_TRUE(D.isUnsigned());
+  EXPECT_EQ(Bits, D.getRawData()); // Verify that "Wide" was really moved.
+
+  A = APSInt(64, true);
+  EXPECT_TRUE(A.isUnsigned());
+
+  Wide = APInt(128, 1);
+  Bits = Wide.getRawData();
+  A = std::move(Wide);
+  EXPECT_TRUE(A.isUnsigned());
+  EXPECT_EQ(Bits, A.getRawData()); // Verify that "Wide" was really moved.
+}
+
+}
index b7d006ac66d4c2962085007f5a8da2ab4c4aba3b..f26ac02feb23b51bf9acd7ca82c54f4cbe83b80f 100644 (file)
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
 set(ADTSources
   APFloatTest.cpp
   APIntTest.cpp
+  APSIntTest.cpp
   ArrayRefTest.cpp
   BitVectorTest.cpp
   DAGDeltaAlgorithmTest.cpp