From 395e9529bb54c84e9bd5ea97da83b02277a1bd73 Mon Sep 17 00:00:00 2001 From: Matt Kulukundis Date: Mon, 14 Aug 2023 14:45:17 -0700 Subject: [PATCH] Incremental migration of message.cc to Emit PiperOrigin-RevId: 556917345 --- .../cpp/field_generators/message_field.cc | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc index e270970804..8fa31cc12b 100644 --- a/src/google/protobuf/compiler/cpp/field_generators/message_field.cc +++ b/src/google/protobuf/compiler/cpp/field_generators/message_field.cc @@ -544,35 +544,33 @@ class OneofMessage : public SingularMessage { }; void OneofMessage::GenerateNonInlineAccessorDefinitions(io::Printer* p) const { - p->Emit( - "void $Msg$::set_allocated_$name$($Submsg$* $name$) {\n" - " $pb$::Arena* message_arena = GetArenaForAllocation();\n" - " clear_$oneof_name$();\n" - " if ($name$) {\n"); - if (field_->file() != field_->message_type()->file()) { - // We have to read the arena through the virtual method, because the type - // isn't defined in this file. - p->Emit( - " $pb$::Arena* submessage_arena =\n" - " $pb$::Arena::InternalGetOwningArena(\n" - " reinterpret_cast<$pb$::MessageLite*>(" - "$name$));\n"); - } else { - p->Emit( - " $pb$::Arena* submessage_arena =\n" - " $pb$::Arena::InternalGetOwningArena($name$);\n"); - } - p->Emit( - " if (message_arena != submessage_arena) {\n" - " $name$ = $pbi$::GetOwnedMessage(\n" - " message_arena, $name$, submessage_arena);\n" - " }\n" - " set_has_$name$();\n" - " $field_$ = $name$;\n" - " }\n" - "$annotate_set$" - " // @@protoc_insertion_point(field_set_allocated:$pkg.Msg.field$)\n" - "}\n"); + p->Emit({{"casted_name", + [&] { + if (field_->file() != field_->message_type()->file()) { + // We have to read the arena through the virtual method, because + // the type isn't defined in this file. + p->Emit(R"cc(reinterpret_cast<$pb$::MessageLite*>($name$))cc"); + } else { + p->Emit(R"cc($name$)cc"); + } + }}}, + R"cc( + void $Msg$::set_allocated_$name$($Submsg$* $name$) { + $pb$::Arena* message_arena = GetArenaForAllocation(); + clear_$oneof_name$(); + if ($name$) { + $pb$::Arena* submessage_arena = + $pb$::Arena::InternalGetOwningArena($casted_name$); + if (message_arena != submessage_arena) { + $name$ = $pbi$::GetOwnedMessage(message_arena, $name$, submessage_arena); + } + set_has_$name$(); + $field_$ = $name$; + } + $annotate_set$; + // @@protoc_insertion_point(field_set_allocated:$pkg.Msg.field$) + } + )cc"); } void OneofMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {