Change abseil hardening assertions in chunked_queue and fixed_array from macros to functions

This associates debug information with the assertion sites,
allowing clearer stack-traces for assertion failures and
better accounting of the performance overhead of assertions.

PiperOrigin-RevId: 911422559
Change-Id: Ifce3fd62685173c6b2f83c4c4e4c97c152a463b1
This commit is contained in:
Abseil Team
2026-05-06 10:41:58 -07:00
committed by Copybara-Service
parent 4dcbb5982d
commit 271ba2770d
4 changed files with 19 additions and 12 deletions

View File

@@ -71,6 +71,7 @@ cc_library(
"//absl/base:config",
"//absl/base:core_headers",
"//absl/base:dynamic_annotations",
"//absl/base:hardening",
"//absl/base:iterator_traits_internal",
"//absl/base:throw_delegate",
"//absl/hash:weakly_mixed_integer",
@@ -1367,6 +1368,7 @@ cc_library(
":layout",
"//absl/base:config",
"//absl/base:core_headers",
"//absl/base:hardening",
"//absl/base:iterator_traits_internal",
],
)

View File

@@ -129,6 +129,7 @@ absl_cc_library(
absl::config
absl::core_headers
absl::dynamic_annotations
absl::hardening
absl::iterator_traits_internal
absl::throw_delegate
absl::memory
@@ -1214,6 +1215,7 @@ absl_cc_library(
DEPS
absl::config
absl::core_headers
absl::hardening
absl::iterator_traits_internal
absl::layout
)

View File

@@ -107,6 +107,7 @@
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/hardening.h"
#include "absl/base/internal/iterator_traits.h"
#include "absl/base/macros.h"
#include "absl/container/internal/chunked_queue.h"
@@ -428,22 +429,22 @@ class chunked_queue {
// Returns a reference to the first element in the container.
// REQUIRES: !empty()
T& front() {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return *head_;
}
const T& front() const {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return *head_;
}
// Returns a reference to the last element in the container.
// REQUIRES: !empty()
T& back() {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return *(&*tail_ - 1);
}
const T& back() const {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return *(&*tail_ - 1);
}
@@ -460,7 +461,8 @@ class chunked_queue {
// (It is undefined behavior to swap between two containers with unequal
// allocators if propagate_on_container_swap is false, so we don't have to
// handle that here like we do in the move-assignment operator.)
ABSL_HARDENING_ASSERT(get_allocator() == other.get_allocator());
absl::base_internal::HardeningAssert(get_allocator() ==
other.get_allocator());
swap(alloc_and_size_.size, other.alloc_and_size_.size);
}
}
@@ -709,7 +711,7 @@ inline void chunked_queue<T, BLo, BHi, Allocator>::DestroyAndDeallocateAll() {
template <typename T, size_t BLo, size_t BHi, typename Allocator>
inline void chunked_queue<T, BLo, BHi, Allocator>::pop_front() {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
ABSL_ASSERT(head_.block);
AllocatorTraits::destroy(alloc_and_size_.allocator(), head_.ptr);
++head_.ptr;

View File

@@ -44,6 +44,7 @@
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/dynamic_annotations.h"
#include "absl/base/internal/hardening.h"
#include "absl/base/internal/iterator_traits.h"
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
@@ -222,7 +223,7 @@ class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
// Returns a reference the ith element of the fixed array.
// REQUIRES: 0 <= i < size()
reference operator[](size_type i) ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(i < size());
absl::base_internal::HardeningAssertLT(i, size());
return data()[i];
}
@@ -230,7 +231,7 @@ class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
// ith element of the fixed array.
// REQUIRES: 0 <= i < size()
const_reference operator[](size_type i) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(i < size());
absl::base_internal::HardeningAssertLT(i, size());
return data()[i];
}
@@ -258,14 +259,14 @@ class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
//
// Returns a reference to the first element of the fixed array.
reference front() ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return data()[0];
}
// Overload of FixedArray::front() to return a reference to the first element
// of a fixed array of const values.
const_reference front() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return data()[0];
}
@@ -273,14 +274,14 @@ class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
//
// Returns a reference to the last element of the fixed array.
reference back() ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return data()[size() - 1];
}
// Overload of FixedArray::back() to return a reference to the last element
// of a fixed array of const values.
const_reference back() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_HARDENING_ASSERT(!empty());
absl::base_internal::HardeningAssertNonEmpty(*this);
return data()[size() - 1];
}