Skip to content

Commit

Permalink
Move the two bools for EmitComment to a flags enum
Browse files Browse the repository at this point in the history
Makes call sites easier to ready and should make things safer
if this ever needs some other support in the future.

PiperOrigin-RevId: 547284873
  • Loading branch information
thomasvl authored and copybara-github committed Jul 11, 2023
1 parent 9d43502 commit 0196d1e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
12 changes: 4 additions & 8 deletions src/google/protobuf/compiler/objectivec/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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())},
Expand All @@ -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;
}
}},
},
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/objectivec/extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/objectivec/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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$;
Expand Down Expand Up @@ -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$;
Expand Down Expand Up @@ -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$
Expand Down
6 changes: 3 additions & 3 deletions src/google/protobuf/compiler/objectivec/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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$ */
)");
Expand Down
15 changes: 10 additions & 5 deletions src/google/protobuf/compiler/objectivec/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/google/protobuf/compiler/objectivec/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/objectivec/oneof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0196d1e

Please sign in to comment.