diff --git a/python/google/protobuf/pyext/descriptor_database.cc b/python/google/protobuf/pyext/descriptor_database.cc index 2a608e5339..fed13b865b 100644 --- a/python/google/protobuf/pyext/descriptor_database.cc +++ b/python/google/protobuf/pyext/descriptor_database.cc @@ -17,6 +17,7 @@ #include "google/protobuf/descriptor.pb.h" #include "absl/log/absl_log.h" #include "google/protobuf/message.h" +#include "google/protobuf/message_lite.h" #include "google/protobuf/pyext/message.h" #include "google/protobuf/pyext/scoped_pyobject_ptr.h" @@ -55,7 +56,7 @@ static bool GetFileDescriptorProto(PyObject* py_descriptor, message->message->GetDescriptor() == filedescriptor_descriptor) { // Fast path: Just use the pointer. FileDescriptorProto* file_proto = - google::protobuf::DownCastToGenerated(message->message); + google::protobuf::DownCastMessage(message->message); *output = *file_proto; return true; } else { diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index c8ccfe24a1..a3cd7b7654 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -28,7 +28,7 @@ using type_info = ::type_info; #endif #include "absl/base/attributes.h" -#include "google/protobuf/stubs/common.h" +#include "absl/base/macros.h" #include "absl/log/absl_check.h" #include "absl/utility/internal/if_constexpr.h" #include "google/protobuf/arena_align.h" diff --git a/src/google/protobuf/arena_align.h b/src/google/protobuf/arena_align.h index b9c9447d7c..991392438e 100644 --- a/src/google/protobuf/arena_align.h +++ b/src/google/protobuf/arena_align.h @@ -55,7 +55,7 @@ #include #include -#include "google/protobuf/stubs/common.h" +#include "absl/base/macros.h" #include "absl/log/absl_check.h" #include "absl/numeric/bits.h" diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc index 3cc55c93b0..7fa06d0cd0 100644 --- a/src/google/protobuf/arena_unittest.cc +++ b/src/google/protobuf/arena_unittest.cc @@ -879,7 +879,7 @@ TEST(ArenaTest, ReleaseFromArenaMessageUsingReflectionMakesCopy) { const Reflection* r = arena_message->GetReflection(); const FieldDescriptor* f = arena_message->GetDescriptor()->FindFieldByName( "optional_nested_message"); - nested_msg = DownCastToGenerated( + nested_msg = DownCastMessage( r->ReleaseMessage(arena_message, f)); } EXPECT_EQ(42, nested_msg->bb()); @@ -1491,7 +1491,7 @@ TEST(ArenaTest, MutableMessageReflection) { const Descriptor* d = message->GetDescriptor(); const FieldDescriptor* field = d->FindFieldByName("optional_nested_message"); TestAllTypes::NestedMessage* submessage = - DownCastToGenerated( + DownCastMessage( r->MutableMessage(message, field)); TestAllTypes::NestedMessage* submessage_expected = message->mutable_optional_nested_message(); @@ -1501,7 +1501,7 @@ TEST(ArenaTest, MutableMessageReflection) { const FieldDescriptor* oneof_field = d->FindFieldByName("oneof_nested_message"); - submessage = DownCastToGenerated( + submessage = DownCastMessage( r->MutableMessage(message, oneof_field)); submessage_expected = message->mutable_oneof_nested_message(); diff --git a/src/google/protobuf/compiler/cpp/service.cc b/src/google/protobuf/compiler/cpp/service.cc index 9da41c198a..23219e8304 100644 --- a/src/google/protobuf/compiler/cpp/service.cc +++ b/src/google/protobuf/compiler/cpp/service.cc @@ -255,9 +255,8 @@ void ServiceGenerator::GenerateCallMethodCases(io::Printer* printer) { }, R"cc( case $index$: - $name$(controller, - ::$proto_ns$::DownCastToGenerated<$input$>(request), - ::$proto_ns$::DownCastToGenerated<$output$>(response), done); + $name$(controller, ::$proto_ns$::DownCastMessage<$input$>(request), + ::$proto_ns$::DownCastMessage<$output$>(response), done); break; )cc"); } diff --git a/src/google/protobuf/compiler/cpp/unittest.inc b/src/google/protobuf/compiler/cpp/unittest.inc index 6c1fe420e0..b08cc950c2 100644 --- a/src/google/protobuf/compiler/cpp/unittest.inc +++ b/src/google/protobuf/compiler/cpp/unittest.inc @@ -1310,17 +1310,17 @@ TEST_F(GENERATED_SERVICE_TEST_NAME, CallMethod) { TEST_F(GENERATED_SERVICE_TEST_NAME, CallMethodTypeFailure) { // Verify death if we call Foo() with Bar's message types. -#if PROTOBUF_RTTI && GTEST_HAS_DEATH_TEST // death tests do not work on Windows yet +#if GTEST_HAS_DEATH_TEST // death tests do not work on Windows yet EXPECT_DEBUG_DEATH( - mock_service_.CallMethod(foo_, &mock_controller_, - &foo_request_, &bar_response_, done_.get()), - "DynamicCastToGenerated"); + mock_service_.CallMethod(foo_, &mock_controller_, &foo_request_, + &bar_response_, done_.get()), + "DynamicCastMessage"); mock_service_.Reset(); EXPECT_DEBUG_DEATH( - mock_service_.CallMethod(foo_, &mock_controller_, - &bar_request_, &foo_response_, done_.get()), - "DynamicCastToGenerated"); + mock_service_.CallMethod(foo_, &mock_controller_, &bar_request_, + &foo_response_, done_.get()), + "DynamicCastMessage"); #endif // GTEST_HAS_DEATH_TEST } diff --git a/src/google/protobuf/compiler/objectivec/BUILD.bazel b/src/google/protobuf/compiler/objectivec/BUILD.bazel index 3152976102..dbc714c84b 100644 --- a/src/google/protobuf/compiler/objectivec/BUILD.bazel +++ b/src/google/protobuf/compiler/objectivec/BUILD.bazel @@ -125,6 +125,7 @@ cc_test( ":line_consumer", "//src/google/protobuf/io", "//src/google/protobuf/stubs", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/strings", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", diff --git a/src/google/protobuf/compiler/objectivec/line_consumer_unittest.cc b/src/google/protobuf/compiler/objectivec/line_consumer_unittest.cc index da959d305c..fb25b1660b 100644 --- a/src/google/protobuf/compiler/objectivec/line_consumer_unittest.cc +++ b/src/google/protobuf/compiler/objectivec/line_consumer_unittest.cc @@ -13,7 +13,7 @@ #include #include -#include "google/protobuf/stubs/common.h" +#include "absl/base/macros.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "google/protobuf/io/zero_copy_stream_impl_lite.h" diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc index 1f5ce3d0aa..bc17e7a70a 100644 --- a/src/google/protobuf/compiler/parser.cc +++ b/src/google/protobuf/compiler/parser.cc @@ -1565,9 +1565,8 @@ bool Parser::ParseOption(Message* options, } UninterpretedOption* uninterpreted_option = - DownCastToGenerated( - options->GetReflection()->AddMessage(options, - uninterpreted_option_field)); + DownCastMessage(options->GetReflection()->AddMessage( + options, uninterpreted_option_field)); // Parse dot-separated name. { diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index eb45d425ed..d6d5ee75d9 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -8584,7 +8584,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretOptionsImpl( *original_options, original_uninterpreted_options_field); for (int i = 0; i < num_uninterpreted_options; ++i) { src_path.push_back(i); - uninterpreted_option_ = DownCastToGenerated( + uninterpreted_option_ = DownCastMessage( &original_options->GetReflection()->GetRepeatedMessage( *original_options, original_uninterpreted_options_field, i)); if (!InterpretSingleOption(options, src_path, diff --git a/src/google/protobuf/extension_set_heavy.cc b/src/google/protobuf/extension_set_heavy.cc index 60ad7b9e83..ea99df6b5b 100644 --- a/src/google/protobuf/extension_set_heavy.cc +++ b/src/google/protobuf/extension_set_heavy.cc @@ -406,7 +406,8 @@ size_t ExtensionSet::Extension::SpaceUsedExcludingSelfLong() const { if (is_lazy) { total_size += lazymessage_value->SpaceUsedLong(); } else { - total_size += DownCastToMessage(message_value)->SpaceUsedLong(); + total_size += + DownCastMessage(message_value)->SpaceUsedLong(); } break; default: diff --git a/src/google/protobuf/extension_set_unittest.cc b/src/google/protobuf/extension_set_unittest.cc index f983f3ca49..5a2b65b88b 100644 --- a/src/google/protobuf/extension_set_unittest.cc +++ b/src/google/protobuf/extension_set_unittest.cc @@ -1323,11 +1323,7 @@ TEST(ExtensionSetTest, DynamicExtensions) { const Message& sub_message = message.GetReflection()->GetMessage(message, message_extension); const unittest::ForeignMessage* typed_sub_message = -#if PROTOBUF_RTTI - dynamic_cast(&sub_message); -#else - static_cast(&sub_message); -#endif + google::protobuf::DynamicCastMessage(&sub_message); ASSERT_TRUE(typed_sub_message != nullptr); EXPECT_EQ(456, typed_sub_message->c()); } diff --git a/src/google/protobuf/generated_message_tctable_full.cc b/src/google/protobuf/generated_message_tctable_full.cc index 430d375da0..acfe0af4e2 100644 --- a/src/google/protobuf/generated_message_tctable_full.cc +++ b/src/google/protobuf/generated_message_tctable_full.cc @@ -63,7 +63,7 @@ const char* TcParser::ReflectionFallback(PROTOBUF_TC_PARAM_DECL) { return ptr; } - auto* full_msg = DownCastToMessage(msg); + auto* full_msg = DownCastMessage(msg); auto* descriptor = full_msg->GetDescriptor(); auto* reflection = full_msg->GetReflection(); int field_number = WireFormatLite::GetTagFieldNumber(tag); @@ -87,7 +87,7 @@ const char* TcParser::ReflectionParseLoop(PROTOBUF_TC_PARAM_DECL) { (void)table; (void)hasbits; // Call into the wire format reflective parse loop. - return WireFormat::_InternalParse(DownCastToMessage(msg), ptr, ctx); + return WireFormat::_InternalParse(DownCastMessage(msg), ptr, ctx); } const char* TcParser::MessageSetWireFormatParseLoop( diff --git a/src/google/protobuf/io/zero_copy_stream_impl_lite.h b/src/google/protobuf/io/zero_copy_stream_impl_lite.h index 288a5181c9..df985bf56b 100644 --- a/src/google/protobuf/io/zero_copy_stream_impl_lite.h +++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.h @@ -29,6 +29,7 @@ #include "google/protobuf/stubs/callback.h" #include "google/protobuf/stubs/common.h" #include "absl/base/attributes.h" +#include "absl/base/macros.h" #include "absl/strings/cord.h" #include "absl/strings/cord_buffer.h" #include "google/protobuf/io/zero_copy_stream.h" diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index f9f0135501..45039fa97e 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -1330,83 +1330,80 @@ TEST(LiteBasicTest, CodedInputStreamRollback) { using CastType1 = protobuf_unittest::TestAllTypesLite; using CastType2 = protobuf_unittest::TestPackedTypesLite; -TEST(LiteTest, DynamicCastToGenerated) { +TEST(LiteTest, DynamicCastMessage) { CastType1 test_type_1; MessageLite* test_type_1_pointer = &test_type_1; - EXPECT_EQ(&test_type_1, - DynamicCastToGenerated(test_type_1_pointer)); - EXPECT_EQ(nullptr, DynamicCastToGenerated(test_type_1_pointer)); + EXPECT_EQ(&test_type_1, DynamicCastMessage(test_type_1_pointer)); + EXPECT_EQ(nullptr, DynamicCastMessage(test_type_1_pointer)); const MessageLite* test_type_1_pointer_const = &test_type_1; EXPECT_EQ(&test_type_1, - DynamicCastToGenerated(test_type_1_pointer_const)); + DynamicCastMessage(test_type_1_pointer_const)); EXPECT_EQ(nullptr, - DynamicCastToGenerated(test_type_1_pointer_const)); + DynamicCastMessage(test_type_1_pointer_const)); MessageLite* test_type_1_pointer_nullptr = nullptr; EXPECT_EQ(nullptr, - DynamicCastToGenerated(test_type_1_pointer_nullptr)); + DynamicCastMessage(test_type_1_pointer_nullptr)); MessageLite& test_type_1_pointer_ref = test_type_1; EXPECT_EQ(&test_type_1, - &DynamicCastToGenerated(test_type_1_pointer_ref)); + &DynamicCastMessage(test_type_1_pointer_ref)); const MessageLite& test_type_1_pointer_const_ref = test_type_1; EXPECT_EQ(&test_type_1, - &DynamicCastToGenerated(test_type_1_pointer_const_ref)); + &DynamicCastMessage(test_type_1_pointer_const_ref)); } #if GTEST_HAS_DEATH_TEST -TEST(LiteTest, DynamicCastToGeneratedInvalidReferenceType) { +TEST(LiteTest, DynamicCastMessageInvalidReferenceType) { CastType1 test_type_1; const MessageLite& test_type_1_pointer_const_ref = test_type_1; - ASSERT_DEATH(DynamicCastToGenerated(test_type_1_pointer_const_ref), + ASSERT_DEATH(DynamicCastMessage(test_type_1_pointer_const_ref), "Cannot downcast " + test_type_1.GetTypeName() + " to " + CastType2::default_instance().GetTypeName()); } #endif // GTEST_HAS_DEATH_TEST -TEST(LiteTest, DownCastToGeneratedValidType) { +TEST(LiteTest, DownCastMessageValidType) { CastType1 test_type_1; MessageLite* test_type_1_pointer = &test_type_1; - EXPECT_EQ(&test_type_1, DownCastToGenerated(test_type_1_pointer)); + EXPECT_EQ(&test_type_1, DownCastMessage(test_type_1_pointer)); const MessageLite* test_type_1_pointer_const = &test_type_1; EXPECT_EQ(&test_type_1, - DownCastToGenerated(test_type_1_pointer_const)); + DownCastMessage(test_type_1_pointer_const)); MessageLite* test_type_1_pointer_nullptr = nullptr; - EXPECT_EQ(nullptr, - DownCastToGenerated(test_type_1_pointer_nullptr)); + EXPECT_EQ(nullptr, DownCastMessage(test_type_1_pointer_nullptr)); MessageLite& test_type_1_pointer_ref = test_type_1; - EXPECT_EQ(&test_type_1, - &DownCastToGenerated(test_type_1_pointer_ref)); + EXPECT_EQ(&test_type_1, &DownCastMessage(test_type_1_pointer_ref)); const MessageLite& test_type_1_pointer_const_ref = test_type_1; EXPECT_EQ(&test_type_1, - &DownCastToGenerated(test_type_1_pointer_const_ref)); + &DownCastMessage(test_type_1_pointer_const_ref)); } #if GTEST_HAS_DEATH_TEST -TEST(LiteTest, DownCastToGeneratedInvalidPointerType) { +TEST(LiteTest, DownCastMessageInvalidPointerType) { CastType1 test_type_1; MessageLite* test_type_1_pointer = &test_type_1; - ASSERT_DEBUG_DEATH(DownCastToGenerated(test_type_1_pointer), + ASSERT_DEBUG_DEATH(DownCastMessage(test_type_1_pointer), "Cannot downcast " + test_type_1.GetTypeName() + " to " + CastType2::default_instance().GetTypeName()); } -TEST(LiteTest, DownCastToGeneratedInvalidReferenceType) { +TEST(LiteTest, DownCastMessageInvalidReferenceType) { CastType1 test_type_1; MessageLite& test_type_1_pointer = test_type_1; - ASSERT_DEBUG_DEATH(DownCastToGenerated(test_type_1_pointer), + ASSERT_DEBUG_DEATH(DownCastMessage(test_type_1_pointer), "Cannot downcast " + test_type_1.GetTypeName() + " to " + CastType2::default_instance().GetTypeName()); } diff --git a/src/google/protobuf/map_test.inc b/src/google/protobuf/map_test.inc index c67cd586c3..189a9bcc7d 100644 --- a/src/google/protobuf/map_test.inc +++ b/src/google/protobuf/map_test.inc @@ -1578,7 +1578,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - DownCastToGenerated( + DownCastMessage( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1615,7 +1615,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - DownCastToGenerated( + DownCastMessage( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1652,7 +1652,7 @@ TEST_F(MapFieldReflectionTest, RegularFields) { int32_t key_int32_message = message_int32_message->GetReflection()->GetInt32( *message_int32_message, fd_map_int32_foreign_message_key); - ForeignMessage* value_int32_message = DownCastToGenerated( + ForeignMessage* value_int32_message = DownCastMessage( message_int32_message->GetReflection()->MutableMessage( message_int32_message, fd_map_int32_foreign_message_value)); value_int32_message->set_c(Func(key_int32_message, -6)); @@ -1808,7 +1808,7 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - DownCastToGenerated( + DownCastMessage( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1849,7 +1849,7 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { message_int32_message.GetReflection()->GetInt32( message_int32_message, fd_map_int32_foreign_message_key); const ForeignMessage& value_int32_message = - DownCastToGenerated( + DownCastMessage( message_int32_message.GetReflection()->GetMessage( message_int32_message, fd_map_int32_foreign_message_value)); EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); @@ -1966,8 +1966,8 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message = *it; int32_t key = message.GetReflection()->GetInt32( message, fd_map_int32_foreign_message_key); - const ForeignMessage& sub_message = DownCastToGenerated( - message.GetReflection()->GetMessage( + const ForeignMessage& sub_message = + DownCastMessage(message.GetReflection()->GetMessage( message, fd_map_int32_foreign_message_value)); result[key].MergeFrom(sub_message); ++index; @@ -2120,14 +2120,14 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { { const Message& message0a = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message0a = DownCastToGenerated( - message0a.GetReflection()->GetMessage( + const ForeignMessage& sub_message0a = + DownCastMessage(message0a.GetReflection()->GetMessage( message0a, fd_map_int32_foreign_message_value)); int32_t int32_value0a = sub_message0a.c(); const Message& message9a = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message9a = DownCastToGenerated( - message9a.GetReflection()->GetMessage( + const ForeignMessage& sub_message9a = + DownCastMessage(message9a.GetReflection()->GetMessage( message9a, fd_map_int32_foreign_message_value)); int32_t int32_value9a = sub_message9a.c(); @@ -2135,14 +2135,14 @@ TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { const Message& message0b = mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message0b = DownCastToGenerated( - message0b.GetReflection()->GetMessage( + const ForeignMessage& sub_message0b = + DownCastMessage(message0b.GetReflection()->GetMessage( message0b, fd_map_int32_foreign_message_value)); int32_t int32_value0b = sub_message0b.c(); const Message& message9b = mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); - const ForeignMessage& sub_message9b = DownCastToGenerated( - message9b.GetReflection()->GetMessage( + const ForeignMessage& sub_message9b = + DownCastMessage(message9b.GetReflection()->GetMessage( message9b, fd_map_int32_foreign_message_value)); int32_t int32_value9b = sub_message9b.c(); diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index bb56ac0621..e9bd77a912 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -67,7 +67,8 @@ using internal::ReflectionOps; using internal::WireFormat; void Message::MergeImpl(MessageLite& to, const MessageLite& from) { - ReflectionOps::Merge(DownCastToMessage(from), DownCastToMessage(&to)); + ReflectionOps::Merge(DownCastMessage(from), + DownCastMessage(&to)); } void Message::MergeFrom(const Message& from) { @@ -108,7 +109,7 @@ void Message::CopyFrom(const Message& from) { void Message::Clear() { ReflectionOps::Clear(this); } bool Message::IsInitializedImpl(const MessageLite& msg) { - return ReflectionOps::IsInitialized(DownCastToMessage(msg)); + return ReflectionOps::IsInitialized(DownCastMessage(msg)); } void Message::FindInitializationErrors(std::vector* errors) const { @@ -184,20 +185,20 @@ size_t Message::SpaceUsedLong() const { } static std::string GetTypeNameImpl(const MessageLite& msg) { - return DownCastToMessage(msg).GetDescriptor()->full_name(); + return DownCastMessage(msg).GetDescriptor()->full_name(); } static std::string InitializationErrorStringImpl(const MessageLite& msg) { - return DownCastToMessage(msg).InitializationErrorString(); + return DownCastMessage(msg).InitializationErrorString(); } const internal::TcParseTableBase* Message::GetTcParseTableImpl( const MessageLite& msg) { - return DownCastToMessage(msg).GetReflection()->GetTcParseTable(); + return DownCastMessage(msg).GetReflection()->GetTcParseTable(); } size_t Message::SpaceUsedLongImpl(const MessageLite& msg_lite) { - auto& msg = DownCastToMessage(msg_lite); + auto& msg = DownCastMessage(msg_lite); return msg.GetReflection()->SpaceUsedLong(msg); } diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h index d2a3be33a7..87a2c718c8 100644 --- a/src/google/protobuf/message.h +++ b/src/google/protobuf/message.h @@ -96,7 +96,7 @@ #include "absl/base/attributes.h" #include "absl/base/call_once.h" -#include "google/protobuf/stubs/common.h" +#include "absl/base/macros.h" #include "absl/log/absl_check.h" #include "absl/memory/memory.h" #include "absl/strings/cord.h" @@ -1448,45 +1448,19 @@ void LinkMessageReflection() { internal::StrongReferenceToType(); } -// Tries to downcast this message from MessageLite to Message. Returns nullptr -// if this class is not an instance of Message. eg if the message was defined -// with optimized_for=LITE_RUNTIME. This works even if RTTI is disabled. -inline const Message* DynamicCastToMessage(const MessageLite* lite) { - return lite == nullptr || internal::GetClassData(*lite)->is_lite +// Specializations to handle cast to `Message`. We can check the `is_lite` bit +// in the class data. +template <> +inline const Message* DynamicCastMessage(const MessageLite* from) { + return from == nullptr || internal::GetClassData(*from)->is_lite ? nullptr - : static_cast(lite); -} -inline Message* DynamicCastToMessage(MessageLite* lite) { - return const_cast( - DynamicCastToMessage(static_cast(lite))); -} -inline const Message& DynamicCastToMessage(const MessageLite& lite) { - auto* res = DynamicCastToMessage(&lite); - ABSL_CHECK(res != nullptr) - << "Cannot to `Message` type " << lite.GetTypeName(); - return *res; + : static_cast(from); } -inline Message& DynamicCastToMessage(MessageLite& lite) { - return const_cast( - DynamicCastToMessage(static_cast(lite))); -} - -// A lightweight function for downcasting a MessageLite to Message. It should -// only be used when the caller is certain that the argument is a Message -// object. -inline const Message* DownCastToMessage(const MessageLite* lite) { - ABSL_CHECK(lite == nullptr || DynamicCastToMessage(lite) != nullptr); - return static_cast(lite); -} -inline Message* DownCastToMessage(MessageLite* lite) { - return const_cast( - DownCastToMessage(static_cast(lite))); -} -inline const Message& DownCastToMessage(const MessageLite& lite) { - return *DownCastToMessage(&lite); -} -inline Message& DownCastToMessage(MessageLite& lite) { - return *DownCastToMessage(&lite); +template <> +inline const Message* DownCastMessage(const MessageLite* from) { + ABSL_DCHECK(DynamicCastMessage(from) == from) + << "Cannot downcast " << from->GetTypeName() << " to Message"; + return static_cast(from); } // ============================================================================= diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index d47922cbc3..19b1800200 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -727,7 +727,7 @@ class PROTOBUF_EXPORT MessageLite { namespace internal { // A typeinfo equivalent for protobuf message types. Used for -// DynamicCastToGenerated. +// DynamicCastMessage. // We might make this class public later on to have an alternative to // `std::type_info` that works when RTTI is disabled. class TypeId { @@ -862,18 +862,26 @@ inline void AssertDownCast(const MessageLite& from, const MessageLite& to) { std::string ShortFormat(const MessageLite& message_lite); std::string Utf8Format(const MessageLite& message_lite); -// Tries to downcast this message to a generated message type. Returns nullptr -// if this class is not an instance of T. This works even if RTTI is disabled. +// Cast functions for message pointer/references. +// This is the supported API to cast from a Message/MessageLite to derived +// types. These work even when RTTI is disabled on message types. // -// This also has the effect of creating a strong reference to T that will -// prevent the linker from stripping it out at link time. This can be important -// if you are using a DynamicMessageFactory that delegates to the generated -// factory. +// The template parameter is simplified and the return type is inferred from the +// input. Eg just `DynamicCastMessage(x)` instead of +// `DynamicCastMessage(x)`. +// +// `DynamicCastMessage` is similar to `dynamic_cast`, returns `nullptr` when the +// input is not an instance of `T`. The overloads that take a reference will +// terminate on mismatch. +// +// `DownCastMessage` is a lightweight function for downcasting base +// `MessageLite` pointer to derived type, where it only does type checking if +// !NDEBUG. It should only be used when the caller is certain that the input +// message is of instance `T`. template -const T* DynamicCastToGenerated(const MessageLite* from) { +const T* DynamicCastMessage(const MessageLite* from) { static_assert(std::is_base_of::value, ""); - internal::StrongReferenceToType(); // We might avoid the call to T::GetClassData() altogether if T were to // expose the class data pointer. if (from == nullptr || @@ -885,20 +893,14 @@ const T* DynamicCastToGenerated(const MessageLite* from) { } template -const T* DynamicCastToGenerated(const MessageLite* from); - -template -T* DynamicCastToGenerated(MessageLite* from) { +T* DynamicCastMessage(MessageLite* from) { return const_cast( - DynamicCastToGenerated(static_cast(from))); + DynamicCastMessage(static_cast(from))); } -// An overloaded version of DynamicCastToGenerated for downcasting references to -// base Message class. If the argument is not an instance of T, it terminates -// with an error. template -const T& DynamicCastToGenerated(const MessageLite& from) { - const T* destination_message = DynamicCastToGenerated(&from); +const T& DynamicCastMessage(const MessageLite& from) { + const T* destination_message = DynamicCastMessage(&from); ABSL_CHECK(destination_message != nullptr) << "Cannot downcast " << from.GetTypeName() << " to " << T::default_instance().GetTypeName(); @@ -906,37 +908,93 @@ const T& DynamicCastToGenerated(const MessageLite& from) { } template -T& DynamicCastToGenerated(MessageLite& from) { +T& DynamicCastMessage(MessageLite& from) { return const_cast( - DynamicCastToGenerated(static_cast(from))); + DynamicCastMessage(static_cast(from))); } -// A lightweight function for downcasting base MessageLite pointer to derived -// type. It should only be used when the caller is certain that the argument is -// of instance T and T is a generated message type. template -const T* DownCastToGenerated(const MessageLite* from) { +const T* DownCastMessage(const MessageLite* from) { internal::StrongReferenceToType(); - ABSL_DCHECK(DynamicCastToGenerated(from) == from) + ABSL_DCHECK(DynamicCastMessage(from) == from) << "Cannot downcast " << from->GetTypeName() << " to " << T::default_instance().GetTypeName(); return static_cast(from); } template -T* DownCastToGenerated(MessageLite* from) { +T* DownCastMessage(MessageLite* from) { return const_cast( - DownCastToGenerated(static_cast(from))); + DownCastMessage(static_cast(from))); +} + +template +const T& DownCastMessage(const MessageLite& from) { + return *DownCastMessage(&from); +} + +template +T& DownCastMessage(MessageLite& from) { + return *DownCastMessage(&from); +} + +template <> +inline const MessageLite* DynamicCastMessage(const MessageLite* from) { + return from; +} +template <> +inline const MessageLite* DownCastMessage(const MessageLite* from) { + return from; +} + +// Deprecated names for the cast functions. +// Prefer the ones above. +template +PROTOBUF_DEPRECATE_AND_INLINE() +const T* DynamicCastToGenerated(const MessageLite* from) { + return DynamicCastMessage(from); +} + +template +PROTOBUF_DEPRECATE_AND_INLINE() +T* DynamicCastToGenerated(MessageLite* from) { + return DynamicCastMessage(from); +} + +template +PROTOBUF_DEPRECATE_AND_INLINE() +const T& DynamicCastToGenerated(const MessageLite& from) { + return DynamicCastMessage(from); +} + +template +PROTOBUF_DEPRECATE_AND_INLINE() +T& DynamicCastToGenerated(MessageLite& from) { + return DynamicCastMessage(from); +} + +template +PROTOBUF_DEPRECATE_AND_INLINE() +const T* DownCastToGenerated(const MessageLite* from) { + return DownCastMessage(from); +} + +template +PROTOBUF_DEPRECATE_AND_INLINE() +T* DownCastToGenerated(MessageLite* from) { + return DownCastMessage(from); } template +PROTOBUF_DEPRECATE_AND_INLINE() const T& DownCastToGenerated(const MessageLite& from) { - return *DownCastToGenerated(&from); + return DownCastMessage(from); } template +PROTOBUF_DEPRECATE_AND_INLINE() T& DownCastToGenerated(MessageLite& from) { - return *DownCastToGenerated(&from); + return DownCastMessage(from); } } // namespace protobuf diff --git a/src/google/protobuf/message_unittest.inc b/src/google/protobuf/message_unittest.inc index 2f647fbf4a..37a1242980 100644 --- a/src/google/protobuf/message_unittest.inc +++ b/src/google/protobuf/message_unittest.inc @@ -750,86 +750,85 @@ TEST(MESSAGE_TEST_NAME, InitializationErrorString) { EXPECT_EQ("a, b, c", message.InitializationErrorString()); } -TEST(MESSAGE_TEST_NAME, DynamicCastToGenerated) { +TEST(MESSAGE_TEST_NAME, DynamicCastMessage) { UNITTEST::TestAllTypes test_all_types; MessageLite* test_all_types_pointer = &test_all_types; - EXPECT_EQ(&test_all_types, DynamicCastToGenerated( - test_all_types_pointer)); - EXPECT_EQ(nullptr, DynamicCastToGenerated( - test_all_types_pointer)); + EXPECT_EQ(&test_all_types, + DynamicCastMessage(test_all_types_pointer)); + EXPECT_EQ(nullptr, + DynamicCastMessage(test_all_types_pointer)); const MessageLite* test_all_types_pointer_const = &test_all_types; - EXPECT_EQ(&test_all_types, - DynamicCastToGenerated( - test_all_types_pointer_const)); - EXPECT_EQ(nullptr, DynamicCastToGenerated( + EXPECT_EQ(&test_all_types, DynamicCastMessage( + test_all_types_pointer_const)); + EXPECT_EQ(nullptr, DynamicCastMessage( test_all_types_pointer_const)); MessageLite* test_all_types_pointer_nullptr = nullptr; - EXPECT_EQ(nullptr, DynamicCastToGenerated( + EXPECT_EQ(nullptr, DynamicCastMessage( test_all_types_pointer_nullptr)); MessageLite& test_all_types_pointer_ref = test_all_types; - EXPECT_EQ(&test_all_types, &DynamicCastToGenerated( + EXPECT_EQ(&test_all_types, &DynamicCastMessage( test_all_types_pointer_ref)); const MessageLite& test_all_types_pointer_const_ref = test_all_types; - EXPECT_EQ(&test_all_types, &DynamicCastToGenerated( + EXPECT_EQ(&test_all_types, &DynamicCastMessage( test_all_types_pointer_const_ref)); } -TEST(MESSAGE_TEST_NAME, DynamicCastToGeneratedInvalidReferenceType) { +TEST(MESSAGE_TEST_NAME, DynamicCastMessageInvalidReferenceType) { UNITTEST::TestAllTypes test_all_types; const MessageLite& test_all_types_pointer_const_ref = test_all_types; - ASSERT_DEATH(DynamicCastToGenerated( + ASSERT_DEATH(DynamicCastMessage( test_all_types_pointer_const_ref), "Cannot downcast " + test_all_types.GetTypeName() + " to " + UNITTEST::TestRequired::default_instance().GetTypeName()); } -TEST(MESSAGE_TEST_NAME, DownCastToGeneratedValidType) { +TEST(MESSAGE_TEST_NAME, DownCastMessageValidType) { UNITTEST::TestAllTypes test_all_types; MessageLite* test_all_types_pointer = &test_all_types; - EXPECT_EQ(&test_all_types, DownCastToGenerated( - test_all_types_pointer)); + EXPECT_EQ(&test_all_types, + DownCastMessage(test_all_types_pointer)); const MessageLite* test_all_types_pointer_const = &test_all_types; - EXPECT_EQ(&test_all_types, DownCastToGenerated( + EXPECT_EQ(&test_all_types, DownCastMessage( test_all_types_pointer_const)); MessageLite* test_all_types_pointer_nullptr = nullptr; - EXPECT_EQ(nullptr, DownCastToGenerated( + EXPECT_EQ(nullptr, DownCastMessage( test_all_types_pointer_nullptr)); MessageLite& test_all_types_pointer_ref = test_all_types; - EXPECT_EQ(&test_all_types, &DownCastToGenerated( + EXPECT_EQ(&test_all_types, &DownCastMessage( test_all_types_pointer_ref)); const MessageLite& test_all_types_pointer_const_ref = test_all_types; - EXPECT_EQ(&test_all_types, &DownCastToGenerated( + EXPECT_EQ(&test_all_types, &DownCastMessage( test_all_types_pointer_const_ref)); } -TEST(MESSAGE_TEST_NAME, DownCastToGeneratedInvalidPointerType) { +TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidPointerType) { UNITTEST::TestAllTypes test_all_types; MessageLite* test_all_types_pointer = &test_all_types; ASSERT_DEBUG_DEATH( - DownCastToGenerated(test_all_types_pointer), + DownCastMessage(test_all_types_pointer), "Cannot downcast " + test_all_types.GetTypeName() + " to " + UNITTEST::TestRequired::default_instance().GetTypeName()); } -TEST(MESSAGE_TEST_NAME, DownCastToGeneratedInvalidReferenceType) { +TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) { UNITTEST::TestAllTypes test_all_types; MessageLite& test_all_types_pointer = test_all_types; ASSERT_DEBUG_DEATH( - DownCastToGenerated(test_all_types_pointer), + DownCastMessage(test_all_types_pointer), "Cannot downcast " + test_all_types.GetTypeName() + " to " + UNITTEST::TestRequired::default_instance().GetTypeName()); } diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 2be3e2073f..8b0c15fd3b 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -32,6 +32,7 @@ #define PROTOBUF_PORT_ #include "absl/base/attributes.h" #include "absl/base/config.h" +#include "absl/base/macros.h" // The definitions in this file are intended to be portable across Clang, // GCC, and MSVC. Function-like macros are usable without an #ifdef guard. @@ -959,3 +960,12 @@ namespace internal { #endif // !NDEBUG #endif // has_builtin(__builtin_assume) +#ifdef PROTOBUF_DEPRECATE_AND_INLINE +#error PROTOBUF_DEPRECATE_AND_INLINE was previously defined +#endif +#if defined(ABSL_DEPRECATE_AND_INLINE) +#define PROTOBUF_DEPRECATE_AND_INLINE() ABSL_DEPRECATE_AND_INLINE() +#else +#define PROTOBUF_DEPRECATE_AND_INLINE() [[deprecated]] +#endif + diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index 83f8a8c573..297dc9417e 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -48,6 +48,7 @@ #undef PROTOBUF_FORCE_ALLOCATION_ON_CONSTRUCTION #undef PROTOBUF_UNUSED #undef PROTOBUF_ASSUME +#undef PROTOBUF_DEPRECATE_AND_INLINE #undef PROTOBUF_EXPORT_TEMPLATE_DECLARE #undef PROTOBUF_EXPORT_TEMPLATE_DEFINE #undef PROTOBUF_ALIGNAS diff --git a/src/google/protobuf/reflection_visit_field_info.h b/src/google/protobuf/reflection_visit_field_info.h index 29056bc9da..f0c65db8a6 100644 --- a/src/google/protobuf/reflection_visit_field_info.h +++ b/src/google/protobuf/reflection_visit_field_info.h @@ -18,6 +18,7 @@ #include "google/protobuf/inlined_string_field.h" #include "google/protobuf/map_field.h" #include "google/protobuf/message.h" +#include "google/protobuf/message_lite.h" #include "google/protobuf/port.h" #include "google/protobuf/wire_format_lite.h" @@ -200,10 +201,10 @@ struct DynamicExtensionInfoHelper { } static const Message& GetMessage(const Extension& ext) { - return DownCastToMessage(*ext.message_value); + return DownCastMessage(*ext.message_value); } static Message& MutableMessage(Extension& ext) { - return DownCastToMessage(*ext.message_value); + return DownCastMessage(*ext.message_value); } static void ClearMessage(Extension& ext) { ext.is_cleared = true; @@ -212,18 +213,18 @@ struct DynamicExtensionInfoHelper { static const Message& GetLazyMessage(const Extension& ext, const Message& prototype, Arena* arena) { - return DownCastToMessage( + return DownCastMessage( ext.lazymessage_value->GetMessage(prototype, arena)); } static const Message& GetLazyMessageIgnoreUnparsed(const Extension& ext, const Message& prototype, Arena* arena) { - return DownCastToMessage( + return DownCastMessage( ext.lazymessage_value->GetMessageIgnoreUnparsed(prototype, arena)); } static Message& MutableLazyMessage(Extension& ext, const Message& prototype, Arena* arena) { - return DownCastToMessage( + return DownCastMessage( *ext.lazymessage_value->MutableMessage(prototype, arena)); } static void ClearLazyMessage(Extension& ext) { diff --git a/src/google/protobuf/reflection_visit_fields.h b/src/google/protobuf/reflection_visit_fields.h index e554a76ef9..3b53155b51 100644 --- a/src/google/protobuf/reflection_visit_fields.h +++ b/src/google/protobuf/reflection_visit_fields.h @@ -422,7 +422,7 @@ void ReflectionVisit::VisitMessageFields(const Message& message, FieldDescriptor::CPPTYPE_MESSAGE) { if constexpr (info.is_repeated) { for (const auto& it : info.Get()) { - func(DownCastToMessage(it)); + func(DownCastMessage(it)); } } else { func(info.Get()); @@ -452,7 +452,7 @@ void ReflectionVisit::VisitMessageFields(Message& message, CallbackFn&& func) { FieldDescriptor::CPPTYPE_MESSAGE) { if constexpr (info.is_repeated) { for (auto& it : info.Mutable()) { - func(DownCastToMessage(it)); + func(DownCastMessage(it)); } } else { func(info.Mutable()); diff --git a/src/google/protobuf/repeated_field_reflection_unittest.inc b/src/google/protobuf/repeated_field_reflection_unittest.inc index 8e561adbe1..da4136fff9 100644 --- a/src/google/protobuf/repeated_field_reflection_unittest.inc +++ b/src/google/protobuf/repeated_field_reflection_unittest.inc @@ -91,7 +91,7 @@ TEST(REFLECTION_TEST, RegularFields) { EXPECT_EQ(rf_double.Get(i), Func(i, 2)); EXPECT_EQ(rpf_string.Get(i), StrFunc(i, 5)); EXPECT_EQ(rpf_foreign_message.Get(i).c(), Func(i, 6)); - EXPECT_EQ(DownCastToGenerated(&rpf_message.Get(i))->c(), + EXPECT_EQ(DownCastMessage(&rpf_message.Get(i))->c(), Func(i, 6)); // Check gets through mutable objects. @@ -99,7 +99,7 @@ TEST(REFLECTION_TEST, RegularFields) { EXPECT_EQ(mrf_double->Get(i), Func(i, 2)); EXPECT_EQ(mrpf_string->Get(i), StrFunc(i, 5)); EXPECT_EQ(mrpf_foreign_message->Get(i).c(), Func(i, 6)); - EXPECT_EQ(DownCastToGenerated(&mrpf_message->Get(i))->c(), + EXPECT_EQ(DownCastMessage(&mrpf_message->Get(i))->c(), Func(i, 6)); // Check sets through mutable objects. @@ -111,7 +111,7 @@ TEST(REFLECTION_TEST, RegularFields) { EXPECT_EQ(message.repeated_double(i), Func(i, -2)); EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5)); EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6)); - DownCastToGenerated(mrpf_message->Mutable(i)) + DownCastMessage(mrpf_message->Mutable(i)) ->set_c(Func(i, 7)); EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7)); } @@ -272,8 +272,7 @@ TEST(REFLECTION_TEST, RepeatedFieldRefForRegularFields) { ForeignMessage scratch_space; EXPECT_EQ(rf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); EXPECT_EQ( - DownCastToGenerated(rf_message.Get(i, &scratch_space)) - .c(), + DownCastMessage(rf_message.Get(i, &scratch_space)).c(), Func(i, 6)); // Check gets through mutable objects. @@ -282,8 +281,7 @@ TEST(REFLECTION_TEST, RepeatedFieldRefForRegularFields) { EXPECT_EQ(mrf_string.Get(i), StrFunc(i, 5)); EXPECT_EQ(mrf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6)); EXPECT_EQ( - DownCastToGenerated(mrf_message.Get(i, &scratch_space)) - .c(), + DownCastMessage(mrf_message.Get(i, &scratch_space)).c(), Func(i, 6)); // Check sets through mutable objects. diff --git a/src/google/protobuf/repeated_ptr_field.cc b/src/google/protobuf/repeated_ptr_field.cc index 010b7aabd9..0547e4c01c 100644 --- a/src/google/protobuf/repeated_ptr_field.cc +++ b/src/google/protobuf/repeated_ptr_field.cc @@ -200,11 +200,8 @@ int RepeatedPtrFieldBase::MergeIntoClearedMessages( int count = std::min(ClearedCount(), from.current_size_); for (int i = 0; i < count; ++i) { ABSL_DCHECK(src[i] != nullptr); -#if PROTOBUF_RTTI - // TODO: remove or replace with a cleaner check. - ABSL_DCHECK(typeid(*src[i]) == typeid(*src[0])) - << typeid(*src[i]).name() << " vs " << typeid(*src[0]).name(); -#endif + ABSL_DCHECK(TypeId::Get(*src[i]) == TypeId::Get(*src[0])) + << src[i]->GetTypeName() << " vs " << src[0]->GetTypeName(); dst[i]->CheckTypeAndMergeFrom(*src[i]); } return count; @@ -251,11 +248,8 @@ void RepeatedPtrFieldBase::MergeFrom( Arena* arena = GetArena(); for (; src < end; ++src, ++dst) { ABSL_DCHECK(*src != nullptr); -#if PROTOBUF_RTTI - // TODO: remove or replace with a cleaner check. - ABSL_DCHECK(typeid(**src) == typeid(*prototype)) - << typeid(**src).name() << " vs " << typeid(*prototype).name(); -#endif + ABSL_DCHECK(TypeId::Get(**src) == TypeId::Get(*prototype)) + << (**src).GetTypeName() << " vs " << prototype->GetTypeName(); *dst = prototype->New(arena); (*dst)->CheckTypeAndMergeFrom(**src); } diff --git a/src/google/protobuf/util/BUILD.bazel b/src/google/protobuf/util/BUILD.bazel index b97287554e..fcc64040ca 100644 --- a/src/google/protobuf/util/BUILD.bazel +++ b/src/google/protobuf/util/BUILD.bazel @@ -55,6 +55,7 @@ cc_library( "//src/google/protobuf/io", "//src/google/protobuf/io:printer", "//src/google/protobuf/stubs", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:fixed_array", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", diff --git a/src/google/protobuf/util/message_differencer.h b/src/google/protobuf/util/message_differencer.h index 6d8e9a9b00..1cfd600e44 100644 --- a/src/google/protobuf/util/message_differencer.h +++ b/src/google/protobuf/util/message_differencer.h @@ -25,7 +25,7 @@ #include #include -#include "google/protobuf/stubs/common.h" +#include "absl/base/macros.h" #include "absl/container/fixed_array.h" #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h"