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

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Tue Jan 23 07:15:41 PST 2024


https://github.com/SchrodingerZhu created https://github.com/llvm/llvm-project/pull/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.

>From c5e3314074eb00331a40433479b6dddb13e57e9b Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Tue, 23 Jan 2024 10:12:46 -0500
Subject: [PATCH] [libc][NFC] use builder pattern for ErrnoSetterMatcher

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.
---
 libc/test/UnitTest/ErrnoSetterMatcher.h | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

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