Strip nonfunctional Javadoc comments from .proto comments.

PiperOrigin-RevId: 601267733
pull/15575/head
Sandy Zhang 10 months ago committed by Copybara-Service
parent 9396fe26f0
commit 7f1ef7ea09
  1. 69
      src/google/protobuf/compiler/java/doc_comment.cc
  2. 38
      src/google/protobuf/compiler/java/doc_comment.h
  3. 10
      src/google/protobuf/compiler/java/enum.cc
  4. 10
      src/google/protobuf/compiler/java/enum_lite.cc
  5. 5
      src/google/protobuf/compiler/java/message.cc
  6. 2
      src/google/protobuf/compiler/java/message_builder.cc
  7. 2
      src/google/protobuf/compiler/java/message_builder_lite.cc
  8. 5
      src/google/protobuf/compiler/java/message_lite.cc
  9. 4
      src/google/protobuf/compiler/java/service.cc

@ -124,6 +124,7 @@ static std::string EscapeKdoc(const std::string& input) {
static void WriteDocCommentBodyForLocation(io::Printer* printer,
const SourceLocation& location,
const Options options,
const bool kdoc) {
std::string comments = location.leading_comments.empty()
? location.trailing_comments
@ -146,20 +147,24 @@ static void WriteDocCommentBodyForLocation(io::Printer* printer,
printer->Print(" * <pre>\n");
}
for (size_t i = 0; i < lines.size(); i++) {
// Lines should start with a single space and any extraneous leading
// spaces should be stripped. For lines starting with a /, the leading
// space will prevent putting it right after the leading asterick from
// closing the comment.
std::string line = lines[i];
line.erase(line.begin(),
std::find_if(line.begin(), line.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
if (!line.empty()) {
printer->Print(" * $line$\n", "line", line);
} else {
printer->Print(" *\n");
// TODO: Remove once prototiller can avoid making
// extraneous formatting changes to comments.
if (!options.strip_nonfunctional_codegen) {
for (size_t i = 0; i < lines.size(); i++) {
// Lines should start with a single space and any extraneous leading
// spaces should be stripped. For lines starting with a /, the leading
// space will prevent putting it right after the leading asterick from
// closing the comment.
std::string line = lines[i];
line.erase(line.begin(),
std::find_if(line.begin(), line.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
if (!line.empty()) {
printer->Print(" * $line$\n", "line", line);
} else {
printer->Print(" *\n");
}
}
}
@ -175,10 +180,10 @@ static void WriteDocCommentBodyForLocation(io::Printer* printer,
template <typename DescriptorType>
static void WriteDocCommentBody(io::Printer* printer,
const DescriptorType* descriptor,
const bool kdoc) {
const Options options, const bool kdoc) {
SourceLocation location;
if (descriptor->GetSourceLocation(&location)) {
WriteDocCommentBodyForLocation(printer, location, kdoc);
WriteDocCommentBodyForLocation(printer, location, options, kdoc);
}
}
@ -213,9 +218,9 @@ static void WriteDebugString(io::Printer* printer, const FieldDescriptor* field,
}
void WriteMessageDocComment(io::Printer* printer, const Descriptor* message,
const bool kdoc) {
const Options options, const bool kdoc) {
printer->Print("/**\n");
WriteDocCommentBody(printer, message, kdoc);
WriteDocCommentBody(printer, message, options, kdoc);
if (kdoc) {
printer->Print(
" * Protobuf type `$fullname$`\n"
@ -238,7 +243,7 @@ void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field,
// And then we end with the javadoc tags if applicable.
// If the field is a group, the debug string might end with {.
printer->Print("/**\n");
WriteDocCommentBody(printer, field, kdoc);
WriteDocCommentBody(printer, field, options, kdoc);
WriteDebugString(printer, field, options, kdoc);
printer->Print(" */\n");
}
@ -276,7 +281,7 @@ void WriteFieldAccessorDocComment(io::Printer* printer,
const Options options, const bool builder,
const bool kdoc) {
printer->Print("/**\n");
WriteDocCommentBody(printer, field, kdoc);
WriteDocCommentBody(printer, field, options, kdoc);
WriteDebugString(printer, field, options, kdoc);
if (!kdoc) WriteDeprecatedJavadoc(printer, field, type, options);
switch (type) {
@ -336,7 +341,7 @@ void WriteFieldEnumValueAccessorDocComment(io::Printer* printer,
const bool builder,
const bool kdoc) {
printer->Print("/**\n");
WriteDocCommentBody(printer, field, kdoc);
WriteDocCommentBody(printer, field, options, kdoc);
WriteDebugString(printer, field, options, kdoc);
if (!kdoc) WriteDeprecatedJavadoc(printer, field, type, options);
switch (type) {
@ -407,7 +412,7 @@ void WriteFieldStringBytesAccessorDocComment(io::Printer* printer,
const bool builder,
const bool kdoc) {
printer->Print("/**\n");
WriteDocCommentBody(printer, field, kdoc);
WriteDocCommentBody(printer, field, options, kdoc);
WriteDebugString(printer, field, options, kdoc);
if (!kdoc) WriteDeprecatedJavadoc(printer, field, type, options);
switch (type) {
@ -461,9 +466,9 @@ void WriteFieldStringBytesAccessorDocComment(io::Printer* printer,
// Enum
void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_,
const bool kdoc) {
const Options options, const bool kdoc) {
printer->Print("/**\n");
WriteDocCommentBody(printer, enum_, kdoc);
WriteDocCommentBody(printer, enum_, options, kdoc);
if (kdoc) {
printer->Print(
" * Protobuf enum `$fullname$`\n"
@ -478,9 +483,10 @@ void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_,
}
void WriteEnumValueDocComment(io::Printer* printer,
const EnumValueDescriptor* value) {
const EnumValueDescriptor* value,
const Options options) {
printer->Print("/**\n");
WriteDocCommentBody(printer, value, /* kdoc */ false);
WriteDocCommentBody(printer, value, options, /* kdoc */ false);
printer->Print(
" * <code>$def$</code>\n"
@ -489,19 +495,20 @@ void WriteEnumValueDocComment(io::Printer* printer,
}
void WriteServiceDocComment(io::Printer* printer,
const ServiceDescriptor* service) {
const ServiceDescriptor* service,
const Options options) {
printer->Print("/**\n");
WriteDocCommentBody(printer, service, /* kdoc */ false);
WriteDocCommentBody(printer, service, options, /* kdoc */ false);
printer->Print(
" * Protobuf service {@code $fullname$}\n"
" */\n",
"fullname", EscapeJavadoc(service->full_name()));
}
void WriteMethodDocComment(io::Printer* printer,
const MethodDescriptor* method) {
void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method,
const Options options) {
printer->Print("/**\n");
WriteDocCommentBody(printer, method, /* kdoc */ false);
WriteDocCommentBody(printer, method, options, /* kdoc */ false);
printer->Print(
" * <code>$def$</code>\n"
" */\n",

@ -46,34 +46,28 @@ enum FieldAccessorType {
};
void WriteMessageDocComment(io::Printer* printer, const Descriptor* message,
const bool kdoc = false);
Options options, bool kdoc = false);
void WriteFieldDocComment(io::Printer* printer, const FieldDescriptor* field,
Options options, const bool kdoc = false);
Options options, bool kdoc = false);
void WriteFieldAccessorDocComment(io::Printer* printer,
const FieldDescriptor* field,
const FieldAccessorType type, Options options,
const bool builder = false,
const bool kdoc = false);
void WriteFieldEnumValueAccessorDocComment(io::Printer* printer,
const FieldDescriptor* field,
const FieldAccessorType type,
Options options,
const bool builder = false,
const bool kdoc = false);
void WriteFieldStringBytesAccessorDocComment(io::Printer* printer,
const FieldDescriptor* field,
const FieldAccessorType type,
Options options,
const bool builder = false,
const bool kdoc = false);
FieldAccessorType type, Options options,
bool builder = false, bool kdoc = false);
void WriteFieldEnumValueAccessorDocComment(
io::Printer* printer, const FieldDescriptor* field, FieldAccessorType type,
Options options, bool builder = false, bool kdoc = false);
void WriteFieldStringBytesAccessorDocComment(
io::Printer* printer, const FieldDescriptor* field, FieldAccessorType type,
Options options, bool builder = false, bool kdoc = false);
void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enum_,
const bool kdoc = false);
Options options, bool kdoc = false);
void WriteEnumValueDocComment(io::Printer* printer,
const EnumValueDescriptor* value);
const EnumValueDescriptor* value,
Options options);
void WriteServiceDocComment(io::Printer* printer,
const ServiceDescriptor* service);
void WriteMethodDocComment(io::Printer* printer,
const MethodDescriptor* method);
const ServiceDescriptor* service, Options options);
void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method,
Options options);
// Exposed for testing only.
// Also called by proto1-Java code generator.

@ -55,7 +55,7 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor,
EnumGenerator::~EnumGenerator() {}
void EnumGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_);
WriteEnumDocComment(printer, descriptor_, context_->options());
MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
if (!context_->options().opensource_runtime) {
@ -84,7 +84,8 @@ void EnumGenerator::Generate(io::Printer* printer) {
vars["name"] = canonical_values_[i]->name();
vars["index"] = absl::StrCat(canonical_values_[i]->index());
vars["number"] = absl::StrCat(canonical_values_[i]->number());
WriteEnumValueDocComment(printer, canonical_values_[i]);
WriteEnumValueDocComment(printer, canonical_values_[i],
context_->options());
if (canonical_values_[i]->options().deprecated()) {
printer->Print("@java.lang.Deprecated\n");
}
@ -123,7 +124,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
vars["classname"] = descriptor_->name();
vars["name"] = aliases_[i].value->name();
vars["canonical_name"] = aliases_[i].canonical_value->name();
WriteEnumValueDocComment(printer, aliases_[i].value);
WriteEnumValueDocComment(printer, aliases_[i].value, context_->options());
printer->Print(
vars, "public static final $classname$ $name$ = $canonical_name$;\n");
printer->Annotate("name", aliases_[i].value);
@ -138,7 +139,8 @@ void EnumGenerator::Generate(io::Printer* printer) {
vars["deprecation"] = descriptor_->value(i)->options().deprecated()
? "@java.lang.Deprecated "
: "";
WriteEnumValueDocComment(printer, descriptor_->value(i));
WriteEnumValueDocComment(printer, descriptor_->value(i),
context_->options());
printer->Print(vars,
"$deprecation$public static final int ${$$name$_VALUE$}$ = "
"$number$;\n");

@ -52,7 +52,7 @@ EnumLiteGenerator::EnumLiteGenerator(const EnumDescriptor* descriptor,
EnumLiteGenerator::~EnumLiteGenerator() {}
void EnumLiteGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_);
WriteEnumDocComment(printer, descriptor_, context_->options());
MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_);
printer->Print(
"$deprecation$public enum $classname$\n"
@ -66,7 +66,8 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
absl::flat_hash_map<absl::string_view, std::string> vars;
vars["name"] = canonical_values_[i]->name();
vars["number"] = absl::StrCat(canonical_values_[i]->number());
WriteEnumValueDocComment(printer, canonical_values_[i]);
WriteEnumValueDocComment(printer, canonical_values_[i],
context_->options());
if (canonical_values_[i]->options().deprecated()) {
printer->Print("@java.lang.Deprecated\n");
}
@ -90,7 +91,7 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
vars["classname"] = descriptor_->name();
vars["name"] = aliases_[i].value->name();
vars["canonical_name"] = aliases_[i].canonical_value->name();
WriteEnumValueDocComment(printer, aliases_[i].value);
WriteEnumValueDocComment(printer, aliases_[i].value, context_->options());
printer->Print(
vars, "public static final $classname$ $name$ = $canonical_name$;\n");
printer->Annotate("name", aliases_[i].value);
@ -105,7 +106,8 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
vars["deprecation"] = descriptor_->value(i)->options().deprecated()
? "@java.lang.Deprecated "
: "";
WriteEnumValueDocComment(printer, descriptor_->value(i));
WriteEnumValueDocComment(printer, descriptor_->value(i),
context_->options());
printer->Print(vars,
"$deprecation$public static final int ${$$name$_VALUE$}$ = "
"$number$;\n");

@ -295,7 +295,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
variables["deprecation"] =
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";
WriteMessageDocComment(printer, descriptor_);
WriteMessageDocComment(printer, descriptor_, context_->options());
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true);
if (!context_->options().opensource_runtime) {
@ -1273,7 +1273,8 @@ void ImmutableMessageGenerator::GenerateKotlinMembers(
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));
WriteMessageDocComment(printer, descriptor_, /* kdoc */ true);
WriteMessageDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Emit(
{
io::Printer::Sub{"name_kt", absl::StrCat(descriptor_->name(), "Kt")}

@ -88,7 +88,7 @@ MessageBuilderGenerator::MessageBuilderGenerator(const Descriptor* descriptor,
MessageBuilderGenerator::~MessageBuilderGenerator() {}
void MessageBuilderGenerator::Generate(io::Printer* printer) {
WriteMessageDocComment(printer, descriptor_);
WriteMessageDocComment(printer, descriptor_, context_->options());
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"public static final class Builder extends\n"

@ -59,7 +59,7 @@ MessageBuilderLiteGenerator::MessageBuilderLiteGenerator(
MessageBuilderLiteGenerator::~MessageBuilderLiteGenerator() {}
void MessageBuilderLiteGenerator::Generate(io::Printer* printer) {
WriteMessageDocComment(printer, descriptor_);
WriteMessageDocComment(printer, descriptor_, context_->options());
absl::flat_hash_map<absl::string_view, std::string> vars = {
{"{", ""},
{"}", ""},

@ -149,7 +149,7 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
variables["deprecation"] =
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";
WriteMessageDocComment(printer, descriptor_);
WriteMessageDocComment(printer, descriptor_, context_->options());
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true);
@ -807,7 +807,8 @@ void ImmutableMessageLiteGenerator::GenerateKotlinMembers(
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));
WriteMessageDocComment(printer, descriptor_, /* kdoc */ true);
WriteMessageDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Print("public object $name$Kt {\n", "name", descriptor_->name());
printer->Indent();
GenerateKotlinDsl(printer);

@ -43,7 +43,7 @@ ImmutableServiceGenerator::~ImmutableServiceGenerator() {}
void ImmutableServiceGenerator::Generate(io::Printer* printer) {
bool is_own_file = IsOwnFile(descriptor_, /* immutable = */ true);
WriteServiceDocComment(printer, descriptor_);
WriteServiceDocComment(printer, descriptor_, context_->options());
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true);
if (!context_->options().opensource_runtime) {
@ -162,7 +162,7 @@ void ImmutableServiceGenerator::GenerateNewReflectiveBlockingServiceMethod(
void ImmutableServiceGenerator::GenerateAbstractMethods(io::Printer* printer) {
for (int i = 0; i < descriptor_->method_count(); i++) {
const MethodDescriptor* method = descriptor_->method(i);
WriteMethodDocComment(printer, method);
WriteMethodDocComment(printer, method, context_->options());
GenerateMethodSignature(printer, method, IS_ABSTRACT);
printer->Print(";\n\n");
}

Loading…
Cancel
Save