From e71deafb6c067a6a561be043e6926877bdb566ec Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Fri, 5 Sep 2025 16:40:13 -0700 Subject: [PATCH] [libc] Use anonymous namespace for file-local symbols A namespace like LIBC_NAMESPACE::internal should only ever be defined if it's providing global symbols declared in headers. These StringUtil implementations were defining global namespaced symbols for their file-local helper code, which they should not. --- .../__support/StringUtil/error_to_string.cpp | 19 +++++++++---------- .../__support/StringUtil/signal_to_string.cpp | 19 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/libc/src/__support/StringUtil/error_to_string.cpp b/libc/src/__support/StringUtil/error_to_string.cpp index 3b22021706bba..38916af83795d 100644 --- a/libc/src/__support/StringUtil/error_to_string.cpp +++ b/libc/src/__support/StringUtil/error_to_string.cpp @@ -7,8 +7,10 @@ //===----------------------------------------------------------------------===// #include "error_to_string.h" -#include "platform_errors.h" +#include + +#include "platform_errors.h" #include "src/__support/CPP/span.h" #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/stringstream.h" @@ -17,10 +19,8 @@ #include "src/__support/macros/attributes.h" #include "src/__support/macros/config.h" -#include - namespace LIBC_NAMESPACE_DECL { -namespace internal { +namespace { constexpr size_t max_buff_size() { constexpr size_t unknown_str_len = sizeof("Unknown error"); @@ -63,23 +63,22 @@ cpp::string_view build_error_string(int err_num, cpp::span buffer) { return buffer_stream.str(); } -} // namespace internal +} // namespace cpp::string_view get_error_string(int err_num) { - return get_error_string(err_num, - {internal::error_buffer, internal::ERR_BUFFER_SIZE}); + return get_error_string(err_num, {error_buffer, ERR_BUFFER_SIZE}); } cpp::string_view get_error_string(int err_num, cpp::span buffer) { - auto opt_str = internal::ERROR_MAPPER.get_str(err_num); + auto opt_str = ERROR_MAPPER.get_str(err_num); if (opt_str) return *opt_str; else - return internal::build_error_string(err_num, buffer); + return build_error_string(err_num, buffer); } cpp::optional try_get_errno_name(int err_num) { - return internal::ERRNO_NAME_MAPPER.get_str(err_num); + return ERRNO_NAME_MAPPER.get_str(err_num); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/__support/StringUtil/signal_to_string.cpp b/libc/src/__support/StringUtil/signal_to_string.cpp index b67d28814816c..e5863ff1e8987 100644 --- a/libc/src/__support/StringUtil/signal_to_string.cpp +++ b/libc/src/__support/StringUtil/signal_to_string.cpp @@ -7,8 +7,11 @@ //===----------------------------------------------------------------------===// #include "signal_to_string.h" -#include "platform_signals.h" +#include +#include + +#include "platform_signals.h" #include "src/__support/CPP/span.h" #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/stringstream.h" @@ -17,11 +20,8 @@ #include "src/__support/macros/attributes.h" #include "src/__support/macros/config.h" -#include -#include - namespace LIBC_NAMESPACE_DECL { -namespace internal { +namespace { constexpr size_t max_buff_size() { constexpr size_t base_str_len = sizeof("Real-time signal"); @@ -63,19 +63,18 @@ cpp::string_view build_signal_string(int sig_num, cpp::span buffer) { return buffer_stream.str(); } -} // namespace internal +} // namespace cpp::string_view get_signal_string(int sig_num) { - return get_signal_string( - sig_num, {internal::signal_buffer, internal::SIG_BUFFER_SIZE}); + return get_signal_string(sig_num, {signal_buffer, SIG_BUFFER_SIZE}); } cpp::string_view get_signal_string(int sig_num, cpp::span buffer) { - auto opt_str = internal::signal_mapper.get_str(sig_num); + auto opt_str = signal_mapper.get_str(sig_num); if (opt_str) return *opt_str; else - return internal::build_signal_string(sig_num, buffer); + return build_signal_string(sig_num, buffer); } } // namespace LIBC_NAMESPACE_DECL