Perform ValidateDownCast only in Abseil Hardened Extensive mode

Enabling downcast validation checks has incurred about 0.5% overhead on some Google workloads.  Consequently it does not seem like a good inclusion in Abseil Hardened Fast mode, though it remains enabled in Abseil Hardened Extensive mode.

PiperOrigin-RevId: 866108195
Change-Id: Ic796e8bce90bb0d80ad60269a0b3857f19f2c95c
This commit is contained in:
Abseil Team
2026-02-05 14:14:00 -08:00
committed by Copybara-Service
parent 0e2e1ba7d4
commit bf24e8b443
2 changed files with 4 additions and 7 deletions

View File

@@ -211,7 +211,7 @@ inline void ValidateDownCast(From* f ABSL_ATTRIBUTE_UNUSED) {
// Assert only if RTTI is enabled and in debug mode or hardened asserts are
// enabled.
#ifdef ABSL_INTERNAL_HAS_RTTI
#if !defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2)
#if !defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1)
// Suppress erroneous nonnull comparison warning on older GCC.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push

View File

@@ -92,8 +92,7 @@ TEST(DownCastTest, Pointer) {
// Tests a bad downcast. We have to disguise the badness just enough
// that the compiler doesn't warn about it at compile time.
BaseForDownCast* base2 = new BaseForDownCast();
#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || \
ABSL_OPTION_HARDENED == 2))
#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1))
EXPECT_DEATH(static_cast<void>(absl::down_cast<DerivedForDownCast*>(base2)),
".*down cast from .*BaseForDownCast.* to "
".*DerivedForDownCast.* failed.*");
@@ -126,8 +125,7 @@ TEST(DownCastTest, Reference) {
// Tests a bad downcast. We have to disguise the badness just enough
// that the compiler doesn't warn about it at compile time.
BaseForDownCast& base2 = *new BaseForDownCast();
#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || \
ABSL_OPTION_HARDENED == 2))
#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1))
EXPECT_DEATH(static_cast<void>(absl::down_cast<DerivedForDownCast&>(base2)),
".*down cast from .*BaseForDownCast.* to "
".*DerivedForDownCast.* failed.*");
@@ -140,8 +138,7 @@ TEST(DownCastTest, ErrorMessage) {
BaseForDownCast& base = derived;
(void)base;
#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || \
ABSL_OPTION_HARDENED == 2))
#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1))
EXPECT_DEATH(static_cast<void>(absl::down_cast<Derived2ForDownCast&>(base)),
".*down cast from .*DerivedForDownCast.* to "
".*Derived2ForDownCast.* failed.*");