[Testing/Support] llvm::Optional => std::optional
authorFangrui Song <i@maskray.me>
Wed, 14 Dec 2022 06:35:39 +0000 (06:35 +0000)
committerFangrui Song <i@maskray.me>
Wed, 14 Dec 2022 06:35:39 +0000 (06:35 +0000)
SupportHelpers.h supports both for now to ease migration.

llvm/include/llvm/Testing/Support/Error.h
llvm/include/llvm/Testing/Support/SupportHelpers.h
llvm/lib/Testing/Support/Annotations.cpp
llvm/unittests/Support/MatchersTest.cpp

index 14bac9f..f6634d5 100644 (file)
@@ -84,7 +84,7 @@ private:
 template <typename InfoT>
 class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
 public:
-  explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT &>> Matcher)
+  explicit ErrorMatchesMono(std::optional<testing::Matcher<InfoT &>> Matcher)
       : Matcher(std::move(Matcher)) {}
 
   bool MatchAndExplain(const ErrorHolder &Holder,
@@ -126,7 +126,7 @@ public:
   }
 
 private:
-  Optional<testing::Matcher<InfoT &>> Matcher;
+  std::optional<testing::Matcher<InfoT &>> Matcher;
 };
 
 class ErrorMessageMatches
index 2745e06..afcad6f 100644 (file)
@@ -64,18 +64,24 @@ public:
       : ValueMatcher(ValueMatcher) {}
 
   template <class T>
-  operator ::testing::Matcher<const llvm::Optional<T> &>() const {
+  operator ::testing::Matcher<const std::optional<T> &>() const {
     return ::testing::MakeMatcher(
         new Impl<T>(::testing::SafeMatcherCast<T>(ValueMatcher)));
   }
 
   template <class T>
-  class Impl : public ::testing::MatcherInterface<const llvm::Optional<T> &> {
+  operator ::testing::Matcher<const Optional<T> &>() const {
+    return ::testing::MakeMatcher(
+        new Impl<T, Optional<T>>(::testing::SafeMatcherCast<T>(ValueMatcher)));
+  }
+
+  template <class T, class O = std::optional<T>>
+  class Impl : public ::testing::MatcherInterface<const O &> {
   public:
     explicit Impl(const ::testing::Matcher<T> &ValueMatcher)
         : ValueMatcher(ValueMatcher) {}
 
-    bool MatchAndExplain(const llvm::Optional<T> &Input,
+    bool MatchAndExplain(const O &Input,
                          testing::MatchResultListener *L) const override {
       return Input && ValueMatcher.MatchAndExplain(*Input, L);
     }
@@ -98,7 +104,7 @@ private:
 };
 } // namespace detail
 
-/// Matches an llvm::Optional<T> with a value that conforms to an inner matcher.
+/// Matches an std::optional<T> with a value that conforms to an inner matcher.
 /// To match std::nullopt you could use Eq(std::nullopt).
 template <class InnerMatcher>
 detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) {
index 0f9fcfe..16b57cb 100644 (file)
@@ -27,8 +27,8 @@ Annotations::Annotations(llvm::StringRef Text) {
   auto Require = [Text](bool Assertion, const char *Msg) {
     require(Assertion, Msg, Text);
   };
-  llvm::Optional<llvm::StringRef> Name;
-  llvm::Optional<llvm::StringRef> Payload;
+  std::optional<llvm::StringRef> Name;
+  std::optional<llvm::StringRef> Payload;
   llvm::SmallVector<Annotation, 8> OpenRanges;
 
   Code.reserve(Text.size());
index d07e82d..d7d638a 100644 (file)
@@ -18,8 +18,8 @@ using ::testing::Not;
 
 namespace {
 TEST(MatchersTest, Optional) {
-  EXPECT_THAT(llvm::Optional<int>(std::nullopt), Not(llvm::ValueIs(_)));
-  EXPECT_THAT(llvm::Optional<int>(10), llvm::ValueIs(10));
-  EXPECT_THAT(llvm::Optional<int>(10), llvm::ValueIs(AllOf(Lt(11), Gt(9))));
+  EXPECT_THAT(std::optional<int>(std::nullopt), Not(llvm::ValueIs(_)));
+  EXPECT_THAT(std::optional<int>(10), llvm::ValueIs(10));
+  EXPECT_THAT(std::optional<int>(10), llvm::ValueIs(AllOf(Lt(11), Gt(9))));
 }
 } // namespace