diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 949c31ce06..46abb54024 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -2397,57 +2397,79 @@ void MessageGenerator::GenerateInitDefaultSplitInstance(io::Printer* p) { void MessageGenerator::GenerateSharedDestructorCode(io::Printer* p) { if (HasSimpleBaseClass(descriptor_, options_)) return; - Formatter format(p); - - format("inline void $classname$::SharedDtor() {\n"); - format.Indent(); - format("$DCHK$(GetArenaForAllocation() == nullptr);\n"); - - if (descriptor_->extension_range_count() > 0) { - format("$extensions$.~ExtensionSet();\n"); - } - - // Write the destructors for each field except oneof members. - // optimized_order_ does not contain oneof fields. - for (auto field : optimized_order_) { - if (ShouldSplit(field, options_)) { - continue; + auto emit_fields_dtors = [&](bool split_fields) { + // Write the destructors for each field except oneof members. + // optimized_order_ does not contain oneof fields. + for (const auto* field : optimized_order_) { + if (ShouldSplit(field, options_) != split_fields) continue; + field_generators_.get(field).GenerateDestructorCode(p); } - field_generators_.get(field).GenerateDestructorCode(p); - } - if (ShouldSplit(descriptor_, options_)) { - format("if (!IsSplitMessageDefault()) {\n"); - format.Indent(); - format("auto* $cached_split_ptr$ = $split$;\n"); - for (auto field : optimized_order_) { - if (ShouldSplit(field, options_)) { - field_generators_.get(field).GenerateDestructorCode(p); - } - } - format("delete $cached_split_ptr$;\n"); - format.Outdent(); - format("}\n"); - } - - // Generate code to destruct oneofs. Clearing should do the work. - for (auto oneof : OneOfRange(descriptor_)) { - format( - "if (has_$1$()) {\n" - " clear_$1$();\n" - "}\n", - oneof->name()); - } - - if (num_weak_fields_) { - format("$weak_field_map$.ClearAll();\n"); - } - - if (IsAnyMessage(descriptor_, options_)) { - format("$any_metadata$.~AnyMetadata();\n"); - } - - format.Outdent(); - format("}\n\n"); + }; + p->Emit( + { + {"extensions_dtor", + [&] { + if (descriptor_->extension_range_count() == 0) return; + p->Emit(R"cc( + $extensions$.~ExtensionSet(); + )cc"); + }}, + {"field_dtors", + [&] { emit_fields_dtors(/* split_fields= */ false); }}, + {"split_field_dtors", + [&] { + if (!ShouldSplit(descriptor_, options_)) return; + p->Emit( + { + {"split_field_dtors_impl", + [&] { emit_fields_dtors(/* split_fields= */ true); }}, + }, + R"cc( + if (!IsSplitMessageDefault()) { + auto* $cached_split_ptr$ = $split$; + $split_field_dtors_impl$; + delete $cached_split_ptr$; + } + )cc"); + }}, + {"oneof_field_dtors", + [&] { + for (const auto* oneof : OneOfRange(descriptor_)) { + p->Emit({{"name", oneof->name()}}, + R"cc( + if (has_$name$()) { + clear_$name$(); + } + )cc"); + } + }}, + {"weak_fields_dtor", + [&] { + if (num_weak_fields_ == 0) return; + // Generate code to destruct oneofs. Clearing should do the work. + p->Emit(R"cc( + $weak_field_map$.ClearAll(); + )cc"); + }}, + {"any_metadata_dtor", + [&] { + if (!IsAnyMessage(descriptor_, options_)) return; + p->Emit(R"cc( + $any_metadata$.~AnyMetadata(); + )cc"); + }}, + }, + R"cc( + inline void $classname$::SharedDtor() { + $DCHK$(GetArenaForAllocation() == nullptr); + $extensions_dtor$; + $field_dtors$; + $split_field_dtors$; + $oneof_field_dtors$; + $weak_fields_dtor$; + $any_metadata_dtor$; + } + )cc"); } ArenaDtorNeeds MessageGenerator::NeedsArenaDestructor() const { diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index b08dd41995..915d9a9067 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -368,7 +368,6 @@ inline void Version::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.suffix_.Destroy(); } - void Version::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -678,7 +677,6 @@ inline void CodeGeneratorRequest::SharedDtor() { _impl_.parameter_.Destroy(); if (this != internal_default_instance()) delete _impl_.compiler_version_; } - void CodeGeneratorRequest::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -1027,7 +1025,6 @@ inline void CodeGeneratorResponse_File::SharedDtor() { _impl_.content_.Destroy(); if (this != internal_default_instance()) delete _impl_.generated_code_info_; } - void CodeGeneratorResponse_File::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -1335,7 +1332,6 @@ inline void CodeGeneratorResponse::SharedDtor() { _impl_.file_.~RepeatedPtrField(); _impl_.error_.Destroy(); } - void CodeGeneratorResponse::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc index 4454c4741f..847afd8be6 100644 --- a/src/google/protobuf/descriptor.pb.cc +++ b/src/google/protobuf/descriptor.pb.cc @@ -2020,7 +2020,6 @@ inline void FileDescriptorSet::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.file_.~RepeatedPtrField(); } - void FileDescriptorSet::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -2310,7 +2309,6 @@ inline void FileDescriptorProto::SharedDtor() { if (this != internal_default_instance()) delete _impl_.options_; if (this != internal_default_instance()) delete _impl_.source_code_info_; } - void FileDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -2874,7 +2872,6 @@ inline void DescriptorProto_ExtensionRange::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); if (this != internal_default_instance()) delete _impl_.options_; } - void DescriptorProto_ExtensionRange::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -3123,7 +3120,6 @@ DescriptorProto_ReservedRange::~DescriptorProto_ReservedRange() { inline void DescriptorProto_ReservedRange::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); } - void DescriptorProto_ReservedRange::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -3392,7 +3388,6 @@ inline void DescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void DescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -3894,7 +3889,6 @@ inline void ExtensionRangeOptions_Declaration::SharedDtor() { _impl_.full_name_.Destroy(); _impl_.type_.Destroy(); } - void ExtensionRangeOptions_Declaration::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -4233,7 +4227,6 @@ inline void ExtensionRangeOptions::SharedDtor() { _impl_.declaration_.~RepeatedPtrField(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void ExtensionRangeOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -4600,7 +4593,6 @@ inline void FieldDescriptorProto::SharedDtor() { _impl_.json_name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void FieldDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -5107,7 +5099,6 @@ inline void OneofDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void OneofDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -5335,7 +5326,6 @@ EnumDescriptorProto_EnumReservedRange::~EnumDescriptorProto_EnumReservedRange() inline void EnumDescriptorProto_EnumReservedRange::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); } - void EnumDescriptorProto_EnumReservedRange::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -5589,7 +5579,6 @@ inline void EnumDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void EnumDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -5932,7 +5921,6 @@ inline void EnumValueDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void EnumValueDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -6221,7 +6209,6 @@ inline void ServiceDescriptorProto::SharedDtor() { _impl_.name_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void ServiceDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -6552,7 +6539,6 @@ inline void MethodDescriptorProto::SharedDtor() { _impl_.output_type_.Destroy(); if (this != internal_default_instance()) delete _impl_.options_; } - void MethodDescriptorProto::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -7120,7 +7106,6 @@ inline void FileOptions::SharedDtor() { _impl_.php_metadata_namespace_.Destroy(); _impl_.ruby_package_.Destroy(); } - void FileOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -7900,7 +7885,6 @@ inline void MessageOptions::SharedDtor() { _impl_._extensions_.~ExtensionSet(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void MessageOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -8279,7 +8263,6 @@ inline void FieldOptions::SharedDtor() { _internal_mutable_targets()->~RepeatedField(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void FieldOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -8752,7 +8735,6 @@ inline void OneofOptions::SharedDtor() { _impl_._extensions_.~ExtensionSet(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void OneofOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -8968,7 +8950,6 @@ inline void EnumOptions::SharedDtor() { _impl_._extensions_.~ExtensionSet(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void EnumOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -9258,7 +9239,6 @@ inline void EnumValueOptions::SharedDtor() { _impl_._extensions_.~ExtensionSet(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void EnumValueOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -9491,7 +9471,6 @@ inline void ServiceOptions::SharedDtor() { _impl_._extensions_.~ExtensionSet(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void ServiceOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -9732,7 +9711,6 @@ inline void MethodOptions::SharedDtor() { _impl_._extensions_.~ExtensionSet(); _impl_.uninterpreted_option_.~RepeatedPtrField(); } - void MethodOptions::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -10016,7 +9994,6 @@ inline void UninterpretedOption_NamePart::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.name_part_.Destroy(); } - void UninterpretedOption_NamePart::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -10319,7 +10296,6 @@ inline void UninterpretedOption::SharedDtor() { _impl_.string_value_.Destroy(); _impl_.aggregate_value_.Destroy(); } - void UninterpretedOption::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -10719,7 +10695,6 @@ inline void SourceCodeInfo_Location::SharedDtor() { _impl_.leading_comments_.Destroy(); _impl_.trailing_comments_.Destroy(); } - void SourceCodeInfo_Location::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -11033,7 +11008,6 @@ inline void SourceCodeInfo::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.location_.~RepeatedPtrField(); } - void SourceCodeInfo::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -11246,7 +11220,6 @@ inline void GeneratedCodeInfo_Annotation::SharedDtor() { _impl_.path_.~RepeatedField(); _impl_.source_file_.Destroy(); } - void GeneratedCodeInfo_Annotation::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); } @@ -11549,7 +11522,6 @@ inline void GeneratedCodeInfo::SharedDtor() { ABSL_DCHECK(GetArenaForAllocation() == nullptr); _impl_.annotation_.~RepeatedPtrField(); } - void GeneratedCodeInfo::SetCachedSize(int size) const { _impl_._cached_size_.Set(size); }