Internal changes

PiperOrigin-RevId: 615051925
pull/16126/head
Mike Kruskal 9 months ago committed by Copybara-Service
parent d2b71b347d
commit ea3fe6389e
  1. 17
      src/google/protobuf/compiler/csharp/csharp_doc_comment.cc
  2. 18
      src/google/protobuf/compiler/csharp/csharp_doc_comment.h
  3. 58
      src/google/protobuf/compiler/csharp/csharp_enum.cc
  4. 2
      src/google/protobuf/compiler/csharp/csharp_enum_field.cc
  5. 2
      src/google/protobuf/compiler/csharp/csharp_map_field.cc
  6. 2
      src/google/protobuf/compiler/csharp/csharp_message.cc
  7. 6
      src/google/protobuf/compiler/csharp/csharp_message_field.cc
  8. 6
      src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
  9. 4
      src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc
  10. 4
      src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc
  11. 4
      src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc
  12. 6
      src/google/protobuf/compiler/csharp/csharp_wrapper_field.cc
  13. 12
      src/google/protobuf/editions/codegen_tests/BUILD
  14. 33
      src/google/protobuf/editions/codegen_tests/proto2_inline_comments.proto
  15. 33
      src/google/protobuf/editions/codegen_tests/proto2_multiline_comments.proto

@ -12,6 +12,7 @@
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "google/protobuf/compiler/csharp/csharp_options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
@ -79,26 +80,32 @@ static void WriteDocCommentBody(io::Printer* printer,
}
}
void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
void WriteMessageDocComment(io::Printer* printer, const Options* options,
const Descriptor* message) {
if (options->strip_nonfunctional_codegen) return;
WriteDocCommentBody(printer, message);
}
void WritePropertyDocComment(io::Printer* printer,
void WritePropertyDocComment(io::Printer* printer, const Options* options,
const FieldDescriptor* field) {
if (options->strip_nonfunctional_codegen) return;
WriteDocCommentBody(printer, field);
}
void WriteEnumDocComment(io::Printer* printer,
void WriteEnumDocComment(io::Printer* printer, const Options* options,
const EnumDescriptor* enumDescriptor) {
if (options->strip_nonfunctional_codegen) return;
WriteDocCommentBody(printer, enumDescriptor);
}
void WriteEnumValueDocComment(io::Printer* printer,
void WriteEnumValueDocComment(io::Printer* printer, const Options* options,
const EnumValueDescriptor* value) {
if (options->strip_nonfunctional_codegen) return;
WriteDocCommentBody(printer, value);
}
void WriteMethodDocComment(io::Printer* printer,
void WriteMethodDocComment(io::Printer* printer, const Options* options,
const MethodDescriptor* method) {
if (options->strip_nonfunctional_codegen) return;
WriteDocCommentBody(printer, method);
}

@ -9,18 +9,24 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_DOC_COMMENT_H__
#define GOOGLE_PROTOBUF_COMPILER_CSHARP_DOC_COMMENT_H__
#include "google/protobuf/io/printer.h"
#include "google/protobuf/compiler/csharp/csharp_options.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace csharp {
void WriteMessageDocComment(io::Printer* printer, const Descriptor* message);
void WritePropertyDocComment(io::Printer* printer, const FieldDescriptor* field);
void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enumDescriptor);
void WriteEnumValueDocComment(io::Printer* printer, const EnumValueDescriptor* value);
void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method);
void WriteMessageDocComment(io::Printer* printer, const Options* options,
const Descriptor* message);
void WritePropertyDocComment(io::Printer* printer, const Options* options,
const FieldDescriptor* field);
void WriteEnumDocComment(io::Printer* printer, const Options* options,
const EnumDescriptor* enumDescriptor);
void WriteEnumValueDocComment(io::Printer* printer, const Options* options,
const EnumValueDescriptor* value);
void WriteMethodDocComment(io::Printer* printer, const Options* options,
const MethodDescriptor* method);
} // namespace csharp
} // namespace compiler
} // namespace protobuf

@ -35,7 +35,7 @@ EnumGenerator::~EnumGenerator() {
}
void EnumGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_);
WriteEnumDocComment(printer, options(), descriptor_);
if (descriptor_->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n");
}
@ -46,33 +46,35 @@ void EnumGenerator::Generate(io::Printer* printer) {
absl::flat_hash_set<std::string> used_names;
absl::flat_hash_set<int> used_number;
for (int i = 0; i < descriptor_->value_count(); i++) {
WriteEnumValueDocComment(printer, descriptor_->value(i));
if (descriptor_->value(i)->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n");
}
std::string original_name = descriptor_->value(i)->name();
std::string name =
GetEnumValueName(descriptor_->name(), descriptor_->value(i)->name());
// Make sure we don't get any duplicate names due to prefix removal.
while (!used_names.insert(name).second) {
// It's possible we'll end up giving this warning multiple times, but that's better than not at all.
ABSL_LOG(WARNING) << "Duplicate enum value " << name << " (originally "
<< original_name << ") in " << descriptor_->name()
<< "; adding underscore to distinguish";
absl::StrAppend(&name, "_");
}
int number = descriptor_->value(i)->number();
if (!used_number.insert(number).second) {
printer->Print("[pbr::OriginalName(\"$original_name$\", PreferredAlias = false)] $name$ = $number$,\n",
"original_name", original_name,
"name", name,
"number", absl::StrCat(number));
} else {
printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n",
"original_name", original_name,
"name", name,
"number", absl::StrCat(number));
}
WriteEnumValueDocComment(printer, options(), descriptor_->value(i));
if (descriptor_->value(i)->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n");
}
std::string original_name = descriptor_->value(i)->name();
std::string name =
GetEnumValueName(descriptor_->name(), descriptor_->value(i)->name());
// Make sure we don't get any duplicate names due to prefix removal.
while (!used_names.insert(name).second) {
// It's possible we'll end up giving this warning multiple times, but
// that's better than not at all.
ABSL_LOG(WARNING) << "Duplicate enum value " << name << " (originally "
<< original_name << ") in " << descriptor_->name()
<< "; adding underscore to distinguish";
absl::StrAppend(&name, "_");
}
int number = descriptor_->value(i)->number();
if (!used_number.insert(number).second) {
printer->Print(
"[pbr::OriginalName(\"$original_name$\", PreferredAlias = false)] "
"$name$ = $number$,\n",
"original_name", original_name, "name", name, "number",
absl::StrCat(number));
} else {
printer->Print(
"[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n",
"original_name", original_name, "name", name, "number",
absl::StrCat(number));
}
}
printer->Outdent();
printer->Print("}\n");

@ -59,7 +59,7 @@ void EnumFieldGenerator::GenerateCodecCode(io::Printer* printer) {
}
void EnumFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,

@ -53,7 +53,7 @@ void MapFieldGenerator::GenerateMembers(io::Printer* printer) {
variables_,
", $tag$);\n"
"private readonly pbc::MapField<$key_type_name$, $value_type_name$> $name$_ = new pbc::MapField<$key_type_name$, $value_type_name$>();\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,

@ -94,7 +94,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
vars["class_name"] = class_name();
vars["access_level"] = class_access_level();
WriteMessageDocComment(printer, descriptor_);
WriteMessageDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
AddSerializableAttribute(printer);

@ -40,7 +40,7 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer) {
printer->Print(
variables_,
"private $type_name$ $name$_;\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
@ -148,7 +148,7 @@ void MessageFieldGenerator::WriteToString(io::Printer* printer) {
"PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n");
}
void MessageFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,
@ -190,7 +190,7 @@ MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {
}
void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,

@ -73,7 +73,7 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
variables_,
"private $type_name$ $name_def_message$;\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
// Most of the work is done in the property:
@ -239,7 +239,7 @@ void PrimitiveFieldGenerator::GenerateCodecCode(io::Printer* printer) {
}
void PrimitiveFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,
@ -259,7 +259,7 @@ PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
}
void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,

@ -39,7 +39,7 @@ void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) {
" = pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x);\n");
printer->Print(variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
@ -107,7 +107,7 @@ void RepeatedEnumFieldGenerator::GenerateCloningCode(io::Printer* printer) {
}
void RepeatedEnumFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,

@ -54,7 +54,7 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
printer->Print(
variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
@ -127,7 +127,7 @@ void RepeatedMessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) {
}
void RepeatedMessageFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,

@ -39,7 +39,7 @@ void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
" = pb::FieldCodec.For$capitalized_type_name$($tag$);\n");
printer->Print(variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
@ -108,7 +108,7 @@ void RepeatedPrimitiveFieldGenerator::GenerateFreezingCode(io::Printer* printer)
}
void RepeatedPrimitiveFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,

@ -48,7 +48,7 @@ void WrapperFieldGenerator::GenerateMembers(io::Printer* printer) {
variables_,
";\n"
"private $type_name$ $name$_;\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
@ -176,7 +176,7 @@ void WrapperFieldGenerator::GenerateCodecCode(io::Printer* printer) {
}
void WrapperFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddDeprecatedFlag(printer);
printer->Print(
variables_,
@ -202,7 +202,7 @@ void WrapperOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
"private static readonly pb::FieldCodec<$type_name$> _oneof_$name$_codec = ");
GenerateCodecCode(printer);
printer->Print(";\n");
WritePropertyDocComment(printer, descriptor_);
WritePropertyDocComment(printer, options(), descriptor_);
AddPublicMemberAttributes(printer);
printer->Print(
variables_,

@ -7,6 +7,18 @@ exports_files(
],
)
proto_library(
name = "proto2_inline_comments_proto",
srcs = ["proto2_inline_comments.proto"],
strip_import_prefix = "/src",
)
proto_library(
name = "proto2_multiline_comments_proto",
srcs = ["proto2_multiline_comments.proto"],
strip_import_prefix = "/src",
)
proto_library(
name = "proto2_required_proto",
srcs = ["proto2_required.proto"],

@ -0,0 +1,33 @@
// This is a detached leading comment
//
// With a forced unwrapped line.
// File detached leading comment
// Syntax leading comment
syntax = "proto2"; // Syntax trailing comment
// Package leading comment
package protobuf_editions_test.proto2; // Package trailing comment
// Leading message comment
message Foo { // Message trailing comment
// Message inner comment
// Field leading comment
optional int32 field1 = 1; // Field trailing comment
optional /* card */ int32 /* type */ field2 /* name */ = 2 /* tag */;
// Message inner trailing comment
} // Message trailing comment
// Leading message comment
enum Bar { // Enum trailing comment
// Enum inner comment
// Enum value leading comment
BAR_UNKNOWN = 0; // Enum value trailing comment
// Enum inner trailing comment
} // Enum trailing comment

@ -0,0 +1,33 @@
syntax = "proto2";
package protobuf_editions_test.proto2;
/**
Multiline message comment - no asterisk
*/
message Message1 {
/**
Multiline field comment - no asterisk
*/
optional string field = 1;
}
/*
* Multiline message comment - single asterisk
*/
message Message2 {
/*
* Multiline message comment - single asterisk
*/
optional string field = 1;
}
/**
* Exactly one trait must be set. Extension # is vendor_id + 1.
*/
message Message3 {
/**
* Exactly one trait must be set. Extension # is vendor_id + 1.
*/
optional string field = 1;
}
Loading…
Cancel
Save