Skip to content

[BitmaskEnum] Add support for shift operators. #118007

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
Nov 29, 2024
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
26 changes: 26 additions & 0 deletions llvm/include/llvm/ADT/BitmaskEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@
using ::llvm::BitmaskEnumDetail::operator|; \
using ::llvm::BitmaskEnumDetail::operator&; \
using ::llvm::BitmaskEnumDetail::operator^; \
using ::llvm::BitmaskEnumDetail::operator<<; \
using ::llvm::BitmaskEnumDetail::operator>>; \
using ::llvm::BitmaskEnumDetail::operator|=; \
using ::llvm::BitmaskEnumDetail::operator&=; \
using ::llvm::BitmaskEnumDetail::operator^=; \
using ::llvm::BitmaskEnumDetail::operator<<=; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the commit message you mention operator>>, should this be supported here too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot to git add those changes. Fixed now.

using ::llvm::BitmaskEnumDetail::operator>>=; \
/* Force a semicolon at the end of this macro. */ \
using ::llvm::BitmaskEnumDetail::any

Expand Down Expand Up @@ -162,6 +166,16 @@ constexpr E operator^(E LHS, E RHS) {
return static_cast<E>(Underlying(LHS) ^ Underlying(RHS));
}

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr E operator<<(E LHS, E RHS) {
return static_cast<E>(Underlying(LHS) << Underlying(RHS));
}

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr E operator>>(E LHS, E RHS) {
return static_cast<E>(Underlying(LHS) >> Underlying(RHS));
}

// |=, &=, and ^= return a reference to LHS, to match the behavior of the
// operators on builtin types.

Expand All @@ -183,6 +197,18 @@ E &operator^=(E &LHS, E RHS) {
return LHS;
}

template <typename e, typename = std::enable_if_t<is_bitmask_enum<e>::value>>
e &operator<<=(e &lhs, e rhs) {
lhs = lhs << rhs;
return lhs;
}

template <typename e, typename = std::enable_if_t<is_bitmask_enum<e>::value>>
e &operator>>=(e &lhs, e rhs) {
lhs = lhs >> rhs;
return lhs;
}

} // namespace BitmaskEnumDetail

// Enable bitmask enums in namespace ::llvm and all nested namespaces.
Expand Down
16 changes: 16 additions & 0 deletions llvm/unittests/ADT/BitmaskEnumTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ TEST(BitmaskEnumTest, BitwiseXorEquals) {
EXPECT_EQ(V3, f2);
}

TEST(BitmaskEnumTest, BitwiseShift) {
Flags f = (F1 << F1);
EXPECT_EQ(f, F2);

Flags f2 = F1;
f2 <<= F1;
EXPECT_EQ(f2, F2);

Flags f3 = (F1 >> F1);
EXPECT_EQ(f3, F0);

Flags f4 = F1;
f4 >>= F1;
EXPECT_EQ(f4, F0);
}

TEST(BitmaskEnumTest, ConstantExpression) {
constexpr Flags f1 = ~F1;
constexpr Flags f2 = F1 | F2;
Expand Down