From d3c6c9f9e144a00ded61dd6e3a2ce0c28ae4bc33 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Thu, 6 Jul 2023 08:12:48 -0700 Subject: [PATCH] Move OneofGenerator over to Printer::Emit PiperOrigin-RevId: 545992294 --- .../protobuf/compiler/objectivec/oneof.cc | 98 +++++++++---------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/oneof.cc b/src/google/protobuf/compiler/objectivec/oneof.cc index 99b2149379..9f5c0fb079 100644 --- a/src/google/protobuf/compiler/objectivec/oneof.cc +++ b/src/google/protobuf/compiler/objectivec/oneof.cc @@ -50,15 +50,6 @@ OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor) variables_["raw_index"] = absl::StrCat(descriptor_->index()); const Descriptor* msg_descriptor = descriptor_->containing_type(); variables_["owning_message_class"] = ClassName(msg_descriptor); - - std::string comments; - SourceLocation location; - if (descriptor_->GetSourceLocation(&location)) { - comments = BuildCommentsString(location, true); - } else { - comments = ""; - } - variables_["comments"] = comments; } void OneofGenerator::SetOneofIndexBase(int index_base) { @@ -68,64 +59,69 @@ void OneofGenerator::SetOneofIndexBase(int index_base) { } void OneofGenerator::GenerateCaseEnum(io::Printer* printer) const { - printer->Print(variables_, "typedef GPB_ENUM($enum_name$) {\n"); - printer->Indent(); - printer->Print(variables_, "$enum_name$_GPBUnsetOneOfCase = 0,\n"); - std::string enum_name = variables_.find("enum_name")->second; - for (int j = 0; j < descriptor_->field_count(); j++) { - const FieldDescriptor* field = descriptor_->field(j); - std::string field_name = FieldNameCapitalized(field); - printer->Print("$enum_name$_$field_name$ = $field_number$,\n", "enum_name", - enum_name, "field_name", field_name, "field_number", - absl::StrCat(field->number())); - } - printer->Outdent(); - // clang-format off - printer->Print( - "};\n" - "\n"); - // clang-format on + auto vars = printer->WithVars(variables_); + printer->Emit({{"cases", + [&] { + for (int j = 0; j < descriptor_->field_count(); j++) { + const FieldDescriptor* field = descriptor_->field(j); + printer->Emit( + {{"field_name", FieldNameCapitalized(field)}, + {"field_number", field->number()}}, + R"objc( + $enum_name$_$field_name$ = $field_number$, + )objc"); + } + }}}, + R"objc( + typedef GPB_ENUM($enum_name$) { + $enum_name$_GPBUnsetOneOfCase = 0, + $cases$ + }; + )objc"); + printer->Emit("\n"); } void OneofGenerator::GeneratePublicCasePropertyDeclaration( io::Printer* printer) const { - // clang-format off - printer->Print( - variables_, - "$comments$" - "@property(nonatomic, readonly) $enum_name$ $name$OneOfCase;\n" - "\n"); - // clang-format on + auto vars = printer->WithVars(variables_); + printer->Emit( + {{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}}, + R"objc( + $comments$; + @property(nonatomic, readonly) $enum_name$ $name$OneOfCase; + )objc"); + printer->Emit("\n"); } void OneofGenerator::GenerateClearFunctionDeclaration( io::Printer* printer) const { - // clang-format off - printer->Print( - variables_, - "/**\n" - " * Clears whatever value was set for the oneof '$name$'.\n" - " **/\n" - "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message);\n"); - // clang-format on + auto vars = printer->WithVars(variables_); + printer->Emit(R"objc( + /** + * Clears whatever value was set for the oneof '$name$'. + **/ + void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message); + )objc"); } void OneofGenerator::GeneratePropertyImplementation( io::Printer* printer) const { - printer->Print(variables_, "@dynamic $name$OneOfCase;\n"); + auto vars = printer->WithVars(variables_); + printer->Emit(R"objc( + @dynamic $name$OneOfCase; + )objc"); } void OneofGenerator::GenerateClearFunctionImplementation( io::Printer* printer) const { - // clang-format off - printer->Print( - variables_, - "void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message) {\n" - " GPBDescriptor *descriptor = [$owning_message_class$ descriptor];\n" - " GPBOneofDescriptor *oneof = [descriptor.oneofs objectAtIndex:$raw_index$];\n" - " GPBClearOneof(message, oneof);\n" - "}\n"); - // clang-format on + auto vars = printer->WithVars(variables_); + printer->Emit(R"objc( + void $owning_message_class$_Clear$capitalized_name$OneOfCase($owning_message_class$ *message) { + GPBDescriptor *descriptor = [$owning_message_class$ descriptor]; + GPBOneofDescriptor *oneof = [descriptor.oneofs objectAtIndex:$raw_index$]; + GPBClearOneof(message, oneof); + } + )objc"); } std::string OneofGenerator::DescriptorName() const {