Skip to content

Commit dee1b6c

Browse files
suertreuscopybara-github
authored andcommitted
Add some nullability annotations in logging and tidy up some NOLINTs and comments.
PiperOrigin-RevId: 694512830 Change-Id: Ibeb8eb5fd18af5c49fe45a903e5cb129ee15974d
1 parent 0c7c676 commit dee1b6c

File tree

6 files changed

+85
-58
lines changed

6 files changed

+85
-58
lines changed

absl/log/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ cc_library(
187187
deps = [
188188
":log_sink",
189189
"//absl/base:config",
190+
"//absl/base:nullability",
190191
"//absl/log/internal:log_sink_set",
191192
],
192193
)

absl/log/CMakeLists.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ absl_cc_library(
196196
absl::config
197197
absl::core_headers
198198
absl::errno_saver
199-
absl::inlined_vector
200199
absl::examine_stack
200+
absl::inlined_vector
201201
absl::log_internal_append_truncated
202202
absl::log_internal_format
203203
absl::log_internal_globals
@@ -210,11 +210,12 @@ absl_cc_library(
210210
absl::log_sink
211211
absl::log_sink_registry
212212
absl::memory
213+
absl::nullability
213214
absl::raw_logging_internal
214-
absl::strings
215+
absl::span
215216
absl::strerror
217+
absl::strings
216218
absl::time
217-
absl::span
218219
)
219220

220221
absl_cc_library(
@@ -603,6 +604,7 @@ absl_cc_library(
603604
absl::config
604605
absl::log_sink
605606
absl::log_internal_log_sink_set
607+
absl::nullability
606608
PUBLIC
607609
)
608610

absl/log/internal/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ cc_library(
179179
"//absl/base:core_headers",
180180
"//absl/base:errno_saver",
181181
"//absl/base:log_severity",
182+
"//absl/base:nullability",
182183
"//absl/base:raw_logging_internal",
183184
"//absl/base:strerror",
184185
"//absl/container:inlined_vector",

absl/log/internal/log_message.cc

+10-7
Original file line numberDiff line numberDiff line change
@@ -418,23 +418,26 @@ LogMessage& LogMessage::operator<<(std::ios_base& (*m)(std::ios_base& os)) {
418418
data_->manipulated << m;
419419
return *this;
420420
}
421+
// NOLINTBEGIN(runtime/int)
422+
// NOLINTBEGIN(google-runtime-int)
421423
template LogMessage& LogMessage::operator<<(const char& v);
422424
template LogMessage& LogMessage::operator<<(const signed char& v);
423425
template LogMessage& LogMessage::operator<<(const unsigned char& v);
424-
template LogMessage& LogMessage::operator<<(const short& v); // NOLINT
425-
template LogMessage& LogMessage::operator<<(const unsigned short& v); // NOLINT
426+
template LogMessage& LogMessage::operator<<(const short& v);
427+
template LogMessage& LogMessage::operator<<(const unsigned short& v);
426428
template LogMessage& LogMessage::operator<<(const int& v);
427429
template LogMessage& LogMessage::operator<<(const unsigned int& v);
428-
template LogMessage& LogMessage::operator<<(const long& v); // NOLINT
429-
template LogMessage& LogMessage::operator<<(const unsigned long& v); // NOLINT
430-
template LogMessage& LogMessage::operator<<(const long long& v); // NOLINT
431-
template LogMessage& LogMessage::operator<<(
432-
const unsigned long long& v); // NOLINT
430+
template LogMessage& LogMessage::operator<<(const long& v);
431+
template LogMessage& LogMessage::operator<<(const unsigned long& v);
432+
template LogMessage& LogMessage::operator<<(const long long& v);
433+
template LogMessage& LogMessage::operator<<(const unsigned long long& v);
433434
template LogMessage& LogMessage::operator<<(void* const& v);
434435
template LogMessage& LogMessage::operator<<(const void* const& v);
435436
template LogMessage& LogMessage::operator<<(const float& v);
436437
template LogMessage& LogMessage::operator<<(const double& v);
437438
template LogMessage& LogMessage::operator<<(const bool& v);
439+
// NOLINTEND(google-runtime-int)
440+
// NOLINTEND(runtime/int)
438441

439442
void LogMessage::Flush() {
440443
if (data_->entry.log_severity() < absl::MinLogLevel()) return;

absl/log/internal/log_message.h

+63-46
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@
2727
#ifndef ABSL_LOG_INTERNAL_LOG_MESSAGE_H_
2828
#define ABSL_LOG_INTERNAL_LOG_MESSAGE_H_
2929

30+
#include <cstddef>
3031
#include <ios>
3132
#include <memory>
3233
#include <ostream>
3334
#include <streambuf>
3435
#include <string>
36+
#include <type_traits>
3537

3638
#include "absl/base/attributes.h"
3739
#include "absl/base/config.h"
3840
#include "absl/base/internal/errno_saver.h"
3941
#include "absl/base/log_severity.h"
42+
#include "absl/base/nullability.h"
4043
#include "absl/log/internal/nullguard.h"
4144
#include "absl/log/log_entry.h"
4245
#include "absl/log/log_sink.h"
@@ -56,15 +59,15 @@ class LogMessage {
5659
struct ErrorTag {};
5760

5861
// Used for `LOG`.
59-
LogMessage(const char* file, int line,
62+
LogMessage(absl::Nonnull<const char*> file, int line,
6063
absl::LogSeverity severity) ABSL_ATTRIBUTE_COLD;
6164
// These constructors are slightly smaller/faster to call; the severity is
6265
// curried into the function pointer.
63-
LogMessage(const char* file, int line,
66+
LogMessage(absl::Nonnull<const char*> file, int line,
6467
InfoTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
65-
LogMessage(const char* file, int line,
68+
LogMessage(absl::Nonnull<const char*> file, int line,
6669
WarningTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
67-
LogMessage(const char* file, int line,
70+
LogMessage(absl::Nonnull<const char*> file, int line,
6871
ErrorTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
6972
LogMessage(const LogMessage&) = delete;
7073
LogMessage& operator=(const LogMessage&) = delete;
@@ -95,59 +98,67 @@ class LogMessage {
9598
// of `errno`.
9699
LogMessage& WithPerror();
97100
// Sends this message to `*sink` in addition to whatever other sinks it would
98-
// otherwise have been sent to. `sink` must not be null.
99-
LogMessage& ToSinkAlso(absl::LogSink* sink);
100-
// Sends this message to `*sink` and no others. `sink` must not be null.
101-
LogMessage& ToSinkOnly(absl::LogSink* sink);
101+
// otherwise have been sent to.
102+
LogMessage& ToSinkAlso(absl::Nonnull<absl::LogSink*> sink);
103+
// Sends this message to `*sink` and no others.
104+
LogMessage& ToSinkOnly(absl::Nonnull<absl::LogSink*> sink);
102105

103106
// Don't call this method from outside this library.
104107
LogMessage& InternalStream() { return *this; }
105108

106109
// By-value overloads for small, common types let us overlook common failures
107110
// to define globals and static data members (i.e. in a .cc file).
108-
// clang-format off
109-
// The CUDA toolchain cannot handle these <<<'s:
111+
// NOLINTBEGIN(runtime/int)
112+
// NOLINTBEGIN(google-runtime-int)
113+
// clang-format off: The CUDA toolchain cannot handle these <<<'s
110114
LogMessage& operator<<(char v) { return operator<< <char>(v); }
111115
LogMessage& operator<<(signed char v) { return operator<< <signed char>(v); }
112116
LogMessage& operator<<(unsigned char v) {
113117
return operator<< <unsigned char>(v);
114118
}
115-
LogMessage& operator<<(signed short v) { // NOLINT
116-
return operator<< <signed short>(v); // NOLINT
119+
LogMessage& operator<<(signed short v) {
120+
return operator<< <signed short>(v);
117121
}
118122
LogMessage& operator<<(signed int v) { return operator<< <signed int>(v); }
119-
LogMessage& operator<<(signed long v) { // NOLINT
120-
return operator<< <signed long>(v); // NOLINT
123+
LogMessage& operator<<(signed long v) {
124+
return operator<< <signed long>(v);
121125
}
122-
LogMessage& operator<<(signed long long v) { // NOLINT
123-
return operator<< <signed long long>(v); // NOLINT
126+
LogMessage& operator<<(signed long long v) {
127+
return operator<< <signed long long>(v);
124128
}
125-
LogMessage& operator<<(unsigned short v) { // NOLINT
126-
return operator<< <unsigned short>(v); // NOLINT
129+
LogMessage& operator<<(unsigned short v) {
130+
return operator<< <unsigned short>(v);
127131
}
128132
LogMessage& operator<<(unsigned int v) {
129133
return operator<< <unsigned int>(v);
130134
}
131-
LogMessage& operator<<(unsigned long v) { // NOLINT
132-
return operator<< <unsigned long>(v); // NOLINT
135+
LogMessage& operator<<(unsigned long v) {
136+
return operator<< <unsigned long>(v);
133137
}
134-
LogMessage& operator<<(unsigned long long v) { // NOLINT
135-
return operator<< <unsigned long long>(v); // NOLINT
138+
LogMessage& operator<<(unsigned long long v) {
139+
return operator<< <unsigned long long>(v);
140+
}
141+
LogMessage& operator<<(absl::Nullable<void*> v) {
142+
return operator<< <void*>(v);
143+
}
144+
LogMessage& operator<<(absl::Nullable<const void*> v) {
145+
return operator<< <const void*>(v);
136146
}
137-
LogMessage& operator<<(void* v) { return operator<< <void*>(v); }
138-
LogMessage& operator<<(const void* v) { return operator<< <const void*>(v); }
139147
LogMessage& operator<<(float v) { return operator<< <float>(v); }
140148
LogMessage& operator<<(double v) { return operator<< <double>(v); }
141149
LogMessage& operator<<(bool v) { return operator<< <bool>(v); }
142150
// clang-format on
151+
// NOLINTEND(google-runtime-int)
152+
// NOLINTEND(runtime/int)
143153

144154
// These overloads are more efficient since no `ostream` is involved.
145155
LogMessage& operator<<(const std::string& v);
146156
LogMessage& operator<<(absl::string_view v);
147157

148158
// Handle stream manipulators e.g. std::endl.
149-
LogMessage& operator<<(std::ostream& (*m)(std::ostream& os));
150-
LogMessage& operator<<(std::ios_base& (*m)(std::ios_base& os));
159+
LogMessage& operator<<(absl::Nonnull<std::ostream& (*)(std::ostream & os)> m);
160+
LogMessage& operator<<(
161+
absl::Nonnull<std::ios_base& (*)(std::ios_base & os)> m);
151162

152163
// Literal strings. This allows us to record C string literals as literals in
153164
// the logging.proto.Value.
@@ -255,7 +266,7 @@ class LogMessage {
255266

256267
// We keep the data in a separate struct so that each instance of `LogMessage`
257268
// uses less stack space.
258-
std::unique_ptr<LogMessageData> data_;
269+
absl::Nonnull<std::unique_ptr<LogMessageData>> data_;
259270
};
260271

261272
// Helper class so that `AbslStringify()` can modify the LogMessage.
@@ -273,7 +284,8 @@ class StringifySink final {
273284
}
274285

275286
// For types that implement `AbslStringify` using `absl::Format()`.
276-
friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
287+
friend void AbslFormatFlush(absl::Nonnull<StringifySink*> sink,
288+
absl::string_view v) {
277289
sink->Append(v);
278290
}
279291

@@ -315,27 +327,28 @@ LogMessage& LogMessage::operator<<(char (&buf)[SIZE]) {
315327
// We instantiate these specializations in the library's TU to save space in
316328
// other TUs. Since the template is marked `ABSL_ATTRIBUTE_NOINLINE` we will be
317329
// emitting a function call either way.
330+
// NOLINTBEGIN(runtime/int)
331+
// NOLINTBEGIN(google-runtime-int)
318332
extern template LogMessage& LogMessage::operator<<(const char& v);
319333
extern template LogMessage& LogMessage::operator<<(const signed char& v);
320334
extern template LogMessage& LogMessage::operator<<(const unsigned char& v);
321-
extern template LogMessage& LogMessage::operator<<(const short& v); // NOLINT
322-
extern template LogMessage& LogMessage::operator<<(
323-
const unsigned short& v); // NOLINT
335+
extern template LogMessage& LogMessage::operator<<(const short& v);
336+
extern template LogMessage& LogMessage::operator<<(const unsigned short& v);
324337
extern template LogMessage& LogMessage::operator<<(const int& v);
338+
extern template LogMessage& LogMessage::operator<<(const unsigned int& v);
339+
extern template LogMessage& LogMessage::operator<<(const long& v);
340+
extern template LogMessage& LogMessage::operator<<(const unsigned long& v);
341+
extern template LogMessage& LogMessage::operator<<(const long long& v);
342+
extern template LogMessage& LogMessage::operator<<(const unsigned long long& v);
325343
extern template LogMessage& LogMessage::operator<<(
326-
const unsigned int& v); // NOLINT
327-
extern template LogMessage& LogMessage::operator<<(const long& v); // NOLINT
328-
extern template LogMessage& LogMessage::operator<<(
329-
const unsigned long& v); // NOLINT
330-
extern template LogMessage& LogMessage::operator<<(
331-
const long long& v); // NOLINT
344+
absl::Nullable<void*> const& v);
332345
extern template LogMessage& LogMessage::operator<<(
333-
const unsigned long long& v); // NOLINT
334-
extern template LogMessage& LogMessage::operator<<(void* const& v);
335-
extern template LogMessage& LogMessage::operator<<(const void* const& v);
346+
absl::Nullable<const void*> const& v);
336347
extern template LogMessage& LogMessage::operator<<(const float& v);
337348
extern template LogMessage& LogMessage::operator<<(const double& v);
338349
extern template LogMessage& LogMessage::operator<<(const bool& v);
350+
// NOLINTEND(google-runtime-int)
351+
// NOLINTEND(runtime/int)
339352

340353
extern template void LogMessage::CopyToEncodedBuffer<
341354
LogMessage::StringType::kLiteral>(absl::string_view str);
@@ -351,8 +364,9 @@ extern template void LogMessage::CopyToEncodedBuffer<
351364
// message.
352365
class LogMessageFatal final : public LogMessage {
353366
public:
354-
LogMessageFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
355-
LogMessageFatal(const char* file, int line,
367+
LogMessageFatal(absl::Nonnull<const char*> file,
368+
int line) ABSL_ATTRIBUTE_COLD;
369+
LogMessageFatal(absl::Nonnull<const char*> file, int line,
356370
absl::string_view failure_msg) ABSL_ATTRIBUTE_COLD;
357371
[[noreturn]] ~LogMessageFatal();
358372
};
@@ -362,7 +376,8 @@ class LogMessageFatal final : public LogMessage {
362376
// for DLOG(FATAL) variants.
363377
class LogMessageDebugFatal final : public LogMessage {
364378
public:
365-
LogMessageDebugFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
379+
LogMessageDebugFatal(absl::Nonnull<const char*> file,
380+
int line) ABSL_ATTRIBUTE_COLD;
366381
~LogMessageDebugFatal();
367382
};
368383

@@ -371,15 +386,17 @@ class LogMessageQuietlyDebugFatal final : public LogMessage {
371386
// DLOG(QFATAL) calls this instead of LogMessageQuietlyFatal to make sure the
372387
// destructor is not [[noreturn]] even if this is always FATAL as this is only
373388
// invoked when DLOG() is enabled.
374-
LogMessageQuietlyDebugFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
389+
LogMessageQuietlyDebugFatal(absl::Nonnull<const char*> file,
390+
int line) ABSL_ATTRIBUTE_COLD;
375391
~LogMessageQuietlyDebugFatal();
376392
};
377393

378394
// Used for LOG(QFATAL) to make sure it's properly understood as [[noreturn]].
379395
class LogMessageQuietlyFatal final : public LogMessage {
380396
public:
381-
LogMessageQuietlyFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
382-
LogMessageQuietlyFatal(const char* file, int line,
397+
LogMessageQuietlyFatal(absl::Nonnull<const char*> file,
398+
int line) ABSL_ATTRIBUTE_COLD;
399+
LogMessageQuietlyFatal(absl::Nonnull<const char*> file, int line,
383400
absl::string_view failure_msg) ABSL_ATTRIBUTE_COLD;
384401
[[noreturn]] ~LogMessageQuietlyFatal();
385402
};

absl/log/log_sink_registry.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define ABSL_LOG_LOG_SINK_REGISTRY_H_
2323

2424
#include "absl/base/config.h"
25+
#include "absl/base/nullability.h"
2526
#include "absl/log/internal/log_sink_set.h"
2627
#include "absl/log/log_sink.h"
2728

@@ -43,8 +44,10 @@ ABSL_NAMESPACE_BEGIN
4344
// sink instead which writes them to `stderr`.
4445
//
4546
// Do not call these inside `absl::LogSink::Send`.
46-
inline void AddLogSink(absl::LogSink* sink) { log_internal::AddLogSink(sink); }
47-
inline void RemoveLogSink(absl::LogSink* sink) {
47+
inline void AddLogSink(absl::Nonnull<absl::LogSink*> sink) {
48+
log_internal::AddLogSink(sink);
49+
}
50+
inline void RemoveLogSink(absl::Nonnull<absl::LogSink*> sink) {
4851
log_internal::RemoveLogSink(sink);
4952
}
5053

0 commit comments

Comments
 (0)