From 4523c9c233dbb61d03996a5bbe25d1b5aea51f7f Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 20 Apr 2017 16:55:56 -0700 Subject: Allow proto files to import descriptor.proto (#2995) descriptor.proto uses proto2 syntax, which is not ready for external usage. However, some proto3 files import descriptor.proto and cannot be used. In this PR, all references (We cheated by only removing extensions, which is enough for now. User should avoid using messages defined in descriptor.proto as field type.) to content in descriptor.proto are removed from generated files. Those that import descriptor.proto can be used like other proto files. --- Makefile.am | 1 + php/src/Google/Protobuf/Internal/MapField.php | 1 - php/src/Google/Protobuf/Internal/Message.php | 1 - php/tests/gdb_test.sh | 2 +- php/tests/proto/test_import_descriptor_proto.proto | 14 ++++++++++++ php/tests/well_known_test.php | 10 +++++++-- src/google/protobuf/compiler/php/php_generator.cc | 26 ++++++++++++++++++++++ tests.sh | 1 + 8 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 php/tests/proto/test_import_descriptor_proto.proto diff --git a/Makefile.am b/Makefile.am index 564bff48..d0f033af 100644 --- a/Makefile.am +++ b/Makefile.am @@ -656,6 +656,7 @@ php_EXTRA_DIST= \ php/tests/map_field_test.php \ php/tests/memory_leak_test.php \ php/tests/php_implementation_test.php \ + php/tests/proto/test_import_descriptor_proto.proto \ php/tests/proto/test_include.proto \ php/tests/proto/test.proto \ php/tests/proto/test_prefix.proto \ diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php index f65bd9b8..55cc12ce 100644 --- a/php/src/Google/Protobuf/Internal/MapField.php +++ b/php/src/Google/Protobuf/Internal/MapField.php @@ -155,7 +155,6 @@ function checkKey($key_type, &$key) GPBUtil::checkString($key, true); break; default: - var_dump($key_type); trigger_error( "Given type cannot be map key.", E_USER_ERROR); diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 0fb6cdc0..887c86ca 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -71,7 +71,6 @@ class Message return; } $pool = DescriptorPool::getGeneratedPool(); - var_dump(get_class($this)); $this->desc = $pool->getDescriptorByClassName(get_class($this)); foreach ($this->desc->getField() as $field) { $setter = $field->getSetter(); diff --git a/php/tests/gdb_test.sh b/php/tests/gdb_test.sh index 484e2edf..0809bef3 100755 --- a/php/tests/gdb_test.sh +++ b/php/tests/gdb_test.sh @@ -3,7 +3,7 @@ # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which # phpunit` --bootstrap autoload.php tmp_test.php # -gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php +gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php well_known_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php # diff --git a/php/tests/proto/test_import_descriptor_proto.proto b/php/tests/proto/test_import_descriptor_proto.proto new file mode 100644 index 00000000..2a19940d --- /dev/null +++ b/php/tests/proto/test_import_descriptor_proto.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +import "google/protobuf/descriptor.proto"; + +message TestImportDescriptorProto { + extend google.protobuf.MethodOptions { + int32 a = 72295727; + } +} + +extend google.protobuf.MethodOptions { + int32 a = 72295728; +} + diff --git a/php/tests/well_known_test.php b/php/tests/well_known_test.php index 40ff1c8f..0c2aec13 100644 --- a/php/tests/well_known_test.php +++ b/php/tests/well_known_test.php @@ -4,8 +4,14 @@ use Google\Protobuf\GPBEmpty; class WellKnownTest extends PHPUnit_Framework_TestCase { - public function testNone() { - $msg = new GPBEmpty(); + public function testNone() + { + $msg = new GPBEmpty(); + } + + public function testImportDescriptorProto() + { + $msg = new TestImportDescriptorProto(); } } diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc index d24e1e5e..4d475b1f 100644 --- a/src/google/protobuf/compiler/php/php_generator.cc +++ b/src/google/protobuf/compiler/php/php_generator.cc @@ -674,6 +674,12 @@ void GenerateAddFileToPool(const FileDescriptor* file, bool is_descriptor, } else { for (int i = 0; i < file->dependency_count(); i++) { const std::string& name = file->dependency(i)->name(); + // Currently, descriptor.proto is not ready for external usage. Skip to + // import it for now, so that its dependencies can still work as long as + // they don't use protos defined in descriptor.proto. + if (name == kDescriptorFile) { + continue; + } std::string dependency_filename = GeneratedMetadataFileName(name, is_descriptor); printer->Print( @@ -685,6 +691,26 @@ void GenerateAddFileToPool(const FileDescriptor* file, bool is_descriptor, FileDescriptorSet files; FileDescriptorProto* file_proto = files.add_file(); file->CopyTo(file_proto); + + // Filter out descriptor.proto as it cannot be depended on for now. + RepeatedPtrField* dependency = file_proto->mutable_dependency(); + for (RepeatedPtrField::iterator it = dependency->begin(); + it != dependency->end(); ++it) { + if (*it != kDescriptorFile) { + dependency->erase(it); + break; + } + } + + // Filter out all extensions, since we do not support extension yet. + file_proto->clear_extension(); + RepeatedPtrField* message_type = + file_proto->mutable_message_type(); + for (RepeatedPtrField::iterator it = message_type->begin(); + it != message_type->end(); ++it) { + it->clear_extension(); + } + string files_data; files.SerializeToString(&files_data); diff --git a/tests.sh b/tests.sh index 8c56172d..edb37da7 100755 --- a/tests.sh +++ b/tests.sh @@ -362,6 +362,7 @@ generate_php_test_proto() { ../../src/protoc --php_out=generated proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto pushd ../../src ./protoc --php_out=../php/tests/generated google/protobuf/empty.proto + ./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto popd popd } -- cgit v1.2.3