[libc-commits] [libc] [libc] Move ASSERT_ERRNO_* macro to ErrnoCheckingTest.h (PR #158727)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Mon Sep 15 12:59:54 PDT 2025


https://github.com/vonosmas created https://github.com/llvm/llvm-project/pull/158727

Move these macro away from Test.h, since the generic Test.h (and associated test framework library) doesn't #include or depend on any errno-handling logic. Conversely, all tests that directly ASSERT various errno values are now migrated to ErrnoCheckingTest framework, which clears it our / validates it after every use case.

>From d8448e8c819e377a56a2d9039fbcdd145db77fcc Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas at gmail.com>
Date: Mon, 15 Sep 2025 12:56:40 -0700
Subject: [PATCH] [libc] Move ASSERT_ERRNO_* macro to ErrnoCheckingTest.h

Move these macro away from Test.h, since the generic Test.h (and
associated test framework library) doesn't #include or depend on
any errno-handling logic. Conversely, all tests that directly ASSERT
various errno values are now migrated to ErrnoCheckingTest framework,
which clears it our / validates it after every use case.
---
 libc/test/UnitTest/ErrnoCheckingTest.h | 15 +++++++++++++++
 libc/test/UnitTest/Test.h              | 15 ---------------
 libc/test/src/errno/CMakeLists.txt     |  1 +
 libc/test/src/errno/errno_test.cpp     |  1 +
 4 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/libc/test/UnitTest/ErrnoCheckingTest.h b/libc/test/UnitTest/ErrnoCheckingTest.h
index 4b7ff452f409c..5b1bc9441d830 100644
--- a/libc/test/UnitTest/ErrnoCheckingTest.h
+++ b/libc/test/UnitTest/ErrnoCheckingTest.h
@@ -13,6 +13,21 @@
 #include "src/__support/macros/config.h"
 #include "test/UnitTest/Test.h"
 
+// Define macro to validate the value stored in the errno and restore it
+// to zero.
+
+#define ASSERT_ERRNO_EQ(VAL)                                                   \
+  do {                                                                         \
+    ASSERT_EQ(VAL, static_cast<int>(libc_errno));                              \
+    libc_errno = 0;                                                            \
+  } while (0)
+#define ASSERT_ERRNO_SUCCESS() ASSERT_EQ(0, static_cast<int>(libc_errno))
+#define ASSERT_ERRNO_FAILURE()                                                 \
+  do {                                                                         \
+    ASSERT_NE(0, static_cast<int>(libc_errno));                                \
+    libc_errno = 0;                                                            \
+  } while (0)
+
 namespace LIBC_NAMESPACE_DECL {
 namespace testing {
 
diff --git a/libc/test/UnitTest/Test.h b/libc/test/UnitTest/Test.h
index e70fc51869624..6643e3882fd2b 100644
--- a/libc/test/UnitTest/Test.h
+++ b/libc/test/UnitTest/Test.h
@@ -37,21 +37,6 @@
 #include "LibcTest.h"
 #endif
 
-// These are defined the same way for each framework, in terms of the macros
-// they all provide.
-
-#define ASSERT_ERRNO_EQ(VAL)                                                   \
-  do {                                                                         \
-    ASSERT_EQ(VAL, static_cast<int>(libc_errno));                              \
-    libc_errno = 0;                                                            \
-  } while (0)
-#define ASSERT_ERRNO_SUCCESS() ASSERT_EQ(0, static_cast<int>(libc_errno))
-#define ASSERT_ERRNO_FAILURE()                                                 \
-  do {                                                                         \
-    ASSERT_NE(0, static_cast<int>(libc_errno));                                \
-    libc_errno = 0;                                                            \
-  } while (0)
-
 // Some macro utility to append file names with LIBC_TEST macro's value to be
 // used in stdio tests.
 #undef STR
diff --git a/libc/test/src/errno/CMakeLists.txt b/libc/test/src/errno/CMakeLists.txt
index b73962fb4de4d..264574204e6cb 100644
--- a/libc/test/src/errno/CMakeLists.txt
+++ b/libc/test/src/errno/CMakeLists.txt
@@ -12,4 +12,5 @@ add_libc_unittest(
     errno_test.cpp
   DEPENDS
     libc.src.errno.errno
+    libc.test.UnitTest.ErrnoCheckingTest
 )
diff --git a/libc/test/src/errno/errno_test.cpp b/libc/test/src/errno/errno_test.cpp
index de82b0077f177..32fb3ec764063 100644
--- a/libc/test/src/errno/errno_test.cpp
+++ b/libc/test/src/errno/errno_test.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/__support/libc_errno.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcErrnoTest, Basic) {



More information about the libc-commits mailing list