diff options
author | Paul Yang <TeBoring@users.noreply.github.com> | 2017-09-15 09:20:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-15 09:20:23 -0700 |
commit | 5dd818cd2226fe8e3818206c47d77b2d9571509a (patch) | |
tree | b169803ed9125d27b7fdd41ced20bcb2ae338937 /src/google | |
parent | b04e5cba356212e4e8c66c61bbe0c3a20537c5b9 (diff) | |
parent | a425dd97f210cd4bab979d7aad980655a7409bfe (diff) | |
download | protobuf-5dd818cd2226fe8e3818206c47d77b2d9571509a.tar.gz protobuf-5dd818cd2226fe8e3818206c47d77b2d9571509a.tar.bz2 protobuf-5dd818cd2226fe8e3818206c47d77b2d9571509a.zip |
Merge pull request #3612 from TeBoring/php-bug
Add prefix to php reserved keywords.
Diffstat (limited to 'src/google')
-rw-r--r-- | src/google/protobuf/compiler/php/php_generator.cc | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index 60e6fce9..e4364ac2 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -49,8 +49,28 @@ const std::string kDescriptorMetadataFile = "GPBMetadata/Google/Protobuf/Internal/Descriptor.php"; const std::string kDescriptorDirName = "Google/Protobuf/Internal"; const std::string kDescriptorPackageName = "Google\\Protobuf\\Internal"; -const char* const kReservedNames[] = {"ARRAY", "Empty", "ECHO"}; -const int kReservedNamesSize = 3; +const char* const kReservedNames[] = { + "abstract", "and", "array", "as", "break", + "callable", "case", "catch", "class", "clone", + "const", "continue", "declare", "default", "die", + "do", "echo", "else", "elseif", "empty", + "enddeclare", "endfor", "endforeach", "endif", "endswitch", + "endwhile", "eval", "exit", "extends", "final", + "for", "foreach", "function", "global", "goto", + "if", "implements", "include", "include_once", "instanceof", + "insteadof", "interface", "isset", "list", "namespace", + "new", "or", "print", "private", "protected", + "public", "require", "require_once", "return", "static", + "switch", "throw", "trait", "try", "unset", + "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; @@ -125,8 +145,11 @@ std::string ClassNamePrefix(const string& classname, bool is_reserved = false; + string lower = classname; + transform(lower.begin(), lower.end(), lower.begin(), ::tolower); + for (int i = 0; i < kReservedNamesSize; i++) { - if (classname == kReservedNames[i]) { + if (lower == kReservedNames[i]) { is_reserved = true; break; } @@ -143,6 +166,33 @@ std::string ClassNamePrefix(const string& classname, return ""; } +std::string ConstantNamePrefix(const string& classname) { + 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) { @@ -678,7 +728,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", ConstantNamePrefix(value->name()) + value->name(), "number", IntToString(value->number())); } printer->Print("->finalizeToPool();\n\n"); @@ -981,7 +1031,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", ConstantNamePrefix(value->name()) + value->name(), "number", IntToString(value->number())); } |