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
pull/13261/head
Thomas Van Lenten 2 years ago committed by Copybara-Service
parent 9d43502e10
commit 0196d1e138
  1. 12
      src/google/protobuf/compiler/objectivec/enum.cc
  2. 2
      src/google/protobuf/compiler/objectivec/extension.cc
  3. 6
      src/google/protobuf/compiler/objectivec/field.cc
  4. 6
      src/google/protobuf/compiler/objectivec/helpers.cc
  5. 15
      src/google/protobuf/compiler/objectivec/helpers.h
  6. 5
      src/google/protobuf/compiler/objectivec/message.cc
  7. 2
      src/google/protobuf/compiler/objectivec/oneof.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;
}
}},
},

@ -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",

@ -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$

@ -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$ */
)");

@ -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 <class TDescriptor>
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);
}
}

@ -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;

@ -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;

Loading…
Cancel
Save