Skip to content

Commit

Permalink
fix: [PHP] add missing reserved classnames (#9458)
Browse files Browse the repository at this point in the history
* fix: add missing reserved classnames

* Try to remove PARENT and SELF from reserved constants

* add back for tests

* add to validConstantNames

* update kReservedNamesSize
  • Loading branch information
bshaffer committed Feb 10, 2022
1 parent dfcbdb8 commit ce57b5b
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 43 deletions.
33 changes: 17 additions & 16 deletions php/ext/google/protobuf/names.c
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions php/src/Google/Protobuf/Internal/GPBUtil.php
Expand Up @@ -284,12 +284,12 @@ public static function getClassNamePrefix(
"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)) {
Expand Down
12 changes: 12 additions & 0 deletions php/tests/GeneratedClassTest.php
Expand Up @@ -934,13 +934,15 @@ public function testPrefixForReservedWords()
$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();
$m = new \Lower\PBpublic();
$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();
Expand Down Expand Up @@ -1012,13 +1014,15 @@ public function testPrefixForReservedWords()
$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();
$m = new \Upper\PBPUBLIC();
$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();
Expand Down Expand Up @@ -1090,13 +1094,15 @@ public function testPrefixForReservedWords()
$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();
$m = new \Lower_enum\PBpublic();
$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();
Expand Down Expand Up @@ -1168,13 +1174,15 @@ public function testPrefixForReservedWords()
$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();
$m = new \Upper_enum\PBPUBLIC();
$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();
Expand Down Expand Up @@ -1273,6 +1281,8 @@ public function testPrefixForReservedWords()
$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;
Expand Down Expand Up @@ -1351,6 +1361,8 @@ public function testPrefixForReservedWords()
$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);
}
Expand Down
2 changes: 2 additions & 0 deletions php/tests/proto/test_reserved_enum_lower.proto
Expand Up @@ -52,13 +52,15 @@ 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; }
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; }
Expand Down
2 changes: 2 additions & 0 deletions php/tests/proto/test_reserved_enum_upper.proto
Expand Up @@ -52,13 +52,15 @@ 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; }
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; }
Expand Down
2 changes: 2 additions & 0 deletions php/tests/proto/test_reserved_enum_value_lower.proto
Expand Up @@ -53,13 +53,15 @@ enum NotAllowed {
namespace = 47;
new = 48;
or = 49;
parent = 77;
print = 50;
private = 51;
protected = 52;
public = 53;
require = 54;
require_once = 55;
return = 56;
self = 78;
static = 57;
switch = 58;
throw = 59;
Expand Down
2 changes: 2 additions & 0 deletions php/tests/proto/test_reserved_enum_value_upper.proto
Expand Up @@ -53,13 +53,15 @@ enum NotAllowed {
NAMESPACE = 47;
NEW = 48;
OR = 49;
PARENT = 77;
PRINT = 50;
PRIVATE = 51;
PROTECTED = 52;
PUBLIC = 53;
REQUIRE = 54;
REQUIRE_ONCE = 55;
RETURN = 56;
SELF = 78;
STATIC = 57;
SWITCH = 58;
THROW = 59;
Expand Down
2 changes: 2 additions & 0 deletions php/tests/proto/test_reserved_message_lower.proto
Expand Up @@ -52,13 +52,15 @@ message match {}
message namespace {}
message new {}
message or {}
message parent {}
message print {}
message private {}
message protected {}
message public {}
message require {}
message require_once {}
message return {}
message self {}
message static {}
message switch {}
message throw {}
Expand Down
2 changes: 2 additions & 0 deletions php/tests/proto/test_reserved_message_upper.proto
Expand Up @@ -52,13 +52,15 @@ message MATCH {}
message NAMESPACE {}
message NEW {}
message OR {}
message PARENT {}
message PRINT {}
message PRIVATE {}
message PROTECTED {}
message PUBLIC {}
message REQUIRE {}
message REQUIRE_ONCE {}
message RETURN {}
message SELF {}
message STATIC {}
message SWITCH {}
message THROW {}
Expand Down
43 changes: 22 additions & 21 deletions src/google/protobuf/compiler/php/php_generator.cc
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -2065,7 +2066,7 @@ void GenerateCMessage(const Descriptor* message, io::Printer* printer) {
break;
default:
break;
}
}

printer->Print(
" ZEND_FE_END\n"
Expand Down

0 comments on commit ce57b5b

Please sign in to comment.