[libc-commits] [libc] 2cff46f - [libc][NFC] use builder pattern for ErrnoSetterMatcher (#79153)

via libc-commits libc-commits at lists.llvm.org
Tue Jan 23 07:43:29 PST 2024


Author: Schrodinger ZHU Yifan
Date: 2024-01-23T10:43:24-05:00
New Revision: 2cff46f8c0aa88222aba776de094c650aa09a33d

URL: https://github.com/llvm/llvm-project/commit/2cff46f8c0aa88222aba776de094c650aa09a33d
DIFF: https://github.com/llvm/llvm-project/commit/2cff46f8c0aa88222aba776de094c650aa09a33d.diff

LOG: [libc][NFC] use builder pattern for ErrnoSetterMatcher (#79153)

ErrnoSetterMatcher::returns can be misleading as it does not initialize
the errno. This is made worse as later on there is a switch statement on
the errno comparator using __builtin_unreachable(). This patch make
ErrnoSetterMatcher::returns give back a builder that is nomially
different from ErrnoSetterMatcher.

Added: 
    

Modified: 
    libc/test/UnitTest/ErrnoSetterMatcher.h

Removed: 
    


################################################################################
diff  --git a/libc/test/UnitTest/ErrnoSetterMatcher.h b/libc/test/UnitTest/ErrnoSetterMatcher.h
index b748c29751c328..6b15bd4e9b79a4 100644
--- a/libc/test/UnitTest/ErrnoSetterMatcher.h
+++ b/libc/test/UnitTest/ErrnoSetterMatcher.h
@@ -161,10 +161,22 @@ static internal::ErrnoSetterMatcher<RetT> Fails(int ExpectedErrno,
                                             EQ(ExpectedErrno));
 }
 
+template <typename RetT = int> class ErrnoSetterMatcherBuilder {
+public:
+  template <typename T> using Cmp = internal::Comparator<T>;
+  ErrnoSetterMatcherBuilder(Cmp<RetT> cmp) : return_cmp(cmp) {}
+
+  internal::ErrnoSetterMatcher<RetT> with_errno(Cmp<int> cmp) {
+    return internal::ErrnoSetterMatcher<RetT>(return_cmp, cmp);
+  }
+
+private:
+  Cmp<RetT> return_cmp;
+};
+
 template <typename RetT>
-static internal::ErrnoSetterMatcher<RetT>
-returns(internal::Comparator<RetT> cmp) {
-  return internal::ErrnoSetterMatcher<RetT>(cmp);
+static ErrnoSetterMatcherBuilder<RetT> returns(internal::Comparator<RetT> cmp) {
+  return ErrnoSetterMatcherBuilder<RetT>(cmp);
 }
 
 } // namespace ErrnoSetterMatcher


        


More information about the libc-commits mailing list