Skip to content

[ADT] Add operator! to BitmaskEnum #139958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/include/llvm/ADT/BitmaskEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
using ::llvm::BitmaskEnumDetail::operator^=; \
using ::llvm::BitmaskEnumDetail::operator<<=; \
using ::llvm::BitmaskEnumDetail::operator>>=; \
using ::llvm::BitmaskEnumDetail::operator!; \
/* Force a semicolon at the end of this macro. */ \
using ::llvm::BitmaskEnumDetail::any

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

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr bool operator!(E Val) {
return Val == static_cast<E>(0);
}

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr bool any(E Val) {
return Val != static_cast<E>(0);
Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/ADT/BitmaskEnumTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ TEST(BitmaskEnumTest, BitwiseNot) {
EXPECT_EQ(15, ~V0);
}

TEST(BitmaskEnumTest, BooleanNot) {
bool b0 = !F0;
EXPECT_TRUE(b0);

bool b1 = !(F1 & F2);
EXPECT_TRUE(b1);

bool b2 = !(F2 | F4);
EXPECT_FALSE(b2);
}

enum class FlagsClass {
F0 = 0,
F1 = 1,
Expand Down