fix: [PHP] add missing reserved classnames (#9458)

* fix: add missing reserved classnames

* Try to remove PARENT and SELF from reserved constants

* add back for tests

* add to validConstantNames

* update kReservedNamesSize
pull/9493/head
Brent Shaffer 3 years ago committed by GitHub
parent dfcbdb8ca0
commit ce57b5bab6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      php/ext/google/protobuf/names.c
  2. 12
      php/src/Google/Protobuf/Internal/GPBUtil.php
  3. 12
      php/tests/GeneratedClassTest.php
  4. 2
      php/tests/proto/test_reserved_enum_lower.proto
  5. 2
      php/tests/proto/test_reserved_enum_upper.proto
  6. 2
      php/tests/proto/test_reserved_enum_value_lower.proto
  7. 2
      php/tests/proto/test_reserved_enum_value_upper.proto
  8. 2
      php/tests/proto/test_reserved_message_lower.proto
  9. 2
      php/tests/proto/test_reserved_message_upper.proto
  10. 43
      src/google/protobuf/compiler/php/php_generator.cc

@ -71,22 +71,23 @@ static void stringsink_uninit(stringsink *sink) { free(sink->ptr); }
/* def name -> classname ******************************************************/
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",
"finally", "fn", "for", "foreach", "function",
"if", "implements", "include", "include_once", "instanceof",
"global", "goto", "insteadof", "interface", "isset",
"list", "match", "namespace", "new", "object",
"or", "print", "private", "protected", "public",
"require", "require_once", "return", "static", "switch",
"throw", "trait", "try", "unset", "use",
"var", "while", "xor", "yield", "int",
"float", "bool", "string", "true", "false",
"null", "void", "iterable", NULL};
"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",
"finally", "fn", "for", "foreach", "function",
"if", "implements", "include", "include_once", "instanceof",
"global", "goto", "insteadof", "interface", "isset",
"list", "match", "namespace", "new", "object",
"or", "parent", "print", "private", "protected",
"public", "require", "require_once", "return", "self",
"static", "switch", "throw", "trait", "try",
"unset", "use", "var", "while", "xor",
"yield", "int", "float", "bool", "string",
"true", "false", "null", "void", "iterable",
NULL};
bool is_reserved_name(const char* name) {
int i;

@ -284,12 +284,12 @@ class GPBUtil
"function"=>0, "global"=>0, "goto"=>0, "if"=>0, "implements"=>0,
"include"=>0, "include_once"=>0, "instanceof"=>0, "insteadof"=>0,
"interface"=>0, "isset"=>0, "list"=>0, "match"=>0, "namespace"=>0,
"new"=>0, "or"=>0, "print"=>0, "private"=>0, "protected"=>0,
"public"=>0, "require"=>0, "require_once"=>0, "return"=>0,
"static"=>0, "switch"=>0, "throw"=>0, "trait"=>0, "try"=>0,
"unset"=>0, "use"=>0, "var"=>0, "while"=>0, "xor"=>0, "yield"=>0,
"int"=>0, "float"=>0, "bool"=>0, "string"=>0, "true"=>0, "false"=>0,
"null"=>0, "void"=>0, "iterable"=>0
"new"=>0, "or"=>0, "parent"=>0, "print"=>0, "private"=>0,
"protected"=>0,"public"=>0, "require"=>0, "require_once"=>0,
"return"=>0, "self"=>0, "static"=>0, "switch"=>0, "throw"=>0,
"trait"=>0, "try"=>0,"unset"=>0, "use"=>0, "var"=>0, "while"=>0,
"xor"=>0, "yield"=>0, "int"=>0, "float"=>0, "bool"=>0, "string"=>0,
"true"=>0, "false"=>0, "null"=>0, "void"=>0, "iterable"=>0
);
if (array_key_exists(strtolower($classname), $reserved_words)) {

@ -934,6 +934,7 @@ class GeneratedClassTest extends TestBase
$m = new \Lower\PBnamespace();
$m = new \Lower\PBnew();
$m = new \Lower\PBor();
$m = new \Lower\PBparent();
$m = new \Lower\PBprint();
$m = new \Lower\PBprivate();
$m = new \Lower\PBprotected();
@ -941,6 +942,7 @@ class GeneratedClassTest extends TestBase
$m = new \Lower\PBrequire();
$m = new \Lower\PBrequire_once();
$m = new \Lower\PBreturn();
$m = new \Lower\PBself();
$m = new \Lower\PBstatic();
$m = new \Lower\PBswitch();
$m = new \Lower\PBthrow();
@ -1012,6 +1014,7 @@ class GeneratedClassTest extends TestBase
$m = new \Upper\PBNAMESPACE();
$m = new \Upper\PBNEW();
$m = new \Upper\PBOR();
$m = new \Upper\PBPARENT();
$m = new \Upper\PBPRINT();
$m = new \Upper\PBPRIVATE();
$m = new \Upper\PBPROTECTED();
@ -1019,6 +1022,7 @@ class GeneratedClassTest extends TestBase
$m = new \Upper\PBREQUIRE();
$m = new \Upper\PBREQUIRE_ONCE();
$m = new \Upper\PBRETURN();
$m = new \Upper\PBSELF();
$m = new \Upper\PBSTATIC();
$m = new \Upper\PBSWITCH();
$m = new \Upper\PBTHROW();
@ -1090,6 +1094,7 @@ class GeneratedClassTest extends TestBase
$m = new \Lower_enum\PBnamespace();
$m = new \Lower_enum\PBnew();
$m = new \Lower_enum\PBor();
$m = new \Lower_enum\PBparent();
$m = new \Lower_enum\PBprint();
$m = new \Lower_enum\PBprivate();
$m = new \Lower_enum\PBprotected();
@ -1097,6 +1102,7 @@ class GeneratedClassTest extends TestBase
$m = new \Lower_enum\PBrequire();
$m = new \Lower_enum\PBrequire_once();
$m = new \Lower_enum\PBreturn();
$m = new \Lower_enum\PBself();
$m = new \Lower_enum\PBstatic();
$m = new \Lower_enum\PBswitch();
$m = new \Lower_enum\PBthrow();
@ -1168,6 +1174,7 @@ class GeneratedClassTest extends TestBase
$m = new \Upper_enum\PBNAMESPACE();
$m = new \Upper_enum\PBNEW();
$m = new \Upper_enum\PBOR();
$m = new \Upper_enum\PBPARENT();
$m = new \Upper_enum\PBPRINT();
$m = new \Upper_enum\PBPRIVATE();
$m = new \Upper_enum\PBPROTECTED();
@ -1175,6 +1182,7 @@ class GeneratedClassTest extends TestBase
$m = new \Upper_enum\PBREQUIRE();
$m = new \Upper_enum\PBREQUIRE_ONCE();
$m = new \Upper_enum\PBRETURN();
$m = new \Upper_enum\PBSELF();
$m = new \Upper_enum\PBSTATIC();
$m = new \Upper_enum\PBSWITCH();
$m = new \Upper_enum\PBTHROW();
@ -1273,6 +1281,8 @@ class GeneratedClassTest extends TestBase
$m = \Lower_enum_value\NotAllowed::null;
$m = \Lower_enum_value\NotAllowed::void;
$m = \Lower_enum_value\NotAllowed::iterable;
$m = \Lower_enum_value\NotAllowed::parent;
$m = \Lower_enum_value\NotAllowed::self;
$m = \Upper_enum_value\NotAllowed::PBABSTRACT;
$m = \Upper_enum_value\NotAllowed::PBAND;
@ -1351,6 +1361,8 @@ class GeneratedClassTest extends TestBase
$m = \Upper_enum_value\NotAllowed::NULL;
$m = \Upper_enum_value\NotAllowed::VOID;
$m = \Upper_enum_value\NotAllowed::ITERABLE;
$m = \Upper_enum_value\NotAllowed::PARENT;
$m = \Upper_enum_value\NotAllowed::SELF;
$this->assertTrue(true);
}

@ -52,6 +52,7 @@ enum match { ZERO47 = 0; }
enum namespace { ZERO48 = 0; }
enum new { ZERO49 = 0; }
enum or { ZERO50 = 0; }
enum parent { ZERO78 = 0; }
enum print { ZERO51 = 0; }
enum private { ZERO52 = 0; }
enum protected { ZERO53 = 0; }
@ -59,6 +60,7 @@ enum public { ZERO54 = 0; }
enum require { ZERO55 = 0; }
enum require_once { ZERO56 = 0; }
enum return { ZERO57 = 0; }
enum self { ZERO79 = 0; }
enum static { ZERO58 = 0; }
enum switch { ZERO59 = 0; }
enum throw { ZERO60 = 0; }

@ -52,6 +52,7 @@ enum MATCH { ZERO47 = 0; }
enum NAMESPACE { ZERO48 = 0; }
enum NEW { ZERO49 = 0; }
enum OR { ZERO50 = 0; }
enum PARENT { ZERO78 = 0; }
enum PRINT { ZERO51 = 0; }
enum PRIVATE { ZERO52 = 0; }
enum PROTECTED { ZERO53 = 0; }
@ -59,6 +60,7 @@ enum PUBLIC { ZERO54 = 0; }
enum REQUIRE { ZERO55 = 0; }
enum REQUIRE_ONCE { ZERO56 = 0; }
enum RETURN { ZERO57 = 0; }
enum SELF { ZERO79 = 0; }
enum STATIC { ZERO58 = 0; }
enum SWITCH { ZERO59 = 0; }
enum THROW { ZERO60 = 0; }

@ -53,6 +53,7 @@ enum NotAllowed {
namespace = 47;
new = 48;
or = 49;
parent = 77;
print = 50;
private = 51;
protected = 52;
@ -60,6 +61,7 @@ enum NotAllowed {
require = 54;
require_once = 55;
return = 56;
self = 78;
static = 57;
switch = 58;
throw = 59;

@ -53,6 +53,7 @@ enum NotAllowed {
NAMESPACE = 47;
NEW = 48;
OR = 49;
PARENT = 77;
PRINT = 50;
PRIVATE = 51;
PROTECTED = 52;
@ -60,6 +61,7 @@ enum NotAllowed {
REQUIRE = 54;
REQUIRE_ONCE = 55;
RETURN = 56;
SELF = 78;
STATIC = 57;
SWITCH = 58;
THROW = 59;

@ -52,6 +52,7 @@ message match {}
message namespace {}
message new {}
message or {}
message parent {}
message print {}
message private {}
message protected {}
@ -59,6 +60,7 @@ message public {}
message require {}
message require_once {}
message return {}
message self {}
message static {}
message switch {}
message throw {}

@ -52,6 +52,7 @@ message MATCH {}
message NAMESPACE {}
message NEW {}
message OR {}
message PARENT {}
message PRINT {}
message PRIVATE {}
message PROTECTED {}
@ -59,6 +60,7 @@ message PUBLIC {}
message REQUIRE {}
message REQUIRE_ONCE {}
message RETURN {}
message SELF {}
message STATIC {}
message SWITCH {}
message THROW {}

@ -48,28 +48,29 @@ const std::string kDescriptorMetadataFile =
const std::string kDescriptorDirName = "Google/Protobuf/Internal";
const std::string kDescriptorPackageName = "Google\\Protobuf\\Internal";
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",
"finally", "fn", "for", "foreach", "function",
"global", "goto", "if", "implements", "include",
"include_once", "instanceof", "insteadof", "interface", "isset",
"list", "match", "namespace", "new", "or",
"print", "private", "protected", "public", "require",
"require_once", "return", "static", "switch", "throw",
"trait", "try", "unset", "use", "var",
"while", "xor", "yield", "int", "float",
"bool", "string", "true", "false", "null",
"void", "iterable"};
"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",
"finally", "fn", "for", "foreach", "function",
"global", "goto", "if", "implements", "include",
"include_once", "instanceof", "insteadof", "interface", "isset",
"list", "match", "namespace", "new", "or",
"parent", "print", "private", "protected", "public",
"require", "require_once", "return", "self", "static",
"switch", "throw", "trait", "try", "unset",
"use", "var", "while", "xor", "yield",
"int", "float", "bool", "string", "true",
"false", "null", "void", "iterable"};
const char* const kValidConstantNames[] = {
"int", "float", "bool", "string", "true",
"false", "null", "void", "iterable",
"false", "null", "void", "iterable", "parent",
"self"
};
const int kReservedNamesSize = 77;
const int kValidConstantNamesSize = 9;
const int kReservedNamesSize = 79;
const int kValidConstantNamesSize = 11;
const int kFieldSetter = 1;
const int kFieldGetter = 2;
const int kFieldProperty = 3;
@ -1114,7 +1115,7 @@ void GenerateAddFilesToPool(const FileDescriptor* file, const Options& options,
std::map<const FileDescriptor*, int> dependency_count;
std::set<const FileDescriptor*> nodes_without_dependency;
FileDescriptorSet sorted_file_set;
AnalyzeDependencyForFile(
file, &nodes_without_dependency, &deps, &dependency_count);
@ -2065,7 +2066,7 @@ void GenerateCMessage(const Descriptor* message, io::Printer* printer) {
break;
default:
break;
}
}
printer->Print(
" ZEND_FE_END\n"

Loading…
Cancel
Save