diff options
author | liujisi@google.com <liujisi@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2012-12-28 23:41:54 +0000 |
---|---|---|
committer | liujisi@google.com <liujisi@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2012-12-28 23:41:54 +0000 |
commit | cb77c4c381ea36e1de8ee947b564424c573fa279 (patch) | |
tree | 8b5d6db5cf5beddb5845dfd0c119ef5166f57f0e /src | |
parent | 4770277ec7a285f0f96e9a88af9d4992b7f4b50b (diff) | |
download | protobuf-cb77c4c381ea36e1de8ee947b564424c573fa279.tar.gz protobuf-cb77c4c381ea36e1de8ee947b564424c573fa279.tar.bz2 protobuf-cb77c4c381ea36e1de8ee947b564424c573fa279.zip |
Generate a warning for duplicated enum values, when allow_alias option isn't
set.
Diffstat (limited to 'src')
-rw-r--r-- | src/google/protobuf/descriptor.cc | 22 | ||||
-rw-r--r-- | src/google/protobuf/descriptor_unittest.cc | 4 | ||||
-rw-r--r-- | src/google/protobuf/unittest.proto | 1 |
3 files changed, 20 insertions, 7 deletions
diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index f808b833..c941aacc 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -4132,16 +4132,26 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm, const EnumDescriptorProto& proto) { VALIDATE_OPTIONS_FROM_ARRAY(enm, value, EnumValue); - if (!enm->options().allow_alias()) { + if (!enm->options().has_allow_alias() || !enm->options().allow_alias()) { map<int, string> used_values; for (int i = 0; i < enm->value_count(); ++i) { const EnumValueDescriptor* enum_value = enm->value(i); if (used_values.find(enum_value->number()) != used_values.end()) { - AddError(enm->full_name(), proto, - DescriptorPool::ErrorCollector::NUMBER, - "\"" + enum_value->full_name() + - "\" uses the same enum value as \"" + - used_values[enum_value->number()] + "\""); + string error = + "\"" + enum_value->full_name() + + "\" uses the same enum value as \"" + + used_values[enum_value->number()] + "\". If this is intended, set " + "'option allow_alias = true;' to the enum definition."; + if (!enm->options().allow_alias()) { + // Generate error if duplicated enum values are explicitly disallowed. + AddError(enm->full_name(), proto, + DescriptorPool::ErrorCollector::NUMBER, + error); + } else { + // Generate warning if duplicated values are found but the option + // isn't set. + GOOGLE_LOG(ERROR) << error; + } } else { used_values[enum_value->number()] = enum_value->full_name(); } diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc index 07f968a7..86e6a49d 100644 --- a/src/google/protobuf/descriptor_unittest.cc +++ b/src/google/protobuf/descriptor_unittest.cc @@ -4001,7 +4001,9 @@ TEST_F(ValidationErrorTest, DisallowEnumAlias) { " options { allow_alias: false }" "}", "foo.proto: Bar: NUMBER: " - "\"ENUM_B\" uses the same enum value as \"ENUM_A\"\n"); + "\"ENUM_B\" uses the same enum value as \"ENUM_A\". " + "If this is intended, set 'option allow_alias = true;' to the enum " + "definition.\n"); } // =================================================================== diff --git a/src/google/protobuf/unittest.proto b/src/google/protobuf/unittest.proto index 0305be65..6eb2d86f 100644 --- a/src/google/protobuf/unittest.proto +++ b/src/google/protobuf/unittest.proto @@ -434,6 +434,7 @@ message TestNestedMessageHasBits { // Test an enum that has multiple values with the same number. enum TestEnumWithDupValue { + option allow_alias = true; FOO1 = 1; BAR1 = 2; BAZ = 3; |