[libc-commits] [libc] [libc][test] Support GoogleTest and zxtest in LibcCTest (PR #247019)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Sun Nov 29 14:52:38 PST 2026


https://github.com/frobtech updated https://github.com/llvm/llvm-project/pull/247019

>From 58909b0e3c58a51ecde3919b282b18ce4fff4799 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Sun, 29 Nov 2026 14:15:01 -0800
Subject: [PATCH 1/2] [libc][test] Support GoogleTest in LibcCTest

C header tests could not run under GoogleTest because LibcCTest.cpp
included LibcTest.h directly and used LibcTest-private APIs.

Switch LibcCTest.cpp to include test/UnitTest/Test.h and provide a
GoogleTest implementation (matching Test.h's LIBC_COPT_TEST_USE_GTEST
and LIBC_COPT_TEST_USE_PIGWEED selection, with an explicit #error for
LIBC_COPT_TEST_USE_ZXTEST) using ::testing::RegisterTest and
ADD_FAILURE_AT so each C test registers under its LIBC_C_TEST_NAME
suite/test name and preserves failure file/line reporting. Hoist the
identical libc_c_test_anchor() definition outside the preprocessor
guard.

Part of #91630

Assisted-by: Gemini
---
 libc/test/UnitTest/LibcCTest.cpp | 46 +++++++++++++++++++++++++++++---
 1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/libc/test/UnitTest/LibcCTest.cpp b/libc/test/UnitTest/LibcCTest.cpp
index 8e19f7dd302271..af340ac1e2c26b 100644
--- a/libc/test/UnitTest/LibcCTest.cpp
+++ b/libc/test/UnitTest/LibcCTest.cpp
@@ -12,7 +12,45 @@
 //===----------------------------------------------------------------------===//
 
 #include "test/UnitTest/LibcCTest.h"
-#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
+
+#if defined(LIBC_COPT_TEST_USE_GTEST) || defined(LIBC_COPT_TEST_USE_PIGWEED)
+
+#include <string>
+#include <string_view>
+
+namespace {
+
+class CTest : public LIBC_NAMESPACE::testing::Test {
+  void TestBody() override { libc_c_test_run(); }
+};
+
+[[maybe_unused]] ::testing::TestInfo *const c_test_info = [] {
+  std::string_view full_name(LIBC_C_TEST_NAME);
+  size_t dot = full_name.find('.');
+  std::string suite = dot == std::string_view::npos
+                          ? "LlvmLibcCTest"
+                          : std::string(full_name.substr(0, dot));
+  const char *name = dot == std::string_view::npos ? LIBC_C_TEST_NAME
+                                                   : LIBC_C_TEST_NAME + dot + 1;
+  return ::testing::RegisterTest(
+      suite.c_str(), name, nullptr, nullptr, __FILE__, __LINE__,
+      []() -> LIBC_NAMESPACE::testing::Test * { return new CTest(); });
+}();
+
+} // namespace
+
+extern "C" void libc_c_test_fail(const char *cond, int expected,
+                                 const char *file, int line) {
+  ADD_FAILURE_AT(file, line)
+      << "Expected (" << cond << ") to be " << (expected ? "true" : "false");
+}
+
+#elif defined(LIBC_COPT_TEST_USE_ZXTEST)
+
+#error "LibcCTest does not support LIBC_COPT_TEST_USE_ZXTEST"
+
+#else
 
 namespace {
 class CTest : public LIBC_NAMESPACE::testing::Test {
@@ -25,8 +63,6 @@ class CTest : public LIBC_NAMESPACE::testing::Test {
 
 static CTest c_test_instance;
 
-extern "C" void libc_c_test_anchor() {}
-
 extern "C" void libc_c_test_fail(const char *cond, int expected,
                                  const char *file, int line) {
   LIBC_NAMESPACE::testing::internal::test(
@@ -34,3 +70,7 @@ extern "C" void libc_c_test_fail(const char *cond, int expected,
       static_cast<bool>(expected), cond, expected ? "true" : "false",
       LIBC_NAMESPACE::testing::internal::Location(file, line));
 }
+
+#endif
+
+extern "C" void libc_c_test_anchor() {}

>From 5faa756c99bc7692efa8cf2288777121796ef2c8 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Sun, 29 Nov 2026 14:47:13 -0800
Subject: [PATCH 2/2] [libc][test] Support zxtest in LibcCTest

When LIBC_COPT_TEST_USE_ZXTEST is defined, use zxtest's native C test
frontend directly in LibcCTest.h (redefining TEST(name) to
LIB_ZXTEST_REGISTER(LlvmLibcCTest, name)) and leave LibcCTest.cpp
empty. Because zxtest generates per-test static registration functions,
multiple C tests can also link cleanly into a single zxtest test
binary.

Part of #91630

Assisted-by: Gemini
---
 libc/test/UnitTest/LibcCTest.cpp | 13 ++++++++-----
 libc/test/UnitTest/LibcCTest.h   | 16 +++++++++++++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/libc/test/UnitTest/LibcCTest.cpp b/libc/test/UnitTest/LibcCTest.cpp
index af340ac1e2c26b..9cbeb4c25e00a9 100644
--- a/libc/test/UnitTest/LibcCTest.cpp
+++ b/libc/test/UnitTest/LibcCTest.cpp
@@ -14,7 +14,12 @@
 #include "test/UnitTest/LibcCTest.h"
 #include "test/UnitTest/Test.h"
 
-#if defined(LIBC_COPT_TEST_USE_GTEST) || defined(LIBC_COPT_TEST_USE_PIGWEED)
+#if defined(LIBC_COPT_TEST_USE_ZXTEST)
+
+// With zxtest, LibcCTest.h uses zxtest's native C test frontend directly and
+// does not need C++ registration glue here.
+
+#elif defined(LIBC_COPT_TEST_USE_GTEST) || defined(LIBC_COPT_TEST_USE_PIGWEED)
 
 #include <string>
 #include <string_view>
@@ -46,10 +51,6 @@ extern "C" void libc_c_test_fail(const char *cond, int expected,
       << "Expected (" << cond << ") to be " << (expected ? "true" : "false");
 }
 
-#elif defined(LIBC_COPT_TEST_USE_ZXTEST)
-
-#error "LibcCTest does not support LIBC_COPT_TEST_USE_ZXTEST"
-
 #else
 
 namespace {
@@ -73,4 +74,6 @@ extern "C" void libc_c_test_fail(const char *cond, int expected,
 
 #endif
 
+#ifndef LIBC_COPT_TEST_USE_ZXTEST
 extern "C" void libc_c_test_anchor() {}
+#endif
diff --git a/libc/test/UnitTest/LibcCTest.h b/libc/test/UnitTest/LibcCTest.h
index cac8a271e7d88a..5c686b3cb1eaf9 100644
--- a/libc/test/UnitTest/LibcCTest.h
+++ b/libc/test/UnitTest/LibcCTest.h
@@ -14,6 +14,18 @@
 #ifndef LLVM_LIBC_TEST_UNITTEST_LIBCCTEST_H
 #define LLVM_LIBC_TEST_UNITTEST_LIBCCTEST_H
 
+#ifdef LIBC_COPT_TEST_USE_ZXTEST
+
+// Use Test.h in C++ code.
+#ifndef __cplusplus
+#include <zxtest/zxtest.h>
+
+#undef TEST
+#define TEST(name) LIB_ZXTEST_REGISTER(LlvmLibcCTest, name)
+#endif
+
+#else // !LIBC_COPT_TEST_USE_ZXTEST
+
 #include "include/__llvm-libc-common.h"
 
 __BEGIN_C_DECLS
@@ -28,7 +40,7 @@ void libc_c_test_anchor(void);
 
 __END_C_DECLS
 
-// Use LibcTest.h in C++ code.
+// Use Test.h in C++ code.
 #ifndef __cplusplus
 #define TEST(name)                                                             \
   const char LIBC_C_TEST_NAME[] = "LlvmLibcCTest." #name;                      \
@@ -53,4 +65,6 @@ __END_C_DECLS
 #define ASSERT_FALSE(cond) LIBC_C_TEST_SCAFFOLDING_(cond, 0, return)
 #endif
 
+#endif // LIBC_COPT_TEST_USE_ZXTEST
+
 #endif // LLVM_LIBC_TEST_UNITTEST_LIBCCTEST_H



More information about the libc-commits mailing list