diff --git a/src/google/protobuf/port.h b/src/google/protobuf/port.h index 6d7bd4b713..9aee882f69 100644 --- a/src/google/protobuf/port.h +++ b/src/google/protobuf/port.h @@ -202,17 +202,21 @@ inline constexpr bool DebugHardenStringValues() { #endif } -#if ABSL_HAVE_BUILTIN(__builtin_FILE) && ABSL_HAVE_BUILTIN(__builtin_LINE) -[[noreturn]] inline void Unreachable(const char* file = __builtin_FILE(), - int line = __builtin_LINE()) { -#else -[[noreturn]] inline void Unreachable(const char* file = "", int line = 0) { -#endif #if defined(NDEBUG) && ABSL_HAVE_BUILTIN(__builtin_unreachable) +[[noreturn]] ABSL_ATTRIBUTE_COLD PROTOBUF_ALWAYS_INLINE inline void +Unreachable() { __builtin_unreachable(); -#endif +} +#elif ABSL_HAVE_BUILTIN(__builtin_FILE) && ABSL_HAVE_BUILTIN(__builtin_LINE) +[[noreturn]] ABSL_ATTRIBUTE_COLD inline void Unreachable( + const char* file = __builtin_FILE(), int line = __builtin_LINE()) { protobuf_assumption_failed("Unreachable", file, line); } +#else +[[noreturn]] ABSL_ATTRIBUTE_COLD inline void Unreachable() { + protobuf_assumption_failed("Unreachable", "", 0); +} +#endif } // namespace internal } // namespace protobuf diff --git a/src/google/protobuf/port_test.cc b/src/google/protobuf/port_test.cc index 5fbd55b67e..a8c6402819 100644 --- a/src/google/protobuf/port_test.cc +++ b/src/google/protobuf/port_test.cc @@ -33,10 +33,15 @@ TEST(PortTest, ProtobufAssume) { TEST(PortTest, UnreachableTrapsOnDebugMode) { #ifdef GTEST_HAS_DEATH_TEST - EXPECT_DEBUG_DEATH(Unreachable(), "Assumption failed: 'Unreachable'"); -#if ABSL_HAVE_BUILTIN(__builtin_FILE) - EXPECT_DEBUG_DEATH(Unreachable(), - "port_test\\.cc:.*Assumption failed: 'Unreachable'"); +#if defined(NDEBUG) + // In NDEBUG we crash with a UD instruction, so we don't get the "Assumption + // failed" error. + GTEST_SKIP() << "Can't test __builtin_unreachable()"; +#elif ABSL_HAVE_BUILTIN(__builtin_FILE) + EXPECT_DEATH(Unreachable(), + "port_test\\.cc:.*Assumption failed: 'Unreachable'"); +#else + EXPECT_DEATH(Unreachable(), "Assumption failed: 'Unreachable'"); #endif #endif }