diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h --- a/llvm/include/llvm/ADT/BitVector.h +++ b/llvm/include/llvm/ADT/BitVector.h @@ -23,6 +23,7 @@ #include #include #include +#include #include namespace llvm { @@ -144,6 +145,15 @@ clear_unused_bits(); } + /// Creates a bitvector with the specified values. + static BitVector make(std::initializer_list Values) { + BitVector Result(Values.size()); + unsigned Idx = 0; + for (bool Value : Values) + Result[Idx++] = Value; + return Result; + } + /// empty - Tests whether there are no bits in this bitvector. bool empty() const { return Size == 0; } diff --git a/llvm/include/llvm/ADT/SmallBitVector.h b/llvm/include/llvm/ADT/SmallBitVector.h --- a/llvm/include/llvm/ADT/SmallBitVector.h +++ b/llvm/include/llvm/ADT/SmallBitVector.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -168,6 +169,15 @@ delete getPointer(); } + /// Creates a bitvector with the specified values. + static SmallBitVector make(std::initializer_list Values) { + SmallBitVector Result(Values.size()); + unsigned Idx = 0; + for (bool Value : Values) + Result[Idx++] = Value; + return Result; + } + using const_set_bits_iterator = const_set_bits_iterator_impl; using set_iterator = const_set_bits_iterator; diff --git a/llvm/unittests/ADT/BitVectorTest.cpp b/llvm/unittests/ADT/BitVectorTest.cpp --- a/llvm/unittests/ADT/BitVectorTest.cpp +++ b/llvm/unittests/ADT/BitVectorTest.cpp @@ -23,6 +23,15 @@ typedef ::testing::Types BitVectorTestTypes; TYPED_TEST_SUITE(BitVectorTest, BitVectorTestTypes, ); +TYPED_TEST(BitVectorTest, Make) { + auto Vec = TypeParam::make({true, false, true, true}); + EXPECT_EQ(4U, Vec.size()); + EXPECT_EQ(true, Vec[0]); + EXPECT_EQ(false, Vec[1]); + EXPECT_EQ(true, Vec[2]); + EXPECT_EQ(true, Vec[3]); +} + TYPED_TEST(BitVectorTest, TrivialOperation) { TypeParam Vec; EXPECT_EQ(0U, Vec.count());