[ADT] Fix a bug in DenseSet's initializer_list constructor.

Without this fix, DenseSet crashes with an assertion if constructed with an
initializer_list whose length is not a power of two.

llvm-svn: 344542
This commit is contained in:
Lang Hames 2018-10-15 18:34:36 +00:00
parent 4cf1da0e02
commit 28332c415c
2 changed files with 10 additions and 1 deletions

View File

@ -16,6 +16,7 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/type_traits.h"
#include <algorithm>
#include <cstddef>
@ -67,7 +68,7 @@ public:
explicit DenseSetImpl(unsigned InitialReserve = 0) : TheMap(InitialReserve) {}
DenseSetImpl(std::initializer_list<ValueT> Elems)
: DenseSetImpl(Elems.size()) {
: DenseSetImpl(PowerOf2Ceil(Elems.size())) {
insert(Elems.begin(), Elems.end());
}

View File

@ -80,6 +80,14 @@ TYPED_TEST(DenseSetTest, InitializerList) {
EXPECT_EQ(0u, set.count(3));
}
TYPED_TEST(DenseSetTest, InitializerListWithNonPowerOfTwoLength) {
TypeParam set({1, 2, 3});
EXPECT_EQ(3u, set.size());
EXPECT_EQ(1u, set.count(1));
EXPECT_EQ(1u, set.count(2));
EXPECT_EQ(1u, set.count(3));
}
TYPED_TEST(DenseSetTest, ConstIteratorComparison) {
TypeParam set({1});
const TypeParam &cset = set;