summaryrefslogtreecommitdiff
path: root/absl/log
diff options
context:
space:
mode:
authorGravatar Andy Getzendanner <durandal@google.com>2023-01-17 14:30:52 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2023-01-17 14:31:34 -0800
commita86f1cec941ee7842a2ce1bce1cc4aa83eeb8d8d (patch)
treecf3ab6382ddd12e570e6a6ed0d78947334878c16 /absl/log
parent4b34e19765c8c03e4066f2226f7fb28d68604a1d (diff)
extern-ify NullGuard's "(null)" strings to save linker input bytes.
PiperOrigin-RevId: 502689876 Change-Id: If75b00e2e257283b60c41411ef7a60dbd7cd8c6d
Diffstat (limited to 'absl/log')
-rw-r--r--absl/log/CMakeLists.txt2
-rw-r--r--absl/log/internal/BUILD.bazel2
-rw-r--r--absl/log/internal/nullguard.cc35
-rw-r--r--absl/log/internal/nullguard.h15
4 files changed, 47 insertions, 7 deletions
diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt
index 91bba002..fb1b59f5 100644
--- a/absl/log/CMakeLists.txt
+++ b/absl/log/CMakeLists.txt
@@ -249,6 +249,7 @@ absl_cc_library(
NAME
log_internal_nullguard
SRCS
+ "internal/nullguard.cc"
HDRS
"internal/nullguard.h"
COPTS
@@ -257,6 +258,7 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
+ absl::core_headers
)
absl_cc_library(
diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel
index 08276887..a1f1a67c 100644
--- a/absl/log/internal/BUILD.bazel
+++ b/absl/log/internal/BUILD.bazel
@@ -226,11 +226,13 @@ cc_library(
cc_library(
name = "nullguard",
+ srcs = ["nullguard.cc"],
hdrs = ["nullguard.h"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:config",
+ "//absl/base:core_headers",
],
)
diff --git a/absl/log/internal/nullguard.cc b/absl/log/internal/nullguard.cc
new file mode 100644
index 00000000..7b785c55
--- /dev/null
+++ b/absl/log/internal/nullguard.cc
@@ -0,0 +1,35 @@
+// Copyright 2023 The Abseil Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/log/internal/nullguard.h"
+
+#include <array>
+
+#include "absl/base/attributes.h"
+#include "absl/base/config.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace log_internal {
+
+ABSL_CONST_INIT const std::array<char, 7> kCharNull{
+ {'(', 'n', 'u', 'l', 'l', ')', '\0'}};
+ABSL_CONST_INIT const std::array<signed char, 7> kSignedCharNull{
+ {'(', 'n', 'u', 'l', 'l', ')', '\0'}};
+ABSL_CONST_INIT const std::array<unsigned char, 7> kUnsignedCharNull{
+ {'(', 'n', 'u', 'l', 'l', ')', '\0'}};
+
+} // namespace log_internal
+ABSL_NAMESPACE_END
+} // namespace absl
diff --git a/absl/log/internal/nullguard.h b/absl/log/internal/nullguard.h
index 8ea38356..a0ed4ad7 100644
--- a/absl/log/internal/nullguard.h
+++ b/absl/log/internal/nullguard.h
@@ -27,26 +27,29 @@
#include <array>
#include <cstddef>
+#include "absl/base/attributes.h"
#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace log_internal {
+ABSL_CONST_INIT extern const std::array<char, 7> kCharNull;
+ABSL_CONST_INIT extern const std::array<signed char, 7> kSignedCharNull;
+ABSL_CONST_INIT extern const std::array<unsigned char, 7> kUnsignedCharNull;
+
template <typename T>
struct NullGuard final {
static const T& Guard(const T& v) { return v; }
};
template <>
struct NullGuard<char*> final {
- static const char* Guard(const char* v) { return v ? v : "(null)"; }
+ static const char* Guard(const char* v) { return v ? v : kCharNull.data(); }
};
template <>
struct NullGuard<const char*> final {
- static const char* Guard(const char* v) { return v ? v : "(null)"; }
+ static const char* Guard(const char* v) { return v ? v : kCharNull.data(); }
};
-constexpr std::array<signed char, 7> kSignedCharNull{
- {'(', 'n', 'u', 'l', 'l', ')', '\0'}};
template <>
struct NullGuard<signed char*> final {
static const signed char* Guard(const signed char* v) {
@@ -59,8 +62,6 @@ struct NullGuard<const signed char*> final {
return v ? v : kSignedCharNull.data();
}
};
-constexpr std::array<unsigned char, 7> kUnsignedCharNull{
- {'(', 'n', 'u', 'l', 'l', ')', '\0'}};
template <>
struct NullGuard<unsigned char*> final {
static const unsigned char* Guard(const unsigned char* v) {
@@ -75,7 +76,7 @@ struct NullGuard<const unsigned char*> final {
};
template <>
struct NullGuard<std::nullptr_t> final {
- static const char* Guard(const std::nullptr_t&) { return "(null)"; }
+ static const char* Guard(const std::nullptr_t&) { return kCharNull.data(); }
};
} // namespace log_internal