Add some nullability annotations in logging and tidy up some NOLINTs and comments.
PiperOrigin-RevId: 694512830
Change-Id: Ibeb8eb5fd18af5c49fe45a903e5cb129ee15974d
diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel
index 8b6a295..f90046b 100644
--- a/absl/log/BUILD.bazel
+++ b/absl/log/BUILD.bazel
@@ -187,6 +187,7 @@
deps = [
":log_sink",
"//absl/base:config",
+ "//absl/base:nullability",
"//absl/log/internal:log_sink_set",
],
)
diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt
index 5484206..715b5b7 100644
--- a/absl/log/CMakeLists.txt
+++ b/absl/log/CMakeLists.txt
@@ -196,8 +196,8 @@
absl::config
absl::core_headers
absl::errno_saver
- absl::inlined_vector
absl::examine_stack
+ absl::inlined_vector
absl::log_internal_append_truncated
absl::log_internal_format
absl::log_internal_globals
@@ -210,11 +210,12 @@
absl::log_sink
absl::log_sink_registry
absl::memory
+ absl::nullability
absl::raw_logging_internal
- absl::strings
- absl::strerror
- absl::time
absl::span
+ absl::strerror
+ absl::strings
+ absl::time
)
absl_cc_library(
@@ -603,6 +604,7 @@
absl::config
absl::log_sink
absl::log_internal_log_sink_set
+ absl::nullability
PUBLIC
)
diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel
index 9b4ebb8..e954194 100644
--- a/absl/log/internal/BUILD.bazel
+++ b/absl/log/internal/BUILD.bazel
@@ -179,6 +179,7 @@
"//absl/base:core_headers",
"//absl/base:errno_saver",
"//absl/base:log_severity",
+ "//absl/base:nullability",
"//absl/base:raw_logging_internal",
"//absl/base:strerror",
"//absl/container:inlined_vector",
diff --git a/absl/log/internal/log_message.cc b/absl/log/internal/log_message.cc
index 4e9b08a..5551ae3 100644
--- a/absl/log/internal/log_message.cc
+++ b/absl/log/internal/log_message.cc
@@ -418,23 +418,26 @@
data_->manipulated << m;
return *this;
}
+// NOLINTBEGIN(runtime/int)
+// NOLINTBEGIN(google-runtime-int)
template LogMessage& LogMessage::operator<<(const char& v);
template LogMessage& LogMessage::operator<<(const signed char& v);
template LogMessage& LogMessage::operator<<(const unsigned char& v);
-template LogMessage& LogMessage::operator<<(const short& v); // NOLINT
-template LogMessage& LogMessage::operator<<(const unsigned short& v); // NOLINT
+template LogMessage& LogMessage::operator<<(const short& v);
+template LogMessage& LogMessage::operator<<(const unsigned short& v);
template LogMessage& LogMessage::operator<<(const int& v);
template LogMessage& LogMessage::operator<<(const unsigned int& v);
-template LogMessage& LogMessage::operator<<(const long& v); // NOLINT
-template LogMessage& LogMessage::operator<<(const unsigned long& v); // NOLINT
-template LogMessage& LogMessage::operator<<(const long long& v); // NOLINT
-template LogMessage& LogMessage::operator<<(
- const unsigned long long& v); // NOLINT
+template LogMessage& LogMessage::operator<<(const long& v);
+template LogMessage& LogMessage::operator<<(const unsigned long& v);
+template LogMessage& LogMessage::operator<<(const long long& v);
+template LogMessage& LogMessage::operator<<(const unsigned long long& v);
template LogMessage& LogMessage::operator<<(void* const& v);
template LogMessage& LogMessage::operator<<(const void* const& v);
template LogMessage& LogMessage::operator<<(const float& v);
template LogMessage& LogMessage::operator<<(const double& v);
template LogMessage& LogMessage::operator<<(const bool& v);
+// NOLINTEND(google-runtime-int)
+// NOLINTEND(runtime/int)
void LogMessage::Flush() {
if (data_->entry.log_severity() < absl::MinLogLevel()) return;
diff --git a/absl/log/internal/log_message.h b/absl/log/internal/log_message.h
index 0c067da..e8bca65 100644
--- a/absl/log/internal/log_message.h
+++ b/absl/log/internal/log_message.h
@@ -27,16 +27,19 @@
#ifndef ABSL_LOG_INTERNAL_LOG_MESSAGE_H_
#define ABSL_LOG_INTERNAL_LOG_MESSAGE_H_
+#include <cstddef>
#include <ios>
#include <memory>
#include <ostream>
#include <streambuf>
#include <string>
+#include <type_traits>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/errno_saver.h"
#include "absl/base/log_severity.h"
+#include "absl/base/nullability.h"
#include "absl/log/internal/nullguard.h"
#include "absl/log/log_entry.h"
#include "absl/log/log_sink.h"
@@ -56,15 +59,15 @@
struct ErrorTag {};
// Used for `LOG`.
- LogMessage(const char* file, int line,
+ LogMessage(absl::Nonnull<const char*> file, int line,
absl::LogSeverity severity) ABSL_ATTRIBUTE_COLD;
// These constructors are slightly smaller/faster to call; the severity is
// curried into the function pointer.
- LogMessage(const char* file, int line,
+ LogMessage(absl::Nonnull<const char*> file, int line,
InfoTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
- LogMessage(const char* file, int line,
+ LogMessage(absl::Nonnull<const char*> file, int line,
WarningTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
- LogMessage(const char* file, int line,
+ LogMessage(absl::Nonnull<const char*> file, int line,
ErrorTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
LogMessage(const LogMessage&) = delete;
LogMessage& operator=(const LogMessage&) = delete;
@@ -95,59 +98,67 @@
// of `errno`.
LogMessage& WithPerror();
// Sends this message to `*sink` in addition to whatever other sinks it would
- // otherwise have been sent to. `sink` must not be null.
- LogMessage& ToSinkAlso(absl::LogSink* sink);
- // Sends this message to `*sink` and no others. `sink` must not be null.
- LogMessage& ToSinkOnly(absl::LogSink* sink);
+ // otherwise have been sent to.
+ LogMessage& ToSinkAlso(absl::Nonnull<absl::LogSink*> sink);
+ // Sends this message to `*sink` and no others.
+ LogMessage& ToSinkOnly(absl::Nonnull<absl::LogSink*> sink);
// Don't call this method from outside this library.
LogMessage& InternalStream() { return *this; }
// By-value overloads for small, common types let us overlook common failures
// to define globals and static data members (i.e. in a .cc file).
- // clang-format off
- // The CUDA toolchain cannot handle these <<<'s:
+ // NOLINTBEGIN(runtime/int)
+ // NOLINTBEGIN(google-runtime-int)
+ // clang-format off: The CUDA toolchain cannot handle these <<<'s
LogMessage& operator<<(char v) { return operator<< <char>(v); }
LogMessage& operator<<(signed char v) { return operator<< <signed char>(v); }
LogMessage& operator<<(unsigned char v) {
return operator<< <unsigned char>(v);
}
- LogMessage& operator<<(signed short v) { // NOLINT
- return operator<< <signed short>(v); // NOLINT
+ LogMessage& operator<<(signed short v) {
+ return operator<< <signed short>(v);
}
LogMessage& operator<<(signed int v) { return operator<< <signed int>(v); }
- LogMessage& operator<<(signed long v) { // NOLINT
- return operator<< <signed long>(v); // NOLINT
+ LogMessage& operator<<(signed long v) {
+ return operator<< <signed long>(v);
}
- LogMessage& operator<<(signed long long v) { // NOLINT
- return operator<< <signed long long>(v); // NOLINT
+ LogMessage& operator<<(signed long long v) {
+ return operator<< <signed long long>(v);
}
- LogMessage& operator<<(unsigned short v) { // NOLINT
- return operator<< <unsigned short>(v); // NOLINT
+ LogMessage& operator<<(unsigned short v) {
+ return operator<< <unsigned short>(v);
}
LogMessage& operator<<(unsigned int v) {
return operator<< <unsigned int>(v);
}
- LogMessage& operator<<(unsigned long v) { // NOLINT
- return operator<< <unsigned long>(v); // NOLINT
+ LogMessage& operator<<(unsigned long v) {
+ return operator<< <unsigned long>(v);
}
- LogMessage& operator<<(unsigned long long v) { // NOLINT
- return operator<< <unsigned long long>(v); // NOLINT
+ LogMessage& operator<<(unsigned long long v) {
+ return operator<< <unsigned long long>(v);
}
- LogMessage& operator<<(void* v) { return operator<< <void*>(v); }
- LogMessage& operator<<(const void* v) { return operator<< <const void*>(v); }
+ LogMessage& operator<<(absl::Nullable<void*> v) {
+ return operator<< <void*>(v);
+ }
+ LogMessage& operator<<(absl::Nullable<const void*> v) {
+ return operator<< <const void*>(v);
+ }
LogMessage& operator<<(float v) { return operator<< <float>(v); }
LogMessage& operator<<(double v) { return operator<< <double>(v); }
LogMessage& operator<<(bool v) { return operator<< <bool>(v); }
// clang-format on
+ // NOLINTEND(google-runtime-int)
+ // NOLINTEND(runtime/int)
// These overloads are more efficient since no `ostream` is involved.
LogMessage& operator<<(const std::string& v);
LogMessage& operator<<(absl::string_view v);
// Handle stream manipulators e.g. std::endl.
- LogMessage& operator<<(std::ostream& (*m)(std::ostream& os));
- LogMessage& operator<<(std::ios_base& (*m)(std::ios_base& os));
+ LogMessage& operator<<(absl::Nonnull<std::ostream& (*)(std::ostream & os)> m);
+ LogMessage& operator<<(
+ absl::Nonnull<std::ios_base& (*)(std::ios_base & os)> m);
// Literal strings. This allows us to record C string literals as literals in
// the logging.proto.Value.
@@ -255,7 +266,7 @@
// We keep the data in a separate struct so that each instance of `LogMessage`
// uses less stack space.
- std::unique_ptr<LogMessageData> data_;
+ absl::Nonnull<std::unique_ptr<LogMessageData>> data_;
};
// Helper class so that `AbslStringify()` can modify the LogMessage.
@@ -273,7 +284,8 @@
}
// For types that implement `AbslStringify` using `absl::Format()`.
- friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
+ friend void AbslFormatFlush(absl::Nonnull<StringifySink*> sink,
+ absl::string_view v) {
sink->Append(v);
}
@@ -315,27 +327,28 @@
// We instantiate these specializations in the library's TU to save space in
// other TUs. Since the template is marked `ABSL_ATTRIBUTE_NOINLINE` we will be
// emitting a function call either way.
+// NOLINTBEGIN(runtime/int)
+// NOLINTBEGIN(google-runtime-int)
extern template LogMessage& LogMessage::operator<<(const char& v);
extern template LogMessage& LogMessage::operator<<(const signed char& v);
extern template LogMessage& LogMessage::operator<<(const unsigned char& v);
-extern template LogMessage& LogMessage::operator<<(const short& v); // NOLINT
-extern template LogMessage& LogMessage::operator<<(
- const unsigned short& v); // NOLINT
+extern template LogMessage& LogMessage::operator<<(const short& v);
+extern template LogMessage& LogMessage::operator<<(const unsigned short& v);
extern template LogMessage& LogMessage::operator<<(const int& v);
+extern template LogMessage& LogMessage::operator<<(const unsigned int& v);
+extern template LogMessage& LogMessage::operator<<(const long& v);
+extern template LogMessage& LogMessage::operator<<(const unsigned long& v);
+extern template LogMessage& LogMessage::operator<<(const long long& v);
+extern template LogMessage& LogMessage::operator<<(const unsigned long long& v);
extern template LogMessage& LogMessage::operator<<(
- const unsigned int& v); // NOLINT
-extern template LogMessage& LogMessage::operator<<(const long& v); // NOLINT
+ absl::Nullable<void*> const& v);
extern template LogMessage& LogMessage::operator<<(
- const unsigned long& v); // NOLINT
-extern template LogMessage& LogMessage::operator<<(
- const long long& v); // NOLINT
-extern template LogMessage& LogMessage::operator<<(
- const unsigned long long& v); // NOLINT
-extern template LogMessage& LogMessage::operator<<(void* const& v);
-extern template LogMessage& LogMessage::operator<<(const void* const& v);
+ absl::Nullable<const void*> const& v);
extern template LogMessage& LogMessage::operator<<(const float& v);
extern template LogMessage& LogMessage::operator<<(const double& v);
extern template LogMessage& LogMessage::operator<<(const bool& v);
+// NOLINTEND(google-runtime-int)
+// NOLINTEND(runtime/int)
extern template void LogMessage::CopyToEncodedBuffer<
LogMessage::StringType::kLiteral>(absl::string_view str);
@@ -351,8 +364,9 @@
// message.
class LogMessageFatal final : public LogMessage {
public:
- LogMessageFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
- LogMessageFatal(const char* file, int line,
+ LogMessageFatal(absl::Nonnull<const char*> file,
+ int line) ABSL_ATTRIBUTE_COLD;
+ LogMessageFatal(absl::Nonnull<const char*> file, int line,
absl::string_view failure_msg) ABSL_ATTRIBUTE_COLD;
[[noreturn]] ~LogMessageFatal();
};
@@ -362,7 +376,8 @@
// for DLOG(FATAL) variants.
class LogMessageDebugFatal final : public LogMessage {
public:
- LogMessageDebugFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
+ LogMessageDebugFatal(absl::Nonnull<const char*> file,
+ int line) ABSL_ATTRIBUTE_COLD;
~LogMessageDebugFatal();
};
@@ -371,15 +386,17 @@
// DLOG(QFATAL) calls this instead of LogMessageQuietlyFatal to make sure the
// destructor is not [[noreturn]] even if this is always FATAL as this is only
// invoked when DLOG() is enabled.
- LogMessageQuietlyDebugFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
+ LogMessageQuietlyDebugFatal(absl::Nonnull<const char*> file,
+ int line) ABSL_ATTRIBUTE_COLD;
~LogMessageQuietlyDebugFatal();
};
// Used for LOG(QFATAL) to make sure it's properly understood as [[noreturn]].
class LogMessageQuietlyFatal final : public LogMessage {
public:
- LogMessageQuietlyFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
- LogMessageQuietlyFatal(const char* file, int line,
+ LogMessageQuietlyFatal(absl::Nonnull<const char*> file,
+ int line) ABSL_ATTRIBUTE_COLD;
+ LogMessageQuietlyFatal(absl::Nonnull<const char*> file, int line,
absl::string_view failure_msg) ABSL_ATTRIBUTE_COLD;
[[noreturn]] ~LogMessageQuietlyFatal();
};
diff --git a/absl/log/log_sink_registry.h b/absl/log/log_sink_registry.h
index bf76cce..3aa3bf6 100644
--- a/absl/log/log_sink_registry.h
+++ b/absl/log/log_sink_registry.h
@@ -22,6 +22,7 @@
#define ABSL_LOG_LOG_SINK_REGISTRY_H_
#include "absl/base/config.h"
+#include "absl/base/nullability.h"
#include "absl/log/internal/log_sink_set.h"
#include "absl/log/log_sink.h"
@@ -43,8 +44,10 @@
// sink instead which writes them to `stderr`.
//
// Do not call these inside `absl::LogSink::Send`.
-inline void AddLogSink(absl::LogSink* sink) { log_internal::AddLogSink(sink); }
-inline void RemoveLogSink(absl::LogSink* sink) {
+inline void AddLogSink(absl::Nonnull<absl::LogSink*> sink) {
+ log_internal::AddLogSink(sink);
+}
+inline void RemoveLogSink(absl::Nonnull<absl::LogSink*> sink) {
log_internal::RemoveLogSink(sink);
}