Skip to content

Commit 4d0b816

Browse files
authored
[libc] Use anonymous namespace for file-local symbols (#157202)
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.
1 parent 9789abb commit 4d0b816

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

libc/src/__support/StringUtil/error_to_string.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "error_to_string.h"
10-
#include "platform_errors.h"
1110

11+
#include <stddef.h>
12+
13+
#include "platform_errors.h"
1214
#include "src/__support/CPP/span.h"
1315
#include "src/__support/CPP/string_view.h"
1416
#include "src/__support/CPP/stringstream.h"
@@ -17,10 +19,8 @@
1719
#include "src/__support/macros/attributes.h"
1820
#include "src/__support/macros/config.h"
1921

20-
#include <stddef.h>
21-
2222
namespace LIBC_NAMESPACE_DECL {
23-
namespace internal {
23+
namespace {
2424

2525
constexpr size_t max_buff_size() {
2626
constexpr size_t unknown_str_len = sizeof("Unknown error");
@@ -63,23 +63,22 @@ cpp::string_view build_error_string(int err_num, cpp::span<char> buffer) {
6363
return buffer_stream.str();
6464
}
6565

66-
} // namespace internal
66+
} // namespace
6767

6868
cpp::string_view get_error_string(int err_num) {
69-
return get_error_string(err_num,
70-
{internal::error_buffer, internal::ERR_BUFFER_SIZE});
69+
return get_error_string(err_num, {error_buffer, ERR_BUFFER_SIZE});
7170
}
7271

7372
cpp::string_view get_error_string(int err_num, cpp::span<char> buffer) {
74-
auto opt_str = internal::ERROR_MAPPER.get_str(err_num);
73+
auto opt_str = ERROR_MAPPER.get_str(err_num);
7574
if (opt_str)
7675
return *opt_str;
7776
else
78-
return internal::build_error_string(err_num, buffer);
77+
return build_error_string(err_num, buffer);
7978
}
8079

8180
cpp::optional<cpp::string_view> try_get_errno_name(int err_num) {
82-
return internal::ERRNO_NAME_MAPPER.get_str(err_num);
81+
return ERRNO_NAME_MAPPER.get_str(err_num);
8382
}
8483

8584
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/StringUtil/signal_to_string.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "signal_to_string.h"
10-
#include "platform_signals.h"
1110

11+
#include <signal.h>
12+
#include <stddef.h>
13+
14+
#include "platform_signals.h"
1215
#include "src/__support/CPP/span.h"
1316
#include "src/__support/CPP/string_view.h"
1417
#include "src/__support/CPP/stringstream.h"
@@ -17,11 +20,8 @@
1720
#include "src/__support/macros/attributes.h"
1821
#include "src/__support/macros/config.h"
1922

20-
#include <signal.h>
21-
#include <stddef.h>
22-
2323
namespace LIBC_NAMESPACE_DECL {
24-
namespace internal {
24+
namespace {
2525

2626
constexpr size_t max_buff_size() {
2727
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<char> buffer) {
6363
return buffer_stream.str();
6464
}
6565

66-
} // namespace internal
66+
} // namespace
6767

6868
cpp::string_view get_signal_string(int sig_num) {
69-
return get_signal_string(
70-
sig_num, {internal::signal_buffer, internal::SIG_BUFFER_SIZE});
69+
return get_signal_string(sig_num, {signal_buffer, SIG_BUFFER_SIZE});
7170
}
7271

7372
cpp::string_view get_signal_string(int sig_num, cpp::span<char> buffer) {
74-
auto opt_str = internal::signal_mapper.get_str(sig_num);
73+
auto opt_str = signal_mapper.get_str(sig_num);
7574
if (opt_str)
7675
return *opt_str;
7776
else
78-
return internal::build_signal_string(sig_num, buffer);
77+
return build_signal_string(sig_num, buffer);
7978
}
8079

8180
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)