diff options
author | 2023-12-11 09:07:07 -0800 | |
---|---|---|
committer | 2023-12-11 09:07:59 -0800 | |
commit | 55621d1af55be7bf9a2c8e5dcef4368d9a6d4e51 (patch) | |
tree | 4c0e74b9a45b8875e35ee543089315a0d9989df1 /absl/status | |
parent | fad946221cec37175e762c399760f54b9de9a9fa (diff) |
Add nullability annotations
PiperOrigin-RevId: 589842893
Change-Id: I9657761d1f71c665582406f278c6605f6d382f6d
Diffstat (limited to 'absl/status')
-rw-r--r-- | absl/status/BUILD.bazel | 2 | ||||
-rw-r--r-- | absl/status/CMakeLists.txt | 2 | ||||
-rw-r--r-- | absl/status/internal/status_internal.cc | 8 | ||||
-rw-r--r-- | absl/status/internal/status_internal.h | 8 | ||||
-rw-r--r-- | absl/status/internal/statusor_internal.h | 6 | ||||
-rw-r--r-- | absl/status/status.cc | 10 | ||||
-rw-r--r-- | absl/status/status.h | 19 | ||||
-rw-r--r-- | absl/status/status_payload_printer.h | 5 | ||||
-rw-r--r-- | absl/status/statusor.cc | 5 | ||||
-rw-r--r-- | absl/status/statusor.h | 7 |
10 files changed, 46 insertions, 26 deletions
diff --git a/absl/status/BUILD.bazel b/absl/status/BUILD.bazel index 3cf0a5e1..a5fad4d4 100644 --- a/absl/status/BUILD.bazel +++ b/absl/status/BUILD.bazel @@ -54,6 +54,7 @@ cc_library( "//absl/base:config", "//absl/base:core_headers", "//absl/base:no_destructor", + "//absl/base:nullability", "//absl/base:raw_logging_internal", "//absl/base:strerror", "//absl/container:inlined_vector", @@ -100,6 +101,7 @@ cc_library( "//absl/base", "//absl/base:config", "//absl/base:core_headers", + "//absl/base:nullability", "//absl/base:raw_logging_internal", "//absl/meta:type_traits", "//absl/strings", diff --git a/absl/status/CMakeLists.txt b/absl/status/CMakeLists.txt index da789901..d9d16830 100644 --- a/absl/status/CMakeLists.txt +++ b/absl/status/CMakeLists.txt @@ -37,6 +37,7 @@ absl_cc_library( absl::inlined_vector absl::memory absl::no_destructor + absl::nullability absl::optional absl::raw_logging_internal absl::span @@ -76,6 +77,7 @@ absl_cc_library( absl::base absl::config absl::core_headers + absl::nullability absl::raw_logging_internal absl::status absl::strings diff --git a/absl/status/internal/status_internal.cc b/absl/status/internal/status_internal.cc index 2307579b..a9156754 100644 --- a/absl/status/internal/status_internal.cc +++ b/absl/status/internal/status_internal.cc @@ -27,6 +27,7 @@ #include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/base/macros.h" +#include "absl/base/nullability.h" #include "absl/debugging/stacktrace.h" #include "absl/debugging/symbolize.h" #include "absl/memory/memory.h" @@ -187,7 +188,7 @@ bool StatusRep::operator==(const StatusRep& other) const { return true; } -StatusRep* StatusRep::CloneAndUnref() const { +absl::Nonnull<StatusRep*> StatusRep::CloneAndUnref() const { // Optimization: no need to create a clone if we already have a refcount of 1. if (ref_.load(std::memory_order_acquire) == 1) { // All StatusRep instances are heap allocated and mutable, therefore this @@ -233,8 +234,9 @@ absl::StatusCode MapToLocalCode(int value) { } } -std::string* MakeCheckFailString(const absl::Status* status, - const char* prefix) { +absl::Nonnull<std::string*> MakeCheckFailString( + absl::Nonnull<const absl::Status*> status, + absl::Nonnull<const char*> prefix) { return new std::string( absl::StrCat(prefix, " (", status->ToString(StatusToStringMode::kWithEverything), ")")); diff --git a/absl/status/internal/status_internal.h b/absl/status/internal/status_internal.h index c84e626f..c9f43832 100644 --- a/absl/status/internal/status_internal.h +++ b/absl/status/internal/status_internal.h @@ -22,6 +22,7 @@ #include "absl/base/attributes.h" #include "absl/base/config.h" +#include "absl/base/nullability.h" #include "absl/container/inlined_vector.h" #include "absl/strings/cord.h" #include "absl/strings/string_view.h" @@ -99,7 +100,7 @@ class StatusRep { // Returns an equivalent heap allocated StatusRep with refcount 1. // // `this` is not safe to be used after calling as it may have been deleted. - StatusRep* CloneAndUnref() const; + absl::Nonnull<StatusRep*> CloneAndUnref() const; private: mutable std::atomic<int32_t> ref_; @@ -119,8 +120,9 @@ absl::StatusCode MapToLocalCode(int value); // // This is an internal implementation detail for Abseil logging. ABSL_ATTRIBUTE_PURE_FUNCTION -std::string* MakeCheckFailString(const absl::Status* status, - const char* prefix); +absl::Nonnull<std::string*> MakeCheckFailString( + absl::Nonnull<const absl::Status*> status, + absl::Nonnull<const char*> prefix); } // namespace status_internal diff --git a/absl/status/internal/statusor_internal.h b/absl/status/internal/statusor_internal.h index 49cead7a..25c11474 100644 --- a/absl/status/internal/statusor_internal.h +++ b/absl/status/internal/statusor_internal.h @@ -18,6 +18,7 @@ #include <utility> #include "absl/base/attributes.h" +#include "absl/base/nullability.h" #include "absl/meta/type_traits.h" #include "absl/status/status.h" #include "absl/utility/utility.h" @@ -123,7 +124,7 @@ using IsForwardingAssignmentValid = absl::disjunction< class Helper { public: // Move type-agnostic error handling to the .cc. - static void HandleInvalidStatusCtorArg(Status*); + static void HandleInvalidStatusCtorArg(absl::Nonnull<Status*>); ABSL_ATTRIBUTE_NORETURN static void Crash(const absl::Status& status); }; @@ -131,7 +132,8 @@ class Helper { // the constructor. // This abstraction is here mostly for the gcc performance fix. template <typename T, typename... Args> -ABSL_ATTRIBUTE_NONNULL(1) void PlacementNew(void* p, Args&&... args) { +ABSL_ATTRIBUTE_NONNULL(1) +void PlacementNew(absl::Nonnull<void*> p, Args&&... args) { new (p) T(std::forward<Args>(args)...); } diff --git a/absl/status/status.cc b/absl/status/status.cc index 7c778ac0..4dd5ae06 100644 --- a/absl/status/status.cc +++ b/absl/status/status.cc @@ -29,6 +29,7 @@ #include "absl/base/internal/strerror.h" #include "absl/base/macros.h" #include "absl/base/no_destructor.h" +#include "absl/base/nullability.h" #include "absl/debugging/stacktrace.h" #include "absl/debugging/symbolize.h" #include "absl/status/internal/status_internal.h" @@ -90,7 +91,7 @@ std::ostream& operator<<(std::ostream& os, StatusCode code) { return os << StatusCodeToString(code); } -const std::string* Status::EmptyString() { +absl::Nonnull<const std::string*> Status::EmptyString() { static const absl::NoDestructor<std::string> kEmpty; return kEmpty.get(); } @@ -99,7 +100,7 @@ const std::string* Status::EmptyString() { constexpr const char Status::kMovedFromString[]; #endif -const std::string* Status::MovedFromString() { +absl::Nonnull<const std::string*> Status::MovedFromString() { static const absl::NoDestructor<std::string> kMovedFrom(kMovedFromString); return kMovedFrom.get(); } @@ -111,7 +112,8 @@ Status::Status(absl::StatusCode code, absl::string_view msg) } } -status_internal::StatusRep* Status::PrepareToModify(uintptr_t rep) { +absl::Nonnull<status_internal::StatusRep*> Status::PrepareToModify( + uintptr_t rep) { if (IsInlined(rep)) { return new status_internal::StatusRep(InlinedRepToCode(rep), absl::string_view(), nullptr); @@ -412,7 +414,7 @@ Status ErrnoToStatus(int error_number, absl::string_view message) { MessageForErrnoToStatus(error_number, message)); } -const char* StatusMessageAsCStr(const Status& status) { +absl::Nonnull<const char*> StatusMessageAsCStr(const Status& status) { // As an internal implementation detail, we guarantee that if status.message() // is non-empty, then the resulting string_view is null terminated. auto sv_message = status.message(); diff --git a/absl/status/status.h b/absl/status/status.h index d4f74140..9ce16db9 100644 --- a/absl/status/status.h +++ b/absl/status/status.h @@ -60,6 +60,7 @@ #include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/base/macros.h" +#include "absl/base/nullability.h" #include "absl/base/optimization.h" #include "absl/functional/function_ref.h" #include "absl/status/internal/status_internal.h" @@ -622,14 +623,15 @@ class ABSL_ATTRIBUTE_TRIVIAL_ABI Status final { // REQUIRES: !ok() // Ensures rep is not inlined or shared with any other Status. - static status_internal::StatusRep* PrepareToModify(uintptr_t rep); + static absl::Nonnull<status_internal::StatusRep*> PrepareToModify( + uintptr_t rep); // MSVC 14.0 limitation requires the const. static constexpr const char kMovedFromString[] = "Status accessed after move."; - static const std::string* EmptyString(); - static const std::string* MovedFromString(); + static absl::Nonnull<const std::string*> EmptyString(); + static absl::Nonnull<const std::string*> MovedFromString(); // Returns whether rep contains an inlined representation. // See rep_ for details. @@ -648,7 +650,8 @@ class ABSL_ATTRIBUTE_TRIVIAL_ABI Status final { // Converts between StatusRep* and the external uintptr_t representation used // by rep_. See rep_ for details. static uintptr_t PointerToRep(status_internal::StatusRep* r); - static const status_internal::StatusRep* RepToPointer(uintptr_t r); + static absl::Nonnull<const status_internal::StatusRep*> RepToPointer( + uintptr_t r); static std::string ToStringSlow(uintptr_t rep, StatusToStringMode mode); @@ -899,12 +902,14 @@ constexpr uintptr_t Status::MovedFromRep() { return CodeToInlinedRep(absl::StatusCode::kInternal) | 2; } -inline const status_internal::StatusRep* Status::RepToPointer(uintptr_t rep) { +inline absl::Nonnull<const status_internal::StatusRep*> Status::RepToPointer( + uintptr_t rep) { assert(!IsInlined(rep)); return reinterpret_cast<const status_internal::StatusRep*>(rep); } -inline uintptr_t Status::PointerToRep(status_internal::StatusRep* rep) { +inline uintptr_t Status::PointerToRep( + absl::Nonnull<status_internal::StatusRep*> rep) { return reinterpret_cast<uintptr_t>(rep); } @@ -929,7 +934,7 @@ inline Status CancelledError() { return Status(absl::StatusCode::kCancelled); } // If the status's message is empty, the empty string is returned. // // StatusMessageAsCStr exists for C support. Use `status.message()` in C++. -const char* StatusMessageAsCStr( +absl::Nonnull<const char*> StatusMessageAsCStr( const Status& status ABSL_ATTRIBUTE_LIFETIME_BOUND); ABSL_NAMESPACE_END diff --git a/absl/status/status_payload_printer.h b/absl/status/status_payload_printer.h index 5e0937f6..f22255e1 100644 --- a/absl/status/status_payload_printer.h +++ b/absl/status/status_payload_printer.h @@ -16,6 +16,7 @@ #include <string> +#include "absl/base/nullability.h" #include "absl/strings/cord.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" @@ -34,8 +35,8 @@ namespace status_internal { // NOTE: This is an internal API and the design is subject to change in the // future in a non-backward-compatible way. Since it's only meant for debugging // purpose, you should not rely on it in any critical logic. -using StatusPayloadPrinter = absl::optional<std::string> (*)(absl::string_view, - const absl::Cord&); +using StatusPayloadPrinter = absl::Nullable<absl::optional<std::string> (*)( + absl::string_view, const absl::Cord&)>; // Sets the global payload printer. Only one printer should be set per process. // If multiple printers are set, it's undefined which one will be used. diff --git a/absl/status/statusor.cc b/absl/status/statusor.cc index bfad75ef..7e6b334c 100644 --- a/absl/status/statusor.cc +++ b/absl/status/statusor.cc @@ -19,6 +19,7 @@ #include "absl/base/call_once.h" #include "absl/base/config.h" #include "absl/base/internal/raw_logging.h" +#include "absl/base/nullability.h" #include "absl/status/internal/statusor_internal.h" #include "absl/status/status.h" #include "absl/strings/str_cat.h" @@ -54,7 +55,7 @@ BadStatusOrAccess& BadStatusOrAccess::operator=(BadStatusOrAccess&& other) { BadStatusOrAccess::BadStatusOrAccess(BadStatusOrAccess&& other) : status_(std::move(other.status_)) {} -const char* BadStatusOrAccess::what() const noexcept { +absl::Nonnull<const char*> BadStatusOrAccess::what() const noexcept { InitWhat(); return what_.c_str(); } @@ -69,7 +70,7 @@ void BadStatusOrAccess::InitWhat() const { namespace internal_statusor { -void Helper::HandleInvalidStatusCtorArg(absl::Status* status) { +void Helper::HandleInvalidStatusCtorArg(absl::Nonnull<absl::Status*> status) { const char* kMessage = "An OK status is not a valid constructor argument to StatusOr<T>"; #ifdef NDEBUG diff --git a/absl/status/statusor.h b/absl/status/statusor.h index 54c7ce02..33a2cf31 100644 --- a/absl/status/statusor.h +++ b/absl/status/statusor.h @@ -44,6 +44,7 @@ #include <utility> #include "absl/base/attributes.h" +#include "absl/base/nullability.h" #include "absl/base/call_once.h" #include "absl/meta/type_traits.h" #include "absl/status/internal/statusor_internal.h" @@ -88,7 +89,7 @@ class BadStatusOrAccess : public std::exception { // // The pointer of this string is guaranteed to be valid until any non-const // function is invoked on the exception object. - const char* what() const noexcept override; + absl::Nonnull<const char*> what() const noexcept override; // BadStatusOrAccess::status() // @@ -750,13 +751,13 @@ T&& StatusOr<T>::operator*() && { } template <typename T> -const T* StatusOr<T>::operator->() const { +absl::Nonnull<const T*> StatusOr<T>::operator->() const { this->EnsureOk(); return &this->data_; } template <typename T> -T* StatusOr<T>::operator->() { +absl::Nonnull<T*> StatusOr<T>::operator->() { this->EnsureOk(); return &this->data_; } |