Skip to content

Commit 56f4cee

Browse files
committed
refactor the test and fix alignment issue
1 parent 8348a98 commit 56f4cee

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

llvm/include/llvm/ADT/FunctionExtras.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ using EnableIfCallable = std::enable_if_t<std::disjunction<
8080
template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
8181
protected:
8282
static constexpr size_t InlineStorageSize = sizeof(void *) * 3;
83+
static constexpr size_t InlineStorageAlign = alignof(void *);
8384

8485
template <typename T, class = void>
8586
struct IsSizeLessThanThresholdT : std::false_type {};
@@ -161,7 +162,8 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
161162
// provide three pointers worth of storage here.
162163
// This is mutable as an inlined `const unique_function<void() const>` may
163164
// still modify its own mutable members.
164-
alignas(void *) mutable std::byte InlineStorage[InlineStorageSize];
165+
alignas(InlineStorageAlign) mutable std::byte
166+
InlineStorage[InlineStorageSize];
165167
} StorageUnion;
166168

167169
// A compressed pointer to either our dispatching callback or our table of
@@ -262,7 +264,7 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
262264
bool IsInlineStorage = true;
263265
void *CallableAddr = getInlineStorage();
264266
if (sizeof(CallableT) > InlineStorageSize ||
265-
alignof(CallableT) > alignof(decltype(StorageUnion.InlineStorage))) {
267+
alignof(CallableT) > InlineStorageAlign) {
266268
IsInlineStorage = false;
267269
// Allocate out-of-line storage. FIXME: Use an explicit alignment
268270
// parameter in C++17 mode.

llvm/unittests/ADT/CountCopyAndMove.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using namespace llvm;
1212

13+
int CountCopyAndMove::Constructions = 0;
1314
int CountCopyAndMove::CopyConstructions = 0;
1415
int CountCopyAndMove::CopyAssignments = 0;
1516
int CountCopyAndMove::MoveConstructions = 0;

llvm/unittests/ADT/CountCopyAndMove.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
namespace llvm {
1313

1414
struct CountCopyAndMove {
15+
static int Constructions;
1516
static int CopyConstructions;
1617
static int CopyAssignments;
1718
static int MoveConstructions;
1819
static int MoveAssignments;
1920
static int Destructions;
2021
int val;
2122

22-
CountCopyAndMove() = default;
23-
explicit CountCopyAndMove(int val) : val(val) {}
23+
CountCopyAndMove() { ++Constructions; }
24+
explicit CountCopyAndMove(int val) : val(val) { ++Constructions; }
2425
CountCopyAndMove(const CountCopyAndMove &other) : val(other.val) {
2526
++CopyConstructions;
2627
}
@@ -40,13 +41,18 @@ struct CountCopyAndMove {
4041
~CountCopyAndMove() { ++Destructions; }
4142

4243
static void ResetCounts() {
44+
Constructions = 0;
4345
CopyConstructions = 0;
4446
CopyAssignments = 0;
4547
MoveConstructions = 0;
4648
MoveAssignments = 0;
4749
Destructions = 0;
4850
}
4951

52+
static int TotalConstructions() {
53+
return Constructions + MoveConstructions + CopyConstructions;
54+
}
55+
5056
static int TotalCopies() { return CopyConstructions + CopyAssignments; }
5157

5258
static int TotalMoves() { return MoveConstructions + MoveAssignments; }

llvm/unittests/ADT/FunctionExtrasTest.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/ADT/FunctionExtras.h"
10+
#include "CountCopyAndMove.h"
1011
#include "gtest/gtest.h"
1112

1213
#include <memory>
@@ -313,20 +314,15 @@ const Incomplete incompleteFunctionConst() { return {}; }
313314
// Check that the moved-from captured state is properly destroyed during
314315
// move construction/assignment.
315316
TEST(UniqueFunctionTest, MovedFromStateIsDestroyedCorrectly) {
316-
static int NumOfMovesCalled = 0;
317-
static int NumOfDestructorsCalled = 0;
318-
struct State {
319-
State() = default;
320-
State(State &&) { ++NumOfMovesCalled; }
321-
~State() { ++NumOfDestructorsCalled; }
322-
};
317+
CountCopyAndMove::ResetCounts();
323318
{
324-
unique_function<void()> CapturingFunction{[state = State{}] {}};
319+
unique_function<void()> CapturingFunction{
320+
[Counter = CountCopyAndMove{}] {}};
325321
unique_function<void()> CapturingFunctionMoved{
326322
std::move(CapturingFunction)};
327323
}
328-
printf("%i, %i\n", NumOfMovesCalled, NumOfDestructorsCalled);
329-
EXPECT_EQ(NumOfDestructorsCalled, 1 + NumOfMovesCalled);
324+
EXPECT_EQ(CountCopyAndMove::TotalConstructions(),
325+
CountCopyAndMove::Destructions);
330326
}
331327

332328
} // anonymous namespace

0 commit comments

Comments
 (0)