From e5d000cbb7bcf8fadddfbdb544070e31ce36fa26 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Fri, 8 Sep 2017 15:11:49 -0700 Subject: Add prefix to php reserved keywords. --- php/ext/google/protobuf/def.c | 30 ++- php/src/Google/Protobuf/Internal/GPBUtil.php | 18 +- php/tests/generated_class_test.php | 222 ++++++++++++++++++++++ php/tests/proto/test_reserved_enum_lower.proto | 79 ++++++++ php/tests/proto/test_reserved_enum_upper.proto | 79 ++++++++ php/tests/proto/test_reserved_message_lower.proto | 77 ++++++++ php/tests/proto/test_reserved_message_upper.proto | 77 ++++++++ 7 files changed, 577 insertions(+), 5 deletions(-) create mode 100644 php/tests/proto/test_reserved_enum_lower.proto create mode 100644 php/tests/proto/test_reserved_enum_upper.proto create mode 100644 php/tests/proto/test_reserved_message_lower.proto create mode 100644 php/tests/proto/test_reserved_message_upper.proto (limited to 'php') diff --git a/php/ext/google/protobuf/def.c b/php/ext/google/protobuf/def.c index f885c145..d615cb3c 100644 --- a/php/ext/google/protobuf/def.c +++ b/php/ext/google/protobuf/def.c @@ -30,8 +30,23 @@ #include "protobuf.h" -const char* const kReservedNames[] = {"Empty", "ECHO", "ARRAY"}; -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 int kReservedNamesSize = 73; // Forward declare. static void descriptor_init_c_instance(Descriptor* intern TSRMLS_DC); @@ -774,12 +789,21 @@ static const char *classname_prefix(const char *classname, return prefix_given; } + char* lower = ALLOC_N(char, strlen(classname) + 1); + i = 0; + while(classname[i]) { + lower[i] = (char)tolower(classname[i]); + i++; + } + lower[i] = 0; + for (i = 0; i < kReservedNamesSize; i++) { - if (strcmp(kReservedNames[i], classname) == 0) { + if (strcmp(kReservedNames[i], lower) == 0) { is_reserved = true; break; } } + FREE(lower); if (is_reserved) { if (package_name != NULL && strcmp("google.protobuf", package_name) == 0) { diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php index 84e8ecf0..659a4ee3 100644 --- a/php/src/Google/Protobuf/Internal/GPBUtil.php +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -252,9 +252,23 @@ class GPBUtil return $prefix; } - $reserved_words = array("Empty", "ECHO", "ARRAY"); + $reserved_words = array( + "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" + + ); foreach ($reserved_words as $reserved_word) { - if ($classname === $reserved_word) { + if (strtolower($classname) === $reserved_word) { if ($file_proto->getPackage() === "google.protobuf") { return "GPB"; } else { diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php index 98918bce..c63318a9 100644 --- a/php/tests/generated_class_test.php +++ b/php/tests/generated_class_test.php @@ -691,6 +691,228 @@ class GeneratedClassTest extends TestBase $m = new \Foo\PBEmpty(); $m = new \PrefixEmpty(); $m = new \Foo\PBARRAY(); + + $m = new \Lower\PBabstract(); + $m = new \Lower\PBand(); + $m = new \Lower\PBarray(); + $m = new \Lower\PBas(); + $m = new \Lower\PBbreak(); + $m = new \Lower\PBcallable(); + $m = new \Lower\PBcase(); + $m = new \Lower\PBcatch(); + $m = new \Lower\PBclass(); + $m = new \Lower\PBclone(); + $m = new \Lower\PBconst(); + $m = new \Lower\PBcontinue(); + $m = new \Lower\PBdeclare(); + $m = new \Lower\PBdefault(); + $m = new \Lower\PBdie(); + $m = new \Lower\PBdo(); + $m = new \Lower\PBecho(); + $m = new \Lower\PBelse(); + $m = new \Lower\PBelseif(); + $m = new \Lower\PBempty(); + $m = new \Lower\PBenddeclare(); + $m = new \Lower\PBendfor(); + $m = new \Lower\PBendforeach(); + $m = new \Lower\PBendif(); + $m = new \Lower\PBendswitch(); + $m = new \Lower\PBendwhile(); + $m = new \Lower\PBeval(); + $m = new \Lower\PBexit(); + $m = new \Lower\PBextends(); + $m = new \Lower\PBfinal(); + $m = new \Lower\PBfor(); + $m = new \Lower\PBforeach(); + $m = new \Lower\PBfunction(); + $m = new \Lower\PBglobal(); + $m = new \Lower\PBgoto(); + $m = new \Lower\PBif(); + $m = new \Lower\PBimplements(); + $m = new \Lower\PBinclude(); + $m = new \Lower\PBinclude_once(); + $m = new \Lower\PBinstanceof(); + $m = new \Lower\PBinsteadof(); + $m = new \Lower\PBinterface(); + $m = new \Lower\PBisset(); + $m = new \Lower\PBlist(); + $m = new \Lower\PBnamespace(); + $m = new \Lower\PBnew(); + $m = new \Lower\PBor(); + $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\PBstatic(); + $m = new \Lower\PBswitch(); + $m = new \Lower\PBthrow(); + $m = new \Lower\PBtrait(); + $m = new \Lower\PBtry(); + $m = new \Lower\PBunset(); + $m = new \Lower\PBuse(); + $m = new \Lower\PBvar(); + $m = new \Lower\PBwhile(); + $m = new \Lower\PBxor(); + $m = new \Lower\PBint(); + $m = new \Lower\PBfloat(); + $m = new \Lower\PBbool(); + $m = new \Lower\PBstring(); + $m = new \Lower\PBtrue(); + $m = new \Lower\PBfalse(); + $m = new \Lower\PBnull(); + $m = new \Lower\PBvoid(); + $m = new \Lower\PBiterable(); + + $m = new \Upper\PBABSTRACT(); + $m = new \Upper\PBAND(); + $m = new \Upper\PBARRAY(); + $m = new \Upper\PBAS(); + $m = new \Upper\PBBREAK(); + $m = new \Upper\PBCALLABLE(); + $m = new \Upper\PBCASE(); + $m = new \Upper\PBCATCH(); + $m = new \Upper\PBCLASS(); + $m = new \Upper\PBCLONE(); + $m = new \Upper\PBCONST(); + $m = new \Upper\PBCONTINUE(); + $m = new \Upper\PBDECLARE(); + $m = new \Upper\PBDEFAULT(); + $m = new \Upper\PBDIE(); + $m = new \Upper\PBDO(); + $m = new \Upper\PBECHO(); + $m = new \Upper\PBELSE(); + $m = new \Upper\PBELSEIF(); + $m = new \Upper\PBEMPTY(); + $m = new \Upper\PBENDDECLARE(); + $m = new \Upper\PBENDFOR(); + $m = new \Upper\PBENDFOREACH(); + $m = new \Upper\PBENDIF(); + $m = new \Upper\PBENDSWITCH(); + $m = new \Upper\PBENDWHILE(); + $m = new \Upper\PBEVAL(); + $m = new \Upper\PBEXIT(); + $m = new \Upper\PBEXTENDS(); + $m = new \Upper\PBFINAL(); + $m = new \Upper\PBFOR(); + $m = new \Upper\PBFOREACH(); + $m = new \Upper\PBFUNCTION(); + $m = new \Upper\PBGLOBAL(); + $m = new \Upper\PBGOTO(); + $m = new \Upper\PBIF(); + $m = new \Upper\PBIMPLEMENTS(); + $m = new \Upper\PBINCLUDE(); + $m = new \Upper\PBINCLUDE_ONCE(); + $m = new \Upper\PBINSTANCEOF(); + $m = new \Upper\PBINSTEADOF(); + $m = new \Upper\PBINTERFACE(); + $m = new \Upper\PBISSET(); + $m = new \Upper\PBLIST(); + $m = new \Upper\PBNAMESPACE(); + $m = new \Upper\PBNEW(); + $m = new \Upper\PBOR(); + $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\PBSTATIC(); + $m = new \Upper\PBSWITCH(); + $m = new \Upper\PBTHROW(); + $m = new \Upper\PBTRAIT(); + $m = new \Upper\PBTRY(); + $m = new \Upper\PBUNSET(); + $m = new \Upper\PBUSE(); + $m = new \Upper\PBVAR(); + $m = new \Upper\PBWHILE(); + $m = new \Upper\PBXOR(); + $m = new \Upper\PBINT(); + $m = new \Upper\PBFLOAT(); + $m = new \Upper\PBBOOL(); + $m = new \Upper\PBSTRING(); + $m = new \Upper\PBTRUE(); + $m = new \Upper\PBFALSE(); + $m = new \Upper\PBNULL(); + $m = new \Upper\PBVOID(); + $m = new \Upper\PBITERABLE(); + + $m = \Lower_enum\NotAllowed::PBabstract; + $m = \Lower_enum\NotAllowed::PBand; + $m = \Lower_enum\NotAllowed::PBarray; + $m = \Lower_enum\NotAllowed::PBas; + $m = \Lower_enum\NotAllowed::PBbreak; + $m = \Lower_enum\NotAllowed::PBcallable; + $m = \Lower_enum\NotAllowed::PBcase; + $m = \Lower_enum\NotAllowed::PBcatch; + $m = \Lower_enum\NotAllowed::PBclass; + $m = \Lower_enum\NotAllowed::PBclone; + $m = \Lower_enum\NotAllowed::PBconst; + $m = \Lower_enum\NotAllowed::PBcontinue; + $m = \Lower_enum\NotAllowed::PBdeclare; + $m = \Lower_enum\NotAllowed::PBdefault; + $m = \Lower_enum\NotAllowed::PBdie; + $m = \Lower_enum\NotAllowed::PBdo; + $m = \Lower_enum\NotAllowed::PBecho; + $m = \Lower_enum\NotAllowed::PBelse; + $m = \Lower_enum\NotAllowed::PBelseif; + $m = \Lower_enum\NotAllowed::PBempty; + $m = \Lower_enum\NotAllowed::PBenddeclare; + $m = \Lower_enum\NotAllowed::PBendfor; + $m = \Lower_enum\NotAllowed::PBendforeach; + $m = \Lower_enum\NotAllowed::PBendif; + $m = \Lower_enum\NotAllowed::PBendswitch; + $m = \Lower_enum\NotAllowed::PBendwhile; + $m = \Lower_enum\NotAllowed::PBeval; + $m = \Lower_enum\NotAllowed::PBexit; + $m = \Lower_enum\NotAllowed::PBextends; + $m = \Lower_enum\NotAllowed::PBfinal; + $m = \Lower_enum\NotAllowed::PBfor; + $m = \Lower_enum\NotAllowed::PBforeach; + $m = \Lower_enum\NotAllowed::PBfunction; + $m = \Lower_enum\NotAllowed::PBglobal; + $m = \Lower_enum\NotAllowed::PBgoto; + $m = \Lower_enum\NotAllowed::PBif; + $m = \Lower_enum\NotAllowed::PBimplements; + $m = \Lower_enum\NotAllowed::PBinclude; + $m = \Lower_enum\NotAllowed::PBinclude_once; + $m = \Lower_enum\NotAllowed::PBinstanceof; + $m = \Lower_enum\NotAllowed::PBinsteadof; + $m = \Lower_enum\NotAllowed::PBinterface; + $m = \Lower_enum\NotAllowed::PBisset; + $m = \Lower_enum\NotAllowed::PBlist; + $m = \Lower_enum\NotAllowed::PBnamespace; + $m = \Lower_enum\NotAllowed::PBnew; + $m = \Lower_enum\NotAllowed::PBor; + $m = \Lower_enum\NotAllowed::PBprint; + $m = \Lower_enum\NotAllowed::PBprivate; + $m = \Lower_enum\NotAllowed::PBprotected; + $m = \Lower_enum\NotAllowed::PBpublic; + $m = \Lower_enum\NotAllowed::PBrequire; + $m = \Lower_enum\NotAllowed::PBrequire_once; + $m = \Lower_enum\NotAllowed::PBreturn; + $m = \Lower_enum\NotAllowed::PBstatic; + $m = \Lower_enum\NotAllowed::PBswitch; + $m = \Lower_enum\NotAllowed::PBthrow; + $m = \Lower_enum\NotAllowed::PBtrait; + $m = \Lower_enum\NotAllowed::PBtry; + $m = \Lower_enum\NotAllowed::PBunset; + $m = \Lower_enum\NotAllowed::PBuse; + $m = \Lower_enum\NotAllowed::PBvar; + $m = \Lower_enum\NotAllowed::PBwhile; + $m = \Lower_enum\NotAllowed::PBxor; + $m = \Lower_enum\NotAllowed::PBint; + $m = \Lower_enum\NotAllowed::PBfloat; + $m = \Lower_enum\NotAllowed::PBbool; + $m = \Lower_enum\NotAllowed::PBstring; + $m = \Lower_enum\NotAllowed::PBtrue; + $m = \Lower_enum\NotAllowed::PBfalse; + $m = \Lower_enum\NotAllowed::PBnull; + $m = \Lower_enum\NotAllowed::PBvoid; + $m = \Lower_enum\NotAllowed::PBiterable; } ######################################################### diff --git a/php/tests/proto/test_reserved_enum_lower.proto b/php/tests/proto/test_reserved_enum_lower.proto new file mode 100644 index 00000000..e2144c0c --- /dev/null +++ b/php/tests/proto/test_reserved_enum_lower.proto @@ -0,0 +1,79 @@ +syntax = "proto3"; + +package lower_enum; + +enum NotAllowed { + abstract = 0; + and = 1; + array = 2; + as = 3; + break = 4; + callable = 5; + case = 6; + catch = 7; + class = 8; + clone = 9; + const = 10; + continue = 11; + declare = 12; + default = 13; + die = 14; + do = 15; + echo = 16; + else = 17; + elseif = 18; + empty = 19; + enddeclare = 20; + endfor = 21; + endforeach = 22; + endif = 23; + endswitch = 24; + endwhile = 25; + eval = 26; + exit = 27; + extends = 28; + final = 29; + for = 30; + foreach = 31; + function = 32; + global = 33; + goto = 34; + if = 35; + implements = 36; + include = 37; + include_once = 38; + instanceof = 39; + insteadof = 40; + interface = 41; + isset = 42; + list = 43; + namespace = 44; + new = 45; + or = 46; + print = 47; + private = 48; + protected = 49; + public = 50; + require = 51; + require_once = 52; + return = 53; + static = 54; + switch = 55; + throw = 56; + trait = 57; + try = 58; + unset = 59; + use = 60; + var = 61; + while = 62; + xor = 63; + int = 64; + float = 65; + bool = 66; + string = 67; + true = 68; + false = 69; + null = 70; + void = 71; + iterable = 72; +} diff --git a/php/tests/proto/test_reserved_enum_upper.proto b/php/tests/proto/test_reserved_enum_upper.proto new file mode 100644 index 00000000..a4cc1f4b --- /dev/null +++ b/php/tests/proto/test_reserved_enum_upper.proto @@ -0,0 +1,79 @@ +syntax = "proto3"; + +package upper_enum; + +enum NotAllowed { + ABSTRACT = 0; + AND = 1; + ARRAY = 2; + AS = 3; + BREAK = 4; + CALLABLE = 5; + CASE = 6; + CATCH = 7; + CLASS = 8; + CLONE = 9; + CONST = 10; + CONTINUE = 11; + DECLARE = 12; + DEFAULT = 13; + DIE = 14; + DO = 15; + ECHO = 16; + ELSE = 17; + ELSEIF = 18; + EMPTY = 19; + ENDDECLARE = 20; + ENDFOR = 21; + ENDFOREACH = 22; + ENDIF = 23; + ENDSWITCH = 24; + ENDWHILE = 25; + EVAL = 26; + EXIT = 27; + EXTENDS = 28; + FINAL = 29; + FOR = 30; + FOREACH = 31; + FUNCTION = 32; + GLOBAL = 33; + GOTO = 34; + IF = 35; + IMPLEMENTS = 36; + INCLUDE = 37; + INCLUDE_ONCE = 38; + INSTANCEOF = 39; + INSTEADOF = 40; + INTERFACE = 41; + ISSET = 42; + LIST = 43; + NAMESPACE = 44; + NEW = 45; + OR = 46; + PRINT = 47; + PRIVATE = 48; + PROTECTED = 49; + PUBLIC = 50; + REQUIRE = 51; + REQUIRE_ONCE = 52; + RETURN = 53; + STATIC = 54; + SWITCH = 55; + THROW = 56; + TRAIT = 57; + TRY = 58; + UNSET = 59; + USE = 60; + VAR = 61; + WHILE = 62; + XOR = 63; + INT = 64; + FLOAT = 65; + BOOL = 66; + STRING = 67; + TRUE = 68; + FALSE = 69; + NULL = 70; + VOID = 71; + ITERABLE = 72; +} diff --git a/php/tests/proto/test_reserved_message_lower.proto b/php/tests/proto/test_reserved_message_lower.proto new file mode 100644 index 00000000..ed120808 --- /dev/null +++ b/php/tests/proto/test_reserved_message_lower.proto @@ -0,0 +1,77 @@ +syntax = "proto3"; + +package lower; + +message abstract {} +message and {} +message array {} +message as {} +message break {} +message callable {} +message case {} +message catch {} +message class {} +message clone {} +message const {} +message continue {} +message declare {} +message default {} +message die {} +message do {} +message echo {} +message else {} +message elseif {} +message empty {} +message enddeclare {} +message endfor {} +message endforeach {} +message endif {} +message endswitch {} +message endwhile {} +message eval {} +message exit {} +message extends {} +message final {} +message for {} +message foreach {} +message function {} +message global {} +message goto {} +message if {} +message implements {} +message include {} +message include_once {} +message instanceof {} +message insteadof {} +message interface {} +message isset {} +message list {} +message namespace {} +message new {} +message or {} +message print {} +message private {} +message protected {} +message public {} +message require {} +message require_once {} +message return {} +message static {} +message switch {} +message throw {} +message trait {} +message try {} +message unset {} +message use {} +message var {} +message while {} +message xor {} +message int {} +message float {} +message bool {} +message string {} +message true {} +message false {} +message null {} +message void {} +message iterable {} diff --git a/php/tests/proto/test_reserved_message_upper.proto b/php/tests/proto/test_reserved_message_upper.proto new file mode 100644 index 00000000..2917fd11 --- /dev/null +++ b/php/tests/proto/test_reserved_message_upper.proto @@ -0,0 +1,77 @@ +syntax = "proto3"; + +package upper; + +message ABSTRACT {} +message AND {} +message ARRAY {} +message AS {} +message BREAK {} +message CALLABLE {} +message CASE {} +message CATCH {} +message CLASS {} +message CLONE {} +message CONST {} +message CONTINUE {} +message DECLARE {} +message DEFAULT {} +message DIE {} +message DO {} +message ECHO {} +message ELSE {} +message ELSEIF {} +message EMPTY {} +message ENDDECLARE {} +message ENDFOR {} +message ENDFOREACH {} +message ENDIF {} +message ENDSWITCH {} +message ENDWHILE {} +message EVAL {} +message EXIT {} +message EXTENDS {} +message FINAL {} +message FOR {} +message FOREACH {} +message FUNCTION {} +message GLOBAL {} +message GOTO {} +message IF {} +message IMPLEMENTS {} +message INCLUDE {} +message INCLUDE_ONCE {} +message INSTANCEOF {} +message INSTEADOF {} +message INTERFACE {} +message ISSET {} +message LIST {} +message NAMESPACE {} +message NEW {} +message OR {} +message PRINT {} +message PRIVATE {} +message PROTECTED {} +message PUBLIC {} +message REQUIRE {} +message REQUIRE_ONCE {} +message RETURN {} +message STATIC {} +message SWITCH {} +message THROW {} +message TRAIT {} +message TRY {} +message UNSET {} +message USE {} +message VAR {} +message WHILE {} +message XOR {} +message INT {} +message FLOAT {} +message BOOL {} +message STRING {} +message TRUE {} +message FALSE {} +message NULL {} +message VOID {} +message ITERABLE {} -- cgit v1.2.3