From 606cb7ed2c55d5bda8aad2c88e5b0af09230fb3b Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Tue, 28 Feb 2017 10:51:19 -0800 Subject: There might be duplicated enum values when allow_alias is true. Add PreferredAlias into OriginalNameAttribute to remove the duplication (#2727) --- src/google/protobuf/compiler/csharp/csharp_enum.cc | 17 +++++++++++++---- src/google/protobuf/unittest_proto3.proto | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/compiler/csharp/csharp_enum.cc b/src/google/protobuf/compiler/csharp/csharp_enum.cc index 9e4da1ed..9759e3ef 100644 --- a/src/google/protobuf/compiler/csharp/csharp_enum.cc +++ b/src/google/protobuf/compiler/csharp/csharp_enum.cc @@ -65,6 +65,7 @@ void EnumGenerator::Generate(io::Printer* printer) { "name", descriptor_->name()); printer->Indent(); std::set used_names; + std::set used_number; for (int i = 0; i < descriptor_->value_count(); i++) { WriteEnumValueDocComment(printer, descriptor_->value(i)); string original_name = descriptor_->value(i)->name(); @@ -76,10 +77,18 @@ void EnumGenerator::Generate(io::Printer* printer) { << ") in " << descriptor_->name() << "; adding underscore to distinguish"; name += "_"; } - printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n", - "original_name", original_name, - "name", name, - "number", SimpleItoa(descriptor_->value(i)->number())); + 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", SimpleItoa(number)); + } else { + printer->Print("[pbr::OriginalName(\"$original_name$\")] $name$ = $number$,\n", + "original_name", original_name, + "name", name, + "number", SimpleItoa(number)); + } } printer->Outdent(); printer->Print("}\n"); diff --git a/src/google/protobuf/unittest_proto3.proto b/src/google/protobuf/unittest_proto3.proto index f59d2178..a27b1b26 100644 --- a/src/google/protobuf/unittest_proto3.proto +++ b/src/google/protobuf/unittest_proto3.proto @@ -200,6 +200,9 @@ message TestMutualRecursionB { int32 optional_int32 = 2; } +message TestEnumAllowAlias { + TestEnumWithDupValue value = 1; +} // Test an enum that has multiple values with the same number. enum TestEnumWithDupValue { -- cgit v1.2.3