[llvm] r344542 - [ADT] Fix a bug in DenseSet's initializer_list constructor.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 15 11:34:36 PDT 2018
Author: lhames
Date: Mon Oct 15 11:34:36 2018
New Revision: 344542
URL: http://llvm.org/viewvc/llvm-project?rev=344542&view=rev
Log:
[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.
Modified:
llvm/trunk/include/llvm/ADT/DenseSet.h
llvm/trunk/unittests/ADT/DenseSetTest.cpp
Modified: llvm/trunk/include/llvm/ADT/DenseSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/DenseSet.h?rev=344542&r1=344541&r2=344542&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/DenseSet.h (original)
+++ llvm/trunk/include/llvm/ADT/DenseSet.h Mon Oct 15 11:34:36 2018
@@ -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());
}
Modified: llvm/trunk/unittests/ADT/DenseSetTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/DenseSetTest.cpp?rev=344542&r1=344541&r2=344542&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/DenseSetTest.cpp (original)
+++ llvm/trunk/unittests/ADT/DenseSetTest.cpp Mon Oct 15 11:34:36 2018
@@ -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;
More information about the llvm-commits
mailing list