Merge pull request #228 from cconroy/sanitize-enums

Sanitize Enum names from collisions with reserved words.
pull/246/head
Jisi Liu 10 years ago
commit 813d6d652d
  1. 11
      src/google/protobuf/compiler/cpp/cpp_enum.cc
  2. 8
      src/google/protobuf/compiler/cpp/cpp_helpers.cc
  3. 3
      src/google/protobuf/compiler/cpp/cpp_helpers.h
  4. 18
      src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto
  5. 15
      src/google/protobuf/compiler/cpp/cpp_unittest.cc

@ -82,7 +82,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
const EnumValueDescriptor* max_value = descriptor_->value(0); const EnumValueDescriptor* max_value = descriptor_->value(0);
for (int i = 0; i < descriptor_->value_count(); i++) { for (int i = 0; i < descriptor_->value_count(); i++) {
vars["name"] = descriptor_->value(i)->name(); vars["name"] = EnumValueName(descriptor_->value(i));
// In C++, an value of -2147483648 gets interpreted as the negative of // In C++, an value of -2147483648 gets interpreted as the negative of
// 2147483648, and since 2147483648 can't fit in an integer, this produces a // 2147483648, and since 2147483648 can't fit in an integer, this produces a
// compiler warning. This works around that issue. // compiler warning. This works around that issue.
@ -90,6 +90,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
vars["prefix"] = (descriptor_->containing_type() == NULL) ? vars["prefix"] = (descriptor_->containing_type() == NULL) ?
"" : classname_ + "_"; "" : classname_ + "_";
if (i > 0) printer->Print(",\n"); if (i > 0) printer->Print(",\n");
printer->Print(vars, "$prefix$$name$ = $number$"); printer->Print(vars, "$prefix$$name$ = $number$");
@ -113,8 +114,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
printer->Outdent(); printer->Outdent();
printer->Print("\n};\n"); printer->Print("\n};\n");
vars["min_name"] = min_value->name(); vars["min_name"] = EnumValueName(min_value);
vars["max_name"] = max_value->name(); vars["max_name"] = EnumValueName(max_value);
if (options_.dllexport_decl.empty()) { if (options_.dllexport_decl.empty()) {
vars["dllexport"] = ""; vars["dllexport"] = "";
@ -174,7 +175,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) {
printer->Print(vars, "typedef $classname$ $nested_name$;\n"); printer->Print(vars, "typedef $classname$ $nested_name$;\n");
for (int j = 0; j < descriptor_->value_count(); j++) { for (int j = 0; j < descriptor_->value_count(); j++) {
vars["tag"] = descriptor_->value(j)->name(); vars["tag"] = EnumValueName(descriptor_->value(j));
printer->Print(vars, printer->Print(vars,
"static const $nested_name$ $tag$ = $classname$_$tag$;\n"); "static const $nested_name$ $tag$ = $classname$_$tag$;\n");
} }
@ -278,7 +279,7 @@ void EnumGenerator::GenerateMethods(io::Printer* printer) {
vars["parent"] = ClassName(descriptor_->containing_type(), false); vars["parent"] = ClassName(descriptor_->containing_type(), false);
vars["nested_name"] = descriptor_->name(); vars["nested_name"] = descriptor_->name();
for (int i = 0; i < descriptor_->value_count(); i++) { for (int i = 0; i < descriptor_->value_count(); i++) {
vars["value"] = descriptor_->value(i)->name(); vars["value"] = EnumValueName(descriptor_->value(i));
printer->Print(vars, printer->Print(vars,
"const $classname$ $parent$::$value$;\n"); "const $classname$ $parent$::$value$;\n");
} }

@ -176,6 +176,14 @@ string FieldName(const FieldDescriptor* field) {
return result; return result;
} }
string EnumValueName(const EnumValueDescriptor* enum_value) {
string result = enum_value->name();
if (kKeywords.count(result) > 0) {
result.append("_");
}
return result;
}
string FieldConstantName(const FieldDescriptor *field) { string FieldConstantName(const FieldDescriptor *field) {
string field_name = UnderscoresToCamelCase(field->name(), true); string field_name = UnderscoresToCamelCase(field->name(), true);
string result = "k" + field_name + "FieldNumber"; string result = "k" + field_name + "FieldNumber";

@ -74,6 +74,9 @@ string SuperClassName(const Descriptor* descriptor);
// anyway, so normally this just returns field->name(). // anyway, so normally this just returns field->name().
string FieldName(const FieldDescriptor* field); string FieldName(const FieldDescriptor* field);
// Get the sanitized name that should be used for the given enum in C++ code.
string EnumValueName(const EnumValueDescriptor* enum_value);
// Get the unqualified name that should be used for a field's field // Get the unqualified name that should be used for a field's field
// number constant. // number constant.
string FieldConstantName(const FieldDescriptor *field); string FieldConstantName(const FieldDescriptor *field);

@ -131,6 +131,24 @@ message TestConflictingSymbolNamesExtension { // NO_PROTO3
} // NO_PROTO3 } // NO_PROTO3
} // NO_PROTO3 } // NO_PROTO3
message TestConflictingEnumNames {
enum NestedConflictingEnum {
and = 1;
class = 2;
int = 3;
typedef = 4;
XOR = 5;
}
optional NestedConflictingEnum conflicting_enum = 1;
}
enum ConflictingEnum {
NOT_EQ = 1;
volatile = 2;
return = 3;
}
message DummyMessage {} message DummyMessage {}
service TestConflictingMethodNames { service TestConflictingMethodNames {

@ -794,6 +794,21 @@ TEST(GeneratedMessageTest, TestConflictingSymbolNames) {
message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); message.GetExtension(ExtensionMessage::repeated_int32_ext, 0));
} }
TEST(GeneratedMessageTest, TestConflictingEnumNames) {
protobuf_unittest::TestConflictingEnumNames message;
message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_NestedConflictingEnum_and_);
EXPECT_EQ(1, message.conflicting_enum());
message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_NestedConflictingEnum_XOR);
EXPECT_EQ(5, message.conflicting_enum());
protobuf_unittest::ConflictingEnum conflicting_enum;
conflicting_enum = protobuf_unittest::NOT_EQ;
EXPECT_EQ(1, conflicting_enum);
conflicting_enum = protobuf_unittest::return_;
EXPECT_EQ(3, conflicting_enum);
}
#ifndef PROTOBUF_TEST_NO_DESCRIPTORS #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
TEST(GeneratedMessageTest, TestOptimizedForSize) { TEST(GeneratedMessageTest, TestOptimizedForSize) {

Loading…
Cancel
Save