Skip to content

Commit 0196d1e

Browse files
thomasvlcopybara-github
authored andcommitted
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
1 parent 9d43502 commit 0196d1e

File tree

7 files changed

+26
-22
lines changed

7 files changed

+26
-22
lines changed

src/google/protobuf/compiler/objectivec/enum.cc

+4-8
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const {
105105
printer->Emit(
106106
{
107107
{"enum_name", name_},
108-
{"enum_comments",
109-
[&] { EmitCommentsString(printer, descriptor_, true); }},
108+
{"enum_comments", [&] { EmitCommentsString(printer, descriptor_); }},
110109
{"enum_deprecated_attribute",
111110
GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file())},
112111
{"maybe_unknown_value",
@@ -125,17 +124,14 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const {
125124
}},
126125
{"enum_values",
127126
[&] {
128-
bool add_leading_newilne = false;
127+
CommentStringFlags comment_flags = CommentStringFlags::kNone;
129128
for (const auto* v : all_values_) {
130129
if (alias_values_to_skip_.contains(v)) continue;
131130
printer->Emit(
132131
{
133132
{"name", EnumValueName(v)},
134133
{"comments",
135-
[&] {
136-
EmitCommentsString(printer, v, true,
137-
add_leading_newilne);
138-
}},
134+
[&] { EmitCommentsString(printer, v, comment_flags); }},
139135
{"deprecated_attribute",
140136
GetOptionalDeprecatedAttribute(v)},
141137
{"value", SafelyPrintIntToCode(v->number())},
@@ -145,7 +141,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const {
145141
$comments$
146142
$name$$deprecated_attribute$ = $value$,
147143
)objc");
148-
add_leading_newilne = true;
144+
comment_flags = CommentStringFlags::kAddLeadingNewline;
149145
}
150146
}},
151147
},

src/google/protobuf/compiler/objectivec/extension.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ExtensionGenerator::ExtensionGenerator(
6060
void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) const {
6161
printer->Emit(
6262
{{"method_name", method_name_},
63-
{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }},
63+
{"comments", [&] { EmitCommentsString(printer, descriptor_); }},
6464
{"storage_attribute",
6565
IsRetainedName(method_name_) ? "NS_RETURNS_NOT_RETAINED" : ""},
6666
{"deprecated_attribute",

src/google/protobuf/compiler/objectivec/field.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void SingleFieldGenerator::GeneratePropertyDeclaration(
313313
io::Printer* printer) const {
314314
auto vars = printer->WithVars(variables_);
315315
printer->Emit(
316-
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}},
316+
{{"comments", [&] { EmitCommentsString(printer, descriptor_); }}},
317317
R"objc(
318318
$comments$
319319
@property(nonatomic, readwrite) $property_type$ $name$$deprecated_attribute$;
@@ -366,7 +366,7 @@ void ObjCObjFieldGenerator::GeneratePropertyDeclaration(
366366

367367
auto vars = printer->WithVars(variables_);
368368
printer->Emit(
369-
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}},
369+
{{"comments", [&] { EmitCommentsString(printer, descriptor_); }}},
370370
R"objc(
371371
$comments$
372372
@property(nonatomic, readwrite, $property_storage_attribute$, null_resettable) $property_type$ *$name$$storage_attribute$$deprecated_attribute$;
@@ -420,7 +420,7 @@ void RepeatedFieldGenerator::GeneratePropertyDeclaration(
420420

421421
auto vars = printer->WithVars(variables_);
422422
printer->Emit(
423-
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }},
423+
{{"comments", [&] { EmitCommentsString(printer, descriptor_); }},
424424
{"array_comment", [&] { EmitArrayComment(printer); }}},
425425
R"objc(
426426
$comments$

src/google/protobuf/compiler/objectivec/helpers.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ std::string ObjCClassDeclaration(absl::string_view class_name) {
342342
}
343343

344344
void EmitCommentsString(io::Printer* printer, const SourceLocation& location,
345-
bool prefer_single_line, bool add_leading_newilne) {
345+
CommentStringFlags flags) {
346346
absl::string_view comments = location.leading_comments.empty()
347347
? location.trailing_comments
348348
: location.leading_comments;
@@ -375,11 +375,11 @@ void EmitCommentsString(io::Printer* printer, const SourceLocation& location,
375375
{"*/", "*\\/"}}));
376376
}
377377

378-
if (add_leading_newilne) {
378+
if (flags & CommentStringFlags::kAddLeadingNewline) {
379379
printer->Emit("\n");
380380
}
381381

382-
if (prefer_single_line && lines.size() == 1) {
382+
if ((flags & CommentStringFlags::kForceMultiline) == 0 && lines.size() == 1) {
383383
printer->Emit({{"text", lines[0]}}, R"(
384384
/** $text$ */
385385
)");

src/google/protobuf/compiler/objectivec/helpers.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,26 @@ std::string ObjCClass(absl::string_view class_name);
112112
// be referred to by ObjCClass.
113113
std::string ObjCClassDeclaration(absl::string_view class_name);
114114

115+
// Flag to control the behavior of `EmitCommentsString`.
116+
enum CommentStringFlags : unsigned int {
117+
kNone = 0,
118+
kAddLeadingNewline = 1 << 1, // Add a newline before the comment.
119+
kForceMultiline = 1 << 2, // Force a multiline comment even if only 1 line.
120+
};
121+
115122
// Emits HeaderDoc/appledoc style comments out of the comments in the .proto
116123
// file.
117124
void EmitCommentsString(io::Printer* printer, const SourceLocation& location,
118-
bool prefer_single_line, bool add_leading_newilne);
125+
CommentStringFlags flags = kNone);
119126

120127
// Emits HeaderDoc/appledoc style comments out of the comments in the .proto
121128
// file.
122129
template <class TDescriptor>
123130
void EmitCommentsString(io::Printer* printer, const TDescriptor* descriptor,
124-
bool prefer_single_line,
125-
bool add_leading_newilne = false) {
131+
CommentStringFlags flags = kNone) {
126132
SourceLocation location;
127133
if (descriptor->GetSourceLocation(&location)) {
128-
EmitCommentsString(printer, location, prefer_single_line,
129-
add_leading_newilne);
134+
EmitCommentsString(printer, location, flags);
130135
}
131136
}
132137

src/google/protobuf/compiler/objectivec/message.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ void MessageGenerator::GenerateMessageHeader(io::Printer* printer) const {
306306
printer->Emit(
307307
{{"deprecated_attribute", deprecated_attribute_},
308308
{"message_comments",
309-
[&] { EmitCommentsString(printer, descriptor_, false); }},
309+
[&] {
310+
EmitCommentsString(printer, descriptor_,
311+
CommentStringFlags::kForceMultiline);
312+
}},
310313
{"message_fieldnum_enum",
311314
[&] {
312315
if (descriptor_->field_count() == 0) return;

src/google/protobuf/compiler/objectivec/oneof.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void OneofGenerator::GeneratePublicCasePropertyDeclaration(
8585
io::Printer* printer) const {
8686
auto vars = printer->WithVars(variables_);
8787
printer->Emit(
88-
{{"comments", [&] { EmitCommentsString(printer, descriptor_, true); }}},
88+
{{"comments", [&] { EmitCommentsString(printer, descriptor_); }}},
8989
R"objc(
9090
$comments$;
9191
@property(nonatomic, readonly) $enum_name$ $name$OneOfCase;

0 commit comments

Comments
 (0)