Add prefix to php reserved keywords.

pull/3612/head
Bo Yang 7 years ago
parent 6a4ffb2f90
commit e5d000cbb7
  1. 4
      Makefile.am
  2. 30
      php/ext/google/protobuf/def.c
  3. 18
      php/src/Google/Protobuf/Internal/GPBUtil.php
  4. 222
      php/tests/generated_class_test.php
  5. 79
      php/tests/proto/test_reserved_enum_lower.proto
  6. 79
      php/tests/proto/test_reserved_enum_upper.proto
  7. 77
      php/tests/proto/test_reserved_message_lower.proto
  8. 77
      php/tests/proto/test_reserved_message_upper.proto
  9. 24
      src/google/protobuf/compiler/php/php_generator.cc
  10. 4
      tests.sh

@ -686,6 +686,10 @@ php_EXTRA_DIST= \
php/tests/proto/test_no_namespace.proto \
php/tests/proto/test_php_namespace.proto \
php/tests/proto/test_prefix.proto \
php/tests/proto/test_reserved_enum_lower.proto \
php/tests/proto/test_reserved_enum_upper.proto \
php/tests/proto/test_reserved_message_lower.proto \
php/tests/proto/test_reserved_message_upper.proto \
php/tests/proto/test_service.proto \
php/tests/proto/test_service_namespace.proto \
php/tests/test.sh \

@ -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) {

@ -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 {

@ -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;
}
#########################################################

@ -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;
}

@ -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;
}

@ -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 {}

@ -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 {}

@ -49,8 +49,23 @@ 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[] = {"ARRAY", "Empty", "ECHO"};
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;
const int kFieldSetter = 1;
const int kFieldGetter = 2;
const int kFieldProperty = 3;
@ -125,8 +140,11 @@ std::string ClassNamePrefix(const string& classname,
bool is_reserved = false;
string lower = classname;
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
for (int i = 0; i < kReservedNamesSize; i++) {
if (classname == kReservedNames[i]) {
if (lower == kReservedNames[i]) {
is_reserved = true;
break;
}

@ -353,6 +353,10 @@ generate_php_test_proto() {
proto/test_prefix.proto \
proto/test_php_namespace.proto \
proto/test_empty_php_namespace.proto \
proto/test_reserved_enum_lower.proto \
proto/test_reserved_enum_upper.proto \
proto/test_reserved_message_lower.proto \
proto/test_reserved_message_upper.proto \
proto/test_service.proto \
proto/test_service_namespace.proto \
proto/test_descriptors.proto

Loading…
Cancel
Save