From dfce35c97cd7eaa93d64decaca74c46a6940dc92 Mon Sep 17 00:00:00 2001
From: Sergiu Deitsch <sergiud@users.noreply.github.com>
Date: Thu, 4 Jan 2024 16:57:18 +0100
Subject: [PATCH] fix: cleanup exports (#1029)

Reduce unnecessary utility function exports.
---
 bazel/glog.bzl          |  2 ++
 src/glog/log_severity.h | 10 +++++++---
 src/glog/logging.h      | 30 +++++-------------------------
 src/googletest.h        |  7 ++++---
 src/logging.cc          | 23 ++++++++++++++++++++++-
 5 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/bazel/glog.bzl b/bazel/glog.bzl
index dfd4c040d..a38e508f5 100644
--- a/bazel/glog.bzl
+++ b/bazel/glog.bzl
@@ -72,6 +72,7 @@ def glog_library(with_gflags = 1, **kwargs):
 
     linux_or_darwin_copts = wasm_copts + [
         "-DGLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
+        "-DGLOG_NO_EXPORT=__attribute__((visibility(\\\"default\\\")))",
         "-DHAVE_MODE_T",
         "-DHAVE_SSIZE_T",
         "-DHAVE_SYS_TYPES_H",
@@ -101,6 +102,7 @@ def glog_library(with_gflags = 1, **kwargs):
     windows_only_copts = [
         # Override -DGLOG_EXPORT= from the cc_library's defines.
         "-DGLOG_EXPORT=__declspec(dllexport)",
+        "-DGLOG_NO_EXPORT=",
         "-DGLOG_NO_ABBREVIATED_SEVERITIES",
         "-DHAVE__CHSIZE_S",
         "-I" + src_windows,
diff --git a/src/glog/log_severity.h b/src/glog/log_severity.h
index 86b02a1d7..5a1497ef1 100644
--- a/src/glog/log_severity.h
+++ b/src/glog/log_severity.h
@@ -67,12 +67,16 @@ enum LogSeverity {
 
 #if defined(__cpp_inline_variables)
 #  if (__cpp_inline_variables >= 201606L)
-inline
+#    define GLOG_INLINE_VARIABLE inline
 #  endif  // (__cpp_inline_variables >= 201606L)
 #endif    // defined(__cpp_inline_variables)
-          // clang-format off
+
+#if !defined(GLOG_INLINE_VARIABLE)
+#  define GLOG_INLINE_VARIABLE
+#endif  // !defined(GLOG_INLINE_VARIABLE)
+
+GLOG_INLINE_VARIABLE
 constexpr int NUM_SEVERITIES = 4;
-// clang-format on
 
 // DFATAL is FATAL in debug mode, ERROR in normal mode
 #ifdef NDEBUG
diff --git a/src/glog/logging.h b/src/glog/logging.h
index 0c821b2eb..925ce6d48 100644
--- a/src/glog/logging.h
+++ b/src/glog/logging.h
@@ -1162,7 +1162,8 @@ GLOG_EXPORT bool IsFailureSignalHandlerInstalled();
                               google::LogMessage::SendToLog)
 
 // We want the special COUNTER value available for LOG_EVERY_X()'ed messages
-enum PRIVATE_Counter { COUNTER };
+struct Counter_t {};
+GLOG_INLINE_VARIABLE constexpr Counter_t COUNTER{};
 
 #ifdef GLOG_NO_ABBREVIATED_SEVERITIES
 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
@@ -1173,7 +1174,8 @@ enum PRIVATE_Counter { COUNTER };
 #  define SYSLOG_0 SYSLOG_ERROR
 #  define LOG_TO_STRING_0 LOG_TO_STRING_ERROR
 // Needed for LOG_IS_ON(ERROR).
-const LogSeverity GLOG_0 = GLOG_ERROR;
+GLOG_INLINE_VARIABLE
+constexpr LogSeverity GLOG_0 = GLOG_ERROR;
 #else
 // Users may include windows.h after logging.h without
 // GLOG_NO_ABBREVIATED_SEVERITIES nor WIN32_LEAN_AND_MEAN.
@@ -1556,7 +1558,7 @@ T CheckNotNull(const char* file, int line, const char* names, T&& t) {
 // Allow folks to put a counter in the LOG_EVERY_X()'ed messages. This
 // only works if ostream is a LogStream. If the ostream is not a
 // LogStream you'll get an assert saying as much at runtime.
-GLOG_EXPORT std::ostream& operator<<(std::ostream& os, const PRIVATE_Counter&);
+GLOG_EXPORT std::ostream& operator<<(std::ostream& os, const Counter_t&);
 
 // Derived class for PLOG*() above.
 class GLOG_EXPORT ErrnoLogMessage : public LogMessage {
@@ -1694,11 +1696,6 @@ GLOG_EXPORT bool SendEmail(const char* dest, const char* subject,
 
 GLOG_EXPORT const std::vector<std::string>& GetLoggingDirectories();
 
-// Returns a set of existing temporary directories, which will be a
-// subset of the directories returned by GetLoggingDirectories().
-// Thread-safe.
-GLOG_EXPORT void GetExistingTempDirectories(std::vector<std::string>* list);
-
 // Print any fatal message again -- useful to call from signal handler
 // so that the last thing in the output is the fatal message.
 // Thread-hostile, but a race is unlikely.
@@ -1771,23 +1768,6 @@ extern GLOG_EXPORT void SetLogger(LogSeverity level, Logger* logger);
 
 }  // namespace base
 
-// glibc has traditionally implemented two incompatible versions of
-// strerror_r(). There is a poorly defined convention for picking the
-// version that we want, but it is not clear whether it even works with
-// all versions of glibc.
-// So, instead, we provide this wrapper that automatically detects the
-// version that is in use, and then implements POSIX semantics.
-// N.B. In addition to what POSIX says, we also guarantee that "buf" will
-// be set to an empty string, if this function failed. This means, in most
-// cases, you do not need to check the error code and you can directly
-// use the value of "buf". It will never have an undefined value.
-// DEPRECATED: Use StrError(int) instead.
-GLOG_EXPORT int posix_strerror_r(int err, char* buf, size_t len);
-
-// A thread-safe replacement for strerror(). Returns a string describing the
-// given POSIX error code.
-GLOG_EXPORT std::string StrError(int err);
-
 // A class for which we define operator<<, which does nothing.
 class GLOG_EXPORT NullStream : public LogMessage::LogStream {
  public:
diff --git a/src/googletest.h b/src/googletest.h
index bc61ff6b3..9d9b9b5e4 100644
--- a/src/googletest.h
+++ b/src/googletest.h
@@ -69,9 +69,10 @@ using std::string;
 using std::vector;
 
 namespace google {
-
-extern GLOG_EXPORT void (*g_logging_fail_func)();
-
+extern void (*g_logging_fail_func)();
+extern void GetExistingTempDirectories(vector<string>* list);
+extern int posix_strerror_r(int err, char* buf, size_t len);
+extern std::string StrError(int err);
 }
 
 #undef GLOG_EXPORT
diff --git a/src/logging.cc b/src/logging.cc
index 08bc1ac1d..7498fd0b0 100644
--- a/src/logging.cc
+++ b/src/logging.cc
@@ -327,6 +327,8 @@ static bool TerminalSupportsColor() {
 
 namespace google {
 
+std::string StrError(int err);
+
 enum GLogColor { COLOR_DEFAULT, COLOR_RED, COLOR_GREEN, COLOR_YELLOW };
 
 static GLogColor SeverityToColor(LogSeverity severity) {
@@ -2051,7 +2053,7 @@ int64 LogMessage::num_messages(int severity) {
 
 // Output the COUNTER value. This is only valid if ostream is a
 // LogStream.
-ostream& operator<<(ostream& os, const PRIVATE_Counter&) {
+ostream& operator<<(ostream& os, const Counter_t&) {
 #ifdef DISABLE_RTTI
   LogMessage::LogStream* log = static_cast<LogMessage::LogStream*>(&os);
 #else
@@ -2435,6 +2437,10 @@ const vector<string>& GetLoggingDirectories() {
   return *logging_directories_list;
 }
 
+// Returns a set of existing temporary directories, which will be a
+// subset of the directories returned by GetLoggingDirectories().
+// Thread-safe.
+GLOG_NO_EXPORT
 void GetExistingTempDirectories(vector<string>* list) {
   GetTempDirectories(list);
   auto i_dir = list->begin();
@@ -2567,6 +2573,18 @@ DEFINE_CHECK_STROP_IMPL(CHECK_STRCASEEQ, strcasecmp, true)
 DEFINE_CHECK_STROP_IMPL(CHECK_STRCASENE, strcasecmp, false)
 #undef DEFINE_CHECK_STROP_IMPL
 
+// glibc has traditionally implemented two incompatible versions of
+// strerror_r(). There is a poorly defined convention for picking the
+// version that we want, but it is not clear whether it even works with
+// all versions of glibc.
+// So, instead, we provide this wrapper that automatically detects the
+// version that is in use, and then implements POSIX semantics.
+// N.B. In addition to what POSIX says, we also guarantee that "buf" will
+// be set to an empty string, if this function failed. This means, in most
+// cases, you do not need to check the error code and you can directly
+// use the value of "buf". It will never have an undefined value.
+// DEPRECATED: Use StrError(int) instead.
+GLOG_NO_EXPORT
 int posix_strerror_r(int err, char* buf, size_t len) {
   // Sanity check input parameters
   if (buf == nullptr || len <= 0) {
@@ -2618,6 +2636,9 @@ int posix_strerror_r(int err, char* buf, size_t len) {
   }
 }
 
+// A thread-safe replacement for strerror(). Returns a string describing the
+// given POSIX error code.
+GLOG_NO_EXPORT
 string StrError(int err) {
   char buf[100];
   int rc = posix_strerror_r(err, buf, sizeof(buf));