Commit a86f1cec by Andy Getzendanner Committed by Copybara-Service

extern-ify NullGuard's "(null)" strings to save linker input bytes.

PiperOrigin-RevId: 502689876
Change-Id: If75b00e2e257283b60c41411ef7a60dbd7cd8c6d
parent 4b34e197
...@@ -249,6 +249,7 @@ absl_cc_library( ...@@ -249,6 +249,7 @@ absl_cc_library(
NAME NAME
log_internal_nullguard log_internal_nullguard
SRCS SRCS
"internal/nullguard.cc"
HDRS HDRS
"internal/nullguard.h" "internal/nullguard.h"
COPTS COPTS
...@@ -257,6 +258,7 @@ absl_cc_library( ...@@ -257,6 +258,7 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::config absl::config
absl::core_headers
) )
absl_cc_library( absl_cc_library(
......
...@@ -226,11 +226,13 @@ cc_library( ...@@ -226,11 +226,13 @@ cc_library(
cc_library( cc_library(
name = "nullguard", name = "nullguard",
srcs = ["nullguard.cc"],
hdrs = ["nullguard.h"], hdrs = ["nullguard.h"],
copts = ABSL_DEFAULT_COPTS, copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers",
], ],
) )
......
// 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
...@@ -27,26 +27,29 @@ ...@@ -27,26 +27,29 @@
#include <array> #include <array>
#include <cstddef> #include <cstddef>
#include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
namespace log_internal { 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> template <typename T>
struct NullGuard final { struct NullGuard final {
static const T& Guard(const T& v) { return v; } static const T& Guard(const T& v) { return v; }
}; };
template <> template <>
struct NullGuard<char*> final { 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 <> template <>
struct NullGuard<const char*> final { 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 <> template <>
struct NullGuard<signed char*> final { struct NullGuard<signed char*> final {
static const signed char* Guard(const signed char* v) { static const signed char* Guard(const signed char* v) {
...@@ -59,8 +62,6 @@ struct NullGuard<const signed char*> final { ...@@ -59,8 +62,6 @@ struct NullGuard<const signed char*> final {
return v ? v : kSignedCharNull.data(); return v ? v : kSignedCharNull.data();
} }
}; };
constexpr std::array<unsigned char, 7> kUnsignedCharNull{
{'(', 'n', 'u', 'l', 'l', ')', '\0'}};
template <> template <>
struct NullGuard<unsigned char*> final { struct NullGuard<unsigned char*> final {
static const unsigned char* Guard(const unsigned char* v) { static const unsigned char* Guard(const unsigned char* v) {
...@@ -75,7 +76,7 @@ struct NullGuard<const unsigned char*> final { ...@@ -75,7 +76,7 @@ struct NullGuard<const unsigned char*> final {
}; };
template <> template <>
struct NullGuard<std::nullptr_t> final { 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 } // namespace log_internal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment