Incremental migration of message.cc to Emit

PiperOrigin-RevId: 532320438
pull/12751/head
Matt Kulukundis 2 years ago committed by Copybara-Service
parent 7b23c9bddf
commit 1e76cbe0ea
  1. 41
      src/google/protobuf/compiler/cpp/message.cc

@ -3611,7 +3611,6 @@ void MessageGenerator::GenerateVerify(io::Printer* p) {
void MessageGenerator::GenerateSerializeOneofFields( void MessageGenerator::GenerateSerializeOneofFields(
io::Printer* p, const std::vector<const FieldDescriptor*>& fields) { io::Printer* p, const std::vector<const FieldDescriptor*>& fields) {
Formatter format(p);
ABSL_CHECK(!fields.empty()); ABSL_CHECK(!fields.empty());
if (fields.size() == 1) { if (fields.size() == 1) {
GenerateSerializeOneField(p, fields[0], -1); GenerateSerializeOneField(p, fields[0], -1);
@ -3619,21 +3618,31 @@ void MessageGenerator::GenerateSerializeOneofFields(
} }
// We have multiple mutually exclusive choices. Emit a switch statement. // We have multiple mutually exclusive choices. Emit a switch statement.
const OneofDescriptor* oneof = fields[0]->containing_oneof(); const OneofDescriptor* oneof = fields[0]->containing_oneof();
format("switch ($1$_case()) {\n", oneof->name()); p->Emit({{"name", oneof->name()},
format.Indent(); {"cases",
for (auto field : fields) { [&] {
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true)); for (const auto* field : fields) {
format.Indent(); p->Emit({{"Name", UnderscoresToCamelCase(field->name(), true)},
field_generators_.get(field).GenerateSerializeWithCachedSizesToArray(p); {"body",
format("break;\n"); [&] {
format.Outdent(); field_generators_.get(field)
format("}\n"); .GenerateSerializeWithCachedSizesToArray(p);
} }}},
format.Outdent(); R"cc(
// Doing nothing is an option. case k$Name$: {
format( $body$;
" default: ;\n" break;
"}\n"); }
)cc");
}
}}},
R"cc(
switch ($name$_case()) {
$cases$;
default:
break;
}
)cc");
} }
void MessageGenerator::GenerateSerializeOneField(io::Printer* p, void MessageGenerator::GenerateSerializeOneField(io::Printer* p,

Loading…
Cancel
Save