[libc-commits] [libc] [libc] Annex K: Add constraint handler unit test class (PR #197707)

Victor Campos via libc-commits libc-commits at lists.llvm.org
Tue Jun 23 06:59:32 PDT 2026


https://github.com/vhscampos updated https://github.com/llvm/llvm-project/pull/197707

>From dc4f489a22c35261db9c504054b20b4e410cd0f0 Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Wed, 13 May 2026 13:51:13 +0100
Subject: [PATCH 1/2] [libc] Annex K: Add constraint handler unit test class

This unit test class will be useful for the tests related to Annex K.
The functions in Annex K may call a constraint handler, so this new unit
test class will facilitate the checks that the constraint handling
mechanism is working as expected.
---
 libc/test/UnitTest/CMakeLists.txt             | 11 +++++
 .../UnitTest/ConstraintHandlerCheckingTest.h  | 44 +++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 libc/test/UnitTest/ConstraintHandlerCheckingTest.h

diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index aa245b2234793..b772fc2f2d3c0 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -221,3 +221,14 @@ add_header_library(
     libc.src.__support.macros.properties.architectures
     libc.src.errno.errno
 )
+
+add_header_library(
+  ConstraintHandlerCheckingTest
+  HDRS
+    ConstraintHandlerCheckingTest.h
+  DEPENDS
+    libc.hdr.types.constraint_handler_t
+    libc.hdr.types.errno_t
+    libc.src.stdlib.set_constraint_handler_s
+    libc.src.string.string_utils
+)
diff --git a/libc/test/UnitTest/ConstraintHandlerCheckingTest.h b/libc/test/UnitTest/ConstraintHandlerCheckingTest.h
new file mode 100644
index 0000000000000..a4bde46cea110
--- /dev/null
+++ b/libc/test/UnitTest/ConstraintHandlerCheckingTest.h
@@ -0,0 +1,44 @@
+//===-- ConstraintHandlerCheckingTest.h ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===---------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_UNITTEST_CONSTRAINTHANDLERCHECKINGTEST_H
+#define LLVM_LIBC_TEST_UNITTEST_CONSTRAINTHANDLERCHECKINGTEST_H
+
+#include "hdr/types/constraint_handler_t.h"
+#include "hdr/types/errno_t.h"
+#include "src/stdlib/set_constraint_handler_s.h"
+#include "src/string/string_utils.h"
+#include "test/UnitTest/Test.h"
+
+namespace {
+
+char buffer[300];
+
+void local_constraint_handler(const char *__restrict msg,
+                              void *__restrict /*ptr*/, errno_t /*error*/) {
+  LIBC_NAMESPACE::internal::strlcpy(buffer, msg, sizeof(buffer));
+}
+
+} // anonymous namespace
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace testing {
+
+class ConstraintHandlerCheckingTest : public Test {
+public:
+  void SetUp() override {
+    Test::SetUp();
+    LIBC_NAMESPACE::set_constraint_handler_s(local_constraint_handler);
+  }
+};
+
+} // namespace testing
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_TEST_UNITTEST_CONSTRAINTHANDLERCHECKINGTEST_H

>From ab48b7e72edb20dfadea61c07ee1b7b1fc3b3059 Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Fri, 19 Jun 2026 17:12:46 +0100
Subject: [PATCH 2/2] Change behavior to use an boolean error flag

---
 .../UnitTest/ConstraintHandlerCheckingTest.h   | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/libc/test/UnitTest/ConstraintHandlerCheckingTest.h b/libc/test/UnitTest/ConstraintHandlerCheckingTest.h
index a4bde46cea110..81e3c54db8a0f 100644
--- a/libc/test/UnitTest/ConstraintHandlerCheckingTest.h
+++ b/libc/test/UnitTest/ConstraintHandlerCheckingTest.h
@@ -1,10 +1,16 @@
-//===-- ConstraintHandlerCheckingTest.h ------------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-//===---------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains a test fixture for checking Annex K's constraint handler
+/// behavior.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_TEST_UNITTEST_CONSTRAINTHANDLERCHECKINGTEST_H
 #define LLVM_LIBC_TEST_UNITTEST_CONSTRAINTHANDLERCHECKINGTEST_H
@@ -12,16 +18,15 @@
 #include "hdr/types/constraint_handler_t.h"
 #include "hdr/types/errno_t.h"
 #include "src/stdlib/set_constraint_handler_s.h"
-#include "src/string/string_utils.h"
 #include "test/UnitTest/Test.h"
 
 namespace {
 
-char buffer[300];
+bool error_flag;
 
-void local_constraint_handler(const char *__restrict msg,
+void local_constraint_handler(const char *__restrict /*msg*/,
                               void *__restrict /*ptr*/, errno_t /*error*/) {
-  LIBC_NAMESPACE::internal::strlcpy(buffer, msg, sizeof(buffer));
+  error_flag = true;
 }
 
 } // anonymous namespace
@@ -34,6 +39,7 @@ class ConstraintHandlerCheckingTest : public Test {
 public:
   void SetUp() override {
     Test::SetUp();
+    error_flag = false;
     LIBC_NAMESPACE::set_constraint_handler_s(local_constraint_handler);
   }
 };



More information about the libc-commits mailing list