[libc-commits] [libc] 2422972 - [libc] Migrate FEnvSafeTest and FPTest to ErrnoCheckingTest. (#152633)

via libc-commits libc-commits at lists.llvm.org
Thu Aug 7 21:50:01 PDT 2025


Author: Alexey Samsonov
Date: 2025-08-07T21:49:57-07:00
New Revision: 2422972eeaebe94f591be2325563785ab7254d4e

URL: https://github.com/llvm/llvm-project/commit/2422972eeaebe94f591be2325563785ab7254d4e
DIFF: https://github.com/llvm/llvm-project/commit/2422972eeaebe94f591be2325563785ab7254d4e.diff

LOG: [libc] Migrate FEnvSafeTest and FPTest to ErrnoCheckingTest. (#152633)

This would ensure that errno value is cleared out before test execution
and tests pass even when LIBC_ERRNO_MODE_SYSTEM_INLINE is specified (and
errno may be clobbered before test execution).

A lot of the tests would fail, however, since errno would end up getting
set to EDOM or ERANGE during test execution and never validated before
the end of the test. This should be fixed - and errno should be
explicitly checked or ignored in all of those cases, but for now add a
TODO to address it later (see open issue #135320) and clear out errno in
test fixture to avoid test failures.

Added: 
    

Modified: 
    libc/test/UnitTest/CMakeLists.txt
    libc/test/UnitTest/FEnvSafeTest.cpp
    libc/test/UnitTest/FEnvSafeTest.h
    libc/test/UnitTest/FPMatcher.h
    utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index d92ab6feccb27..f1a83fc601e5e 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -125,6 +125,7 @@ add_unittest_framework_library(
     RoundingModeUtils.h
   DEPENDS
     LibcTest
+    libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.string_utils
     libc.src.__support.CPP.array
     libc.src.__support.FPUtil.fp_bits

diff  --git a/libc/test/UnitTest/FEnvSafeTest.cpp b/libc/test/UnitTest/FEnvSafeTest.cpp
index 168b1d4159d8d..f644569695eec 100644
--- a/libc/test/UnitTest/FEnvSafeTest.cpp
+++ b/libc/test/UnitTest/FEnvSafeTest.cpp
@@ -9,8 +9,10 @@
 #include "FEnvSafeTest.h"
 
 #include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace testing {
@@ -25,6 +27,10 @@ void FEnvSafeTest::TearDown() {
   if (!should_be_unchanged) {
     restore_fenv();
   }
+  // TODO (PR 135320): Remove this override once all FEnvSafeTest instances are
+  // updated to validate or ignore errno.
+  libc_errno = 0;
+  ErrnoCheckingTest::TearDown();
 }
 
 void FEnvSafeTest::get_fenv(fenv_t &fenv) {

diff  --git a/libc/test/UnitTest/FEnvSafeTest.h b/libc/test/UnitTest/FEnvSafeTest.h
index a3c5e62120bc0..1e10629ba3be8 100644
--- a/libc/test/UnitTest/FEnvSafeTest.h
+++ b/libc/test/UnitTest/FEnvSafeTest.h
@@ -12,6 +12,7 @@
 #include "hdr/types/fenv_t.h"
 #include "src/__support/CPP/utility.h"
 #include "src/__support/macros/config.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/Test.h"
 
 namespace LIBC_NAMESPACE_DECL {
@@ -20,7 +21,7 @@ namespace testing {
 // This provides a test fixture (or base class for other test fixtures) that
 // asserts that each test does not leave the FPU state represented by `fenv_t`
 // (aka `FPState`) perturbed from its initial state.
-class FEnvSafeTest : public Test {
+class FEnvSafeTest : public ErrnoCheckingTest {
 public:
   void TearDown() override;
 

diff  --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index da15cf2907f7c..592cd1b3f37ab 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -14,8 +14,10 @@
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/fpbits_str.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/RoundingModeUtils.h"
 #include "test/UnitTest/StringUtils.h"
 #include "test/UnitTest/Test.h"
@@ -166,7 +168,7 @@ CFPMatcher<T, C> getMatcherComplex(T expectedValue) {
   return CFPMatcher<T, C>(expectedValue);
 }
 
-template <typename T> struct FPTest : public Test {
+template <typename T> struct FPTest : public ErrnoCheckingTest {
   using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
   using StorageType = typename FPBits::StorageType;
   static constexpr StorageType STORAGE_MAX =
@@ -191,6 +193,13 @@ template <typename T> struct FPTest : public Test {
       fputil::testing::RoundingMode::Downward,
       fputil::testing::RoundingMode::TowardZero,
   };
+
+  void TearDown() override {
+    // TODO (PR 135320): Remove this override once all FPTest instances are
+    // updated to validate or ignore errno.
+    libc_errno = 0;
+    ErrnoCheckingTest::TearDown();
+  }
 };
 
 // Add facility to test Flush-Denormal-To-Zero (FTZ) and Denormal-As-Zero (DAZ)

diff  --git a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
index 156515309d8ca..24baaf1983a08 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
@@ -22,11 +22,11 @@ libc_test_library(
         "//libc:__support_macros_properties_types",
         "//libc:__support_osutil_io",
         "//libc:__support_uint128",
-        "//libc:hdr_stdint_proxy",
         "//libc:func_aligned_alloc",
         "//libc:func_free",
         "//libc:func_malloc",
         "//libc:func_realloc",
+        "//libc:hdr_stdint_proxy",
     ],
 )
 
@@ -65,12 +65,12 @@ libc_test_library(
         "//libc:__support_macros_properties_types",
         "//libc:__support_stringutil",
         "//libc:__support_uint128",
-        "//libc:hdr_stdint_proxy",
         "//libc:errno",
         "//libc:func_aligned_alloc",
         "//libc:func_free",
         "//libc:func_malloc",
         "//libc:func_realloc",
+        "//libc:hdr_stdint_proxy",
         "//libc:llvm_libc_macros_stdfix_macros",
         "//llvm:Support",
     ],
@@ -107,6 +107,7 @@ libc_test_library(
     ],
     deps = [
         ":LibcUnitTest",
+        ":errno_test_helpers",
         ":string_utils",
         ":test_logger",
         "//libc:__support_cpp_array",
@@ -119,6 +120,7 @@ libc_test_library(
         "//libc:__support_fputil_fp_bits",
         "//libc:__support_fputil_fpbits_str",
         "//libc:__support_fputil_rounding_mode",
+        "//libc:__support_libc_errno",
         "//libc:__support_macros_config",
         "//libc:__support_macros_properties_architectures",
         "//libc:hdr_fenv_macros",


        


More information about the libc-commits mailing list