Files
abseil-cpp/absl/log/die_if_null.h
Abseil Team f138b9d6fa Annotate ABSL_DIE_IF_NULL's return type with absl_nonnull
This helps inform Nullability inference without needing any special casing.

Since ABSL_DIE_IF_NULL is allowed for pointers, smart pointers, or classes marked with ABSL_NULLABILITY_COMPATIBLE we introduce a trait to help add the annotation only when compatible. This trait is kept internal for now out of caution for what we are supporting, though people have asked about it before (see b/394789178).

Simple cases are tested by `-Wnonnull` in absl/base/nullability_nc_test.cc. However, it's unclear how to test the complex cases like templates with universal references with the simple compiler `-Wnonnull`. There is a followup nullability inference test (cl/808830606).

Since there is Wnullability-completeness, needed to annotate the rest of die_if_null.h

PiperOrigin-RevId: 810463021
Change-Id: Id5156996bf3a29a99e689974ac2af7b94b21c460
2025-09-23 09:09:36 -07:00

98 lines
3.6 KiB
C++

// Copyright 2022 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.
//
// -----------------------------------------------------------------------------
// File: log/die_if_null.h
// -----------------------------------------------------------------------------
//
// This header declares macro `ABSL_DIE_IF_NULL`.
#ifndef ABSL_LOG_DIE_IF_NULL_H_
#define ABSL_LOG_DIE_IF_NULL_H_
#include <stdint.h>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/nullability_traits.h"
#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
// ABSL_DIE_IF_NULL()
//
// `ABSL_DIE_IF_NULL` behaves as `CHECK_NE` against `nullptr` but *also*
// "returns" its argument. It is useful in initializers where statements (like
// `CHECK_NE`) can't be used. Outside initializers, prefer `CHECK` or
// `CHECK_NE`. `ABSL_DIE_IF_NULL` works for both raw pointers and (compatible)
// smart pointers including `std::unique_ptr` and `std::shared_ptr`; more
// generally, it works for any type that can be compared to nullptr_t. For
// types that aren't raw pointers, `ABSL_DIE_IF_NULL` returns a reference to
// its argument, preserving the value category. Example:
//
// Foo() : bar_(ABSL_DIE_IF_NULL(MethodReturningUniquePtr())) {}
//
// Use `CHECK(ptr)` or `CHECK(ptr != nullptr)` if the returned pointer is
// unused.
#define ABSL_DIE_IF_NULL(val) \
::absl::log_internal::DieIfNull(__FILE__, __LINE__, #val, (val))
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace log_internal {
// Crashes the process after logging `exprtext` annotated at the `file` and
// `line` location. Called when `ABSL_DIE_IF_NULL` fails. Calling this function
// generates less code than its implementation would if inlined, for a slight
// code size reduction each time `ABSL_DIE_IF_NULL` is called.
[[noreturn]] ABSL_ATTRIBUTE_NOINLINE void DieBecauseNull(
const char* absl_nonnull file, int line, const char* absl_nonnull exprtext);
// Helper for `ABSL_DIE_IF_NULL`.
// Since we use `remove_reference_t` before `AddNonnullIfCompatible`, we need
// to explicitly have overloads for both lvalue reference and rvalue reference
// arguments and returns.
template <typename T>
[[nodiscard]] typename absl::base_internal::AddNonnullIfCompatible<
std::remove_reference_t<T>>::type&
DieIfNull(const char* absl_nonnull file, int line,
const char* absl_nonnull exprtext, T& t) {
if (ABSL_PREDICT_FALSE(t == nullptr)) {
// Call a non-inline helper function for a small code size improvement.
DieBecauseNull(file, line, exprtext);
}
return t;
}
template <typename T>
[[nodiscard]] typename absl::base_internal::AddNonnullIfCompatible<
std::remove_reference_t<T>>::type&&
DieIfNull(const char* absl_nonnull file, int line,
const char* absl_nonnull exprtext, T&& t) {
if (ABSL_PREDICT_FALSE(t == nullptr)) {
// Call a non-inline helper function for a small code size improvement.
DieBecauseNull(file, line, exprtext);
}
return std::forward<T>(t);
}
} // namespace log_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_LOG_DIE_IF_NULL_H_