From 151c7370194aa25496941052bb659a2f54166b46 Mon Sep 17 00:00:00 2001 From: Matt Kulukundis Date: Mon, 8 May 2023 17:30:07 -0700 Subject: [PATCH] Incremental migration of message.cc to Emit PiperOrigin-RevId: 530452483 --- src/google/protobuf/compiler/cpp/message.cc | 74 +++++++++++---------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 076812124b..69ea21009a 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -1073,40 +1073,46 @@ void MessageGenerator::GenerateOneofMemberHasBits(const FieldDescriptor* field, void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field, bool is_inline, io::Printer* p) { auto t = p->WithVars(MakeTrackerCalls(field, options_)); - Formatter format(p); - - // Generate clear_$name$(). - if (is_inline) { - format("inline "); - } - format("void $classname$::clear_$name$() {\n"); - - format.Indent(); - - if (field->real_containing_oneof()) { - // Clear this field only if it is the active field in this oneof, - // otherwise ignore - auto t = p->WithVars(MakeTrackerCalls(field, options_)); - format("if ($has_field$) {\n"); - format.Indent(); - field_generators_.get(field).GenerateClearingCode(p); - format("clear_has_$oneof_name$();\n"); - format.Outdent(); - format("}\n"); - } else { - if (ShouldSplit(field, options_)) { - format("if (IsSplitMessageDefault()) return;\n"); - } - field_generators_.get(field).GenerateClearingCode(p); - if (HasHasbit(field)) { - int has_bit_index = HasBitIndex(field); - auto v = p->WithVars(HasbitVars(has_bit_index)); - format("$has_bits$[$has_array_index$] &= ~$has_mask$;\n"); - } - } - format("$annotate_clear$"); - format.Outdent(); - format("}\n"); + p->Emit({{"inline", is_inline ? "inline" : ""}, + {"body", + [&] { + if (field->real_containing_oneof()) { + // Clear this field only if it is the active field in this + // oneof, otherwise ignore + p->Emit( + {{"clearing_code", + [&] { + field_generators_.get(field).GenerateClearingCode(p); + }}}, + R"cc( + if ($has_field$) { + $clearing_code$; + clear_has_$oneof_name$(); + } + )cc"); + } else { + // TODO(b/281513105): figure out if early return breaks tracking + if (ShouldSplit(field, options_)) { + p->Emit(R"cc( + if (IsSplitMessageDefault()) return; + )cc"); + } + field_generators_.get(field).GenerateClearingCode(p); + if (HasHasbit(field)) { + int has_bit_index = HasBitIndex(field); + auto v = p->WithVars(HasbitVars(has_bit_index)); + p->Emit(R"cc( + $has_bits$[$has_array_index$] &= ~$has_mask$; + )cc"); + } + } + }}}, + R"cc( + $inline $void $classname$::clear_$name$() { + $body$; + $annotate_clear$; + } + )cc"); } void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {