Skip to content

Commit 41fcd7e

Browse files
authored
[ADT] Add operator! to BitmaskEnum (llvm#139958)
Add a logical (boolean) "not" operator.
1 parent 8b53ada commit 41fcd7e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

llvm/include/llvm/ADT/BitmaskEnum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
using ::llvm::BitmaskEnumDetail::operator^=; \
9393
using ::llvm::BitmaskEnumDetail::operator<<=; \
9494
using ::llvm::BitmaskEnumDetail::operator>>=; \
95+
using ::llvm::BitmaskEnumDetail::operator!; \
9596
/* Force a semicolon at the end of this macro. */ \
9697
using ::llvm::BitmaskEnumDetail::any
9798

@@ -141,6 +142,11 @@ constexpr unsigned bitWidth(uint64_t Value) {
141142
return Value ? 1 + bitWidth(Value >> 1) : 0;
142143
}
143144

145+
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
146+
constexpr bool operator!(E Val) {
147+
return Val == static_cast<E>(0);
148+
}
149+
144150
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
145151
constexpr bool any(E Val) {
146152
return Val != static_cast<E>(0);

llvm/unittests/ADT/BitmaskEnumTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ TEST(BitmaskEnumTest, BitwiseNot) {
176176
EXPECT_EQ(15, ~V0);
177177
}
178178

179+
TEST(BitmaskEnumTest, BooleanNot) {
180+
bool b0 = !F0;
181+
EXPECT_TRUE(b0);
182+
183+
bool b1 = !(F1 & F2);
184+
EXPECT_TRUE(b1);
185+
186+
bool b2 = !(F2 | F4);
187+
EXPECT_FALSE(b2);
188+
}
189+
179190
enum class FlagsClass {
180191
F0 = 0,
181192
F1 = 1,

0 commit comments

Comments
 (0)