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

Conversation

kparzysz
Copy link
Contributor

Add a logical (boolean) "not" operator.

Add a logical (boolean) "not" operator.
@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-llvm-adt

Author: Krzysztof Parzyszek (kparzysz)

Changes

Add a logical (boolean) "not" operator.


Full diff: https://github.com/llvm/llvm-project/pull/139958.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/BitmaskEnum.h (+6)
  • (modified) llvm/unittests/ADT/BitmaskEnumTest.cpp (+11)
diff --git a/llvm/include/llvm/ADT/BitmaskEnum.h b/llvm/include/llvm/ADT/BitmaskEnum.h
index dcb13bd8ba51a..7214f25b0aa10 100644
--- a/llvm/include/llvm/ADT/BitmaskEnum.h
+++ b/llvm/include/llvm/ADT/BitmaskEnum.h
@@ -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
 
@@ -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);
diff --git a/llvm/unittests/ADT/BitmaskEnumTest.cpp b/llvm/unittests/ADT/BitmaskEnumTest.cpp
index 2c0a80342a54c..b1ef8482416a9 100644
--- a/llvm/unittests/ADT/BitmaskEnumTest.cpp
+++ b/llvm/unittests/ADT/BitmaskEnumTest.cpp
@@ -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,

@kparzysz
Copy link
Contributor Author

Next PR: #139960

@kparzysz kparzysz merged commit 41fcd7e into main May 15, 2025
13 checks passed
@kparzysz kparzysz deleted the users/kparzysz/spr/n01-bitmask-enum branch May 15, 2025 00:36
TIFitis pushed a commit to TIFitis/llvm-project that referenced this pull request May 19, 2025
Add a logical (boolean) "not" operator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants