Refactor PrintFieldComment in protoc to use io::Printer and Emit. PiperOrigin-RevId: 1002701946
diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index db4003c..b63fdec 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h
@@ -1077,9 +1077,10 @@ } template <class T> -void PrintFieldComment(const Formatter& format, const T* field, - const Options& options) { - format("// $1$\n", FieldComment(field, options)); +void PrintFieldComment(io::Printer* p, const T* field, const Options& options) { + p->Emit({{"comment", FieldComment(field, options)}}, R"cc( + // $comment$ + )cc"); } class PROTOC_EXPORT NamespaceOpener {
diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index 1659027..c7c8a7b 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc
@@ -1283,7 +1283,7 @@ const FieldDescriptor* field, io::Printer* p, int& cached_has_word_index) const { p->Emit( - {{"comment", [&] { PrintFieldComment(Formatter{p}, field, options_); }}, + {{"comment", [&] { PrintFieldComment(p, field, options_); }}, {"update_cached_has_bits", [&] { MaybeEmitUpdateCachedHasbits(field, p, cached_has_word_index); }}, {"check_and_update_byte_size_for_field", @@ -1312,7 +1312,7 @@ const auto accessor_verifier = p->WithSubstitutionListener(AccessorVerifier(field)); - PrintFieldComment(Formatter{p}, field, options_); + PrintFieldComment(p, field, options_); auto v = p->WithVars(FieldVars(field, options_)); auto t = p->WithVars(MakeTrackerCalls(field, options_)); @@ -4252,7 +4252,7 @@ field_generators_.get(field).GenerateSerializeWithCachedSizesToArray(p); }; - PrintFieldComment(Formatter{p}, field, options_); + PrintFieldComment(p, field, options_); if (HasHasbit(field, options_)) { int has_bit_index = field_layout_.GetHasBitIndex(field).value(); int has_word_index = field_layout_.GetHasWordIndex(field).value(); @@ -4997,7 +4997,7 @@ {"case_per_field", [&] { for (auto field : internal::FieldRange(oneof)) { - PrintFieldComment(Formatter{p}, field, options_); + PrintFieldComment(p, field, options_); p->Emit( {{"field_name", UnderscoresToCamelCase(field->name(), true)}, @@ -5086,7 +5086,7 @@ {"case_per_field", [&] { for (auto field : internal::FieldRange(oneof)) { - PrintFieldComment(Formatter{p}, field, options_); + PrintFieldComment(p, field, options_); p->Emit({{"field_name", UnderscoresToCamelCase(field->name(), true)}, {"field_byte_size", [&] { emitter(p, field); }}},
diff --git a/src/google/protobuf/compiler/cpp/parse_function_generator.cc b/src/google/protobuf/compiler/cpp/parse_function_generator.cc index b479a18..b4080ea 100644 --- a/src/google/protobuf/compiler/cpp/parse_function_generator.cc +++ b/src/google/protobuf/compiler/cpp/parse_function_generator.cc
@@ -464,16 +464,14 @@ {$target$, {$coded_tag$, ::uint16_t{$nonfield_info$}}}, )cc"); } else if (auto* mp_field = info.AsMpField()) { - { - // TODO: refactor this to use Emit. - Formatter format(p, variables_); - PrintFieldComment(format, mp_field->field, options_); - } - p->Emit({{"target", TcParseFunctionName(mp_field->func)}, + p->Emit({{"comment", + [&] { PrintFieldComment(p, mp_field->field, options_); }}, + {"target", TcParseFunctionName(mp_field->func)}, {"coded_tag", mp_field->coded_tag}, {"function_index", mp_field->function_index}, {"index", mp_field->field_index}}, R"cc( + $comment$; {$target$, {$coded_tag$, ::uint8_t{$function_index$}, ::uint32_t{ @@ -481,11 +479,6 @@ sizeof(_pbi::TcParseTableBase::FieldEntry) * $index$}}}, )cc"); } else if (auto* as_field = info.AsField()) { - { - // TODO: refactor this to use Emit. - Formatter format(p, variables_); - PrintFieldComment(format, as_field->field, options_); - } ABSL_CHECK(!ShouldSplit(as_field->field, options_)); std::string func_name = TcParseFunctionName(as_field->func); @@ -514,6 +507,8 @@ p->Emit( { + {"comment", + [&] { PrintFieldComment(p, as_field->field, options_); }}, {"target", func_name}, {"coded_tag", as_field->coded_tag}, {"hasbit_idx", as_field->hasbit_idx}, @@ -521,6 +516,7 @@ {"field_name", FieldMemberName(as_field->field, /*split=*/false)}, }, R"cc( + $comment$; {$target$, {$coded_tag$, $hasbit_idx$, $aux_idx$, PROTOBUF_FIELD_OFFSET($Msg$, $field_name$)}}, @@ -537,9 +533,6 @@ void ParseFunctionGenerator::GenerateFieldEntries(io::Printer* p) { for (const auto& entry : tc_table_info_->field_entries) { const FieldDescriptor* field = entry.field; - // TODO: refactor this to use Emit. - Formatter format(p, variables_); - PrintFieldComment(format, field, options_); bool weak = IsWeak(field, options_); bool split = ShouldSplit(field, options_); @@ -549,7 +542,8 @@ {{"field_name", FieldName(field)}, {"field_member_name", FieldMemberName(field, /*split=*/false)}}); - p->Emit({{"offset", + p->Emit({{"comment", [&] { PrintFieldComment(p, field, options_); }}, + {"offset", [&] { if (weak) { p->Emit("/* weak */ 0,"); @@ -580,6 +574,7 @@ // Use `0|` prefix to eagerly convert the enums to int to avoid // enum-enum operations. They are deprecated in C++20. R"cc( + $comment$; {$offset$, $has_idx$, $aux_idx$, (0 | $type_card$)}, )cc"); }