[PATCH] D122079: [ADT] Add Enum matcher

Chris Bieneman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 28 14:56:15 PDT 2022


beanz updated this revision to Diff 418714.
beanz added a comment.

Updating to be a specialization on is_contained rather than a standalone thing.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122079/new/

https://reviews.llvm.org/D122079

Files:
  llvm/include/llvm/ADT/STLExtras.h
  llvm/unittests/ADT/STLExtrasTest.cpp


Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -964,4 +964,28 @@
       (std::is_same<double, llvm::TypeAtIndex<2, int, float, double>>::value));
 }
 
+enum Doggos {
+  Floofer,
+  Woofer,
+  SubWoofer,
+  Pupper,
+  Pupperino,
+  Longboi,
+};
+
+TEST(STLExtrasTest, IsContainedInitializerList) {
+  {
+    bool Val = is_contained(Woofer, {Woofer, SubWoofer});
+    ASSERT_TRUE(Val);
+  }
+  {
+    bool Val = is_contained(SubWoofer, {Woofer, SubWoofer});
+    ASSERT_TRUE(Val);
+  }
+  {
+    bool Val = is_contained(Pupper, {Woofer, SubWoofer});
+    ASSERT_FALSE(Val);
+  }
+}
+
 } // namespace
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1657,6 +1657,15 @@
   return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range);
 }
 
+template <typename T>
+constexpr bool is_contained(T value, std::initializer_list<T> set) {
+  // TODO: Use std::find when we switch to C++20.
+  for (T v : set)
+    if (v == value)
+      return true;
+  return false;
+}
+
 /// Wrapper function around std::is_sorted to check if elements in a range \p R
 /// are sorted with respect to a comparator \p C.
 template <typename R, typename Compare> bool is_sorted(R &&Range, Compare C) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122079.418714.patch
Type: text/x-patch
Size: 1481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220328/9231de57/attachment.bin>


More information about the llvm-commits mailing list