[llvm] 5bdf25b - [Testing/Support] llvm::Optional => std::optional

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 22:35:46 PST 2022


Author: Fangrui Song
Date: 2022-12-14T06:35:39Z
New Revision: 5bdf25bc38baf33eb354a3dfb6959dce0aa30a85

URL: https://github.com/llvm/llvm-project/commit/5bdf25bc38baf33eb354a3dfb6959dce0aa30a85
DIFF: https://github.com/llvm/llvm-project/commit/5bdf25bc38baf33eb354a3dfb6959dce0aa30a85.diff

LOG: [Testing/Support] llvm::Optional => std::optional

SupportHelpers.h supports both for now to ease migration.

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Testing/Support/Error.h b/llvm/include/llvm/Testing/Support/Error.h
index 14bac9fd9f8a0..f6634d52eb893 100644
--- a/llvm/include/llvm/Testing/Support/Error.h
+++ b/llvm/include/llvm/Testing/Support/Error.h
@@ -84,7 +84,7 @@ class ValueMatchesPoly {
 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 @@ class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
   }
 
 private:
-  Optional<testing::Matcher<InfoT &>> Matcher;
+  std::optional<testing::Matcher<InfoT &>> Matcher;
 };
 
 class ErrorMessageMatches

diff  --git a/llvm/include/llvm/Testing/Support/SupportHelpers.h b/llvm/include/llvm/Testing/Support/SupportHelpers.h
index 2745e06e249c1..afcad6f9ba307 100644
--- a/llvm/include/llvm/Testing/Support/SupportHelpers.h
+++ b/llvm/include/llvm/Testing/Support/SupportHelpers.h
@@ -64,18 +64,24 @@ template <class InnerMatcher> class ValueIsMatcher {
       : 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 @@ template <class InnerMatcher> class ValueIsMatcher {
 };
 } // 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) {

diff  --git a/llvm/lib/Testing/Support/Annotations.cpp b/llvm/lib/Testing/Support/Annotations.cpp
index 0f9fcfef681d8..16b57cbcd9ed2 100644
--- a/llvm/lib/Testing/Support/Annotations.cpp
+++ b/llvm/lib/Testing/Support/Annotations.cpp
@@ -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());

diff  --git a/llvm/unittests/Support/MatchersTest.cpp b/llvm/unittests/Support/MatchersTest.cpp
index d07e82d9851be..d7d638a58ace0 100644
--- a/llvm/unittests/Support/MatchersTest.cpp
+++ b/llvm/unittests/Support/MatchersTest.cpp
@@ -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


        


More information about the llvm-commits mailing list