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

Lang Hames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 15 09:11:47 PDT 2018


lhames updated this revision to Diff 169717.
lhames added a comment.

Use PowerOf2Ceil as per Craig's suggestion, and validate set contents in the non power-of-two length initializer list unit test as per Dave's suggestion.


Repository:
  rL LLVM

https://reviews.llvm.org/D53260

Files:
  include/llvm/ADT/DenseSet.h
  unittests/ADT/DenseSetTest.cpp


Index: unittests/ADT/DenseSetTest.cpp
===================================================================
--- unittests/ADT/DenseSetTest.cpp
+++ unittests/ADT/DenseSetTest.cpp
@@ -80,6 +80,14 @@
   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;
Index: include/llvm/ADT/DenseSet.h
===================================================================
--- include/llvm/ADT/DenseSet.h
+++ include/llvm/ADT/DenseSet.h
@@ -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 @@
   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());
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53260.169717.patch
Type: text/x-patch
Size: 1235 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181015/a78b1fe5/attachment.bin>


More information about the llvm-commits mailing list