From 0196d1e1387a903ee0cc44b301bb94f7ce7d5d2a Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 11 Jul 2023 13:42:28 -0700 Subject: [PATCH] Move the two bools for EmitComment to a flags enum Makes call sites easier to ready and should make things safer if this ever needs some other support in the future. PiperOrigin-RevId: 547284873 --- src/google/protobuf/compiler/objectivec/enum.cc | 12 ++++-------- .../protobuf/compiler/objectivec/extension.cc | 2 +- src/google/protobuf/compiler/objectivec/field.cc | 6 +++--- .../protobuf/compiler/objectivec/helpers.cc | 6 +++--- src/google/protobuf/compiler/objectivec/helpers.h | 15 ++++++++++----- .../protobuf/compiler/objectivec/message.cc | 5 ++++- src/google/protobuf/compiler/objectivec/oneof.cc | 2 +- 7 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/enum.cc b/src/google/protobuf/compiler/objectivec/enum.cc index ee1ad1937b..c5d409a30a 100644 --- a/src/google/protobuf/compiler/objectivec/enum.cc +++ b/src/google/protobuf/compiler/objectivec/enum.cc @@ -105,8 +105,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const { printer->Emit( { {"enum_name", name_}, - {"enum_comments", - [&] { EmitCommentsString(printer, descriptor_, true); }}, + {"enum_comments", [&] { EmitCommentsString(printer, descriptor_); }}, {"enum_deprecated_attribute", GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file())}, {"maybe_unknown_value", @@ -125,17 +124,14 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const { }}, {"enum_values", [&] { - bool add_leading_newilne = false; + CommentStringFlags comment_flags = CommentStringFlags::kNone; for (const auto* v : all_values_) { if (alias_values_to_skip_.contains(v)) continue; printer->Emit( { {"name", EnumValueName(v)}, {"comments", - [&] { - EmitCommentsString(printer, v, true, - add_leading_newilne); - }}, + [&] { EmitCommentsString(printer, v, comment_flags); }}, {"deprecated_attribute", GetOptionalDeprecatedAttribute(v)}, {"value", SafelyPrintIntToCode(v->number())}, @@ -145,7 +141,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const { $comments$ $name$$deprecated_attribute$ = $value$, )objc"); - add_leading_newilne = true; + comment_flags = CommentStringFlags::kAddLeadingNewline; } }}, }, diff --git a/src/google/protobuf/compiler/objectivec/extension.cc b/src/google/protobuf/compiler/objectivec/extension.cc index 9f0f3c925b..053112d0c8 100644 --- a/src/google/protobuf/compiler/objectivec/extension.cc +++ b/src/google/protobuf/compiler/objectivec/extension.cc @@ -60,7 +60,7 @@ ExtensionGenerator::ExtensionGenerator( void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) const { printer->Emit( {{"method_name", method_name_}, - {"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}, + {"comments", [&] { EmitCommentsString(printer, descriptor_); }}, {"storage_attribute", IsRetainedName(method_name_) ? "NS_RETURNS_NOT_RETAINED" : ""}, {"deprecated_attribute", diff --git a/src/google/protobuf/compiler/objectivec/field.cc b/src/google/protobuf/compiler/objectivec/field.cc index 40960b66ba..4cac9d70b9 100644 --- a/src/google/protobuf/compiler/objectivec/field.cc +++ b/src/google/protobuf/compiler/objectivec/field.cc @@ -313,7 +313,7 @@ void SingleFieldGenerator::GeneratePropertyDeclaration( io::Printer* printer) const { auto vars = printer->WithVars(variables_); printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}}, + {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}}, R"objc( $comments$ @property(nonatomic, readwrite) $property_type$ $name$$deprecated_attribute$; @@ -366,7 +366,7 @@ void ObjCObjFieldGenerator::GeneratePropertyDeclaration( auto vars = printer->WithVars(variables_); printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}}, + {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}}, R"objc( $comments$ @property(nonatomic, readwrite, $property_storage_attribute$, null_resettable) $property_type$ *$name$$storage_attribute$$deprecated_attribute$; @@ -420,7 +420,7 @@ void RepeatedFieldGenerator::GeneratePropertyDeclaration( auto vars = printer->WithVars(variables_); printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}, + {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}, {"array_comment", [&] { EmitArrayComment(printer); }}}, R"objc( $comments$ diff --git a/src/google/protobuf/compiler/objectivec/helpers.cc b/src/google/protobuf/compiler/objectivec/helpers.cc index bf655a5020..85c800d06b 100644 --- a/src/google/protobuf/compiler/objectivec/helpers.cc +++ b/src/google/protobuf/compiler/objectivec/helpers.cc @@ -342,7 +342,7 @@ std::string ObjCClassDeclaration(absl::string_view class_name) { } void EmitCommentsString(io::Printer* printer, const SourceLocation& location, - bool prefer_single_line, bool add_leading_newilne) { + CommentStringFlags flags) { absl::string_view comments = location.leading_comments.empty() ? location.trailing_comments : location.leading_comments; @@ -375,11 +375,11 @@ void EmitCommentsString(io::Printer* printer, const SourceLocation& location, {"*/", "*\\/"}})); } - if (add_leading_newilne) { + if (flags & CommentStringFlags::kAddLeadingNewline) { printer->Emit("\n"); } - if (prefer_single_line && lines.size() == 1) { + if ((flags & CommentStringFlags::kForceMultiline) == 0 && lines.size() == 1) { printer->Emit({{"text", lines[0]}}, R"( /** $text$ */ )"); diff --git a/src/google/protobuf/compiler/objectivec/helpers.h b/src/google/protobuf/compiler/objectivec/helpers.h index 45963aac6c..0191e82e53 100644 --- a/src/google/protobuf/compiler/objectivec/helpers.h +++ b/src/google/protobuf/compiler/objectivec/helpers.h @@ -112,21 +112,26 @@ std::string ObjCClass(absl::string_view class_name); // be referred to by ObjCClass. std::string ObjCClassDeclaration(absl::string_view class_name); +// Flag to control the behavior of `EmitCommentsString`. +enum CommentStringFlags : unsigned int { + kNone = 0, + kAddLeadingNewline = 1 << 1, // Add a newline before the comment. + kForceMultiline = 1 << 2, // Force a multiline comment even if only 1 line. +}; + // Emits HeaderDoc/appledoc style comments out of the comments in the .proto // file. void EmitCommentsString(io::Printer* printer, const SourceLocation& location, - bool prefer_single_line, bool add_leading_newilne); + CommentStringFlags flags = kNone); // Emits HeaderDoc/appledoc style comments out of the comments in the .proto // file. template void EmitCommentsString(io::Printer* printer, const TDescriptor* descriptor, - bool prefer_single_line, - bool add_leading_newilne = false) { + CommentStringFlags flags = kNone) { SourceLocation location; if (descriptor->GetSourceLocation(&location)) { - EmitCommentsString(printer, location, prefer_single_line, - add_leading_newilne); + EmitCommentsString(printer, location, flags); } } diff --git a/src/google/protobuf/compiler/objectivec/message.cc b/src/google/protobuf/compiler/objectivec/message.cc index ddc8a6020e..bc95d23664 100644 --- a/src/google/protobuf/compiler/objectivec/message.cc +++ b/src/google/protobuf/compiler/objectivec/message.cc @@ -306,7 +306,10 @@ void MessageGenerator::GenerateMessageHeader(io::Printer* printer) const { printer->Emit( {{"deprecated_attribute", deprecated_attribute_}, {"message_comments", - [&] { EmitCommentsString(printer, descriptor_, false); }}, + [&] { + EmitCommentsString(printer, descriptor_, + CommentStringFlags::kForceMultiline); + }}, {"message_fieldnum_enum", [&] { if (descriptor_->field_count() == 0) return; diff --git a/src/google/protobuf/compiler/objectivec/oneof.cc b/src/google/protobuf/compiler/objectivec/oneof.cc index 9f5c0fb079..5a38783616 100644 --- a/src/google/protobuf/compiler/objectivec/oneof.cc +++ b/src/google/protobuf/compiler/objectivec/oneof.cc @@ -85,7 +85,7 @@ void OneofGenerator::GeneratePublicCasePropertyDeclaration( io::Printer* printer) const { auto vars = printer->WithVars(variables_); printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}}, + {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}}, R"objc( $comments$; @property(nonatomic, readonly) $enum_name$ $name$OneOfCase;