diff options
Diffstat (limited to 'src/google')
-rw-r--r-- | src/google/protobuf/compiler/php/php_generator.cc | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index fe781df2..b73062de 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -65,7 +65,12 @@ const char* const kReservedNames[] = { "use", "var", "while", "xor", "int", "float", "bool", "string", "true", "false", "null", "void", "iterable"}; +const char* const kValidConstantNames[] = { + "int", "float", "bool", "string", "true", + "false", "null", "void", "iterable", +}; const int kReservedNamesSize = 73; +const int kValidConstantNamesSize = 9; const int kFieldSetter = 1; const int kFieldGetter = 2; const int kFieldProperty = 3; @@ -161,6 +166,34 @@ std::string ClassNamePrefix(const string& classname, return ""; } +std::string ClassNamePrefix(const string& classname, + const EnumValueDescriptor* desc) { + bool is_reserved = false; + + string lower = classname; + transform(lower.begin(), lower.end(), lower.begin(), ::tolower); + + for (int i = 0; i < kReservedNamesSize; i++) { + if (lower == kReservedNames[i]) { + is_reserved = true; + break; + } + } + + for (int i = 0; i < kValidConstantNamesSize; i++) { + if (lower == kValidConstantNames[i]) { + is_reserved = false; + break; + } + } + + if (is_reserved) { + return "PB"; + } + + return ""; +} + template <typename DescriptorType> std::string NamespacedName(const string& classname, const DescriptorType* desc, bool is_descriptor) { @@ -696,7 +729,7 @@ void GenerateEnumToPool(const EnumDescriptor* en, io::Printer* printer) { const EnumValueDescriptor* value = en->value(i); printer->Print( "->value(\"^name^\", ^number^)\n", - "name", ClassNamePrefix(value->name(), en) + value->name(), + "name", ClassNamePrefix(value->name(), value) + value->name(), "number", IntToString(value->number())); } printer->Print("->finalizeToPool();\n\n"); @@ -999,7 +1032,7 @@ void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en, const EnumValueDescriptor* value = en->value(i); GenerateEnumValueDocComment(&printer, value); printer.Print("const ^name^ = ^number^;\n", - "name", ClassNamePrefix(value->name(), en) + value->name(), + "name", ClassNamePrefix(value->name(), value) + value->name(), "number", IntToString(value->number())); } |