aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2017-04-20 01:19:03 -0700
committerGravatar GitHub <noreply@github.com>2017-04-20 01:19:03 -0700
commit4c57e8475f78ccac80407f03c2d23d30014785f9 (patch)
treefa9c6ca31796db77c3367acac83233a44938e117 /src/google
parentb97cd573e405dd511b09a9fae124427a29741395 (diff)
Prepend "PB" to generated classes whose name are reserved words. (#2990)
Diffstat (limited to 'src/google')
-rw-r--r--src/google/protobuf/compiler/php/php_generator.cc35
1 files changed, 27 insertions, 8 deletions
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 <typename DescriptorType>
-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;