From 4c57e8475f78ccac80407f03c2d23d30014785f9 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 20 Apr 2017 01:19:03 -0700 Subject: Prepend "PB" to generated classes whose name are reserved words. (#2990) --- src/google/protobuf/compiler/php/php_generator.cc | 35 +++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index 32f40b2e..d24e1e5e 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -49,6 +49,8 @@ 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[] = {"Empty"}; +const int kReservedNamesSize = 1; namespace google { namespace protobuf { @@ -105,14 +107,31 @@ std::string EnumFullName(const EnumDescriptor* envm, bool is_descriptor) { } template -std::string ClassNamePrefix(const DescriptorType* desc) { - // Empty cannot be php class name. - if (desc->name() == "Empty" && - desc->file()->package() == "google.protobuf") { - return "GPB"; - } else { - return (desc->file()->options()).php_class_prefix(); +std::string ClassNamePrefix(const string& classname, + const DescriptorType* desc) { + const string& prefix = (desc->file()->options()).php_class_prefix(); + if (prefix != "") { + return prefix; + } + + bool is_reserved = false; + + for (int i = 0; i < kReservedNamesSize; i++) { + if (classname == kReservedNames[i]) { + is_reserved = true; + break; + } } + + if (is_reserved) { + if (desc->file()->package() == "google.protobuf") { + return "GPB"; + } else { + return "PB"; + } + } + + return ""; } @@ -124,7 +143,7 @@ std::string FullClassName(const DescriptorType* desc, bool is_descriptor) { classname = containing->name() + '_' + classname; containing = containing->containing_type(); } - classname = ClassNamePrefix(desc) + classname; + classname = ClassNamePrefix(classname, desc) + classname; if (desc->file()->package() == "") { return classname; -- cgit v1.2.3