From 30a8ef5008f9dec3f4270d3139a318099a7fc8ac Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 26 Aug 2024 09:48:32 -0700 Subject: [PATCH] Prepare `MessageLite::GetTypeName` to be upgraded to return `absl::string_view`. This is a modernization of the API and performance improvement for all callers. Currently gated behind opt-in macro `PROTOBUF_TEMPORARY_ENABLE_STRING_VIEW_RETURN_TYPE` for users to test and prevent issues when the breaking change happens. PiperOrigin-RevId: 667616632 --- src/google/protobuf/lite_unittest.cc | 21 ++++++++++++--------- src/google/protobuf/message_lite.cc | 4 ++-- src/google/protobuf/message_lite.h | 8 +++++++- src/google/protobuf/message_unittest.inc | 17 +++++++++-------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index be4c428a5927..2999f7e5dce4 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -1363,9 +1363,10 @@ TEST(LiteTest, DynamicCastMessage) { TEST(LiteTest, DynamicCastMessageInvalidReferenceType) { CastType1 test_type_1; const MessageLite& test_type_1_pointer_const_ref = test_type_1; - ASSERT_DEATH(DynamicCastMessage(test_type_1_pointer_const_ref), - "Cannot downcast " + test_type_1.GetTypeName() + " to " + - CastType2::default_instance().GetTypeName()); + ASSERT_DEATH( + DynamicCastMessage(test_type_1_pointer_const_ref), + absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ", + CastType2::default_instance().GetTypeName())); } #endif // GTEST_HAS_DEATH_TEST @@ -1396,9 +1397,10 @@ TEST(LiteTest, DownCastMessageInvalidPointerType) { MessageLite* test_type_1_pointer = &test_type_1; - ASSERT_DEBUG_DEATH(DownCastMessage(test_type_1_pointer), - "Cannot downcast " + test_type_1.GetTypeName() + " to " + - CastType2::default_instance().GetTypeName()); + ASSERT_DEBUG_DEATH( + DownCastMessage(test_type_1_pointer), + absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ", + CastType2::default_instance().GetTypeName())); } TEST(LiteTest, DownCastMessageInvalidReferenceType) { @@ -1406,9 +1408,10 @@ TEST(LiteTest, DownCastMessageInvalidReferenceType) { MessageLite& test_type_1_pointer = test_type_1; - ASSERT_DEBUG_DEATH(DownCastMessage(test_type_1_pointer), - "Cannot downcast " + test_type_1.GetTypeName() + " to " + - CastType2::default_instance().GetTypeName()); + ASSERT_DEBUG_DEATH( + DownCastMessage(test_type_1_pointer), + absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ", + CastType2::default_instance().GetTypeName())); } #endif // GTEST_HAS_DEATH_TEST diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index b188a9e2d2a4..132bdc608b7d 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -74,8 +74,8 @@ const char* MessageLite::_InternalParse(const char* ptr, return internal::TcParser::ParseLoop(this, ptr, ctx, GetTcParseTable()); } -std::string MessageLite::GetTypeName() const { - return std::string(TypeId::Get(*this).name()); +internal::GetTypeNameReturnType MessageLite::GetTypeName() const { + return internal::GetTypeNameReturnType(TypeId::Get(*this).name()); } absl::string_view TypeId::name() const { diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index 058b11476819..c811e04941ba 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -190,6 +190,12 @@ inline int ToIntSize(size_t size) { return static_cast(size); } +#if defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE) +using GetTypeNameReturnType = absl::string_view; +#else +using GetTypeNameReturnType = std::string; +#endif + // Default empty string object. Don't use this directly. Instead, call // GetEmptyString() to get the reference. This empty string is aligned with a // minimum alignment of 8 bytes to match the requirement of ArenaStringPtr. @@ -240,7 +246,7 @@ class PROTOBUF_EXPORT MessageLite { // Basic Operations ------------------------------------------------ // Get the name of this message type, e.g. "foo.bar.BazProto". - std::string GetTypeName() const; + internal::GetTypeNameReturnType GetTypeName() const; // Construct a new instance of the same type. Ownership is passed to the // caller. diff --git a/src/google/protobuf/message_unittest.inc b/src/google/protobuf/message_unittest.inc index dfd6fe5c9f33..e555c21ad798 100644 --- a/src/google/protobuf/message_unittest.inc +++ b/src/google/protobuf/message_unittest.inc @@ -792,10 +792,11 @@ TEST(MESSAGE_TEST_NAME, DynamicCastMessage) { TEST(MESSAGE_TEST_NAME, DynamicCastMessageInvalidReferenceType) { UNITTEST::TestAllTypes test_all_types; const MessageLite& test_all_types_pointer_const_ref = test_all_types; - ASSERT_DEATH(DynamicCastMessage( - test_all_types_pointer_const_ref), - "Cannot downcast " + test_all_types.GetTypeName() + " to " + - UNITTEST::TestRequired::default_instance().GetTypeName()); + ASSERT_DEATH( + DynamicCastMessage( + test_all_types_pointer_const_ref), + absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ", + UNITTEST::TestRequired::default_instance().GetTypeName())); } TEST(MESSAGE_TEST_NAME, DownCastMessageValidType) { @@ -829,8 +830,8 @@ TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidPointerType) { ASSERT_DEBUG_DEATH( DownCastMessage(test_all_types_pointer), - "Cannot downcast " + test_all_types.GetTypeName() + " to " + - UNITTEST::TestRequired::default_instance().GetTypeName()); + absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ", + UNITTEST::TestRequired::default_instance().GetTypeName())); } TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) { @@ -840,8 +841,8 @@ TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) { ASSERT_DEBUG_DEATH( DownCastMessage(test_all_types_ref), - "Cannot downcast " + test_all_types.GetTypeName() + " to " + - UNITTEST::TestRequired::default_instance().GetTypeName()); + absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ", + UNITTEST::TestRequired::default_instance().GetTypeName())); } TEST(MESSAGE_TEST_NAME, MessageDebugStringMatchesBehindPointerAndLitePointer) {