Skip to content

Conversation

frobtech
Copy link
Contributor

@frobtech frobtech commented Sep 5, 2025

Tests can be at top-level or inside an anonymous namespace,
doesn't matter. But putting their helper code inside anonymous
namespaces both makes the code compatible with compiling using
-Wmissing-declarations and might let the compiler optimize the
test good a bit better.

Tests can be at top-level or inside an anonymous namespace,
doesn't matter.  But putting their helper code inside anonymous
namespaces both makes the code compatible with compiling using
-Wmissing-declarations and might let the compiler optimize the
test good a bit better.
@frobtech frobtech marked this pull request as ready for review September 5, 2025 23:51
@llvmbot llvmbot added the libc label Sep 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

Tests can be at top-level or inside an anonymous namespace,
doesn't matter. But putting their helper code inside anonymous
namespaces both makes the code compatible with compiling using
-Wmissing-declarations and might let the compiler optimize the
test good a bit better.


Full diff: https://github.com/llvm/llvm-project/pull/157203.diff

5 Files Affected:

  • (modified) libc/test/src/setjmp/setjmp_test.cpp (+4)
  • (modified) libc/test/src/stdio/printf_core/writer_test.cpp (+5-2)
  • (modified) libc/test/src/stdlib/quick_sort_test.cpp (+4)
  • (modified) libc/test/src/string/memchr_test.cpp (+8-2)
  • (modified) libc/test/src/string/memrchr_test.cpp (+8-2)
diff --git a/libc/test/src/setjmp/setjmp_test.cpp b/libc/test/src/setjmp/setjmp_test.cpp
index 27113cd6e0631..d13bb8d55d46a 100644
--- a/libc/test/src/setjmp/setjmp_test.cpp
+++ b/libc/test/src/setjmp/setjmp_test.cpp
@@ -10,6 +10,8 @@
 #include "src/setjmp/setjmp_impl.h"
 #include "test/UnitTest/Test.h"
 
+namespace {
+
 constexpr int MAX_LOOP = 123;
 int longjmp_called = 0;
 
@@ -45,3 +47,5 @@ TEST(LlvmLibcSetJmpTest, SetAndJumpBackValOne) {
   ASSERT_EQ(longjmp_called, 1);
   ASSERT_EQ(val, 1);
 }
+
+} // namespace
diff --git a/libc/test/src/stdio/printf_core/writer_test.cpp b/libc/test/src/stdio/printf_core/writer_test.cpp
index 8611caa2dfa58..d036341be7981 100644
--- a/libc/test/src/stdio/printf_core/writer_test.cpp
+++ b/libc/test/src/stdio/printf_core/writer_test.cpp
@@ -6,13 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/__support/CPP/string_view.h"
 #include "src/stdio/printf_core/writer.h"
 
+#include "src/__support/CPP/string_view.h"
 #include "src/string/memory_utils/inline_memcpy.h"
-
 #include "test/UnitTest/Test.h"
 
+namespace {
+
 using LIBC_NAMESPACE::cpp::string_view;
 using LIBC_NAMESPACE::printf_core::WriteBuffer;
 using LIBC_NAMESPACE::printf_core::WriteMode;
@@ -314,3 +315,5 @@ TEST(LlvmLibcPrintfWriterTest, NullStringWithZeroMaxLengthWithCallback) {
   ASSERT_EQ(writer.get_chars_written(), 12);
   ASSERT_STREQ("aaaDEF111456", str);
 }
+
+} // namespace
diff --git a/libc/test/src/stdlib/quick_sort_test.cpp b/libc/test/src/stdlib/quick_sort_test.cpp
index 2832c855370bc..6d132428743b6 100644
--- a/libc/test/src/stdlib/quick_sort_test.cpp
+++ b/libc/test/src/stdlib/quick_sort_test.cpp
@@ -9,6 +9,8 @@
 #include "SortingTest.h"
 #include "src/stdlib/qsort_util.h"
 
+namespace {
+
 void quick_sort(void *array, size_t array_size, size_t elem_size,
                 int (*compare)(const void *, const void *)) {
   constexpr bool USE_QUICKSORT = true;
@@ -23,3 +25,5 @@ void quick_sort(void *array, size_t array_size, size_t elem_size,
 }
 
 LIST_SORTING_TESTS(Qsort, quick_sort);
+
+} // namespace
diff --git a/libc/test/src/string/memchr_test.cpp b/libc/test/src/string/memchr_test.cpp
index 4a7c88bfe6e1a..ede841118fe03 100644
--- a/libc/test/src/string/memchr_test.cpp
+++ b/libc/test/src/string/memchr_test.cpp
@@ -6,11 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "hdr/signal_macros.h"
 #include "src/string/memchr.h"
-#include "test/UnitTest/Test.h"
+
 #include <stddef.h>
 
+#include "hdr/signal_macros.h"
+#include "test/UnitTest/Test.h"
+
+namespace {
+
 // A helper function that calls memchr and abstracts away the explicit cast for
 // readability purposes.
 const char *call_memchr(const void *src, int c, size_t size) {
@@ -130,3 +134,5 @@ TEST(LlvmLibcMemChrTest, CrashOnNullPtr) {
 }
 
 #endif // defined(LIBC_ADD_NULL_CHECKS)
+
+} // namespace
diff --git a/libc/test/src/string/memrchr_test.cpp b/libc/test/src/string/memrchr_test.cpp
index 140395b3046f6..e92dd3ad9c919 100644
--- a/libc/test/src/string/memrchr_test.cpp
+++ b/libc/test/src/string/memrchr_test.cpp
@@ -6,11 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "hdr/signal_macros.h"
 #include "src/string/memrchr.h"
-#include "test/UnitTest/Test.h"
+
 #include <stddef.h>
 
+#include "hdr/signal_macros.h"
+#include "test/UnitTest/Test.h"
+
+namespace {
+
 // A helper function that calls memrchr and abstracts away the explicit cast for
 // readability purposes.
 const char *call_memrchr(const void *src, int c, size_t size) {
@@ -122,3 +126,5 @@ TEST(LlvmLibcMemRChrTest, CrashOnNullPtr) {
 }
 
 #endif // defined(LIBC_ADD_NULL_CHECKS)
+
+} // namespace

@frobtech frobtech merged commit 9789abb into llvm:main Sep 6, 2025
23 checks passed
@frobtech frobtech deleted the p/libc-test-namespace branch September 6, 2025 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants