Skip to content

Commit 3efb8b2

Browse files
committed
Give APInt move semantics.
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
1 parent 833687b commit 3efb8b2

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

include/llvm/ADT/APSInt.h

+4-10
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,12 @@ class APSInt : public APInt {
3030
explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
3131
: APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
3232

33-
explicit APSInt(const APInt &I, bool isUnsigned = true)
34-
: APInt(I), IsUnsigned(isUnsigned) {}
33+
explicit APSInt(APInt I, bool isUnsigned = true)
34+
: APInt(std::move(I)), IsUnsigned(isUnsigned) {}
3535

36-
APSInt &operator=(const APSInt &RHS) {
37-
APInt::operator=(RHS);
38-
IsUnsigned = RHS.IsUnsigned;
39-
return *this;
40-
}
41-
42-
APSInt &operator=(const APInt &RHS) {
36+
APSInt &operator=(APInt RHS) {
4337
// Retain our current sign.
44-
APInt::operator=(RHS);
38+
APInt::operator=(std::move(RHS));
4539
return *this;
4640
}
4741

unittests/ADT/APSIntTest.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===- llvm/unittest/ADT/APSIntTest.cpp - APSInt unit tests ---------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm/ADT/APSInt.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace llvm;
14+
15+
namespace {
16+
17+
TEST(APSIntTest, MoveTest) {
18+
APSInt A(32, true);
19+
EXPECT_TRUE(A.isUnsigned());
20+
21+
APSInt B(128, false);
22+
A = B;
23+
EXPECT_FALSE(A.isUnsigned());
24+
25+
APSInt C(B);
26+
EXPECT_FALSE(C.isUnsigned());
27+
28+
APInt Wide(256, 0);
29+
const uint64_t *Bits = Wide.getRawData();
30+
APSInt D(std::move(Wide));
31+
EXPECT_TRUE(D.isUnsigned());
32+
EXPECT_EQ(Bits, D.getRawData()); // Verify that "Wide" was really moved.
33+
34+
A = APSInt(64, true);
35+
EXPECT_TRUE(A.isUnsigned());
36+
37+
Wide = APInt(128, 1);
38+
Bits = Wide.getRawData();
39+
A = std::move(Wide);
40+
EXPECT_TRUE(A.isUnsigned());
41+
EXPECT_EQ(Bits, A.getRawData()); // Verify that "Wide" was really moved.
42+
}
43+
44+
}

unittests/ADT/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
set(ADTSources
66
APFloatTest.cpp
77
APIntTest.cpp
8+
APSIntTest.cpp
89
ArrayRefTest.cpp
910
BitVectorTest.cpp
1011
DAGDeltaAlgorithmTest.cpp

0 commit comments

Comments
 (0)